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)
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):
-
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)
See tye's poll, The best "true zero" is..., for others.
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."
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:
-
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
-
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.