http://www.perlmonks.org?node_id=980677


in reply to Re: ||= (poorly documented?)
in thread ||= (poorly documented?)

There is a rather strange thing that can happen in Perl.
It is possible for Perl to return a "true", "zero" value.
It does this by returning the string "0E0": 0 * 10**1 =0 numerically, but that evaluates to "true" in a logical sense.
This works because there are only two strings which evaluate as false in Perl: "" and "0".

Any other string which begins with one or more zeroes followed by a non-digit character will by logically true (since it's not one of the two false strings) while still having the value 0 when evaluated as a number. This actually will also work with strings that start with a non-numeric character (true as a boolean, 0 as a number), but, if warnings are enabled (as they generally should be), it will complain that "Argument "..." isn't numeric" if you try to use it as a number.

Aside from "0E0", the other string I often see used for this purpose (I'm not sure which is more common) is "0 but true".

Replies are listed 'Best First'.
Re^3: ||= (poorly documented?)
by Marshall (Canon) on Jul 09, 2012 at 09:37 UTC
    I don't see how you and I are in any kind of disagreement.
      We're not. You seemed surprised that this works, so I provided an explanation for why it works.