develooper Front page | perl.perl5.porters | Postings from February 2003

Re: [perl #20661] Constant strings representing a number can BECOME numbers

Thread Previous | Thread Next
February 11, 2003 20:31
Re: [perl #20661] Constant strings representing a number can BECOME numbers
Message ID:
" (via RT)" <> wrote:
:perl -wle 'sub fun { print+(shift() & "+0") eq "0" ? "yes" : "no"} fun("waf"); fun(0); fun("waf")'
:perl -wle 'sub fun { my $str = shift; print "$str: ", ($str & "+0") eq "0" ? "numeric" : "string"} fun("waf"); fun(0); fun("waf")'
:waf: string
:0: numeric
:Argument "waf" isn't numeric in bitwise and (&) at -e line 1.
:waf: numeric
:This was code attempting to determine if a passed argument was "really"
:numeric at the perl level or not.
:However, it seems that once "+0" has interacted in the & with a number, it
:starts to behave like the number 0, so from then on on strings you get the
:warning and and everything looks like a number.
:While this is "normal" behaviour for variables, I don't think that 
:constant strings like "+0" should do that.

Hmm, tricky I think. In most cases, caching conversion results for
constants is a desirable thing to do.

In this case, the easiest way to avoid it is not to be a constant:
  sub fun { my $z = "+0"; my $str = shift; print "$str: ", ($str & $z) eq "0" ? "numeric" :"string"}

What else is broken by upgrading constants in this way?


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