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
From:
hv
Date:
February 11, 2003 20:31
Subject:
Re: [perl #20661] Constant strings representing a number can BECOME numbers
Message ID:
200302120433.h1C4XdX03020@crypt.compulink.co.uk
"perl-5.8.0@ton.iguana.be (via RT)" <perlbug-followup@perl.org> 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?

Hugo

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