Re: 'around' method modifier does not seem to work
[email protected] (Ben Tilly) Thu, 6 Aug 2015 19:02:42 -0700
| Newsgroups | perl.moose |
|---|---|
| Message-ID | <CANoac9XELUJ0Jx_=uZ_zc45k-JjECXOV2utZ--J+ruyv0YUy7A@mail.gmail.com> |
--089e0115f83a026bd7051caf0815 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Stupid guess. You set the attribute successfully then unset it when you fetch it because you dropped the test for @_ that tells you not to set it then. On Thursday, August 6, 2015, Marcos Barbeitos <[email protected]> wrote= : > Howdy, > > I looked up the behavior of the modifier 'around' in < > http://search.cpan.org/~ether/Moose-2.1600/lib/Moose/Manual/MethodModifie= rs.pod#Around_modifiers>, > and the code snipet is: > > around 'size' =3D> sub { > my $orig =3D shift; > my $self =3D shift; > > return $self->$orig() > unless @_; > > my $size =3D shift; > $size =3D $size / 2 > if $self->likes_small_things(); > > return $self->$orig($size); > }; > > In my code, I have: > > has 'sequence' =3D> > ( > is =3D> 'rw' > , isa =3D> 'Str' > , predicate =3D> 'has_sequence' > ); > > around 'sequence' =3D> sub > { > my $orig =3D shift; > my $self =3D shift; > my $sequence =3D uc shift; > > # Do lots of things with $sequence and then > > return $self->$orig( $sequence ); > } > > But the attribute is not set. > > I've tried lots of variations of the last line: > > $self->$orig( $sequence ); > return $orig->( $self, $sequence ); > $orig->( $self, $sequence ); > return $sequence; > > With no success, as expected. However, if I do: > > around 'sequence' =3D> sub > { > my $orig =3D shift; > my $self =3D shift; > > return $self->$orig( @_ ); > } > > The attribute is set and life goes on. Of course, that does not work for > me because I need to do a bunch of things to the argument passed to this > method. > > Any ideas about the reasons for the (apparent?) discrepancy in behavior? > > Best wishes and thanks in advance. > > -- > Marcos S. Barbeitos > > Departamento de Zoologia - Sala 360 > Setor de Ci=C3=AAncias Biol=C3=B3gicas > Universidade Federal do Paran=C3=A1 > Caixa Postal 19020 > Curitiba, PR 81531-990 > Brazil > > Phone: (55 41) 3361-1634 > --089e0115f83a026bd7051caf0815 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Stupid guess.<div><br></div><div>You set the attribute successfully then un= set it when you fetch it because you dropped the test for @_ that tells you= not to set it then.<span></span><br><br>On Thursday, August 6, 2015, Marco= s Barbeitos <<a href=3D"mailto:[email protected]">msbarbeitos@gmail.= com</a>> wrote:<br><blockquote class=3D"gmail_quote" style=3D"margin:0 0= 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir=3D"ltr">Howdy= ,<br><br>I looked up the behavior of the modifier 'around' in <<= a href=3D"http://search.cpan.org/~ether/Moose-2.1600/lib/Moose/Manual/Metho= dModifiers.pod#Around_modifiers" target=3D"_blank">http://search.cpan.org/~= ether/Moose-2.1600/lib/Moose/Manual/MethodModifiers.pod#Around_modifiers</a= >>, and the code snipet is:<br><br>=C2=A0 around 'size' =3D> = sub {<br>=C2=A0 =C2=A0 =C2=A0 my $orig =3D shift;<br>=C2=A0 =C2=A0 =C2=A0 m= y $self =3D shift;<br><br>=C2=A0 =C2=A0 =C2=A0 return $self->$orig()<br>= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 unless @_;<br><br>=C2=A0 =C2=A0 =C2=A0 m= y $size =3D shift;<br>=C2=A0 =C2=A0 =C2=A0 $size =3D $size / 2<br>=C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 if $self->likes_small_things();<br><br>=C2= =A0 =C2=A0 =C2=A0 return $self->$orig($size);<br>=C2=A0 };<div><br></div= ><div>In my code, I have:</div><div><div><br></div><div>has 'sequence&#= 39; =3D><br></div><div><div>(</div><div>=C2=A0 =C2=A0 is =3D> 'rw= '</div><div>=C2=A0 , isa =3D> 'Str'</div><div>=C2=A0 , predi= cate =3D> 'has_sequence'</div><div>);</div></div><div><br></div>= <div>around 'sequence' =3D> sub</div><div>{</div><div>=C2=A0 =C2= =A0 my $orig =3D shift;</div></div><div><div>=C2=A0 =C2=A0 my $self =3D shi= ft;</div><div>=C2=A0 =C2=A0 my $sequence =3D uc shift;<br></div><div><br></= div><div>=C2=A0 =C2=A0 # Do lots of things with $sequence and then</div><di= v><br></div><div>=C2=A0 =C2=A0 return $self->$orig( $sequence );<br></di= v><div>}</div><div><br></div><div>But the attribute is not set.</div><div><= br></div><div>I've tried lots of variations of the last line:</div><div= ><br></div><div>$self->$orig( $sequence );<br></div><div><div>return $or= ig->( $self, $sequence );</div><div>$orig->( $self, $sequence ); =C2= =A0 =C2=A0</div><div>return $sequence;</div><div><br></div><div>With no suc= cess, as expected. However, if I do:</div><div><br></div><div><div><div>aro= und 'sequence' =3D> sub</div><div>{</div><div>=C2=A0 =C2=A0 my $= orig =3D shift;</div></div><div><div>=C2=A0 =C2=A0 my $self =3D shift;</div= ><div><br></div><div>=C2=A0 =C2=A0 return $self->$orig( @_ );<br></div><= div>}</div></div></div><div><br></div><div>The attribute is set and life go= es on. Of course, that does not work for me because I need to do a bunch of= things to the argument passed to this method.</div></div><div><br></div><d= iv>Any ideas about the reasons for the (apparent?) discrepancy in behavior?= <br></div><div><br></div><div>Best wishes and thanks in advance.</div><br>-= -<br>Marcos S. Barbeitos<br><br>Departamento de Zoologia - Sala 360<br>Seto= r de Ci=C3=AAncias Biol=C3=B3gicas<br>Universidade Federal do Paran=C3=A1<b= r>Caixa Postal 19020<br>Curitiba, PR 81531-990<br>Brazil<br><br>Phone: (55 = 41) 3361-1634<br><div> </div></div></div> </blockquote></div> --089e0115f83a026bd7051caf0815--