基类中的Moose方法修饰符不会被调用(Moose method modifiers in base class don't get called)

编程入门 行业动态 更新时间:2024-10-28 06:27:30
类中的Moose方法修饰符不会被调用(Moose method modifiers in base class don't get called)

很酷的是,可以在子类中添加它们或者将它们混合在一起。 我的问题是,当子类重新定义方法本身(而不是修饰符)时,基类中的方法修饰符似乎被取消激活。 也许我理解方法修饰符错了。 例:

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; # 0

I 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::Call

The 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

更多推荐

本文发布于:2023-08-07 04:02:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1460069.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类中   方法   修饰符   Moose   method

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!