Re: Moose triggers & method modifiers
[email protected] (Hernan Lopes) Fri, 30 Mar 2018 16:55:00 -0300
| Newsgroups | perl.moose |
|---|---|
| Message-ID | <CACE0rhrD908R84eThPinuhBvCcP5LheD7q46OMS1HE9_dqHJHw@mail.gmail.com> |
--001a114af28a9852ff0568a69d06
Content-Type: text/plain; charset="UTF-8"
You can do:
has 'attrib' => (is => 'rw', trigger => sub { my $self = shift;
$self->attrib_changed } );
On Wed, Mar 28, 2018 at 6:46 PM, Robert Freimuth via moose <[email protected]>
wrote:
> I've been learning Moose and I came across unexpected behavior: method
> modifiers (such as 'after') do not seem to work with attribute triggers.
> This is an issue because I want a subclass to extend a method that is
> defined as a trigger in the parent. The issue and one workaround is shown
> below.
>
> use strict;
> use warnings;
>
> package Foo;
>
> use Moose;
>
> has 'attrib' => ( is => 'rw', trigger => \&attrib_changed );
> has 'workaround' => ( is => 'rw', trigger => \&workaround_changed );
>
> sub attrib_changed
> {
> print " in attrib_changed\n"; # called
> }
>
> after 'attrib_changed' => sub
> {
> print " in 'after' attrib_changed\n"; # not called
> };
>
> sub workaround_changed
> {
> my ( $self ) = @_;
> print " in workaround_changed\n"; # called
> $self->not_a_trigger;
> }
>
> after 'workaround_changed' => sub
> {
> print " in 'after' workaround_changed\n"; # not called
> };
>
> sub not_a_trigger
> {
> print " in not_a_trigger\n"; # called
> }
>
> after 'not_a_trigger' => sub
> {
> print " in 'after' not_a_trigger\n"; # called
> };
>
> my $foo = Foo->new;
>
> print "Calling attrib( 1 ):\n";
> $foo->attrib( 1 );
>
> print "Calling workaround( 1 ):\n";
> $foo->workaround( 1 );
>
>
> Output:
>
> Calling attrib( 1 ):
> in attrib_changed
> Calling workaround( 1 ):
> in workaround_changed
> in not_a_trigger
> in 'after' not_a_trigger
>
> I could not find anything in the Moose docs or online that documents this
> behavior as a known limitation. I'd be surprised if I were the first one to
> notice it, so it makes me wonder whether I am trying to do something that
> is best done some other way. Thoughts?
>
>
>
--001a114af28a9852ff0568a69d06
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">You can do:<br><br>has 'attrib'=C2=A0=C2=A0=C2=A0=
=C2=A0 =3D> (is =3D> 'rw', trigger =3D> sub { my $self =3D=
shift; $self->attrib_changed } );<br></div><div class=3D"gmail_extra"><=
br><div class=3D"gmail_quote">On Wed, Mar 28, 2018 at 6:46 PM, Robert Freim=
uth via moose <span dir=3D"ltr"><<a href=3D"mailto:[email protected]" targe=
t=3D"_blank">[email protected]</a>></span> wrote:<br><blockquote class=3D"g=
mail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-l=
eft:1ex"><div><div style=3D"font-family:Helvetica Neue,Helvetica,Arial,sans=
-serif;font-size:13px"><div><span><div>I've been learning Moose and I c=
ame across unexpected behavior: method modifiers (such as 'after') =
do not seem to work with attribute triggers. This is an issue because I wan=
t a subclass to extend a method that is defined as a trigger in the parent.=
The issue and one workaround is shown below.</div><div><br></div><div>use =
strict;</div><div>use warnings;</div><div><br></div><div>package Foo;</div>=
<div><br></div><div>use Moose;</div><div><br></div><div>has 'attrib'=
; =3D> ( is =3D> 'rw', trigger =3D> \&attrib_changed )=
;</div><div>has 'workaround' =3D> ( is =3D> 'rw', tri=
gger =3D> \&workaround_changed );</div><div><br></div><div>sub attri=
b_changed</div><div>{</div><div>=C2=A0 =C2=A0 print " =C2=A0in attrib_=
changed\n"; # called</div><div>}</div><div><br></div><div>after 'a=
ttrib_changed' =3D> sub</div><div>{</div><div>=C2=A0 =C2=A0 print &q=
uot; =C2=A0 =C2=A0in 'after' attrib_changed\n"; # not called</=
div><div>};</div><div><br></div><div>sub workaround_changed</div><div>{</di=
v><div>=C2=A0 =C2=A0 my ( $self ) =3D @_;</div><div>=C2=A0 =C2=A0 print &qu=
ot; =C2=A0in workaround_changed\n"; # called</div><div>=C2=A0 =C2=A0 $=
self->not_a_trigger;</div><div>}</div><div><br></div><div>after 'wor=
karound_changed' =3D> sub</div><div>{</div><div>=C2=A0 =C2=A0 print =
" =C2=A0 =C2=A0in 'after' workaround_changed\n"; # not ca=
lled</div><div>};</div><div><br></div><div>sub not_a_trigger</div><div>{</d=
iv><div>=C2=A0 =C2=A0 print " =C2=A0in not_a_trigger\n"; # called=
</div><div>}</div><div><br></div><div>after 'not_a_trigger' =3D>=
sub</div><div>{</div><div>=C2=A0 =C2=A0 print " =C2=A0 =C2=A0in '=
after' not_a_trigger\n"; # called</div><div>};</div><div><br></div=
><div>my $foo =3D Foo->new;</div><div><br></div><div>print "Calling=
attrib( 1 ):\n";</div><div>$foo->attrib( 1 );</div><div><br></div>=
<div>print "Calling workaround( 1 ):\n";</div><div>$foo->worka=
round( 1 );</div><div><br></div><div><br></div><div>Output:</div><div><br><=
/div><div>Calling attrib( 1 ):</div><div>=C2=A0 in attrib_changed</div><div=
>Calling workaround( 1 ):</div><div>=C2=A0 in workaround_changed</div><div>=
=C2=A0 in not_a_trigger</div><div>=C2=A0 =C2=A0 in 'after' not_a_tr=
igger</div><div><br></div><div>I could not find anything in the Moose docs =
or online that documents this behavior as a known limitation. I'd be su=
rprised if I were the first one to notice it, so it makes me wonder whether=
I am trying to do something that is best done some other way. Thoughts?</d=
iv></span><br></div><div><br></div></div></div></blockquote></div><br></div=
>
--001a114af28a9852ff0568a69d06--