=head1 NAME
perlop - Perl operators and precedence
=head1 SYNOPSIS
Perl operators have the following associativity and precedence,
listed from highest precedence to lowest. Note that all operators
borrowed from C keep the same precedence relationship with each other,
even where C's precedence is slightly screwy. (This makes learning
Perl easier for C folks.) With very few exceptions, these all
operate on scalar values only, not array values.
left terms and list operators (leftward)
left ->
nonassoc ++ --
right **
right ! ~ \ and unary + and -
left =~ !~
left * / % x
left + - .
left >
nonassoc named unary operators
nonassoc = lt gt le ge
nonassoc == != eq ne cmp
left &
left | ^
left &&
left ||
nonassoc .. ...
right ?:
right = += -= *= etc.
left , =>
nonassoc list operators (rightward)
right not
left and
left or xor
In the following sections, these operators are covered in precedence order.
Many operators can be overloaded for objects. See L.
=head1 DESCRIPTION
=head2 Terms and List Operators (Leftward)
A TERM has the highest precedence in Perl. They includes variables,
quote and quote-like operators, any expression in parentheses,
and any function whose arguments are parenthesized. Actually, there
aren't really functions in this sense, just list operators and unary
operators behaving as functions because you put parentheses around
the arguments. These are all documented in L.
If any list operator (print(), etc.) or any unary operator (chdir(), etc.)
is followed by a left parenthesis as the next token, the operator and
arguments within parentheses are taken to be of highest precedence,
just like a normal function call.
In the absence of parentheses, the precedence of list operators such as
C, C, or C is either very high or very low depending on
whether you are looking at the left side or the right side of the operator.
For example, in
@ary = (1, 3, sort 4, 2);
print @ary; # prints 1324
the commas on the right of the sort are evaluated before the sort, but
the commas on the left are evaluated after. In other words, list
operators tend to gobble up all the arguments that follow them, and
then act like a simple TERM with regard to the preceding expression.
Note that you have to be careful with parentheses:
# These evaluate exit before doing the print:
print($foo, exit); # Obviously not what you want.
print $foo, exit; # Nor is this.
# These do the print before evaluating exit:
(print $foo), exit; # This is what you want.
print($foo), exit; # Or this.
print ($foo), exit; # Or even this.
Also note that
print ($foo & 255) + 1, "\n";
probably doesn't do what you expect at first glance. See
L for more discussion of this.
Also parsed as terms are the C and C constructs, as
well as subroutine and method calls, and the anonymous
constructors C and C.
See also L toward the end of this section,
as well as L.
=head2 The Arrow Operator
Just as in C and C++, "C>" is an infix dereference operator. If the
right side is either a C or C subscript, then the left side
must be either a hard or symbolic reference to an array or hash (or
a location capable of holding a hard reference, if it's an lvalue (assignable)).
See L.
Otherwise, the right side is a method name or a simple scalar variable
containing the method name, and the left side must either be an object
(a blessed reference) or a class name (that is, a package name).
See L.
=head2 Auto-increment and Auto-decrement
"++" and "--" work as in C. That is, if placed before a variable, they
increment or decrement the variable before returning the value, and if
placed after, increment or decrement the variable after returning the value.
The auto-increment operator has a little extra builtin magic to it. If
you increment a variable that is numeric, or that has ever been used in
a numeric context, you get a normal increment. If, however, the
variable has been used in only string contexts since it was set, and
has a value that is not the empty string and matches the pattern
C^[a-zA-Z]*[0-9]*$/>, the increment is done as a string, preserving each
character within its range, with carry:
print ++($foo = '99'); # prints '100'
print ++($foo = 'a0'); # prints 'a1'
print ++($foo = 'Az'); # prints 'Ba'
print ++($foo = 'zz'); # prints 'aaa'
The auto-decrement operator is not magical.
=head2 Exponentiation
Binary "**" is the exponentiation operator. Note that it binds even more
tightly than unary minus, so -2**4 is -(2**4), not (-2)**4. (This is
implemented using C's pow(3) function, which actually works on doubles
internally.)
=head2 Symbolic Unary Operators
Unary "!" performs logical negation, i.e., "not". See also C for a lower
precedence version of this.
Unary "-" performs arithmetic negation if the operand is numeric. If
the operand is an identifier, 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
is returned. One effect of these rules is that C is equivalent
to C.
Unary "~" performs bitwise negation, i.e., 1's complement. For example,
C is 0640. (See also L and L.)
Unary "+" has no effect whatsoever, even on strings. It is useful
syntactically for separating a function name from a parenthesized expression
that would otherwise be interpreted as the complete list of function
arguments. (See examples above under L.)
Unary "\" creates a reference to whatever follows it. See L.
Do not confuse this behavior with the behavior of backslash within a
string, although both forms do convey the notion of protecting the next
thing from interpretation.
=head2 Binding Operators
Binary "=~" binds a scalar expression to a pattern match. Certain operations
search or modify the string $_ by default. This operator makes that kind
of operation work on some other string. The right argument is a search
pattern, substitution, or transliteration. The left argument is what is
supposed to be searched, substituted, or transliterated instead of the default
$_. The return value indicates the success of the operation. (If the
right argument is an expression rather than a search pattern,
substitution, or transliteration, it is interpreted as a search pattern at run
time. This can be is less efficient than an explicit search, because the
pattern must be compiled every time the expression is evaluated.
Binary "!~" is just like "=~" except the return value is negated in
the logical sense.
=head2 Multiplicative Operators
Binary "*" multiplies two numbers.
Binary "/" divides two numbers.
Binary "%" computes the modulus of two numbers. Given integer
operands C and C: If C is positive, then C is
C minus the largest multiple of C that is not greater than
C. If C is negative, then C is C minus the
smallest multiple of C that is not less than C (i.e. the
result will be less than or equal to zero).
Note than when C