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

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

Thread Previous | Thread Next
From:
John Macdonald
Date:
August 9, 2015 20:57
Subject:
RE: 'around' method modifier does not seem to work
Message ID:
01EF6156B387B14E8278ECC376793BE19E153533@exmb2.ad.oicr.on.ca
As Ben suggested, setting the attribute is working fine; but any read of the attribute sets it to undef.  I added two lines to the end of Chris' test:

...
say $at->dump;
say $at->foo;
say $at->dump;

Now running it gives:

perl test.pl
$VAR1 = bless( {
                 'foo' => 'BAR'
               }, 'AroundTest' );

Use of uninitialized value $seq in uc at test.pl line 13.

$VAR1 = bless( {
                 'foo' => ''
               }, 'AroundTest' );

Write-only attributes are generally not very useful.  :-)


John Macdonald
Software Engineer

Ontario Institute for Cancer Research
MaRS Centre

661 University Avenue

Suite 510
Toronto, Ontario

Canada M5G 0A3


Tel:

Email: John.Macdonald@oicr.on.ca

Toll-free: 1-866-678-6427
Twitter: @OICR_news


www.oicr.on.ca<http://www.oicr.on.ca/>

This message and any attachments may contain confidential and/or privileged information for the sole use of the intended recipient. Any review or distribution by anyone other than the person for whom it was originally intended is strictly prohibited. If you have received this message in error, please contact the sender and delete all copies. Opinions, conclusions or other information contained in this message may not be that of the organization.

________________________________
From: Chris Prather [perigrin@prather.org]
Sent: August 7, 2015 12:27 PM
To: Marcos Barbeitos
Cc: moose@perl.org
Subject: Re: 'around' method modifier does not seem to work

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 => ( is => 'rw' );
    around foo => sub {
        my ( $next, $self, $seq ) = @_;
        $seq = uc($seq);
        $self->$next($seq);
    };
}

my $at = AroundTest->new();
$at->foo('bar');
say $at->dump;




On Thu, Aug 6, 2015 at 7:04 PM, Marcos Barbeitos <msbarbeitos@gmail.com<mailto: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 | Thread Next


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