Truth and undef
Perl has no separate boolean type. Instead, every scalar is either true or false when used in a condition. Knowing exactly which values are false avoids a whole category of bugs.
The false values
Only these values are false:
| Value | Why it is false |
|---|---|
undef |
no value at all |
0 |
the number zero |
0.0 |
also the number zero |
"" |
the empty string |
"0" |
the string containing just the digit zero |
Everything else is true, including "0.0", "00", " " (a space), "false" and negative numbers.
use strict;
use warnings;
my @tests = (undef, 0, 0.0, "", "0", "0.0", "00", " ", "false", -1, 42);
foreach my $value (@tests) {
my $label = defined $value ? "'$value'" : "undef";
printf "%-8s is %s\n", $label, ($value ? "true" : "false");
}undef is false
'0' is false
'0' is false
'' is false
'0' is false
'0.0' is true
'00' is true
' ' is true
'false' is true
'-1' is true
'42' is true
Notice that the number 0.0 prints as 0: by the time it is stored it is simply zero. The string "0.0" is a different matter: it is not "" and not "0", so it is true.
Tip: The rule is easiest to remember as "a value is false if, converted to a string, it is
""or"0"".
What comparisons return
Comparison and logical-not operators return a special true value (which prints as 1) or a special false value (which prints as the empty string "" but acts like 0 in arithmetic):
use strict;
use warnings;
my $yes = (5 > 3);
my $no = (5 < 3);
print "yes = '$yes'\n";
print "no = '$no'\n";
print "no + 10 = ", $no + 10, "\n";
print "!!7 = '", !!7, "'\n";yes = '1'
no = ''
no + 10 = 10
!!7 = '1'
!! (double negation) is a common way to turn any value into a clean 1 or "".
Working with undef
undef means "no value". Variables start as undef, missing hash keys and array elements read as undef, and many functions return undef to signal failure.
use strict;
use warnings;
my %stock = (apples => 12, pears => 0);
foreach my $fruit (qw(apples pears kiwis)) {
my $qty = $stock{$fruit};
if (!defined $qty) {
print "$fruit: not stocked\n";
} elsif ($qty == 0) {
print "$fruit: sold out\n";
} else {
print "$fruit: $qty left\n";
}
}
my $x = 5;
undef $x; # or: $x = undef;
print "x is ", (defined $x ? "defined" : "undefined"), "\n";apples: 12 left
pears: sold out
kiwis: not stocked
x is undefined
Here a plain truth test would be wrong: pears has a quantity of 0, which is false but very much defined. Use defined whenever 0 or the empty string are legitimate values.
qw(apples pears kiwis) is the "quote words" operator: a quick way to write a list of single-word strings.
undef in calculations
In arithmetic, undef acts like 0; in string operations it acts like "". With use warnings you get an "uninitialized value" warning, which almost always points to a real bug:
use strict;
use warnings;
my $total;
$total += 5; # += on undef is allowed silently: a common idiom
print "total = $total\n";
my $missing;
my $sum = $missing + 1;
print "sum = $sum\n";total = 5
sum = 1
Use of uninitialized value $missing in addition (+) at /app/output.s line 9.
Perl deliberately does not warn for +=, ++, .= and similar on an undefined variable, because "start from nothing and accumulate" is so common.
Test yourself
Which of these values is TRUE in Perl?
0"""0""0.0"
Why test defined $qty rather than just $qty?
definedis faster- Because 0 is false but may be a valid quantity
- Because
$qtycannot be used in a condition - There is no difference
What does print (3 < 2); print?
- 0
- false
- nothing (the empty string)
- -1