【问题标题】:perl moose triggers in subclasses disrupt method modifiers子类中的 perl moose 触发器会破坏方法修饰符
【发布时间】:2012-09-04 01:04:20
【问题描述】:

我发现如果子类添加触发器,则基类中的方法修饰符不会运行。这似乎是一个 Moose 错误,或者至少是不直观的。这是我的例子:

package Foo {
    use Moose;

    has 'foo' => (
        is  => 'rw',
        isa => 'Str',
    );

    before 'foo' => sub {
        warn "before foo";
    };
};

package FooChild {

    use Moose;
    extends 'Foo';

    has '+foo' => ( trigger => \&my_trigger, );

    sub my_trigger {
        warn 'this is my_trigger';
    }
};

my $fc = FooChild->new();
$fc->foo(10);

如果您运行此示例,则只会运行“this is my_trigger”警告,而忽略“before”修饰符。我正在使用 Perl 5.14.2 和 Moose 2.0402。

这是正确的行为吗?这似乎不对,尤其是当触发器直接在基类中定义时,触发器将在之前触发。

【问题讨论】:

    标签: perl moose


    【解决方案1】:

    根据你不应该区分继承代码和类中的代码的原则,我称之为错误。

    添加到属性会删除方法修饰符似乎是一个普遍问题。此代码演示了您的错误,但不涉及触发器。

    package Foo {
        use Moose;
    
        has 'foo' => (
            is  => 'rw',
            isa => 'Str',
            default => 5,
        );
    
        before 'foo' => sub {
            warn "before foo";
        };
    };
    
    package FooChild {
    
        use Moose;
        extends 'Foo';
    
        has '+foo' => ( default => 99 );
    };
    
    my $fc = FooChild->new();
    print $fc->foo;
    

    Please report this to the Moose folks.

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-30
    • 2011-06-25
    • 2013-02-02
    • 2012-08-11
    • 1970-01-01
    • 2018-02-10
    • 2010-12-14
    • 2013-10-23
    相关资源
    最近更新 更多