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 >
Text File  |  1996-06-28  |  40KB  |  1,120 lines

  1. =head1 NAME
  2.  
  3. perlop - Perl operators and precedence
  4.  
  5. =head1 SYNOPSIS
  6.  
  7. Perl operators have the following associativity and precedence,
  8. listed from highest precedence to lowest.  Note that all operators
  9. borrowed from C keep the same precedence relationship with each other,
  10. even where C's precedence is slightly screwy.  (This makes learning
  11. Perl easier for C folks.)  With very few exceptions, these all 
  12. operate on scalar values only, not array values.
  13.  
  14.     left    terms and list operators (leftward)
  15.     left    ->
  16.     nonassoc    ++ --
  17.     right    **
  18.     right    ! ~ \ and unary + and -
  19.     left    =~ !~ 
  20.     left    * / % x
  21.     left    + - .
  22.     left    << >>
  23.     nonassoc    named unary operators
  24.     nonassoc    < > <= >= lt gt le ge
  25.     nonassoc    == != <=> eq ne cmp
  26.     left    &
  27.     left    | ^
  28.     left    &&
  29.     left    ||
  30.     nonassoc    ..
  31.     right    ?:
  32.     right    = += -= *= etc.
  33.     left    , =>
  34.     nonassoc    list operators (rightward)
  35.     right    not
  36.     left    and
  37.     left    or xor
  38.  
  39. In the following sections, these operators are covered in precedence order.
  40.  
  41. =head1 DESCRIPTION
  42.  
  43. =head2 Terms and List Operators (Leftward)
  44.  
  45. Any TERM is of highest precedence of Perl.  These includes variables,
  46. quote and quotelike operators, any expression in parentheses,
  47. and any function whose arguments are parenthesized.  Actually, there
  48. aren't really functions in this sense, just list operators and unary
  49. operators behaving as functions because you put parentheses around
  50. the arguments.  These are all documented in L<perlfunc>.
  51.  
  52. If any list operator (print(), etc.) or any unary operator (chdir(), etc.)
  53. is followed by a left parenthesis as the next token, the operator and
  54. arguments within parentheses are taken to be of highest precedence,
  55. just like a normal function call.
  56.  
  57. In the absence of parentheses, the precedence of list operators such as
  58. C<print>, C<sort>, or C<chmod> is either very high or very low depending on
  59. whether you look at the left side of operator or the right side of it.
  60. For example, in
  61.  
  62.     @ary = (1, 3, sort 4, 2);
  63.     print @ary;        # prints 1324
  64.  
  65. the commas on the right of the sort are evaluated before the sort, but
  66. the commas on the left are evaluated after.  In other words, list
  67. operators tend to gobble up all the arguments that follow them, and
  68. then act like a simple TERM with regard to the preceding expression.
  69. Note that you have to be careful with parens:
  70.  
  71.     # These evaluate exit before doing the print:
  72.     print($foo, exit);    # Obviously not what you want.
  73.     print $foo, exit;    # Nor is this.
  74.  
  75.     # These do the print before evaluating exit:
  76.     (print $foo), exit;    # This is what you want.
  77.     print($foo), exit;    # Or this.
  78.     print ($foo), exit;    # Or even this.
  79.  
  80. Also note that
  81.  
  82.     print ($foo & 255) + 1, "\n";
  83.  
  84. probably doesn't do what you expect at first glance.  See 
  85. L<Named Unary Operators> for more discussion of this.
  86.  
  87. Also parsed as terms are the C<do {}> and C<eval {}> constructs, as
  88. well as subroutine and method calls, and the anonymous 
  89. constructors C<[]> and C<{}>.
  90.  
  91. See also L<Quote and Quotelike Operators> toward the end of this section,
  92. as well as L<"I/O Operators">.
  93.  
  94. =head2 The Arrow Operator
  95.  
  96. Just as in C and C++, "C<-E<gt>>" is an infix dereference operator.  If the
  97. right side is either a C<[...]> or C<{...}> subscript, then the left side
  98. must be either a hard or symbolic reference to an array or hash (or
  99. a location capable of holding a hard reference, if it's an lvalue (assignable)).
  100. See L<perlref>.
  101.  
  102. Otherwise, the right side is a method name or a simple scalar variable
  103. containing the method name, and the left side must either be an object
  104. (a blessed reference) or a class name (that is, a package name).
  105. See L<perlobj>.
  106.  
  107. =head2 Autoincrement and Autodecrement
  108.  
  109. "++" and "--" work as in C.  That is, if placed before a variable, they
  110. increment or decrement the variable before returning the value, and if
  111. placed after, increment or decrement the variable after returning the value.
  112.  
  113. The autoincrement operator has a little extra built-in magic to it.  If
  114. you increment a variable that is numeric, or that has ever been used in
  115. a numeric context, you get a normal increment.  If, however, the
  116. variable has only been used in string contexts since it was set, and
  117. has a value that is not null and matches the pattern
  118. C</^[a-zA-Z]*[0-9]*$/>, the increment is done as a string, preserving each
  119. character within its range, with carry:
  120.  
  121.     print ++($foo = '99');    # prints '100'
  122.     print ++($foo = 'a0');    # prints 'a1'
  123.     print ++($foo = 'Az');    # prints 'Ba'
  124.     print ++($foo = 'zz');    # prints 'aaa'
  125.  
  126. The autodecrement operator is not magical.
  127.  
  128. =head2 Exponentiation
  129.  
  130. Binary "**" is the exponentiation operator.  Note that it binds even more
  131. tightly than unary minus, so -2**4 is -(2**4), not (-2)**4. (This is
  132. implemented using C's pow(3) function, which actually works on doubles
  133. internally.)
  134.  
  135. =head2 Symbolic Unary Operators
  136.  
  137. Unary "!" performs logical negation, i.e. "not".  See also C<not> for a lower
  138. precedence version of this.
  139.  
  140. Unary "-" performs arithmetic negation if the operand is numeric.  If
  141. the operand is an identifier, a string consisting of a minus sign
  142. concatenated with the identifier is returned.  Otherwise, if the string
  143. starts with a plus or minus, a string starting with the opposite sign
  144. is returned.  One effect of these rules is that C<-bareword> is equivalent
  145. to C<"-bareword">.
  146.  
  147. Unary "~" performs bitwise negation, i.e. 1's complement.
  148.  
  149. Unary "+" has no effect whatsoever, even on strings.  It is useful
  150. syntactically for separating a function name from a parenthesized expression
  151. that would otherwise be interpreted as the complete list of function
  152. arguments.  (See examples above under L<List Operators>.)
  153.  
  154. Unary "\" creates a reference to whatever follows it.  See L<perlref>.
  155. Do not confuse this behavior with the behavior of backslash within a
  156. string, although both forms do convey the notion of protecting the next
  157. thing from interpretation.
  158.  
  159. =head2 Binding Operators
  160.  
  161. Binary "=~" binds a scalar expression to a pattern match.  Certain operations
  162. search or modify the string $_ by default.  This operator makes that kind
  163. of operation work on some other string.  The right argument is a search
  164. pattern, substitution, or translation.  The left argument is what is
  165. supposed to be searched, substituted, or translated instead of the default
  166. $_.  The return value indicates the success of the operation.  (If the
  167. right argument is an expression rather than a search pattern,
  168. substitution, or translation, it is interpreted as a search pattern at run
  169. time.  This is less efficient than an explicit search, since the pattern
  170. must be compiled every time the expression is evaluated--unless you've
  171. used C</o>.)
  172.  
  173. Binary "!~" is just like "=~" except the return value is negated in
  174. the logical sense.
  175.  
  176. =head2 Multiplicative Operators
  177.  
  178. Binary "*" multiplies two numbers.
  179.  
  180. Binary "/" divides two numbers.
  181.  
  182. Binary "%" computes the modulus of the two numbers.
  183.  
  184. Binary "x" is the repetition operator.  In a scalar context, it
  185. returns a string consisting of the left operand repeated the number of
  186. times specified by the right operand.  In a list context, if the left
  187. operand is a list in parens, it repeats the list.
  188.  
  189.     print '-' x 80;        # print row of dashes
  190.  
  191.     print "\t" x ($tab/8), ' ' x ($tab%8);    # tab over
  192.  
  193.     @ones = (1) x 80;        # a list of 80 1's
  194.     @ones = (5) x @ones;    # set all elements to 5
  195.  
  196.  
  197. =head2 Additive Operators
  198.  
  199. Binary "+" returns the sum of two numbers.
  200.  
  201. Binary "-" returns the difference of two numbers.
  202.  
  203. Binary "." concatenates two strings.
  204.  
  205. =head2 Shift Operators
  206.  
  207. Binary "<<" returns the value of its left argument shifted left by the
  208. number of bits specified by the right argument.  Arguments should be 
  209. integers.
  210.  
  211. Binary ">>" returns the value of its left argument shifted right by the
  212. number of bits specified by the right argument.  Arguments should be 
  213. integers.
  214.  
  215. =head2 Named Unary Operators
  216.  
  217. The various named unary operators are treated as functions with one
  218. argument, with optional parentheses.  These include the filetest
  219. operators, like C<-f>, C<-M>, etc.  See L<perlfunc>.
  220.  
  221. If any list operator (print(), etc.) or any unary operator (chdir(), etc.)
  222. is followed by a left parenthesis as the next token