home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
BURKS 2
/
BURKS_AUG97.ISO
/
SLAKWARE
/
D13
/
PERL2.TGZ
/
perl2.tar
/
usr
/
lib
/
perl5
/
pod
/
perlop.pod
< prev
next >
Wrap
Text File
|
1996-06-28
|
40KB
|
1,120 lines
=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.
=head1 DESCRIPTION
=head2 Terms and List Operators (Leftward)
Any TERM is of highest precedence of Perl. These includes variables,
quote and quotelike 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<perlfunc>.
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<print>, C<sort>, or C<chmod> is either very high or very low depending on
whether you look at the left side of operator or the right side of it.
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 parens:
# 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<Named Unary Operators> for more discussion of this.
Also parsed as terms are the C<do {}> and C<eval {}> constructs, as
well as subroutine and method calls, and the anonymous
constructors C<[]> and C<{}>.
See also L<Quote and Quotelike Operators> toward the end of this section,
as well as L<"I/O Operators">.
=head2 The Arrow Operator
Just as in C and C++, "C<-E<gt>>" 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<perlref>.
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<perlobj>.
=head2 Autoincrement and Autodecrement
"++" 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 autoincrement operator has a little extra built-in 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 only been used in string contexts since it was set, and
has a value that is not null 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 autodecrement 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<not> 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<-bareword> is equivalent
to C<"-bareword">.
Unary "~" performs bitwise negation, i.e. 1's complement.
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<List Operators>.)
Unary "\" creates a reference to whatever follows it. See L<perlref>.
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 translation. The left argument is what is
supposed to be searched, substituted, or translated 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 translation, it is interpreted as a search pattern at run
time. This is less efficient than an explicit search, since the pattern
must be compiled every time the expression is evaluated--unless you've
used C</o>.)
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 the two numbers.
Binary "x" is the repetition operator. In a scalar context, it
returns a string consisting of the left operand repeated the number of
times specified by the right operand. In a list context, if the left
operand is a list in parens, it repeats the list.
print '-' x 80; # print row of dashes
print "\t" x ($tab/8), ' ' x ($tab%8); # tab over
@ones = (1) x 80; # a list of 80 1's
@ones = (5) x @ones; # set all elements to 5
=head2 Additive Operators
Binary "+" returns the sum of two numbers.
Binary "-" returns the difference of two numbers.
Binary "." concatenates two strings.
=head2 Shift Operators
Binary "<<" returns the value of its left argument shifted left by the
number of bits specified by the right argument. Arguments should be
integers.
Binary ">>" returns the value of its left argument shifted right by the
number of bits specified by the right argument. Arguments should be
integers.
=head2 Named Unary Operators
The various named unary operators are treated as functions with one
argument, with optional parentheses. These include the filetest
operators, like C<-f>, C<-M>, etc. See L<perlfunc>.
If any list operator (print(), etc.) or any unary operator (chdir(), etc.)
is followed by a left parenthesis as the next token