I wrote this quick reference guide for those times, late at night, when I forget how Perl treats various values. (After debugging a troublesome if(expression ) statement I occasionally rediscover that while both 0 and the empty string ('') are false, 0 eq '' is false but 0 == '' is true.) This meditation is not meant to be an in-depth discussion of the concept of truth, and the tables are not comprehensive. Nevertheless, I hope someone else finds this useful.
Truth and Falsehood
Identifying which values are treated as 'true' and which are treated as 'false' is straightforward once a few rules are established, but sometimes it is difficult to find exactly where in the docs a particular rule is documented. The following quotes summarize the main points to keep in mind (from the ActiveState docs for 5.6.1).
- "A scalar value is interpreted as TRUE in the Boolean sense if it is not the null string or the number 0 (or its string equivalent, "0"). The Boolean context is just a special kind of scalar context where no conversion to a string or a number is ever performed." (from perldata)
- "When used as a number, undef is treated as 0; when used as a string, it is treated the empty string, ""; and when used as a reference that isn't being assigned to, it is treated as an error." (from perlsyn)
- "The while statement executes the block as long as the expression is true (does not evaluate to the null string "" or 0 or "0")." (from perlsyn)
Result of the expression when $var is: | ||||||
---|---|---|---|---|---|---|
Expression | 1 | '0.0' | a string | 0 | empty str | undef |
if( $var ) | true | true | true | false | false | false |
if( defined $var ) | true | true | true | true | true | false |
if( $var eq '' ) | false | false | false | false | true | true |
if( $var == 0 ) | false | true | true | true | true | true |
Several common values were left out of the table in an effort to keep the size manageable. Those values include the following and are treated as noted (this is not an exhaustive list):
- String zero ("0") is evaluated as numeric zero (0)
-
These values are interpreted as strings (like '0.0'):
- '0E0' (exponential)
- '00' (octal)
- '0x0' (hexadecimal)
- '+0' (positive), '-0' (negative)
- ' 0' (spatial), '0\n' (linear)
- '.0' (fractional), '0.' (periodic)
Logical Tests: AND, OR
Using the table and rules above, the outcome of a logical AND or a logical OR test can be predicted. In Perl these operators do not simply return 0 or 1, however. Instead, they return the last value that was evaluated in the expression, which is usually only tested for truth (using the table and rules given above) so the actual value is ignored. Due to the short-circuiting nature of the operators, the returned value can be either the first or last value in the expression. This behavior is documented in perlop:
- "Binary ``&&'' performs a short-circuit logical AND operation. That is, if the left operand is false, the right operand is not even evaluated."
- "Binary ``||'' performs a short-circuit logical OR operation. That is, if the left operand is true, the right operand is not even evaluated."
- "The || and && operators differ from C's in that, rather than returning 0 or 1, they return the last value evaluated."
Value of B | ||||||
---|---|---|---|---|---|---|
Value of A | 1 | '0.0' | B string | 0 | empty str | undef |
1 | 1 | 0.0 | B string | 0 | empty str | undef |
'0.0' | 1 | 0.0 | B string | 0 | empty str | undef |
A string | 1 | 0.0 | B string | 0 | empty str | undef |
0 | 0 | 0 | 0 | 0 | 0 | 0 |
empty str | empty str | empty str | empty str | empty str | empty str | empty str |
undef | undef | undef | undef | undef | undef | undef |
Value of B | ||||||
---|---|---|---|---|---|---|
Value of A | 1 | '0.0' | B string | 0 | empty str | undef |
1 | 1 | 1 | 1 | 1 | 1 | 1 |
'0.0' | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 |
A string | A string | A string | A string | A string | A string | A string |
0 | 1 | 0.0 | B string | 0 | empty str | undef |
empty str | 1 | 0.0 | B string | 0 | empty str | undef |
undef | 1 | 0.0 | B string | 0 | empty str | undef |
Exclusive OR: XOR
The result of an XOR expression (minimally documented in perlop) can also be predicted using the truth table given above. The following table summarizes the results for the values tested above.
Value of B | ||||||
---|---|---|---|---|---|---|
Value of A | 1 | '0.0' | a string | 0 | empty str | undef |
1 | false | false | false | true | true | true |
'0.0' | false | false | false | true | true | true |
a string | false | false | false | true | true | true |
0 | true | true | true | false | false | false |
empty str | true | true | true | false | false | false |
undef | true | true | true | false | false | false |
References
Thanks to tye, castaway, GrandFather, and Detonite for their pre-post comments.
Updates:
- Changed the AND and OR tables to more clearly show which value is returned, per Skeeve's suggestion.
- Minor rewording to make it more clear that the rules are quoted directly from the Perl docs, per Anonymous Monk's comments.
- Updated legacy HTML syntax
|
---|