Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Maybe just a long day. Here are four good explanations from trusted sources.

But how does Perl decide whether a given value is true or false? Perl doesn’t have a separate Boolean datatype, like some languages have. Instead, it uses a few simple rules:

  • If the value is a number, 0 means false; all other numbers mean true.
  • Otherwise, if the value is a string, the empty string ('') means false; all other strings mean true.
  • Otherwise (that is, if the value is another kind of scalar than a number or a string), convert it to a number or a string and try again.

There’s one trick hidden in those rules. Because the string '0' is the exact same scalar value as the number 0, Perl has to treat them both the same. That means that the string '0' is the only non-empty string that is false.

Randal L. Schwartz; brian d foy; Tom Phoenix. Learning Perl, 6th Edition (Kindle Locations 1895-1904). O'Reilly Media.

And...

The number 0, the strings '0' and '' , the empty list () , and undef are all false in a boolean context. All other values are true. Negation of a true value by ! or not returns a special false value. When evaluated as a string it is treated as '' , but as a number, it is treated as 0.

perlsyn, Truth and Falsehood

And of course there's the Camel book you referred to:

Truth in Perl is always evaluated in a scalar context. Other than that, no type coercion is done. So here are the rules for the various kinds of values a scalar can hold:

  1. Any string is true except for "" and "0".
  2. Any number is true except for 0.
  3. Any reference is true.
  4. Any undefined value is false.

Larry Wall; Tom Christiansen; Jon Orwant. Programming Perl, 3rd Edition (Kindle Locations 1991-1995). O'Reilly Media.

The Perl Cookbook delivers a brief explanation as to "why."

Two defined strings are false: the empty string ("") and a string of length one containing the digit zero ("0"). All other defined values (e.g., "false", 15, and \$x) are true. You might be surprised to learn that "0" is false, but this is due to Perl's on-demand conversion between strings and numbers. The values 0., 0.00, and 0.0000000 are all numbers and are therefore false when unquoted, since the number zero in any of its guises is always false. However, those three values ("0.", "0.00", and "0.0000000") are true when used as literal quoted strings in your program code or when they're read from the command line, an environment variable, or an input file.

This is seldom an issue, since conversion is automatic when the value is used numerically. If it has never been used numerically, though, and you just test whether it's true or false, you might get an unexpected answer—Boolean tests never force any sort of conversion. Adding 0 to the variable makes Perl explicitly convert the string to a number:

Tom Christiansen; Nathan Torkington. Perl Cookbook, 2nd Edition (Kindle Locations 2815-2825). O'Reilly Media.

I bolded the most important part of that last excerpt.


Dave


In reply to Re: What is the correct definition of False and Null in Perl? by davido
in thread What is the correct definition of False and Null in Perl? by flexvault

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-19 21:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found