Perl true - false FAQ: What is true in Perl? What is false in Perl?
The Perl programming language 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 in Perl, so I decided to create this page.
In short, the following elements evalue to false in Perl:
As a corollary to this, many other things equate to true in Perl, including:
Those are the basic Perl true and false rules. Let's look at some true/false examples to help understand this a little more.
To help demonstrate these Perl true/false test conditions, 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
(And finally, while I'm in the printf neighborhood, if you're interested in learning more about printf, I just put together a printf reference page.)
I hope these Perl true false examples have been helpful to you. If you have any other true/false examples you'd like to share, just leave a note in the Comments section below.
Post new comment