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 &#39;attrib&#39;=C2=A0=C2=A0=C2=A0=
=C2=A0 =3D&gt; (is =3D&gt; &#39;rw&#39;, trigger =3D&gt; sub { my $self =3D=
 shift; $self-&gt;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">&lt;<a href=3D"mailto:[email protected]" targe=
t=3D"_blank">[email protected]</a>&gt;</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&#39;ve been learning Moose and I c=
ame across unexpected behavior: method modifiers (such as &#39;after&#39;) =
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 &#39;attrib&#39=
; =3D&gt; ( is =3D&gt; &#39;rw&#39;, trigger =3D&gt; \&amp;attrib_changed )=
;</div><div>has &#39;workaround&#39; =3D&gt; ( is =3D&gt; &#39;rw&#39;, tri=
gger =3D&gt; \&amp;workaround_changed );</div><div><br></div><div>sub attri=
b_changed</div><div>{</div><div>=C2=A0 =C2=A0 print &quot; =C2=A0in attrib_=
changed\n&quot;; # called</div><div>}</div><div><br></div><div>after &#39;a=
ttrib_changed&#39; =3D&gt; sub</div><div>{</div><div>=C2=A0 =C2=A0 print &q=
uot; =C2=A0 =C2=A0in &#39;after&#39; attrib_changed\n&quot;; # 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&quot;; # called</div><div>=C2=A0 =C2=A0 $=
self-&gt;not_a_trigger;</div><div>}</div><div><br></div><div>after &#39;wor=
karound_changed&#39; =3D&gt; sub</div><div>{</div><div>=C2=A0 =C2=A0 print =
&quot; =C2=A0 =C2=A0in &#39;after&#39; workaround_changed\n&quot;; # not ca=
lled</div><div>};</div><div><br></div><div>sub not_a_trigger</div><div>{</d=
iv><div>=C2=A0 =C2=A0 print &quot; =C2=A0in not_a_trigger\n&quot;; # called=
</div><div>}</div><div><br></div><div>after &#39;not_a_trigger&#39; =3D&gt;=
 sub</div><div>{</div><div>=C2=A0 =C2=A0 print &quot; =C2=A0 =C2=A0in &#39;=
after&#39; not_a_trigger\n&quot;; # called</div><div>};</div><div><br></div=
><div>my $foo =3D Foo-&gt;new;</div><div><br></div><div>print &quot;Calling=
 attrib( 1 ):\n&quot;;</div><div>$foo-&gt;attrib( 1 );</div><div><br></div>=
<div>print &quot;Calling workaround( 1 ):\n&quot;;</div><div>$foo-&gt;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 &#39;after&#39; 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&#39;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--