Programming with Moose/Syntax/before, after and around

 ,  , and   are method modifiers. They give control over the chain of events around the actual call to a method. This allows the user to do simple things, easily. Two of the keywords, and  , do nothing more than trigger code. While  permits dynamic rewriting of the arguments.

These features are documented only in.

Infinite recursion
It might be tempting to call the function that the modifier is attached to within the modifier's sub. This is bad; observe the following and never do it:

The result is a bad infinite loop:
 * 1) The after modifier executes and calls
 * 2) The after modifier executes and calls
 * 3) ad infinitum
 * 1) The after modifier executes and calls
 * 2) ad infinitum

What not to do
This is an example of what most people think will work, but it doesn't: the hackish solution that it seems everyone must attempt once. Observe what you should never do!!:

which returns: $VAR1 = [ 'modifier before', [            'foo' ]        ]; $VAR1 = [ 'modifier after', [            'hax3d' ]        ]; $VAR1 = [ 'method', [            'hax3d' ]        ];

Here you might figure this works, because your able to hack a method of modification into method. but be aware, employing this technique will yield a drastic difference when you pass something other than a reference.

$VAR1 = [ 'modifier before', 'foo' ]; $VAR1 = [ 'modifier after', 'bar' ]; $VAR1 = [ 'method', 'foo' ];

Almost surely not what you were looking for? Don't worry, now for what you should do.

What to do
The Way of the Moose&#153; is simple, use the more suited  . Moose makes it very direct and clear what you intend to do. Using  your modifier will get: (a) the name of the desired method, (b) a reference to self, and (c) the arguments sent to the function. You can then  if you wish to dispatch the function or totally rewrite or halt the call.

returns the following: $VAR1 = [ 'modifier', sub { "DUMMY" }, [            'foo' ]        ]; $VAR1 = [ 'method', 'foo' ];