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.
Class::Method::Modifiers::Fast
before
around
after
MODIFIERS
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!
@_
$_[0]
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.
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.
$orig
You can use around to:
around 'method' => sub { my $orig = shift; my $self = shift; $orig->($self, reverse @_); };
around 'method' => sub { my $orig = shift; ucfirst $orig->(@_); };
around 'method' => sub { my $orig = shift; return $orig->(@_) if time() % 2; return "no dice, captain"; };
Takatoshi Kitano <kitano.tk@gmail.com> gfx
Class::Method::Modifiers
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Class::Method::Modifiers::Fast, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Class::Method::Modifiers::Fast
CPAN shell
perl -MCPAN -e shell install Class::Method::Modifiers::Fast
For more information on module installation, please visit the detailed CPAN module installation guide.