Yes, it looks at the string. Like you said, it can't look at the number part or:

my $val= "0 "; my $dummy= 0+$val; print "True\n" if $val;
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:
  1. undef is false
  2. (references are true unless they reference a destroyed thing)
  3. strings are false if-and-only-if they are "" or "0"
  4. integers are false if-and-only-if they are 0 (zero)
  5. reals are false if-and-only-if they are 0.0 (zero)

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.