【发布时间】:2016-06-30 16:46:55
【问题描述】:
Base.pm:
package Base;
use Moose::Role;
sub f {
my ($self) = @_;
print "In role.\n";
}
1;
X.pm:
package X;
use Moose;
with 'Base';
around 'f' => sub {
my ($next, $self) = @_;
print "Nevermind, we are in class X.\n";
};
__PACKAGE__->meta->make_immutable;
1;
Y.pm:
package Y;
use Moose;
with 'Base';
override 'f' => sub {
my ($self) = @_;
print "Nevermind, we are in class Y.\n";
};
__PACKAGE__->meta->make_immutable;
1;
那么 X 有效,而 Y 无效。这是一个奇怪的设计,因为override 只是around 的一个特例,而且作为一个特例也应该可以工作。
谁能解释为什么这个设计决定以及为什么它如此奇怪?
$ perl X.pm
$ perl Y.pm
Cannot add an override method if a local method is already present at /usr/lib/i386-linux-gnu/perl5/5.22/Moose/Exporter.pm line 419
Moose::override('f', 'CODE(0x9c002f8)') called at Y.pm line 9
【问题讨论】:
标签: oop overriding moose perl