| Class::Method::Modifiers::Fast - provides Moose-like method modifiers |
Class::Method::Modifiers::Fast - provides Moose-like method modifiers
package Child;
use parent 'Parent';
use Class::Method::Modifiers::Fast;
sub new_method { }
before 'old_method' => sub {
carp "old_method is deprecated, use new_method";
};
around 'other_method' => sub {
my $orig = shift;
my $ret = $orig->(@_);
return $ret =~ /\d/ ? $ret : lc $ret;
};
Method modifiers are a powerful feature from the CLOS (Common Lisp Object System) world.
Class::Method::Modifiers::Fast provides three modifiers: before, around,
and after. before and after are run just before and after the method they
modify, but can not really affect that original method. around is run in
place of the original method, with a hook to easily call that original method.
See the MODIFIERS section for more details on how the particular modifiers
work.
method(s) => sub { ... }before is called before the method it is modifying. Its return value is
totally ignored. It receives the same @_ as the the method it is modifying
would have received. You can modify the @_ the original method will receive
by changing $_[0] and friends (or by changing anything inside a reference).
This is a feature!
method(s) => sub { ... }after is called after the method it is modifying. Its return value is
totally ignored. It receives the same @_ as the the method it is modifying
received, mostly. The original method can modify @_ (such as by changing
$_[0] or references) and after will see the modified version. If you
don't like this behavior, specify both a before and after, and copy the
@_ during before for after to use.
method(s) => sub { ... }around is called instead of the method it is modifying. The method you're
overriding is passed in as the first argument (called $orig by convention).
Watch out for contextual return values of $orig.
You can use around to:
$orig a different @_
around 'method' => sub {
my $orig = shift;
my $self = shift;
$orig->($self, reverse @_);
};
$orig
around 'method' => sub {
my $orig = shift;
ucfirst $orig->(@_);
};
$orig -- conditionally
around 'method' => sub {
my $orig = shift;
return $orig->(@_) if time() % 2;
return "no dice, captain";
};
Takatoshi Kitano <kitano.tk@gmail.com>
the Class::Method::Modifiers manpage
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
| Class::Method::Modifiers::Fast - provides Moose-like method modifiers |