CC: | GRAF [...] cpan.org |
Subject: | The missing manual: Execution order of method modifiers with regard to inheritance |
The documentation on method modifiers leaves out the (imho) important concept of the ordering in the case when both parent and child have method modifiers. The rest of this ticket is a tentative first stab at documenting what goes on. Feel free to improve on it:), but please do not drop the ball as I'm sure quite a few Moose users would not know about this.
Enjoy,
=head2 Execution order of method modifiers with regard to inheritance
When both a superclass and an inheriting class have the same method
modifiers, the method modifiers of the inheriting class are wrapped
around the method modifiers of the superclass, as the following
examples illustrate:
Parent.pm:
package Parent;
use Moose;
sub rant { printf "rant Parent\n" }
before 'rant' => sub { printf "In %s before\n", __PACKAGE__ };
after 'rant' => sub { printf "In %s after\n", __PACKAGE__ };
around 'rant' => sub {
my $orig = shift;
my $self = shift;
printf "In %s around-before\n", __PACKAGE__;
$self->$orig;
printf "In %s around-after\n", __PACKAGE__;
};
1;
Child.pm:
use Parent;
package Child;
use Moose;
extends 'Parent';
before 'rant' => sub { printf "In %s before\n", __PACKAGE__ };
after 'rant' => sub { printf "In %s after\n", __PACKAGE__ };
around 'rant' => sub {
my $orig = shift;
my $self = shift;
printf "In %s around-before\n", __PACKAGE__;
$self->$orig;
printf "In %s around-after\n", __PACKAGE__;
};
Program and output:
% perl -e '
use Child;
package main;
Child->new->rant;
'
In Child before
In Child around-before
In Parent before
In Parent around-before
rant Parent
In Parent around-after
In Parent after
In Child around-after
In Child after