Perl has two kind of logical operators: pure operators and keywords. Some share the same function but with different precedence when interpreted by Perl. The following table shows their function and similarity.
Function | Pure Operator | Keyword |
---|---|---|
AND | && | and |
OR | ∥ | or |
NOT | ! | not |
XOR | xor |
Generally, keywords have lower precedence to both the pure operators and the assignment operator (=
), whereas pure operators have higher precedence over the assignment operator. Therefore, a typical use of a keyword logical operator is to use it as a method to control flow, rather than used together with an assignment operator which could be confusing.
open (my $fileHandle, $fileName)
or die "can't open the file $fileName" # if fail, print out
# the error message
my $wrongNumber = 0 or 1 # expected to be 1, but resulted in 0
# because `or` has lower precedence than `=`
If an assignment operator is involved, use pure logical operators instead of keywords.