Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Mail Systems
Eclipse Documentation

How To Guides
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Problem Solutions
Privacy Policy




[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

4.2 A Digression--Truth Values

We have mentioned truth and "true and false" a few times now; however, we have yet to give a clear definition of what truth values are in Perl.

Every expression in Perl has a truth value. Usually, we ignore the truth value of the expressions we use. In fact, we have been ignoring them so far! However, now that we are going to begin studying various control structures that rely on the truth value of a given expression, we should look at true and false values in Perl a bit more closely.

The basic rule that most Perl programmers remember is that 0, the empty string and undef are false, and everything else is true. However, it turns out that this rule is not actually completely accurate.

The actual rule is as follows:

Everything in Perl is true, except:

  • the strings "" (the empty string) and "0" (the string containing only the character, 0), or any string expression that evaluates to either "" (the empty string) or "0".

  • any numeric expression that evaluates to a numeric 0.

  • any value that is not defined (i.e., equivalent to undef).

If that rule is not completely clear, the following table gives some example Perl expressions and states whether they are true or not:

Expression String/Number? Boolean value
0 number false
0.0 number false
0.0000 number false
"" string false
"0" string false
"0.0" string true
undef N/A false
42 - (6 * 7) number false
"0.0" + 0.0 number false
"foo" string true

There are two expressions above that easily confuse new Perl programmers. First of all, the expression "0.0" is true. This is true because it is a string that is not "0". The only string that is not empty that can be false is "0". Thus, "0.0" must be true.

Next, consider "0.0" + 0.0. After what was just stated, one might assume that this expression is true. However, this expression is false. It is false because + is a numeric operator, and as such, "0.0" must be turned into its numeric equivalent. Since the numeric equivalent to "0.0" is 0.0, we get the expression 0.0 + 0.0, which evaluates to 0.0, which is the same as 0, which is false.

Finally, it should be noted that all references are true. The topic of Perl references is beyond the scope of this book. However, if we did not mention it, we would not be giving you the whole truth story.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  Published under the terms of the GNU General Public License Design by Interspire