Use PERL=../miniperl
[p5sagit/p5-mst-13.2.git] / pod / perlop.pod
index d33ce93..13655a7 100644 (file)
@@ -37,7 +37,7 @@ Perl easier for C folks.)
 
 In the following sections, these operators are covered in precedence order.
 
-=head1 DESCRIPTIONS
+=head1 DESCRIPTION
 
 =head2 Terms and List Operators (Leftward)
 
@@ -127,7 +127,9 @@ 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.
+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
 
@@ -155,17 +157,17 @@ thing from interpretation.
 
 =head2 Binding Operators
 
-Binary "=~" binds an 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 "=~" binds an 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.
@@ -404,17 +406,26 @@ specified.
 Ternary "?:" is the conditional operator, just as in C.  It works much
 like an if-then-else.  If the argument before the ? is true, the
 argument before the : is returned, otherwise the argument after the :
-is returned.  Scalar or list context propagates downward into the 2nd
-or 3rd argument, whichever is selected.  The operator may be assigned
-to if both the 2nd and 3rd arguments are legal lvalues (meaning that you
-can assign to them):
+is returned.  For example:
+
+    printf "I have %d dog%s.\n", $n, 
+           ($n == 1) ? '' : "s";
+
+Scalar or list context propagates downward into the 2nd
+or 3rd argument, whichever is selected.  
+
+    $a = $ok ? $b : $c;  # get a scalar
+    @a = $ok ? @b : @c;  # get an array
+    $a = $ok ? @b : @c;  # oops, that's just a count!
+
+The operator may be assigned to if both the 2nd and 3rd arguments are
+legal lvalues (meaning that you can assign to them):
 
     ($a_or_b ? $a : $b) = $c;
 
-Note that this is not guaranteed to contribute to the readability of
-your program.
+This is not necessarily guaranteed to contribute to the readability of your program.
 
-=head2 Assigment Operators
+=head2 Assignment Operators
 
 "=" is the ordinary assignment operator.
 
@@ -454,7 +465,7 @@ is equivalent to
     $a += 2;
     $a *= 3;
 
-=head2
+=head2 Comma Operator
 
 Binary "," is the comma operator.  In a scalar context it evaluates
 its left argument, throws that value away, then evaluates its right
@@ -463,6 +474,10 @@ argument and returns that value.  This is just like C's comma operator.
 In a 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.
+
 =head2 List Operators (Rightward)
 
 On the right side of a list operator, it has very low precedence,
@@ -539,7 +554,7 @@ the same character fore and aft, but the 4 sorts of brackets
                 s{}{}   Substitution      yes
                tr{}{}   Translation       no
 
-For constructs that do interpolation, variables beginning with "C<$> or "C<@>"
+For constructs that do interpolation, variables beginning with "C<$>" or "C<@>"
 are interpolated, as are the following sequences:
 
     \t         tab
@@ -571,6 +586,11 @@ particular, contrary to the expectations of shell programmers, backquotes
 do I<NOT> interpolate within double quotes, nor do single quotes impede
 evaluation of variables when used within double quotes.
 
+=head2 Regexp Quotelike Operators
+
+Here are the quotelike operators that apply to pattern
+matching and related activities.
+
 =over 8
 
 =item ?PATTERN?
@@ -619,8 +639,8 @@ interpolating won't change over the life of the script.  However, mentioning
 C</o> constitutes a promise that you won't change the variables in the pattern.
 If you change them, Perl won't even notice.
 
-If the PATTERN evaluates to a null string, the most recently executed 
-(and successfully compiled) regular expression is used instead.
+If the PATTERN evaluates to a null string, the last
+successfully executed regular expression is used instead.
 
 If used in a context that requires a list value, a pattern match returns a
 list consisting of the subexpressions matched by the parentheses in the
@@ -742,7 +762,7 @@ PATTERN contains a $ that looks like a variable rather than an
 end-of-string test, the variable will be interpolated into the pattern
 at run-time.  If you only want the pattern compiled once the first time
 the variable is interpolated, use the C</o> option.  If the pattern
-evaluates to a null string, the most recently executed (and successfully compiled) regular
+evaluates to a null string, the last successfully executed regular
 expression is used instead.  See L<perlre> for further explanation on these.
 
 Options are:
@@ -794,9 +814,9 @@ Examples:
 
     # Delete C comments.
     $program =~ s {
-       /\*     (?# Match the opening delimiter.)
-       .*?     (?# Match a minimal number of characters.)
-       \*/     (?# Match the closing delimiter.)
+       /\*     # Match the opening delimiter.
+       .*?     # Match a minimal number of characters.
+       \*/     # Match the closing delimiter.
     } []gsx;
 
     s/^\s*(.*?)\s*$/$1/;       # trim white space
@@ -874,6 +894,12 @@ Examples:
     tr [\200-\377]
        [\000-\177];            # delete 8th bit
 
+If multiple translations are given for a character, only the first one is used:
+
+    tr/AAA/XYZ/
+
+will translate any A to X.
+
 Note that because the translation table is built at compile time, neither
 the SEARCHLIST nor the REPLACEMENTLIST are subjected to double quote
 interpolation.  That means that if you want to use variables, you must use
@@ -902,30 +928,32 @@ of C<$?>).  Unlike in B<csh>, no translation is done on the return
 data--newlines remain newlines.  Unlike in any of the shells, single
 quotes do not hide variable names in the command from interpretation.
 To pass a $ through to the shell you need to hide it with a backslash.
-The generalized form of backticks is C<qx//>.
+The generalized form of backticks is C<qx//>.  (Because backticks
+always undergo shell expansion as well, see L<perlsec> for 
+security concerns.)
 
 Evaluating a filehandle in angle brackets yields the next line from
-that file (newline included, so it's never false until end of file, at which
-time an undefined value is returned).  Ordinarily you must assign that
-value to a variable, but there is one situation where an automatic
+that file (newline included, so it's never false until end of file, at
+which time an undefined value is returned).  Ordinarily you must assign
+that value to a variable, but there is one situation where an automatic
 assignment happens.  I<If and ONLY if> the input symbol is the only
 thing inside the conditional of a C<while> loop, the value is
-automatically assigned to the variable C<$_>.  (This may seem like an
-odd thing to you, but you'll use the construct in almost every Perl
-script you write.)  Anyway, the following lines are equivalent to each
-other:
+automatically assigned to the variable C<$_>.  The assigned value is
+then tested to see if it is defined.  (This may seem like an odd thing
+to you, but you'll use the construct in almost every Perl script you
+write.)  Anyway, the following lines are equivalent to each other:
 
-    while ($_ = <STDIN>) { print; }
+    while (defined($_ = <STDIN>)) { print; }
     while (<STDIN>) { print; }
     for (;<STDIN>;) { print; }
-    print while $_ = <STDIN>;
+    print while defined($_ = <STDIN>);
     print while <STDIN>;
 
 The filehandles STDIN, STDOUT and STDERR are predefined.  (The
 filehandles C<stdin>, C<stdout> and C<stderr> will also work except in
 packages, where they would be interpreted as local identifiers rather
 than global.)  Additional filehandles may be created with the open()
-function.
+function.  See L<perlfunc/open()> for details on this.
 
 If a <FILEHANDLE> is used in a context that is looking for a list, a
 list consisting of all the input lines is returned, one line per list
@@ -986,17 +1014,22 @@ haven't set @ARGV, will input from STDIN.
 
 If the string inside the angle brackets is a reference to a scalar
 variable (e.g. <$foo>), then that variable contains the name of the
-filehandle to input from.
-
-If the string inside angle brackets is not a filehandle, it is
-interpreted as a filename pattern to be globbed, and either a list of
-filenames or the next filename in the list is returned, depending on
-context.  One level of $ interpretation is done first, but you can't
-say C<E<lt>$fooE<gt>> because that's an indirect filehandle as explained in the
-previous paragraph.  You could insert curly brackets to force
-interpretation as a filename glob: C<E<lt>${foo}E<gt>>.  (Alternately, you can
-call the internal function directly as C<glob($foo)>, which is probably
-the right way to have done it in the first place.)  Example:
+filehandle to input from, or a reference to the same.  For example:
+
+    $fh = \*STDIN;
+    $line = <$fh>;
+
+If the string inside angle brackets is not a filehandle or a scalar
+variable containing a filehandle name or reference, then it is interpreted
+as a filename pattern to be globbed, and either a list of filenames or the
+next filename in the list is returned, depending on context.  One level of
+$ interpretation is done first, but you can't say C<E<lt>$fooE<gt>>
+because that's an indirect filehandle as explained in the previous
+paragraph.  In older version of Perl, programmers would insert curly
+brackets to force interpretation as a filename glob: C<E<lt>${foo}E<gt>>.
+These days, it's consdired cleaner to call the internal function directly
+as C<glob($foo)>, which is probably the right way to have done it in the
+first place.)  Example:
 
     while (<*.c>) {
        chmod 0644, $_;
@@ -1021,6 +1054,30 @@ and just do your own grep() on the filenames.  Furthermore, due to its current
 implementation of using a shell, the glob() routine may get "Arg list too 
 long" errors (unless you've installed tcsh(1L) as F</bin/csh>).
 
+A glob only evaluates its (embedded) argument when it is starting a new
+list.  All values must be read before it will start over.  In a list
+context this isn't important, because you automatically get them all
+anyway.  In a scalar context, however, the operator returns the next value
+each time it is called, or a FALSE value if you've just run out.  Again,
+FALSE is returned only once.  So if you're expecting a single value from
+a glob, it is much better to say
+
+    ($file) = <blurch*>;
+
+than
+
+    $file = <blurch*>;
+
+because the latter will alternate between returning a filename and
+returning FALSE.  
+
+It you're trying to do variable interpolation, it's definitely better
+to use the glob() function, because the older notation can cause people
+to become confused with the indirect filehandle notatin.
+
+    @files = glob("$dir/*.[ch]");
+    @files = glob($files[$i]);
+
 =head2 Constant Folding
 
 Like C, Perl does a certain amount of expression evaluation at