RE: 'around' method modifier does not seem to work
[email protected] (John Macdonald) Fri, 7 Aug 2015 16:42:31 +0000
| Newsgroups | perl.moose |
|---|---|
| Message-ID | <[email protected]> |
--_000_01EF6156B387B14E8278ECC376793BE19E153533exmb2adoicronca_
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
As Ben suggested, setting the attribute is working fine; but any read of th=
e 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 =3D bless( {
'foo' =3D> 'BAR'
}, 'AroundTest' );
Use of uninitialized value $seq in uc at test.pl line 13.
$VAR1 =3D bless( {
'foo' =3D> ''
}, '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: [email protected]
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 dist=
ribution by anyone other than the person for whom it was originally intende=
d is strictly prohibited. If you have received this message in error, pleas=
e 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 [[email protected]]
Sent: August 7, 2015 12:27 PM
To: Marcos Barbeitos
Cc: [email protected]
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 attribu=
te 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 @_;
$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 <[email protected]<mai=
lto:[email protected]>> wrote:
Howdy,
I looked up the behavior of the modifier 'around' in <http://search.cpan.or=
g/~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 meth=
od.
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=EAncias Biol=F3gicas
Universidade Federal do Paran=E1
Caixa Postal 19020
Curitiba, PR 81531-990
Brazil
Phone: (55 41) 3361-1634
--_000_01EF6156B387B14E8278ECC376793BE19E153533exmb2adoicronca_
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<html dir=3D"ltr">
<head>
<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Diso-8859-=
1">
<style id=3D"owaParaStyle" type=3D"text/css">P {margin-top:0;margin-bottom:=
0;}</style>
</head>
<body ocsi=3D"0" fpstyle=3D"1">
<div style=3D"direction: ltr;font-family: Tahoma;color: #000000;font-size: =
10pt;">As Ben suggested, setting the attribute is working fine; but any rea=
d of the attribute sets it to undef. I added two lines to the end of =
Chris' test:<br>
<br>
...<br>
say $at->dump;<br>
say $at->foo;<br>
say $at->dump;<br>
<br>
Now running it gives:<br>
<br>
perl test.pl<br>
$VAR1 =3D bless( {<br>
&nb=
sp; 'foo' =3D> 'BAR'<br>
&nb=
sp; }, 'AroundTest' );<br>
<br>
Use of uninitialized value $seq in uc at test.pl line 13.<br>
<br>
$VAR1 =3D bless( {<br>
&nb=
sp; 'foo' =3D> ''<br>
&nb=
sp; }, 'AroundTest' );<br>
<br>
Write-only attributes are generally not very useful. :-)<br>
<div><br>
<div style=3D"font-family:Tahoma; font-size:13px">
<p class=3D"western" style=3D"margin-bottom:0cm"><font color=3D"#39793b"><f=
ont style=3D"font-size:9pt" size=3D"2"><span lang=3D"en-US"><b>John Macdona=
ld</b></span></font></font><font color=3D"#39793b"><font style=3D"font-size=
:9pt" size=3D"2"><b><br>
</b></font></font><font style=3D"font-size:8pt" size=3D"1"><span lang=3D"en=
-US">Software Engineer<br>
<br>
</span></font><font color=3D"#39793b"><font style=3D"font-size:9pt" size=3D=
"2"><b>Ontario Institute for Cancer Research</b></font></font><font color=
=3D"#39793b"><font style=3D"font-size:8pt" size=3D"1"><br>
</font></font><font style=3D"font-size:8pt" size=3D"1">MaRS Centre</font></=
p>
<p class=3D"western" style=3D"margin-bottom:0cm"><font style=3D"font-size:8=
pt" size=3D"1">661 University Avenue</font></p>
<p class=3D"western" style=3D"margin-bottom:0cm"><font style=3D"font-size:8=
pt" size=3D"1">Suite 510
<br>
Toronto, Ontario </font></p>
<p class=3D"western" style=3D"margin-bottom:0cm"><font style=3D"font-size:8=
pt" size=3D"1">Canada M5G 0A3</font><font style=3D"font-size:8pt" size=
=3D"1"><span lang=3D"en-US"><br>
</span></font><br>
</p>
<p class=3D"western" style=3D"margin-bottom:0cm"><font style=3D"font-size:8=
pt" size=3D"1"><span lang=3D"en-US">Tel:
</span></font></p>
<p class=3D"western" style=3D"margin-bottom:0cm"><font style=3D"font-size:8=
pt" size=3D"1"><span lang=3D"en-US">Email: [email protected]</span>=
</font></p>
<p class=3D"western" style=3D"margin-bottom:0cm"><font style=3D"font-size:8=
pt" size=3D"1">Toll-free: 1-866-678-6427</font><font style=3D"font-size:8pt=
" size=3D"1"><span lang=3D"en-US"><br>
</span></font><font style=3D"font-size:8pt" size=3D"1">Twitter: @OICR_news<=
/font></p>
<p class=3D"western" style=3D"margin-bottom:0cm"><br>
</p>
<p class=3D"western" style=3D"margin-bottom:0cm"><font color=3D"#0000ff"><u=
><a class=3D"western" href=3D"http://www.oicr.on.ca/"><font style=3D"font-s=
ize:8pt" size=3D"1">www.oicr.on.ca</font></a></u></font><font style=3D"font=
-size:8pt" size=3D"1"><span lang=3D"en-US"><br>
<br>
</span></font><font color=3D"#000000"><font face=3D"Arial, sans-serif"><fon=
t style=3D"font-size:8pt" size=3D"1">This message and any attachments may c=
ontain confidential and/or privileged information for the sole use of the i=
ntended recipient. Any review or distribution
by anyone other than the person for whom it was originally intended is str=
ictly prohibited. If you have received this message in error, please contac=
t the sender and delete all copies. Opinions, conclusions or other informat=
ion contained in this message may
not be that of the organization.</font></font></font></p>
</div>
</div>
<div style=3D"font-family: Times New Roman; color: #000000; font-size: 16px=
">
<hr tabindex=3D"-1">
<div style=3D"direction: ltr;" id=3D"divRpF774846"><font face=3D"Tahoma" si=
ze=3D"2" color=3D"#000000"><b>From:</b> Chris Prather [[email protected]=
]<br>
<b>Sent:</b> August 7, 2015 12:27 PM<br>
<b>To:</b> Marcos Barbeitos<br>
<b>Cc:</b> [email protected]<br>
<b>Subject:</b> Re: 'around' method modifier does not seem to work<br>
</font><br>
</div>
<div></div>
<div>
<div>So you'll need to provide a reduced example that demonstrates the beha=
vior your showing. When I tried to reproduce (with the script below) the at=
tribute was being set just fine.</div>
<div><br>
</div>
<div>#!/usr/bin/env perl</div>
<div>use 5.12.1;</div>
<div>use warnings;</div>
<div><br>
</div>
<div>{</div>
<div><br>
</div>
<div> package AroundTest;</div>
<div> use Moose;</div>
<div><br>
</div>
<div> has foo =3D> ( is =3D> 'rw' );</div>
<div> around foo =3D> sub {</div>
<div> my ( $next, $self, $seq ) =3D @_;</div>
<div> $seq =3D uc($seq);</div>
<div> $self->$next($seq);</div>
<div> };</div>
<div>}</div>
<div><br>
</div>
<div>my $at =3D AroundTest->new();</div>
<div>$at->foo('bar');</div>
<div>say $at->dump;</div>
<div class=3D"mailbox_signature"><br>
</div>
<br>
<br>
<div class=3D"gmail_quote">
<p>On Thu, Aug 6, 2015 at 7:04 PM, Marcos Barbeitos <span dir=3D"ltr"><<=
a href=3D"mailto:[email protected]" target=3D"_blank">msbarbeitos@gmail=
.com</a>></span> wrote:<br>
</p>
<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex; border-left:1=
px #ccc solid; padding-left:1ex">
<div>
<div dir=3D"ltr">Howdy,<br>
<br>
I looked up the behavior of the modifier 'around' in <<a href=3D"http://=
search.cpan.org/~ether/Moose-2.1600/lib/Moose/Manual/MethodModifiers.pod#Ar=
ound_modifiers" target=3D"_blank">http://search.cpan.org/~ether/Moose-2.160=
0/lib/Moose/Manual/MethodModifiers.pod#Around_modifiers</a>>,
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 @_;<br>
<br>
my $size =3D shift;<br>
$size =3D $size / 2<br>
if $self->likes_small_things();<br>
<br>
return $self->$orig($size);<br>
};
<div><br>
</div>
<div>In my code, I have:</div>
<div>
<div><br>
</div>
<div>has 'sequence' =3D><br>
</div>
<div>
<div>(</div>
<div> is =3D> 'rw'</div>
<div> , isa =3D> 'Str'</div>
<div> , predicate =3D> 'has_sequence'</div>
<div>);</div>
</div>
<div><br>
</div>
<div>around 'sequence' =3D> sub</div>
<div>{</div>
<div> my $orig =3D shift;</div>
</div>
<div>
<div> my $self =3D shift;</div>
<div> my $sequence =3D uc shift;<br>
</div>
<div><br>
</div>
<div> # Do lots of things with $sequence and then</div>
<div><br>
</div>
<div> return $self->$orig( $sequence );<br>
</div>
<div>}</div>
<div><br>
</div>
<div>But the attribute is not set.</div>
<div><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 ); </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> my $orig =3D shift;</div>
</div>
<div>
<div> my $self =3D shift;</div>
<div><br>
</div>
<div> return $self->$orig( @_ );<br>
</div>
<div>}</div>
</div>
</div>
<div><br>
</div>
<div>The attribute is set and life goes on. Of course, that does not work f=
or 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 behavio=
r?<br>
</div>
<div><br>
</div>
<div>Best wishes and thanks in advance.</div>
<br>
--<br>
Marcos S. Barbeitos<br>
<br>
Departamento de Zoologia - Sala 360<br>
Setor de Ci=EAncias Biol=F3gicas<br>
Universidade Federal do Paran=E1<br>
Caixa Postal 19020<br>
Curitiba, PR 81531-990<br>
Brazil<br>
<br>
Phone: (55 41) 3361-1634<br>
<div></div>
</div>
</div>
</div>
</blockquote>
</div>
<br>
</div>
</div>
</div>
</body>
</html>
--_000_01EF6156B387B14E8278ECC376793BE19E153533exmb2adoicronca_--