in reply to Re: Re: (MeowChow) Re2: When does $_ get used?
in thread When does $_ get used?

No, at least part of your testing was flawed. "0 ", "0\n", "00", and even "0.0" are all true values. The only false values are "" and "0", period. Boolean context does not first convert the scalar to a number. undef is a special case of "" and numeric 0 is a special case "0" (or at least that is one way to think of it).

The real false values are "" and numeric 0 but "0" is also considered false because otherwise it would get really confusing. (: But there is no way for a numeric zero to get silently promoted to include a string value like "0 " or "0\n". Numeric zero always becomes simply "0", so we don't need to make any other strings false.

        - tye (but my friends call me "Tye")

Replies are listed 'Best First'.
Re: (tye)Re: Why isn't "0\n" false?
by John M. Dlugosz (Monsignor) on Jul 27, 2001 at 18:10 UTC
    Boolean context does not first convert the scalar to a number

    Ah, I see.

    No, at least part of your testing was flawed. "0 ", "0\n", "00", and even "0.0" are all true values. The only false values are "" and "0", period.

    perl -e "print (0+qq(0 )?'true':'false')" perl -e "print (qq(0\n)?'true':'false')"
    What am I doing wrong? It looks like "0 " is indeed false, while "0\n" is true.

    Update: I didn't notice I still had the 0+ in there. I tried a bunch of variations. That's what I get for experimenting at 2am!

    —John

      0+"0 " is the same as 0 which is false. "0 " is true. Likewise 0+"0\n" is false while "0\n" is true.

              - tye (but my friends call me "Tye")
        "0 " is true

        So, pray tell me, why does the line I showed print "false"?

        —John

        Oops, I edited the wrong line, and didn't notice I still had the 0+ in there. I'm sure we all know how that goes...☺

        But, here's another question. A value is not converted when boolean context is needed, OK. But what if it is a value that has both numeric and string "slots" filled? Which does it use? My guess is it looks at the string, so that "0 but true" is still true even after it's been used in a numeric expression (filling the number slot with 0).

        —John