Boolean Algebra in Perl
These conditional operators perform the common boolean algebraic operations: and, or, and not.
Operator | Description |
&& | boolean and |
|| | boolean or |
! | boolean not |
Perl 6
Here are several examples:
if($this && $that) {
some code here
}
unless($this || !$the_other) {
some code here
}
Some older code would occasionally use these operators for shortcuts, but Perl 5 now has low precedence operators specifically for that purpose.
The &&
and ||
operators have the special property of returning the last value evaluated,
instead of 1
for truth. That allows the following to work:
$x = shift || "default";
If the shift
operator doesn't return a value, the "default"
value will be assigned to $x
instead.