develooper Front page | perl.perl5.porters | Postings from January 2011

Changing variable to numeric without assignment (was: Using smart matching)

Thread Previous | Thread Next
From:
Ed Avis
Date:
January 27, 2011 02:39
Subject:
Changing variable to numeric without assignment (was: Using smart matching)
Message ID:
loom.20110127T113604-626@post.gmane.org
Konovalov, Vadim <vadim.konovalov <at> alcatel-lucent.com> writes:

>>>   $ perl -E '$a = $b = "abc100"; 0 + $b; say $a; say $b;
>>>              $a ++; $b ++; say $a; say $b'
>> 
>>That is a nasty one.  Surely the expression 0 + $b should not 
>>mutate the value of $b.

>  perldoc Scalar::Util
> 
>dualvar section?

Thanks for the pointer.  I hadn't read that although I was aware of the common
'zero but true' trick.

However the existence of dual-valued variables isn't the issue here.  The point
is that just evaluating the expression ($b + 0) in some other code, even if you
do not assign the result to $b or try to change $b in any way, has the
unexpected side-effect of changing the value stored in $b.  That can't be right.

-- 
Ed Avis <eda@waniasset.com>


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