|Left||Terms and list operators (leftward)|
Named unary operators and file-test operators
|Nonassociative||List operators (rightward)|
With very few exceptions, Perl operators act upon scalar values only, not upon list values.
Terms that take highest precedence in Perl include variables, quote and quotelike operators, any expression in parentheses, and any function whose arguments are in parentheses.
A list operator is a function that can take a list of values as its argument. List operators take highest precedence when considering what's to the left of them. They have considerably lower precedence when looking at their right side, which is the expected result.
Also parsed as high-precedence terms are the
as well as subroutine and method calls, the anonymous array and hash
the anonymous subroutine composer
A unary operator is a function that takes a single scalar value as its argument. Unary operators have a lower precedence than list operators because they only expect and take one value.
The arrow operator is a dereference operator. It can be used for references to arrays, hashes, code references, or for calling methods on objects. See the discussion of references in Chapter 7, Packages, Modules, and Objects.
- performs arithmetic negation if the operand is numeric. If
the operand is an identifier, then a string consisting of a minus sign
concatenated with the identifier is returned. Otherwise, if the string
starts with a plus or minus, a string starting with the opposite sign
~ performs bitwise negation, that is, one's complement.
For example, on a 32-bit machine,
If the argument to
a string instead of a number, a string of identical length is
returned, but with all the bits of the string complemented.
+ has no semantic effect whatsoever, even on strings. It is
syntactically useful for separating a function name from a parenthesized
expression that would otherwise be interpreted as the complete list of
\ creates a reference to whatever follows it
(see "References and Complex Data Structures" later in this chapter).
Do not confuse this behavior with the behavior of backslash within a
\ operator may also be used on a parenthesized list value in a list
context, in which case it returns references to each element of the list.
evaluates to 2 to the 81st power, not 8 to the 4th power.$e = 2 ** 3 ** 4;
* (multiply) and
/ (divide) operators
work exactly as you might expect, multiplying or
dividing their two operands. Division is done in floating-point mode,
unless integer mode in enabled (via
% (modulus) operator converts its operands to integers before finding the
remainder according to integer division. For the same operation in
floating-point mode, you may prefer to use the
function from the
POSIX module (see Chapter 8).
Perl has two classes of relational operators. One class operates on
numeric values, and the other operates on string values.
comparisons are based on the ASCII collating sequence.
Relational operators are nonassociative, so
$a < $b < $c is a syntax error.
|Greater than or equal to|
|Less than or equal to|
The equal and not-equal operators return 1 for true, and
"" for false (just as
the relational operators do). The
<=> and cmp operators return
-1 if the left operand is less than the right operand, 0 if they are
equal, and +1 if the left operand is greater than the right.
|Not equal to|
|Comparison, with signed result|
If placed before a variable,
increment or decrement the variable before returning the value, and if
placed after, they increment or decrement the variable after returning the
Each operator requires a variable on the left side and some expression on the right side. For the simple assignment operator,= **= += *= &= <<= &&= -= /= |= >>= ||= .= %= ^= x=
=, the value of the expression is stored into the designated variable. For the other operators, Perl evaluates the expression:
as if it were written:$var OP= $value
except that$var = $var OP $value
$varis evaluated only once. For example:
$a += 2; # same as $a = $a + 2
See Section 4.6" later in this chapter.$string !~ /pattern/ not $string =~ /pattern/
|File is readable by effective uid/gid.|
|File is writable by effective uid/gid.|
|File is executable by effective uid/gid.|
|File is owned by effective uid.|
|File is readable by real uid/gid.|
|File is writable by real uid/gid.|
|File is executable by real uid/gid.|
|File is owned by real uid.|
|File has zero size.|
|File has non-zero size (returns size).|
|File is a plain file.|
|File is a directory.|
|File is a symbolic link.|
|File is a named pipe (FIFO).|
|File is a socket.|
|File is a block special file.|
|File is a character special file.|
|Filehandle is opened to a tty.|
|File has setuid bit set.|
|File has setgid bit set.|
|File has sticky bit set.|
|File is a text file.|
|File is a binary file (opposite of |
|Age of file (at startup) in days since modification.|
|Age of file (at startup) in days since last access.|
|Age of file (at startup) in days since inode change.|
For example, an oft-appearing idiom in Perl programs is:
In this case, Perl first evaluates theopen(FILE, "somefile") || die "Cannot open somefile: $!\n";
openfunction. If the value is true (because
somefilewas successfully opened), the execution of the
diefunction is unnecessary and is skipped.
Perl also provides lower-precedence
or operators that are
Perl has bitwise AND, OR, and XOR (exclusive
operators work differently on numeric values than they do on strings.
If either operand is a number, then both
operands are converted to integers, and the bitwise operation is
performed between the two integers.
If both operands are strings,
these operators do bitwise operations between corresponding
bits from the two strings.
In a scalar context,
.. returns a Boolean value.
It is false
as long as its left operand is false. Once the left operand is true,
the range operator stays true until the right operand is true,
after which the range operator becomes false again.
The right operand is not evaluated while the operator is in the false
state, and the left operand is not evaluated while the operator is in the true
If thetest_expr ? if_true_expr : if_false_expr
test_expris true, only the
if_true_expris evaluated. Otherwise, only the
if_false_expris evaluated. Either way, the value of the evaluated expression becomes the value of the entire expression.
In a list context, "
," is the list argument separator and
inserts both its arguments into the list.
In scalar context, "
its left argument, throws that value away, then evaluates its right
argument and returns that value.
=> operator is mostly just a synonym for the comma operator.
It's useful for documenting arguments that come in pairs. It also
forces any identifier to the left of it to be interpreted as a string.
Binaryprint 'abc' . 'def'; # prints abcdef print $a . $b; # concatenates the string values of $a and $b
xis the string repetition operator. In scalar context, it returns a concatenated string consisting of the left operand repeated the number of times specified by the right operand.
In list context, if the left operand is a list in parentheses, theprint '-' x 80; # prints row of dashes print "\t" x ($tab/8), ' ' x ($tab%8); # tabs over
xworks as a list replicator rather than a string replicator. This is useful for initializing all the elements of an array of indeterminate length to the same value:
@ones = (1) x 80; # a list of 80 1s @ones = (5) x @ones; # set all elements to 5