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

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

Thread Previous | Thread Next
Ed Avis
January 27, 2011 07:00
Re: Changing variable to numeric without assignment (was: Using?smart matching)
Message ID:
Abigail <abigail <at>> writes:

>>>>>>>   $ perl -E '$a = $b = "abc100"; 0 + $b; say $a; say $b;
>>>>>>>              $a ++; $b ++; say $a; say $b'

>>The numeric value could be cached in the scalar and used next time,
>>but without flipping the switch to mark the scalar as numeric.  Then
>>later numeric context uses would be fast, but the value would not
>>mysteriously change.

>That would probably mean we have to use a bit (or two, or three, if
>we want to use the same trick for floats and strings) in the very
>limited supply of bits reserved in the SV structure, for a case
>that's only seldom useful.

Hmm.  To my mind the example of C<map { $_ + $b } @a> is the case
that's only seldom useful - or at least the speedup from mutating $b
is relatively unimportant - whereas the guarantee that variable
contents won't change without an explicit assignment (or ++, etc) is
something to be absolutely respected.  But this is very much a matter
of opinion.

If the example you mentioned is considered a bug, then spending a bit
in every SV to fix it is a bargain.  If it's not a bug, then the
documentation of + and similar numeric operators in perlop needs to
have some kind of caveat that just using them can affect the value of
a scalar (though I don't know where exactly this should go).

Ed Avis <>

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About