Control Structures
"&&" and "||" as Control Structures
- In keeping with the perl philosophy that no command can be too
short (this was inherited from Unix), it is possible to use "&&" and
"||" as control structures.
- if ($x < 10) { print "$x < 10\n"; } could also be
written as ($x < 10) && (print "$x < 10\n");
- Likewise, unless ($x < 10) { print "$x > 10\n"; } could also be
written as ($x < 10) || (print "$x > 10\n");
- This works because Perl uses short circuit evaluation... in other
words, the second part of the expression is only executed if the first
part of the expression meets minimum.
- In other words, if Perl can see that the expression is going to
be false by looking at the first part of the expression only, it does
not even bother to look at the second part of the expression, and it
is not executed.
Back to Syllabus
Previous: Expression Modifiers
Next: Reading from Standard Input