很酷的是,可以在子类中添加它们或者将它们混合在一起。 我的问题是,当子类重新定义方法本身(而不是修饰符)时,基类中的方法修饰符似乎被取消激活。 也许我理解方法修饰符错了。 例:
use feature 'say'; package Foo; use Moose; has called => (is => 'rw', isa => 'Bool', default => 0); sub call { 'Foo called' } after call => sub { shift->called(1) }; my $foo = Foo->new(); say $foo->called; # 0 say $foo->call; # Foo called say $foo->called; # 1 package Bar; use Moose; extends 'Foo'; sub call { 'Bar called' } my $bar = Bar->new(); say $bar->called; # 0 say $bar->call; # Bar called say $bar->called; # 0我期望最后的输出为1就像$foo 。 我究竟做错了什么?
It's cool that it's possible to add them in sub classes or mix them in in roles. My problem is that it seems method modifiers from the base class get deactivated when subclasses redefine the method itself (not the modifier). Maybe I'm understanding method modifiers wrong. Example:
use feature 'say'; package Foo; use Moose; has called => (is => 'rw', isa => 'Bool', default => 0); sub call { 'Foo called' } after call => sub { shift->called(1) }; my $foo = Foo->new(); say $foo->called; # 0 say $foo->call; # Foo called say $foo->called; # 1 package Bar; use Moose; extends 'Foo'; sub call { 'Bar called' } my $bar = Bar->new(); say $bar->called; # 0 say $bar->call; # Bar called say $bar->called; # 0I expected the last output to be 1 like with $foo. What am I doing wrong?
最满意答案
这是怎么回事
你定义了一个Foo :: call 你用后修改它 你定义一个不调用Foo :: Call的Bar :: call修饰符不是神奇的运行时事物,而是类定义时间事物。 要做你在这里尝试做的事情,你必须以不同的方式构建你的代码
What happens is this
you define a Foo::call you modify that with after you define a Bar::call that doesn't call Foo::CallThe modifiers are not magical runtime things, but class-definition time things. To do what you try to do here you'd have to structure your code differently
更多推荐
发布评论