Subject: | Discrepancy in trigger arguments |
Date: | Thu, 19 Mar 2009 13:23:27 -0700 |
To: | bug-Moose [...] rt.cpan.org |
From: | Mark Swayne <mark.swayne [...] q.com> |
Report For Moose 0.72 with Class::MOP 0.78
Using Perl 5.8.8 (ActivePerl build 819). Windows XP SP 2.
In Moose::Manual::Attributes in the section on triggers, it says that
the code ref specified will by called as a method with two arguments:
the value set and the Moose::Meta::Attribute object for the attribute.
However, in Moose::Meta::Attribute the docs for the trigger method say
that the code ref will get the invocant and the new value.
When an attribute is set using the constructor, an attribute object is
passed to the trigger. However, when the attribute is set by calling
its setter method, no attribute object is passed to the trigger.
Test code:
use strict;
use warnings;
# _test_trigger gets attribute object:
my $test = MyClass->new(foo => 0);
# _test_trigger gets no attribute object:
$test->foo(1);
BEGIN {
package MyClass;
use Moose;
has 'foo' => (
is => 'rw',
isa => 'Str',
trigger => \&_test_trigger,
);
sub _test_trigger {
my $args = @_;
warn "_test_trigger called\n";
warn join ' ', "Args $args:", map "<$_>", @_;
}
no Moose;
}
Results:
C:\>test.pl
_test_trigger called
Args 3: <MyClass=HASH(0x20d73d8)> <0>
<Moose::Meta::Attribute=HASH(0x20d72d0)> at C:\test.pl line 21.
_test_trigger called
Args 2: <MyClass=HASH(0x20d73d8)> <1> at C:\test.pl line 21.
I'm not familiar with Moose internals, but believe changing the
set_value method of Moose::Meta::Attribute will fix the problem.
if ($self->has_trigger) {
$self->trigger->($instance, $value);
}
Should be:
if ($self->has_trigger) {
$self->trigger->($instance, $value, $self);
}
In case you don't hear it often enough, Moose is a great thing, thank you.