Perl Shortcut Operators
Operator | Description |
and | boolean and (low precedence) |
or | boolean or (low precedence) |
not | boolean not (low precedence) |
The shortcut operators are identical to the Boolean algebra operators, except that they are of the lowest possible precedence.
Shortcut operators are designed for simple conditional execution of code.
This purpose used to be served by the Boolean algebraic operators, but these shortcut operators have low-enough precedence to make them work properly without parenthesis.
Here's an example:
$x = shift or $state = "first";
If you tried to do this with the ||
operator, like this:
$x = shift || $state = "first";
you would find that you needed parenthesis around the ($x = shift)
part of the expression to make the assignment bind first.
Since the or
operator has lower precedence than the =
assignment operator, the entire expression $x = shift
is evaluated before the condition is checked, giving the expected behavior.