home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


Book HomeJava and XSLTSearch this book

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.11. Miscellaneous Operators

The following operators don't fit into any of the previous categories.



Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.