Re: 'around' method modifier does not seem to work

[email protected] ("Chris Prather") Fri, 07 Aug 2015 14:09:30 -0700 (PDT)
Newsgroups perl.moose
Message-ID <1438981770245.5432f7da@Nodemailer>
------Nodemailer-0.5.0-?=_1-1438981770611
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable

If you check the part he quoted in the original email, that is in there. =
Obviously three of us missed it so maybe it could be highlighted better.




-Chris

On Fri, Aug 7, 2015 at 2:28 PM, Ben Tilly <[email protected]> wrote:

> Change your example to say $at->foo instead of $at->dump to see the bug.
> Add a check in the around to only try to set foo if @=5F actually had 3
> elements.  Then the bug should go away.
> I suggest that the documentation add a comment like this:
>       # Return the existing value if we are not trying to change it.
>       return $self->$orig()
>           unless @=5F;
> That might keep someone else from making the same mistake.
> On Fri, Aug 7, 2015 at 9:27 AM, Chris Prather <[email protected]> =
wrote:
>> 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;
>>
>> {
>>
>>     package AroundTest;
>>     use Moose;
>>
>>     has foo =3D> ( is =3D> 'rw' );
>>     around foo =3D> sub {
>>         my ( $next, $self, $seq ) =3D @=5F;
>>         $seq =3D uc($seq);
>>         $self->$next($seq);
>>     };
>> }
>>
>> my $at =3D AroundTest->new();
>> $at->foo('bar');
>> say $at->dump;
>>
>>
>>
>> On Thu, Aug 6, 2015 at 7:04 PM, Marcos Barbeitos <msbarbeitos@gmail.=
com>
>> wrote:
>>>
>>> Howdy,
>>>
>>> I looked up the behavior of the modifier 'around' in
>>> <http://search.cpan.org/~ether/Moose-2.1600/lib/Moose/Manual/MethodModi=
fiers.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-1438981770611
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable


<div>If you check the part he quoted in the original email, that is in =
there. Obviously three of us missed it so maybe it could be highlighted =
better.</div>
<div><br></div>
<div>-Chris</div>
<div class=3D=22mailbox=5Fsignature=22><br></div>
<br><br><div class=3D=22gmail=5Fquote=22><p>On Fri, Aug 7, 2015 at 2:28 PM,=
 Ben Tilly <span dir=3D=22ltr=22>&lt;<a href=3D=22mailto:btilly@gmail.=
com=22 target=3D=22=5Fblank=22>[email protected]</a>&gt;</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><p>Change your =
example to say $at-&gt;foo instead of $at-&gt;dump to see the bug.
<br><br>Add a check in the around to only try to set foo if @=5F actually =
had 3
<br>elements.  Then the bug should go away.
<br><br>I suggest that the documentation add a comment like this:
<br><br>      # Return the existing value if we are not trying to change it=
.
<br>      return $self-&gt;$orig()
<br>          unless @=5F;
<br><br>That might keep someone else from making the same mistake.
<br><br>On Fri, Aug 7, 2015 at 9:27 AM, Chris Prather &lt;perigrin@prather.=
org&gt; wrote:
<br>&gt; So you'll need to provide a reduced example that demonstrates the =
behavior
<br>&gt; your showing. When I tried to reproduce (with the script below) =
the
<br>&gt; attribute was being set just fine.
<br>&gt;
<br>&gt; #!/usr/bin/env perl
<br>&gt; use 5.12.1;
<br>&gt; use warnings;
<br>&gt;
<br>&gt; {
<br>&gt;
<br>&gt;     package AroundTest;
<br>&gt;     use Moose;
<br>&gt;
<br>&gt;     has foo =3D&gt; ( is =3D&gt; 'rw' );
<br>&gt;     around foo =3D&gt; sub {
<br>&gt;         my ( $next, $self, $seq ) =3D @=5F;
<br>&gt;         $seq =3D uc($seq);
<br>&gt;         $self-&gt;$next($seq);
<br>&gt;     };
<br>&gt; }
<br>&gt;
<br>&gt; my $at =3D AroundTest-&gt;new();
<br>&gt; $at-&gt;foo('bar');
<br>&gt; say $at-&gt;dump;
<br>&gt;
<br>&gt;
<br>&gt;
<br>&gt; On Thu, Aug 6, 2015 at 7:04 PM, Marcos Barbeitos =
&lt;[email protected]&gt;
<br>&gt; wrote:
<br>&gt;&gt;
<br>&gt;&gt; Howdy,
<br>&gt;&gt;
<br>&gt;&gt; I looked up the behavior of the modifier 'around' in
<br>&gt;&gt; &lt;http://search.cpan.org/~ether/Moose-2.=
1600/lib/Moose/Manual/MethodModifiers.pod#Around=5Fmodifiers&gt;,
<br>&gt;&gt; and the code snipet is:
<br>&gt;&gt;
<br>&gt;&gt;   around 'size' =3D&gt; sub {
<br>&gt;&gt;       my $orig =3D shift;
<br>&gt;&gt;       my $self =3D shift;
<br>&gt;&gt;
<br>&gt;&gt;       return $self-&gt;$orig()
<br>&gt;&gt;           unless @=5F;
<br>&gt;&gt;
<br>&gt;&gt;       my $size =3D shift;
<br>&gt;&gt;       $size =3D $size / 2
<br>&gt;&gt;           if $self-&gt;likes=5Fsmall=5Fthings();
<br>&gt;&gt;
<br>&gt;&gt;       return $self-&gt;$orig($size);
<br>&gt;&gt;   };
<br>&gt;&gt;
<br>&gt;&gt; In my code, I have:
<br>&gt;&gt;
<br>&gt;&gt; has 'sequence' =3D&gt;
<br>&gt;&gt; (
<br>&gt;&gt;     is =3D&gt; 'rw'
<br>&gt;&gt;   , isa =3D&gt; 'Str'
<br>&gt;&gt;   , predicate =3D&gt; 'has=5Fsequence'
<br>&gt;&gt; );
<br>&gt;&gt;
<br>&gt;&gt; around 'sequence' =3D&gt; sub
<br>&gt;&gt; {
<br>&gt;&gt;     my $orig =3D shift;
<br>&gt;&gt;     my $self =3D shift;
<br>&gt;&gt;     my $sequence =3D uc shift;
<br>&gt;&gt;
<br>&gt;&gt;     # Do lots of things with $sequence and then
<br>&gt;&gt;
<br>&gt;&gt;     return $self-&gt;$orig( $sequence );
<br>&gt;&gt; }
<br>&gt;&gt;
<br>&gt;&gt; But the attribute is not set.
<br>&gt;&gt;
<br>&gt;&gt; I've tried lots of variations of the last line:
<br>&gt;&gt;
<br>&gt;&gt; $self-&gt;$orig( $sequence );
<br>&gt;&gt; return $orig-&gt;( $self, $sequence );
<br>&gt;&gt; $orig-&gt;( $self, $sequence );
<br>&gt;&gt; return $sequence;
<br>&gt;&gt;
<br>&gt;&gt; With no success, as expected. However, if I do:
<br>&gt;&gt;
<br>&gt;&gt; around 'sequence' =3D&gt; sub
<br>&gt;&gt; {
<br>&gt;&gt;     my $orig =3D shift;
<br>&gt;&gt;     my $self =3D shift;
<br>&gt;&gt;
<br>&gt;&gt;     return $self-&gt;$orig( @=5F );
<br>&gt;&gt; }
<br>&gt;&gt;
<br>&gt;&gt; The attribute is set and life goes on. Of course, that does =
not work for
<br>&gt;&gt; me because I need to do a bunch of things to the argument =
passed to this
<br>&gt;&gt; method.
<br>&gt;&gt;
<br>&gt;&gt; Any ideas about the reasons for the (apparent=3F) discrepancy =
in behavior=3F
<br>&gt;&gt;
<br>&gt;&gt; Best wishes and thanks in advance.
<br>&gt;&gt;
<br>&gt;&gt; --
<br>&gt;&gt; Marcos S. Barbeitos
<br>&gt;&gt;
<br>&gt;&gt; Departamento de Zoologia - Sala 360
<br>&gt;&gt; Setor de Ci=C3=AAncias Biol=C3=B3gicas
<br>&gt;&gt; Universidade Federal do Paran=C3=A1
<br>&gt;&gt; Caixa Postal 19020
<br>&gt;&gt; Curitiba, PR 81531-990
<br>&gt;&gt; Brazil
<br>&gt;&gt;
<br>&gt;&gt; Phone: (55 41) 3361-1634
<br>&gt;
<br>&gt;
<br></p></blockquote></div><br>
------Nodemailer-0.5.0-?=_1-1438981770611--