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.
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).
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):
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:
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 |
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 |
Thanks to tye, castaway, GrandFather, and Detonite for their pre-post comments.
Updates:
|
---|