X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=pod%2Fperlop.pod;h=aa0e33905e5f94b5f20e8e4caadf68c129b94b26;hb=27bcc0a7e6b15b7b0d6f632d5f31918abd005ef4;hp=1550660df67785f127b347d07a7123940127ff97;hpb=fb53bbb20d623d0dbe701aa5d8875b725debcd99;p=p5sagit%2Fp5-mst-13.2.git diff --git a/pod/perlop.pod b/pod/perlop.pod index 1550660..aa0e339 100644 --- a/pod/perlop.pod +++ b/pod/perlop.pod @@ -2,7 +2,24 @@ perlop - Perl operators and precedence -=head1 SYNOPSIS +=head1 DESCRIPTION + +=head2 Operator Precedence and Associativity + +Operator precedence and associativity work in Perl more or less like +they do in mathematics. + +I means some operators are evaluated before +others. For example, in C<2 + 4 * 5>, the multiplication has higher +precedence so C<4 * 5> is evaluated first yielding C<2 + 20 == +22> and not C<6 * 5 == 30>. + +I defines what happens if a sequence of the +same operators is used one after another: whether the evaluator will +evaluate the left operations first or the right. For example, in C<8 +- 4 - 2>, subtraction is left associative so Perl evaluates the +expression left to right. C<8 - 4> is evaluated first making the +expression C<4 - 2 == 2> and not C<8 - 2 == 6>. Perl operators have the following associativity and precedence, listed from highest precedence to lowest. Operators borrowed from @@ -40,8 +57,6 @@ 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 include variables, @@ -83,8 +98,18 @@ 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. +probably doesn't do what you expect at first glance. The parentheses +enclose the argument list for C which is evaluated (printing +the result of C<$foo & 255>). Then one is added to the return value +of C (usually 1). The result is something like this: + + 1 + 1, "\n"; # Obviously not what you meant. + +To do what you meant properly, you must write: + + print(($foo & 255) + 1, "\n"); + +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 @@ -110,9 +135,25 @@ 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. +"++" and "--" work as in C. That is, if placed before a variable, +they increment or decrement the variable by one before returning the +value, and if placed after, increment or decrement after returning the +value. + + $i = 0; $j = 0; + print $i++; # prints 0 + print ++$j; # prints 1 + +Note that just as in C, Perl doesn't define B the variable is +incremented or decremented. You just know it will be done sometime +before or after the value is returned. This also means that modifying +a variable twice in the same statement will lead to undefined behaviour. +Avoid statements like: + + $i = $i ++; + print ++ $i + $i ++; + +Perl will not guarantee what the result of the above statements is. 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 @@ -149,15 +190,15 @@ 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">. +is returned. One effect of these rules is that -bareword is equivalent +to "-bareword". Unary "~" performs bitwise negation, i.e., 1's complement. For example, C<0666 & ~027> is 0640. (See also L and L.) Note that the width of the result is platform-dependent: ~0 is 32 bits wide on a 32-bit platform, but 64 bits wide on a 64-bit platform, so if you are expecting a certain bit -width, remember use the & operator to mask off the excess bits. +width, remember to use the & operator to mask off the excess bits. Unary "+" has no effect whatsoever, even on strings. It is useful syntactically for separating a function name from a parenthesized expression @@ -178,7 +219,8 @@ pattern, substitution, or transliteration. The left argument is what is supposed to be searched, substituted, or transliterated instead of the default $_. When used in scalar context, the return value generally indicates the success of the operation. Behavior in list context depends on the particular -operator. See L for details. +operator. See L for details and +L for examples using these operators. If the right argument is an expression rather than a search pattern, substitution, or transliteration, it is interpreted as a search pattern at run @@ -208,7 +250,9 @@ Binary "x" is the repetition operator. In scalar context or if the left operand is not enclosed in parentheses, it returns a string consisting of the left operand repeated the number of times specified by the right operand. In list context, if the left operand is enclosed in -parentheses, it repeats the list. +parentheses, it repeats the list. If the right operand is zero or +negative, it returns an empty string or an empty list, depending on the +context. print '-' x 80; # print row of dashes @@ -251,8 +295,7 @@ of bits is also undefined. =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. +argument, with optional parentheses. 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 @@ -277,6 +320,11 @@ but, because * is higher precedence than named operators: rand (10) * 20; # (rand 10) * 20 rand +(10) * 20; # rand (10 * 20) +Regarding precedence, the filetest operators, like C<-f>, C<-M>, etc. are +treated like named unary operators, but they don't follow this functional +parenthesis rule. That means, for example, that C<-f($file).".bak"> is +equivalent to C<-f "$file.bak">. + See also L<"Terms and List Operators (Leftward)">. =head2 Relational Operators @@ -382,12 +430,12 @@ tests the left hand side's definedness instead of its truth. Thus, C<$a // $b> is similar to C (except that it returns the value of C<$a> rather than the value of C) and is exactly equivalent to C. This is very useful for providing default values -for variables. If you actually want to test if at least one of C<$a> and C<$b> is -defined, use C. +for variables. If you actually want to test if at least one of C<$a> and +C<$b> is defined, use C. -The C<||>, C and C<&&> operators differ from C's in that, rather than returning -0 or 1, they return the last value evaluated. Thus, a reasonably portable -way to find out the home directory might be: +The C<||>, C and C<&&> operators return the last value evaluated +(unlike C's C<||> and C<&&>, which return 0 or 1). Thus, a reasonably +portable way to find out the home directory might be: $home = $ENV{'HOME'} // $ENV{'LOGDIR'} // (getpwuid($<))[7] // die "You're homeless!\n"; @@ -496,7 +544,27 @@ As a scalar operator: close ARGV if eof; # reset $. each file } -As a list operator: +Here's a simple example to illustrate the difference between +the two range operators: + + @lines = (" - Foo", + "01 - Bar", + "1 - Baz", + " - Quux"); + + foreach(@lines) + { + if (/0/ .. /1/) + { + print "$_\n"; + } + } + +This program will print only the line containing "Bar". If +the range operator is changed to C<...>, it will also print the +"Baz" line. + +And now some examples as a list operator: for (101 .. 200) { print; } # print $_ 100 times @foo = @foo[0 .. $#foo]; # an expensive no-op @@ -619,9 +687,10 @@ argument and returns that value. This is just like C's comma operator. In list context, it's just the list argument separator, and inserts both its arguments into the list. -The => digraph is mostly just a synonym for the comma operator. It's useful for -documenting arguments that come in pairs. As of release 5.001, it also forces -any word to the left of it to be interpreted as a string. +The C<< => >> operator is a synonym for the comma, but forces any word +to its left to be interpreted as a string (as of 5.001). It is helpful +in documenting the correspondence between keys and values in hashes, +and other paired elements in lists. =head2 List Operators (Rightward) @@ -767,6 +836,9 @@ and in transliterations. \c[ control char (ESC) \N{name} named Unicode character +B: Unlike C and other languages, Perl has no \v escape sequence for +the vertical tab (VT - ASCII 11). + The following escape sequences are available in constructs that interpolate but not in transliterations. @@ -1147,10 +1219,10 @@ but leave its STDOUT to come out the old STDERR: $output = `cmd 3>&1 1>&2 2>&3 3>&-`; To read both a command's STDOUT and its STDERR separately, it's easiest -and safest to redirect them separately to files, and then read from those -files when the program is done: +to redirect them separately to files, and then read from those files +when the program is done: - system("program args 1>/tmp/program.stdout 2>/tmp/program.stderr"); + system("program args 1>program.stdout 2>program.stderr"); Using single-quote as a delimiter protects the command from Perl's double-quote interpolation, passing it on to the shell instead: @@ -1988,6 +2060,14 @@ you say the compiler will precompute the number which that expression represents so that the interpreter won't have to. +=head2 No-ops + +Perl doesn't officially have a no-op operator, but the bare constants +C<0> and C<1> are special-cased to not produce a warning in a void +context, so you can for example safely do + + 1 while foo(); + =head2 Bitwise String Operators Bitstrings of any size may be manipulated by the bitwise operators