What is true and false in Perl?

By Alvin J. Alexander, devdaily.com

Perl is a little unusual in not having true and false boolean operators. Because of this, and my advancing age, I can never seem to remember what equates to true and false when using Perl, so I decided to create this page.

True/false summary

In short, the following elements in Perl will equate to "false":

  • The number zero (0) means false.
  • The string zero ('0') means false.
  • The empty string ('') means false.

Lots of other things equate to "true", including:

  • Non-zero numbers
  • Non-empty strings

Sample program output

To help demonstrate this I've written a small Perl test program. Here's the source code for the program:

@truths = (0, "0", "", 1, "1", "foo");

for (@truths)
{
  if ($_) { printf("'%s' is true\n", $_);  }
  else    { printf("'%s' is false\n", $_); }
}

And here's the output from the program:

'0' is false
'0' is false
'' is false
'1' is true
'1' is true
'foo' is true

devdaily logo