in reply to
What is the correct definition of False and Null in Perl?
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.
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:
- Any string is true except for "" and "0".
- Any number is true except for 0.
- Any reference is true.
- 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.