What is Truth?

In Perl, Truth is defined as follows:

  1. The string "0" is False.
  2. The number 0 is False.
  3. The empty string ("" or '') is False
  4. The empty list is False
  5. The undefined value is False (e.g. an uninitialized variable)
  6. A number is false if it converts to string "0".
  7. Everything else is True.

$a;           # FALSE (not yet defined)
$a = 1;       # TRUE
$b = 0;       # FALSE
$c = "";      # FALSE
$d = 'true';  # TRUE
$e = 'false'; # TRUE (watch out! "false" is a nonemty string)
$f = ' ';     # TRUE (a single space is non-empty)
$g = "\n";    # TRUE (a single newline is non-empty)
@h = ();      # FALSE array
$i = 0.0;     # FALSE (converts to string "0")
$j = '0.0';   # TRUE (watch out! The string "0.0" is not the same as "0")

Truth and the Comparison Operations


$a =  4 == 1+3;
print "The answer is $a","\n";
The answer is 1.

<< Previous
Contents >> Next >>

Lincoln D. Stein, lstein@cshl.org
Cold Spring Harbor Laboratory
Last modified: Tue Oct 16 16:35:06 EDT 2001