Closed p5pRT closed 14 years ago
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.
"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
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 ?
I believed the attached patch is self-explanatory.
On Aug 1\, 2010\, at 2:22 PM\, Father Chrysostomos wrote:
I believed the attached patch is self-explanatory. \<open_nKvdUEYU.txt>
Someone pointed out to me that it would likely be reviewed more quickly if I provided a commit message\, so here it is:
This patch solves the problem of $x & "+0" not treating the RHS as a string if\, on a previous invocation\, the LHS happened to be a number (similarly with the other bitwise ops\, too). This patch takes the conservative approach of fixing *just* those cases that have explicit quotation marks in the source code\, which are clearly broken (and also ‘use constant’-style string constants\, which are indistinguishable). (Read-only variables are slightly controversial still\, and my patch does not affect those.)
It does this by making the appropriate pp_ funcitons look at the op tree to see whether either operand is a constant during a numeric bitwise operation. If it is\, and it is not numeric\, it turns off the numericness (numericality?) before returning.
On Wed Feb 12 14:13:32 2003\, perl5-porters@ton.iguana.be wrote:
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*.
Commit b20c4ee1f stops bitops from coercing read-only arguments.
@cpansprout - Status changed from 'open' to 'resolved'
Migrated from rt.perl.org#20661 (status was 'resolved')
Searchable as RT20661$