http://www.perlmonks.org?node_id=495975

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).

The table below summarizes these rules. The text 'empty str' represents the empty string (""). The first test (if( $var )) reveals whether perl treats the value as true or false. The results of the next two expressions (if( defined $var ) and if( $var eq '' )) are fairly obvious, but they are included in the table to highlight the different interpretation of undef in each case. Finally, the expression if( $var == 0 ) tests the value's numeric equivalency to 0.

Truth tests for different values
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):

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:

The tables below list the return value for logical AND (&&, and) and logical OR (||, or) operations for the values tested above. The values are colored according to whether they are interpreted as true or false (as given in the table above). In these tables, 'a string' has been replaced by 'A string' and 'B string' so it is clear which value is returned.

Logical AND tests for different values: A && B
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

Logical OR tests for different values: A || B
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.

Logical XOR tests for different values: A xor B
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: