Moose triggers & method modifiers

[email protected] (Robert Freimuth via moose) Wed, 28 Mar 2018 21:46:33 +0000 (UTC)
Newsgroups perl.moose
Message-ID <[email protected]>
------=_Part_517944_957974293.1522273593152
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

I've been learning Moose and I came across unexpected behavior: method modi=
fiers (such as 'after') do not seem to work with attribute triggers. This i=
s 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' =3D> ( is =3D> 'rw', trigger =3D> \&attrib_changed );has 'work=
around' =3D> ( is =3D> 'rw', trigger =3D> \&workaround_changed );
sub attrib_changed{=C2=A0 =C2=A0 print " =C2=A0in attrib_changed\n"; # call=
ed}
after 'attrib_changed' =3D> sub{=C2=A0 =C2=A0 print " =C2=A0 =C2=A0in 'afte=
r' attrib_changed\n"; # not called};
sub workaround_changed{=C2=A0 =C2=A0 my ( $self ) =3D @_;=C2=A0 =C2=A0 prin=
t " =C2=A0in workaround_changed\n"; # called=C2=A0 =C2=A0 $self->not_a_trig=
ger;}
after 'workaround_changed' =3D> sub{=C2=A0 =C2=A0 print " =C2=A0 =C2=A0in '=
after' workaround_changed\n"; # not called};
sub not_a_trigger{=C2=A0 =C2=A0 print " =C2=A0in not_a_trigger\n"; # called=
}
after 'not_a_trigger' =3D> sub{=C2=A0 =C2=A0 print " =C2=A0 =C2=A0in 'after=
' not_a_trigger\n"; # called};
my $foo =3D Foo->new;
print "Calling attrib( 1 ):\n";$foo->attrib( 1 );
print "Calling workaround( 1 ):\n";$foo->workaround( 1 );

Output:
Calling attrib( 1 ):=C2=A0 in attrib_changedCalling workaround( 1 ):=C2=A0 =
in workaround_changed=C2=A0 in not_a_trigger=C2=A0 =C2=A0 in 'after' not_a_=
trigger
I could not find anything in the Moose docs or online that documents this b=
ehavior 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 i=
s best done some other way. Thoughts?


------=_Part_517944_957974293.1522273593152
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<html><head></head><body><div style=3D"font-family:Helvetica Neue, Helvetic=
a, Arial, sans-serif;font-size:13px;"><div fgid=3D"46768"><span fgid=3D"467=
80"><div fgid=3D"46786">I've been learning Moose and I came across unexpect=
ed behavior: method modifiers (such as 'after') do not seem to work with at=
tribute triggers. This is an issue because I want a subclass to extend a me=
thod that is defined as a trigger in the parent. The issue and one workarou=
nd is shown below.</div><div fgid=3D"46793"><br fgid=3D"46799"></div><div f=
gid=3D"46805">use strict;</div><div fgid=3D"46812">use warnings;</div><div =
fgid=3D"46819"><br fgid=3D"46825"></div><div fgid=3D"46831">package Foo;</d=
iv><div fgid=3D"46838"><br fgid=3D"46844"></div><div fgid=3D"46850">use Moo=
se;</div><div fgid=3D"46857"><br fgid=3D"46863"></div><div fgid=3D"46869">h=
as 'attrib' =3D&gt; ( is =3D&gt; 'rw', trigger =3D&gt; \&amp;attrib_changed=
 );</div><div fgid=3D"46876">has 'workaround' =3D&gt; ( is =3D&gt; 'rw', tr=
igger =3D&gt; \&amp;workaround_changed );</div><div fgid=3D"46883"><br fgid=
=3D"46889"></div><div fgid=3D"46895">sub attrib_changed</div><div fgid=3D"4=
6902">{</div><div fgid=3D"46909">&nbsp; &nbsp; print " &nbsp;in attrib_chan=
ged\n"; # called</div><div fgid=3D"46916">}</div><div fgid=3D"46923"><br fg=
id=3D"46929"></div><div fgid=3D"46935">after 'attrib_changed' =3D&gt; sub</=
div><div fgid=3D"46942">{</div><div fgid=3D"46949">&nbsp; &nbsp; print " &n=
bsp; &nbsp;in 'after' attrib_changed\n"; # not called</div><div fgid=3D"469=
56">};</div><div fgid=3D"46963"><br fgid=3D"46969"></div><div fgid=3D"46975=
">sub workaround_changed</div><div fgid=3D"46982">{</div><div fgid=3D"46989=
">&nbsp; &nbsp; my ( $self ) =3D @_;</div><div fgid=3D"46996">&nbsp; &nbsp;=
 print " &nbsp;in workaround_changed\n"; # called</div><div fgid=3D"47003">=
&nbsp; &nbsp; $self-&gt;not_a_trigger;</div><div fgid=3D"47010">}</div><div=
 fgid=3D"47017"><br fgid=3D"47023"></div><div fgid=3D"47029">after 'workaro=
und_changed' =3D&gt; sub</div><div fgid=3D"47036">{</div><div fgid=3D"47043=
">&nbsp; &nbsp; print " &nbsp; &nbsp;in 'after' workaround_changed\n"; # no=
t called</div><div fgid=3D"47050">};</div><div fgid=3D"47057"><br fgid=3D"4=
7063"></div><div fgid=3D"47069">sub not_a_trigger</div><div fgid=3D"47076">=
{</div><div fgid=3D"47083">&nbsp; &nbsp; print " &nbsp;in not_a_trigger\n";=
 # called</div><div fgid=3D"47090">}</div><div fgid=3D"47097"><br fgid=3D"4=
7103"></div><div fgid=3D"47109">after 'not_a_trigger' =3D&gt; sub</div><div=
 fgid=3D"47116">{</div><div fgid=3D"47123">&nbsp; &nbsp; print " &nbsp; &nb=
sp;in 'after' not_a_trigger\n"; # called</div><div fgid=3D"47130">};</div><=
div fgid=3D"47137"><br fgid=3D"47143"></div><div fgid=3D"47149">my $foo =3D=
 Foo-&gt;new;</div><div fgid=3D"47156"><br fgid=3D"47162"></div><div fgid=
=3D"47168">print "Calling attrib( 1 ):\n";</div><div fgid=3D"47175">$foo-&g=
t;attrib( 1 );</div><div fgid=3D"47182"><br fgid=3D"47188"></div><div fgid=
=3D"47194">print "Calling workaround( 1 ):\n";</div><div fgid=3D"47201">$fo=
o-&gt;workaround( 1 );</div><div fgid=3D"47208"><br fgid=3D"47290"></div><d=
iv fgid=3D"47296"><br fgid=3D"47302"></div><div fgid=3D"47215">Output:</div=
><div fgid=3D"47222"><br fgid=3D"47228"></div><div fgid=3D"47234">Calling a=
ttrib( 1 ):</div><div fgid=3D"47241">&nbsp; in attrib_changed</div><div fgi=
d=3D"47248">Calling workaround( 1 ):</div><div fgid=3D"47255">&nbsp; in wor=
karound_changed</div><div fgid=3D"47262">&nbsp; in not_a_trigger</div><div =
fgid=3D"47269">&nbsp; &nbsp; in 'after' not_a_trigger</div><div fgid=3D"472=
76"><br fgid=3D"47308"></div><div fgid=3D"47283">I could not find anything =
in the Moose docs or online that documents this behavior as a known limitat=
ion. 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?</div></span><br fgid=3D"46774"></div><div fgid=3D"47314"><br fg=
id=3D"47320"></div></div></body></html>
------=_Part_517944_957974293.1522273593152--