Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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

by dsheroh (Parson)
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".


Comment on Re^2: ||= (poorly documented?)
Select or Download Code
Re^3: ||= (poorly documented?)
by Marshall (Prior) 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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://980677]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2014-07-12 18:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (240 votes), past polls