MooseX::Mangle - mangle the argument list or return values of your methods


MooseX-Mangle documentation Contained in the MooseX-Mangle distribution.

Index


Code Index:

NAME

Top

MooseX::Mangle - mangle the argument list or return values of your methods

VERSION

Top

version 0.02

SYNOPSIS

Top

  package Foo;
  use Moose;

  sub foo { "FOO" }
  sub bar { shift; join '-', @_ }

  package Foo::Sub;
  use Moose;
  use MooseX::Mangle;
  extends 'Foo';

  mangle_return foo => sub {
      my $self = shift;
      my ($foo) = @_;
      return lc($foo) . 'BAR';
  };

  mangle_args bar => sub {
      my $self = shift;
      my ($a, $b, $c) = @_;
      return ($b, $c, $a);
  };

  my $foo = Foo::Sub->new->foo            # 'fooBAR'
  my $bar = Foo::Sub->new->bar(qw(a b c)) # 'b-c-a'

DESCRIPTION

Top

MooseX::Mangle provides some simple sugar for common usages of around. Oftentimes all that is needed is to adjust the argument list or returned values of a method, but using around directly for this can be tedious. This module exports a few subroutines which make this a bit easier.

EXPORTS

Top

mangle_args METHOD_NAME CODE

Applies an around method modifier to METHOD_NAME, using CODE to mangle the argument list. CODE is called as a method, and additionally receives the arguments passed to the method; it should return the list of arguments to actually pass to the method.

mangle_return METHOD_NAME CODE

Applies an around method modifier to METHOD_NAME, using CODE to mangle the returned values. CODE is called as a method, and additionally receives the values returned by the method; it should return the list of values to actually return.

guard METHOD_NAME CODE

Provides a requirement that must be satisfied in order for METHOD_NAME to be called. CODE is called as a method, receiving the arguments passed to the method. If CODE returns true, the method is called as normal, otherwise undef is returned without the original method being called at all.

BUGS

Top

No known bugs.

Suggestions for more modifiers are always welcome, though.

Please report any bugs through RT: email bug-moosex-mangle at rt.cpan.org, or browse to http://rt.cpan.org/NoAuth/ReportBug.html?Queue=MooseX-Mangle.

SEE ALSO

Top

Moose

SUPPORT

Top

You can find this documentation for this module with the perldoc command.

    perldoc MooseX::Mangle

You can also look for information at:

* AnnoCPAN: Annotated CPAN documentation

http://annocpan.org/dist/MooseX-Mangle

* CPAN Ratings

http://cpanratings.perl.org/d/MooseX-Mangle

* RT: CPAN's request tracker

http://rt.cpan.org/NoAuth/Bugs.html?Dist=MooseX-Mangle

* Search CPAN

http://search.cpan.org/dist/MooseX-Mangle

AUTHOR

Top

  Jesse Luehrs <doy at tozt dot net>

COPYRIGHT AND LICENSE

Top


MooseX-Mangle documentation Contained in the MooseX-Mangle distribution.
package MooseX::Mangle;
our $VERSION = '0.02';
use Moose ();
use Moose::Exporter;

sub mangle_args {
    my $meta = shift;
    my ($method_name, $code) = @_;
    $meta->add_around_method_modifier($method_name => sub {
        my $orig = shift;
        my $self = shift;
        my @args = $self->$code(@_);
        return $self->$orig(@args);
    });
}

sub mangle_return {
    my $meta = shift;
    my ($method_name, $code) = @_;
    $meta->add_around_method_modifier($method_name => sub {
        my $orig = shift;
        my $self = shift;
        if (wantarray) {
            my @ret = $self->$orig(@_);
            return $self->$code(@ret);
        }
        elsif (defined(wantarray)) {
            my $ret = $self->$orig(@_);
            return $self->$code($ret);
        }
        else {
            $self->$orig(@_);
            $self->$code();
            return;
        }
    });
}

sub guard {
    my $meta = shift;
    my ($method_name, $code) = @_;
    $meta->add_around_method_modifier($method_name => sub {
        my $orig = shift;
        my $self = shift;
        if ($self->$code(@_)) {
            return $self->$orig(@_);
        }
        return;
    });
}

Moose::Exporter->setup_import_methods(
    with_meta => [qw(mangle_args mangle_return guard)],
);

1;