Class::Method::Modifiers::Fast(3) | User Contributed Perl Documentation | Class::Method::Modifiers::Fast(3) |
NAME¶
Class::Method::Modifiers::Fast - provides Moose-like method modifiers
SYNOPSIS¶
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; };
DESCRIPTION¶
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.
MODIFIERS¶
before 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!
after 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.
around 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:
- Pass $orig a different @_
-
around 'method' => sub { my $orig = shift; my $self = shift; $orig->($self, reverse @_); };
- Munge the return value of $orig
-
around 'method' => sub { my $orig = shift; ucfirst $orig->(@_); };
- Avoid calling $orig -- conditionally
-
around 'method' => sub { my $orig = shift; return $orig->(@_) if time() % 2; return "no dice, captain"; };
AUTHOR¶
Takatoshi Kitano <kitano.tk@gmail.com> gfx
SEE ALSO¶
Class::Method::Modifiers
LICENSE¶
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
2017-10-12 | perl v5.40.0 |