Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

perl moose triggers in subclasses disrupt method modifiers

Tags:

perl

moose

I've found that if a subclass adds a trigger, then method modifiers from the base class don't run. This seems like a Moose bug, or at least non-intuitive. Here's my example:

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);

If you run this example, only the "this is my_trigger" warn runs, and the "before" modifier is ignored. I'm using Perl 5.14.2 with Moose 2.0402.

Is this correct behavior? It doesn't seem right, especially since the trigger will fire after the before when the trigger is defined directly in the base class.

like image 262
rrm1 Avatar asked Oct 07 '22 16:10

rrm1


1 Answers

On the principle that you should not be able to distinguish between inherited code and code in the class, I'd call this a bug.

It appears to be a general problem where adding to an attribute removes method modifiers. This code demonstrates your bug without involving triggers.

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.

like image 71
Schwern Avatar answered Oct 10 '22 04:10

Schwern