Boolean Values
You may actually use any scalar value as the conditional of the if
control structure. That's handy if you want to store a true or false value into a variable,
like this:
$is_bigger = $name gt 'fred';
if($is_bigger) {
o o o
}
But how does Perl decide whether a given value is true or false? Perl doesn't have a
separate Boolean data type, like some languages have. Instead, it uses a few simple rules:
-
The special value undef is false. (We'll see this a little later
in this section.)
-
Zero is false; all other numbers are true.
-
The empty string ('') is false; all other strings are normally true.
-
The one exception: since numbers and strings are equivalent, the string form of
zero, '0', has the same value as its numeric form: false.
So, if your scalar value is undef, 0, '', or '0', it's false. All other
scalars are true - including all of the types of scalars that we haven't talked about yet.
If you need to get the opposite of any Boolean value, use the unary not operator,
!. If what follows it is a true value, it returns false; if what follows is false,
it returns true:
if(! $is_bigger) {
# do something when $is_bigger is not true
}
Next: Logical Operators
OR
Other Control Structures