Yes, it looks at the string. Like you said, it can't look at the number part or:
would print "True". This is recorded in the source code in at least 4 places (for speed reasons). Each version boils down to these tests, in order:my $val= "0 "; my $dummy= 0+$val; print "True\n" if $val;
That second item is in parens because it only occurs in one copy of this algorithm. It doesn't matter much because I think the only way to get a reference to a destroyed item is during "global destruction".
But it did make me curious about a possible low-impact bug which turned out to not exist because of something more interesting: Every time you use a reference in a string context, the resulting string is reconstructed from scratch. When you use a number in a string context, the string representation of that number is computed and then cached inside the scalar so future uses of that (formerly number-only) scalar in a string context don't have to repeat that work. This optimization is not applied to references.
- tye (but my friends call me "Tye")In reply to (tye)Re3: Why isn't "0\n" false?
by tye
in thread When does $_ get used?
by elbie
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |