一种可能性是使用由Moose或Moo等对象系统提供的方法修饰符:
use strict; use warnings;
package Child {
use Moose;
sub method_x { warn "call method_x" }
sub method_y { warn "call method_y" }
sub method_z { warn "call method_z" }
}
package Parent {
use Moose;
has child => (is => 'rw');
sub x { shift->child->method_x(@_) }
sub y { shift->child->method_y(@_) }
sub z { shift->child->method_z(@_) }
# A method modifier in action
after [qw/ x y z /] => sub {
warn "called after every Parent (!) invocation";
};
}
my $p = Parent->new(child => Child->new);
$p->x; $p->y; $p->z;
输出:
call method_x at - line 7.
called after every Parent (!) invocation at - line 23.
call method_y at - line 8.
called after every Parent (!) invocation at - line 23.
call method_z at - line 9.
called after every Parent (!) invocation at - line 23.
如果您真的希望包装的所有方法Child
,请使用子类:
package WrappedChild {
use Moose;
extends 'Child';
# the /(?=)/ regex matches always
after qr/(?=)/ => sub {
warn "called after each method in Child";
};
}
my $p = Parent->new(child => WrappedChild->new);
$p->x; $p->y; $p->z;
这产生
called after each method in Child at - line 32.
called after each method in Child at - line 32.
called after each method in Child at - line 32.
called after each method in Child at - line 32.
called after each method in Child at - line 32.
called after each method in Child at - line 32.
call method_x at - line 7.
called after each method in Child at - line 32.
called after every Parent (!) invocation at - line 22.
call method_y at - line 8.
called after each method in Child at - line 32.
called after every Parent (!) invocation at - line 22.
call method_z at - line 9.
called after each method in Child at - line 32.
called after every Parent (!) invocation at - line 22.
called after each method in Child at - line 32.
called after each method in Child at - line 32.
called after each method in Child at - line 32.
这可能有点过分。坚持使用明确的名称可能更可取。
有关Moose::Manual::MethodModifiers
更多信息,请参阅。
如果您不想使用任何模块,您可以通过丛林符号表:
for my $name (qw/method_x method_y method_z/) {
no strict 'refs';
no warnings 'redefine';
my $orig = \&{"Child::$name"};
*{"Child::$name} = sub {
my @return_values = wantarray ? $orig->() : scalar $orig->();
warn "called after each method";
return wantarray ? @return_values : $return_values[0];
};
}
输出:
call method_x at - line 7.
called after each method at - line 31.
call method_y at - line 8.
called after each method at - line 31.
call method_z at - line 9.
called after each method at - line 31.