Re: 'around' method modifier does not seem to work
[email protected] ("Chris Prather") Fri, 07 Aug 2015 09:27:44 -0700 (PDT)
| Newsgroups | perl.moose |
|---|---|
| Message-ID | <1438964863928.d6f7c45f@Nodemailer> |
------Nodemailer-0.5.0-?=_1-1438964864291
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable
So you'll need to provide a reduced example that demonstrates the behavior =
your showing. When I tried to reproduce (with the script below) the =
attribute was being set just fine.
#!/usr/bin/env perl
use 5.12.1;
use warnings;
{
=C2=A0 =C2=A0 package AroundTest;
=C2=A0 =C2=A0 use Moose;
=C2=A0 =C2=A0 has foo =3D> ( is =3D> 'rw' );
=C2=A0 =C2=A0 around foo =3D> sub {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 my ( $next, $self, $seq ) =3D @=5F;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 $seq =3D uc($seq);
=C2=A0 =C2=A0 =C2=A0 =C2=A0 $self->$next($seq);
=C2=A0 =C2=A0 };
}
my $at =3D AroundTest->new();
$at->foo('bar');
say $at->dump;
On Thu, Aug 6, 2015 at 7:04 PM, 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=5Fmodifiers>,
> and the code snipet is:
> around 'size' =3D> sub {
> my $orig =3D shift;
> my $self =3D shift;
> return $self->$orig()
> unless @=5F;
> my $size =3D shift;
> $size =3D $size / 2
> if $self->likes=5Fsmall=5Fthings();
> return $self->$orig($size);
> };
> In my code, I have:
> has 'sequence' =3D>
> (
> is =3D> 'rw'
> , isa =3D> 'Str'
> , predicate =3D> 'has=5Fsequence'
> );
> 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( @=5F );
> }
> 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=3F) discrepancy in =
behavior=3F
> 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
------Nodemailer-0.5.0-?=_1-1438964864291
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable
<div>So you'll need to provide a reduced example that demonstrates the =
behavior your showing. When I tried to reproduce (with the script below) =
the attribute was being set just fine.</div>
<div><br></div>
<div>#!/usr/bin/env perl</div>
<div>use 5.12.1;</div>
<div>use warnings;</div>
<div><br></div>
<div>{</div>
<div><br></div>
<div>=C2=A0 =C2=A0 package AroundTest;</div>
<div>=C2=A0 =C2=A0 use Moose;</div>
<div><br></div>
<div>=C2=A0 =C2=A0 has foo =3D> ( is =3D> 'rw' );</div>
<div>=C2=A0 =C2=A0 around foo =3D> sub {</div>
<div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 my ( $next, $self, $seq ) =3D @=5F;</div>
<div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 $seq =3D uc($seq);</div>
<div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 $self->$next($seq);</div>
<div>=C2=A0 =C2=A0 };</div>
<div>}</div>
<div><br></div>
<div>my $at =3D AroundTest->new();</div>
<div>$at->foo('bar');</div>
<div>say $at->dump;</div>
<div class=3D=22mailbox=5Fsignature=22><br></div>
<br><br><div class=3D=22gmail=5Fquote=22><p>On Thu, Aug 6, 2015 at 7:04 PM,=
Marcos Barbeitos <span dir=3D=22ltr=22><<a href=3D=22mailto:msbarbeitos=
@gmail.com=22 target=3D=22=5Fblank=22>[email protected]</a>></span> =
wrote:<br></p><blockquote class=3D=22gmail=5Fquote=22 style=3D=22margin:0 0=
0 .8ex;border-left:1px #ccc solid;padding-left:1ex;=22><div><div =
dir=3D=22ltr=22>Howdy,<br><br>I looked up the behavior of the modifier =
'around' in <<a href=3D=22http://search.cpan.org/~ether/Moose-2.=
1600/lib/Moose/Manual/MethodModifiers.pod#Around=5Fmodifiers=22>http://sear=
ch.cpan.org/~ether/Moose-2.1600/lib/Moose/Manual/MethodModifiers.=
pod#Around=5Fmodifiers</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 my $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 @=5F;<br><br>=C2=A0 =C2=A0 =C2=A0 my $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=5Fsmall=5Fthings();<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' =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 , predicate =3D> 'has=5Fsequence'</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 shift;</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>
<div><br></div>
<div>=C2=A0 =C2=A0 return $self->$orig( $sequence );<br></div>
<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 $orig->( $self, $sequence );</div>
<div>$orig->( $self, $sequence ); =C2=A0 =C2=A0</div>
<div>return $sequence;</div>
<div><br></div>
<div>With no success, as expected. However, if I do:</div>
<div><br></div>
<div>
<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 shift;</div>
<div><br></div>
<div>=C2=A0 =C2=A0 return $self->$orig( @=5F );<br></div>
<div>}</div>
</div>
</div>
<div><br></div>
<div>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.</div>
</div>
<div><br></div>
<div>Any ideas about the reasons for the (apparent=3F) discrepancy in =
behavior=3F<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>Setor de Ci=C3=AAncias Biol=C3=B3gicas<br>Universidade Federal do =
Paran=C3=A1<br>Caixa Postal 19020<br>Curitiba, PR 81531-990<br>Brazil<br><b=
r>Phone: (55 41) 3361-1634<br><div>
</div>
</div>
</div></div></blockquote></div><br>
------Nodemailer-0.5.0-?=_1-1438964864291--