4.5. Operators
Table 4-3 lists all
the Perl operators from highest to lowest precedence and indicates
their associativity.
Table 4-3. Perl associativity and operators, listed by precedence
Associativity
|
Operators
|
Left
|
Terms and list operators (leftward)
|
Left
|
-> (method call, dereference)
|
Nonassociative
|
++ -- (autoincrement, autodecrement)
|
Right
|
** (exponentiation)
|
Right
|
! ~ \ and unary + and
- (logical not, bit-not, reference, unary plus,
unary minus)
|
Left
|
=~ !~ (matches, doesn't match)
|
Left
|
* / % x (multiply, divide, modulus, string
replicate)
|
Left
|
+ - . (addition, subtraction, string concatenation)
|
Left
|
<< >> (left bit-shift, right bit-shift)
|
Nonassociative
|
Named unary operators and file-test operators
|
Nonassociative
|
< > <= >= lt gt le ge (less than,
greater than, less than or equal to, greater than or equal to, and
their string equivalents)
|
Nonassociative
|
== != <=> eq ne cmp (equal to, not equal to,
signed comparison, and their string equivalents)
|
Left
|
& (bit-and)
|
Left
|
| ^ (bit-or, bit-xor)
|
Left
|
&& (logical AND)
|
Left
|
|| (logical OR)
|
Nonassociative
|
.. ... (range)
|
Right
|
?: (ternary conditional)
|
Right
|
= += -= *= and so on (assignment operators)
|
Left
|
, => (comma, arrow comma)
|
Nonassociative
|
List operators (rightward)
|
Right
|
not (logical not)
|
Left
|
and (logical and)
|
Left
|
or xor (logical or, xor)
|
You can clarify your expressions by using
parentheses to group any part of an expression. Anything in
parentheses will be evaluated as a single unit within a larger
expression.
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 with arguments 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 to their right, which is the expected
result.
Also parsed as high-precedence terms are the do{}
and eval{} constructs, as well as subroutine and
method calls, the anonymous array and hash composers
([] and {}), and the anonymous
subroutine composer sub{}.
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 expect and take only one value.
4.5.2. Unary Operators
Unary ! performs
logical negation, that is, "not."
The not operator is a lower-precedence version of
!.
Unary - 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 is
returned.
Unary ~ performs
bitwise negation, that is, one's complement. For
example, on a 32-bit machine, ~0xFF is
0xFFFFFF00. If the argument to
~ is a string instead of a number, a string of
identical length is returned, but with all the bits of the string
complemented.
Unary
+ 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 function arguments.
Unary \ creates a
reference to whatever follows it (see Section 4.8, "References and Complex Data Structures" later in this chapter).
Do not confuse this behavior with the behavior of the backslash
within a string. The \ 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.
4.5.8. File Test Operators
A file test
operator is a unary operator that tests a filename or a filehandle.
Operator
|
Meaning
|
-r
|
File is readable by effective uid/gid
|
-w
|
File is writable by effective uid/gid
|
-x
|
File is executable by effective uid/gid
|
-o
|
File is owned by effective uid
|
-R
|
File is readable by real uid/gid
|
-W
|
File is writable by real uid/gid
|
-X
|
File is executable by real uid/gid
|
-O
|
File is owned by real uid
|
-e
|
File exists
|
-z
|
File has zero size
|
-s
|
File has nonzero size (returns size)
|
-f
|
File is a plain file
|
-d
|
File is a directory
|
-l
|
File is a symbolic link
|
-p
|
File is a named pipe (FIFO)
|
-S
|
File is a socket
|
-b
|
File is a block special file
|
-c
|
File is a character special file
|
-t
|
Filehandle is opened to a tty
|
-u
|
File has setuid bit set
|
-g
|
File has setgid bit set
|
-k
|
File has sticky bit set
|
-T
|
File is a text file
|
-B
|
File is a binary file (opposite of -T)
|
-M
|
Age of file (at startup) in days since modification
|
-A
|
Age of file (at startup) in days since last access
|
-C
|
Age of file (at startup) in days since inode change
|
 |  |  | 4.4. Special Variables |  | 4.6. Regular Expressions |
Copyright © 2002 O'Reilly & Associates. All rights reserved.
|