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:
perl5-porters
Date:
February 12, 2003 14:13
Subject:
Re: [perl #20661] Constant strings representing a number can BECOME numbers
Message ID:
b2egtg$d49$2@post.home.lunix
In article <200302120433.h1C4XdX03020@crypt.compulink.co.uk>,
	hv@crypt.org writes:
> "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.

Is it really ? How often would somebody write e.g.

$x += "1" ?

> 
> 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?
> 

It probably only matters in the cases where the behaviour of string 
and number differ, which is only the bitops I think. Since these are known
for their iffy behaviour, how about just doing the constant upgrade in
general, *except* when involved in a bitop ? If someone writes
$var & "0", he probably *cares*.

Mmm, can the constant conversion actually be done at compile time,
since it's already known there in what kind of operation the constant is
involved ?

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