develooper Front page | perl.moose | Postings from August 2015

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

Thread Previous
From:
Ben Tilly
Date:
August 7, 2015 02:02
Subject:
Re: 'around' method modifier does not seem to work
Message ID:
CANoac9XELUJ0Jx_=uZ_zc45k-JjECXOV2utZ--J+ruyv0YUy7A@mail.gmail.com
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 <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/MethodModifiers.pod#Around_modifiers>,
> and the code snipet is:
>
>   around 'size' => sub {
>       my $orig = shift;
>       my $self = shift;
>
>       return $self->$orig()
>           unless @_;
>
>       my $size = shift;
>       $size = $size / 2
>           if $self->likes_small_things();
>
>       return $self->$orig($size);
>   };
>
> In my code, I have:
>
> has 'sequence' =>
> (
>     is => 'rw'
>   , isa => 'Str'
>   , predicate => 'has_sequence'
> );
>
> around 'sequence' => sub
> {
>     my $orig = shift;
>     my $self = shift;
>     my $sequence = 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' => sub
> {
>     my $orig = shift;
>     my $self = 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ências Biológicas
> Universidade Federal do Paraná
> Caixa Postal 19020
> Curitiba, PR 81531-990
> Brazil
>
> Phone: (55 41) 3361-1634
>

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About