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><<a href=3D=22mailto:btilly@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><p>Change your = example to say $at->foo instead of $at->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->$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 <perigrin@prather.= org> wrote: <br>> So you'll need to provide a reduced example that demonstrates the = behavior <br>> your showing. When I tried to reproduce (with the script below) = the <br>> attribute was being set just fine. <br>> <br>> #!/usr/bin/env perl <br>> use 5.12.1; <br>> use warnings; <br>> <br>> { <br>> <br>> package AroundTest; <br>> use Moose; <br>> <br>> has foo =3D> ( is =3D> 'rw' ); <br>> around foo =3D> sub { <br>> my ( $next, $self, $seq ) =3D @=5F; <br>> $seq =3D uc($seq); <br>> $self->$next($seq); <br>> }; <br>> } <br>> <br>> my $at =3D AroundTest->new(); <br>> $at->foo('bar'); <br>> say $at->dump; <br>> <br>> <br>> <br>> On Thu, Aug 6, 2015 at 7:04 PM, Marcos Barbeitos = <[email protected]> <br>> wrote: <br>>> <br>>> Howdy, <br>>> <br>>> I looked up the behavior of the modifier 'around' in <br>>> <http://search.cpan.org/~ether/Moose-2.= 1600/lib/Moose/Manual/MethodModifiers.pod#Around=5Fmodifiers>, <br>>> and the code snipet is: <br>>> <br>>> around 'size' =3D> sub { <br>>> my $orig =3D shift; <br>>> my $self =3D shift; <br>>> <br>>> return $self->$orig() <br>>> unless @=5F; <br>>> <br>>> my $size =3D shift; <br>>> $size =3D $size / 2 <br>>> if $self->likes=5Fsmall=5Fthings(); <br>>> <br>>> return $self->$orig($size); <br>>> }; <br>>> <br>>> In my code, I have: <br>>> <br>>> has 'sequence' =3D> <br>>> ( <br>>> is =3D> 'rw' <br>>> , isa =3D> 'Str' <br>>> , predicate =3D> 'has=5Fsequence' <br>>> ); <br>>> <br>>> around 'sequence' =3D> sub <br>>> { <br>>> my $orig =3D shift; <br>>> my $self =3D shift; <br>>> my $sequence =3D uc shift; <br>>> <br>>> # Do lots of things with $sequence and then <br>>> <br>>> return $self->$orig( $sequence ); <br>>> } <br>>> <br>>> But the attribute is not set. <br>>> <br>>> I've tried lots of variations of the last line: <br>>> <br>>> $self->$orig( $sequence ); <br>>> return $orig->( $self, $sequence ); <br>>> $orig->( $self, $sequence ); <br>>> return $sequence; <br>>> <br>>> With no success, as expected. However, if I do: <br>>> <br>>> around 'sequence' =3D> sub <br>>> { <br>>> my $orig =3D shift; <br>>> my $self =3D shift; <br>>> <br>>> return $self->$orig( @=5F ); <br>>> } <br>>> <br>>> The attribute is set and life goes on. Of course, that does = not work for <br>>> me because I need to do a bunch of things to the argument = passed to this <br>>> method. <br>>> <br>>> Any ideas about the reasons for the (apparent=3F) discrepancy = in behavior=3F <br>>> <br>>> Best wishes and thanks in advance. <br>>> <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>>> <br>>> Phone: (55 41) 3361-1634 <br>> <br>> <br></p></blockquote></div><br> ------Nodemailer-0.5.0-?=_1-1438981770611--