假设像这样的Moose对象
package Foo;
use Moose;
has a => ( is => 'rw', isa => 'Int' );
has b => ( is => 'rw', isa => 'Str' );
has c => ( is => 'rw', isa => 'HashRef' );
around [ qw(a b c) ] => sub {
my $orig = shift;
my $self = shift;
return $self->$orig() unless @_;
my $aname = ???? # meta something?
$self->myfunction($aname, @_);
};
如何将$aname
设置为正在设置的属性的名称。换句话说,如果
$foo->a(2)
我希望能够将$aname
设置为a
。
我可以为每个属性设置around
,但这似乎是重复的。
答案 0 :(得分:4)
一种方法是使用Moose::Manual::MethodModifiers #Wrapping multiple methods at once中建模的for循环:
for my $aname (qw(a b c)) {
around $aname => sub {
my $orig = shift;
my $self = shift;
return $self->$orig() unless @_;
$self->myfunction( $aname, @_ );
};
}