'around' method modifier does not seem to work
[email protected] (Marcos Barbeitos) Thu, 6 Aug 2015 20:04:39 -0300
| Newsgroups | perl.moose |
|---|---|
| Message-ID | <CA+gEHsXKLYBq6ZhU412k=-iqJZup4ru0sNxVUgp2HiDVKUo5Aw@mail.gmail.com> |
--001a11425c52419140051cac8b4c
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
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' =3D> sub {
my $orig =3D shift;
my $self =3D shift;
return $self->$orig()
unless @_;
my $size =3D shift;
$size =3D $size / 2
if $self->likes_small_things();
return $self->$orig($size);
};
In my code, I have:
has 'sequence' =3D>
(
is =3D> 'rw'
, isa =3D> 'Str'
, predicate =3D> 'has_sequence'
);
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( @_ );
}
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=C3=AAncias Biol=C3=B3gicas
Universidade Federal do Paran=C3=A1
Caixa Postal 19020
Curitiba, PR 81531-990
Brazil
Phone: (55 41) 3361-1634
--001a11425c52419140051cac8b4c
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">Howdy,<br><br>I looked up the behavior of the modifier =
9;around' in <<a href=3D"http://search.cpan.org/~ether/Moose-2.1600/=
lib/Moose/Manual/MethodModifiers.pod#Around_modifiers">http://search.cpan.o=
rg/~ether/Moose-2.1600/lib/Moose/Manual/MethodModifiers.pod#Around_modifier=
s</a>>, and the code snipet is:<br><br>=C2=A0 around 'size' =3D&=
gt; 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 @_;<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_small_things();<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 'sequenc=
e' =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 , pr=
edicate =3D> 'has_sequence'</div><div>);</div></div><div><br></d=
iv><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><di=
v><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 m=
y $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( @_ );<br></di=
v><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?) discrepancy in behavi=
or?<br></div><div><br></div><div>Best wishes and thanks in advance.</div><b=
r>--<br>Marcos S. Barbeitos<br><br>Departamento de Zoologia - Sala 360<br>S=
etor 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><div>
</div></div></div>
--001a11425c52419140051cac8b4c--