Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re^2: ||= (poorly documented?)

by dsheroh (Prior)
on Jul 09, 2012 at 09:23 UTC ( #980677=note: print w/replies, xml ) Need Help??

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 (Abbot) 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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://980677]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2017-05-30 06:21 GMT
Find Nodes?
    Voting Booth?