suppress bogus warning on C<sub x {} x()>
[p5sagit/p5-mst-13.2.git] / pod / perlop.pod
index c534234..35f9e5f 100644 (file)
@@ -187,10 +187,7 @@ operands C<$a> and C<$b>: If C<$b> is positive, then C<$a % $b> is
 C<$a> minus the largest multiple of C<$b> that is not greater than
 C<$a>.  If C<$b> is negative, then C<$a % $b> is C<$a> minus the
 smallest multiple of C<$b> that is not less than C<$a> (i.e. the
-result will be less than or equal to zero).  If C<use integer> is
-in effect, the native hardware will be used instead of this rule,
-which may be construed a bug that will be fixed at some point.
-
+result will be less than or equal to zero). 
 Note than when C<use integer> is in scope, "%" give you direct access
 to the modulus operator as implemented by your C compiler.  This
 operator is not as well defined for negative operands, but it will
@@ -369,10 +366,11 @@ Use "or" for assignment is unlikely to do what you want; see below.
 Binary ".." is the range operator, which is really two different
 operators depending on the context.  In list context, it returns an
 array of values counting (by ones) from the left value to the right
-value.  This is useful for writing C<for (1..10)> loops and for doing
-slice operations on arrays.  Be aware that under the current implementation,
-a temporary array is created, so you'll burn a lot of memory if you
-write something like this:
+value.  This is useful for writing C<foreach (1..10)> loops and for
+doing slice operations on arrays.  In the current implementation, no
+temporary array is created when the range operator is used as the
+expression in C<foreach> loops, but older versions of Perl might burn
+a lot of memory when you write something like this:
 
     for (1 .. 1_000_000) {
        # code
@@ -398,7 +396,7 @@ encountered.  The final sequence number in a range has the string "E0"
 appended to it, which doesn't affect its numeric value, but gives you
 something to search for if you want to exclude the endpoint.  You can
 exclude the beginning point by waiting for the sequence number to be
-greater than 1.  If either operand of scalar ".." is a numeric literal,
+greater than 1.  If either operand of scalar ".." is a constant expression,
 that operand is implicitly compared to the C<$.> variable, the current
 line number.  Examples:
 
@@ -623,6 +621,7 @@ the same character fore and aft, but the 4 sorts of brackets
        ``      qx{}          Command             yes (unless '' is delimiter)
                qw{}         Word list            no
        //       m{}       Pattern match          yes
+               qr{}          Pattern             yes
                 s{}{}      Substitution          yes
                tr{}{}    Transliteration         no (but see below)
 
@@ -637,7 +636,7 @@ next line. This allows you to write:
 
 For constructs that do interpolation, variables beginning with "C<$>"
 or "C<@>" are interpolated, as are the following sequences. Within
-a transliteration, the first ten of these sequences may be used.
+a transliteration, the first eleven of these sequences may be used.
 
     \t         tab             (HT, TAB)
     \n         newline         (NL)
@@ -646,8 +645,9 @@ a transliteration, the first ten of these sequences may be used.
     \b         backspace       (BS)
     \a         alarm (bell)    (BEL)
     \e         escape          (ESC)
-    \033       octal char
-    \x1b       hex char
+    \033       octal char      (ESC)
+    \x1b       hex char        (ESC)
+    \x{263a}   wide hex char   (SMILEY)
     \c[                control char
 
     \l         lowercase next char
@@ -749,7 +749,6 @@ Options are:
     m  Treat string as multiple lines.
     o  Compile pattern only once.
     s  Treat string as single line.
-    t  Taint $1 etc. if target string is tainted.
     x  Use extended regular expressions.
 
 If "/" is the delimiter then the initial C<m> is optional.  With the C<m>
@@ -774,12 +773,13 @@ If you change them, Perl won't even notice.
 If the PATTERN evaluates to the empty string, the last
 I<successfully> matched regular expression is used instead.
 
-If used in a context that requires a list value, a pattern match returns a
+If the C</g> option is not used, C<m//> in a list context returns a
 list consisting of the subexpressions matched by the parentheses in the
-pattern, i.e., (C<$1>, $2, $3...).  (Note that here $1 etc. are also set, and
-that this differs from Perl 4's behavior.)  If there are no parentheses,
-the return value is the list C<(1)> for success or C<('')> upon failure.
-With parentheses, C<()> is returned upon failure.
+pattern, i.e., (C<$1>, C<$2>, C<$3>...).  (Note that here C<$1> etc. are
+also set, and that this differs from Perl 4's behavior.)  When there are
+no parentheses in the pattern, the return value is the list C<(1)> for
+success.  With or without parentheses, an empty list is returned upon
+failure.
 
 Examples:
 
@@ -810,15 +810,13 @@ substrings matched by all the parentheses in the regular expression.
 If there are no parentheses, it returns a list of all the matched
 strings, as if there were parentheses around the whole pattern.
 
-In scalar context, C<m//g> iterates through the string, returning TRUE
-each time it matches, and FALSE when it eventually runs out of matches.
-(In other words, it remembers where it left off last time and restarts
-the search at that point.  You can actually find the current match
-position of a string or set it using the pos() function; see
-L<perlfunc/pos>.)  A failed match normally resets the search position to
-the beginning of the string, but you can avoid that by adding the C</c>
-modifier (e.g. C<m//gc>).  Modifying the target string also resets the
-search position.
+In scalar context, each execution of C<m//g> finds the next match,
+returning TRUE if it matches, and FALSE if there is no further match.
+The position after the last match can be read or set using the pos()
+function; see L<perlfunc/pos>.   A failed match normally resets the
+search position to the beginning of the string, but you can avoid that
+by adding the C</c> modifier (e.g. C<m//gc>).  Modifying the target
+string also resets the search position.
 
 You can intermix C<m//g> matches with C<m/\G.../g>, where C<\G> is a
 zero-width assertion that matches the exact position where the previous
@@ -910,6 +908,39 @@ A double-quoted, interpolated string.
                if /(tcl|rexx|python)/;      # :-)
     $baz = "\n";               # a one-character string
 
+=item qr/STRING/imosx
+
+A string which is (possibly) interpolated and then compiled as a
+regular expression. The result may be used as a pattern in a match
+
+    $re = qr/$pattern/;
+    $string =~ /foo${re}bar/;  # can be interpolated in other patterns
+    $string =~ $re;            # or used standalone
+
+Options are:
+
+    i  Do case-insensitive pattern matching.
+    m  Treat string as multiple lines.
+    o  Compile pattern only once.
+    s  Treat string as single line.
+    x  Use extended regular expressions.
+
+The benefit from this is that the pattern is precompiled into an internal
+representation, and does not need to be recompiled every time a match
+is attempted.  This makes it very efficient to do something like:
+
+    foreach $pattern (@pattern_list) {
+       my $re = qr/$pattern/;
+       foreach $line (@lines) {
+           if($line =~ /$re/) {
+               do_something($line);
+           }
+       }
+    }
+
+See L<perlre> for additional information on valid syntax for STRING, and
+for a detailed look at the semantics of regular expressions.
+
 =item qx/STRING/
 
 =item `STRING`
@@ -1108,9 +1139,9 @@ to occur.  Here are two common cases:
     1 while s/\t+/' ' x (length($&)*8 - length($`)%8)/e;
 
 
-=item tr/SEARCHLIST/REPLACEMENTLIST/cds
+=item tr/SEARCHLIST/REPLACEMENTLIST/cdsUC
 
-=item y/SEARCHLIST/REPLACEMENTLIST/cds
+=item y/SEARCHLIST/REPLACEMENTLIST/cdsUC
 
 Transliterates all occurrences of the characters found in the search list
 with the corresponding character in the replacement list.  It returns
@@ -1130,6 +1161,8 @@ Options:
     c  Complement the SEARCHLIST.
     d  Delete found but unreplaced characters.
     s  Squash duplicate replaced characters.
+    U  Translate to/from UTF-8.
+    C  Translate to/from 8-bit char (octet).
 
 If the C</c> modifier is specified, the SEARCHLIST character set is
 complemented.  If the C</d> modifier is specified, any characters specified
@@ -1147,6 +1180,10 @@ enough.  If the REPLACEMENTLIST is empty, the SEARCHLIST is replicated.
 This latter is useful for counting characters in a class or for
 squashing character sequences in a class.
 
+The first C</U> or C</C> modifier applies to the left side of the translation.
+The second one applies to the right side.  If present, these modifiers override
+the current utf8 state.
+
 Examples:
 
     $ARGV[1] =~ tr/A-Z/a-z/;   # canonicalize to lower case
@@ -1166,6 +1203,9 @@ Examples:
     tr [\200-\377]
        [\000-\177];            # delete 8th bit
 
+    tr/\0-\xFF//CU;            # translate Latin-1 to Unicode
+    tr/\0-\x{FF}//UC;          # translate Unicode to Latin-1
+
 If multiple transliterations are given for a character, only the first one is used:
 
     tr/AAA/XYZ/
@@ -1210,7 +1250,7 @@ one to five, but they are always performed in the same order.
 
 First pass is finding the end of the quoted construct, be it multichar ender
 C<"\nEOF\n"> of C<<<EOF> construct, C</> which terminates C<qq/> construct,
-C<E<]>> which terminates C<qq[> construct, or C<E<gt>> which terminates a
+C<]> which terminates C<qq[> construct, or C<E<gt>> which terminates a
 fileglob started with C<<>.
 
 When searching for multichar construct no skipping is performed.  When 
@@ -1219,9 +1259,9 @@ C<\\> and C<\/> are skipped.  When searching for one-char matching delimiter,
 such as C<]>, combinations C<\\>, C<\]> and C<\[> are skipped, and 
 nested C<[>, C<]> are skipped as well.
 
-For 3-parts constructs C<s///> etc. the search is repeated once more.
+For 3-parts constructs, C<s///> etc. the search is repeated once more.
 
-During this search no attension is paid to the semantic of the construct, thus
+During this search no attention is paid to the semantic of the construct, thus
 
     "$hash{"$foo/$bar"}"
 
@@ -1252,7 +1292,7 @@ parsing.
 =item Interpolation
 
 Next step is interpolation in the obtained delimiter-independent text.
-There are many different cases.
+There are four different cases.
 
 =over
 
@@ -1282,11 +1322,11 @@ constructs, thus C<"'@arr'"> becomes
 Since all three above steps are performed simultaneously left-to-right,
 the is no way to insert a literal C<$> or C<@> inside C<\Q\E> pair: it
 cannot be protected by C<\>, since any C<\> (except in C<\E>) is 
-interpreted as a literal inside C<\Q\E>, and any $ is 
+interpreted as a literal inside C<\Q\E>, and any C<$> is 
 interpreted as starting an interpolated scalar.
 
 Note also that the interpolating code needs to make decision where the 
-interpolated scalar ends, say, whether C<"a $b -> {c}"> means 
+interpolated scalar ends, say, whether C<"a $b -E<gt> {c}"> means 
 
   "a " . $b . " -> {c}";
 
@@ -1300,24 +1340,24 @@ not include spaces between components and contains matching braces/brackets.
 =item C<?RE?>, C</RE/>, C<m/RE/>, C<s/RE/foo/>, 
 
 Processing of C<\Q>, C<\U>, C<\u>, C<\L>, C<\l> and interpolation happens 
-(almost) as with qq// constructs, but I<the substitution of C<\> followed by
+(almost) as with C<qq//> constructs, but I<the substitution of C<\> followed by
 other chars is not performed>!  Moreover, inside C<(?{BLOCK})> no processing
 is performed at all.
 
-Interpolation has several quirks: $|, $( and $) are not interpolated, and
+Interpolation has several quirks: C<$|>, C<$(> and C<$)> are not interpolated, and
 constructs C<$var[SOMETHING]> are I<voted> (by several different estimators) 
-to be an array element or $var followed by a RE alternative.  This is
+to be an array element or C<$var> followed by a RE alternative.  This is
 the place where the notation C<${arr[$bar]}> comes handy: C</${arr[0-9]}/>
-is interpreted as an array element -9, not as a regular expression from
-variable $arr followed by a digit, which is the interpretation of 
+is interpreted as an array element C<-9>, not as a regular expression from
+variable C<$arr> followed by a digit, which is the interpretation of 
 C</$arr[0-9]/>.
 
-Note that absense of processing of C<\\> creates specific restrictions on the
-post-processed text: if the delimeter is C</>, one cannot get the combination
+Note that absence of processing of C<\\> creates specific restrictions on the
+post-processed text: if the delimiter is C</>, one cannot get the combination
 C<\/> into the result of this step: C</> will finish the regular expression,
 C<\/> will be stripped to C</> on the previous step, and C<\\/> will be left
 as is.  Since C</> is equivalent to C<\/> inside a regular expression, this
-does not matter unless the delimiter is special character for RE engine, as 
+does not matter unless the delimiter is special character for the RE engine, as 
 in C<s*foo*bar*>, C<m[foo]>, or C<?foo?>.
 
 =back
@@ -1331,19 +1371,19 @@ All the previous steps were performed during the compilation of Perl code,
 this one happens in run time (though it may be optimized to be calculated
 at compile time if appropriate).  After all the preprocessing performed 
 above (and possibly after evaluation if catenation, joining, up/down-casing 
-and quotemeta()ing are involved) the resulting I<string> is passed to RE
+and C<quotemeta()>ing are involved) the resulting I<string> is passed to RE
 engine for compilation.
 
 Whatever happens in the RE engine is better be discussed in L<perlre>,
 but for the sake of continuity let us do it here.
 
-This is the first step where presense of the C<//x> switch is relevant.
-RE engine scans the string left-to-right, and converts it to a finite 
+This is the first step where presence of the C<//x> switch is relevant.
+The RE engine scans the string left-to-right, and converts it to a finite 
 automaton.  
 
 Backslashed chars are either substituted by corresponding literal 
 strings, or generate special nodes of the finite automaton.  Characters
-which are special to RE engine generate corresponding nodes.  C<(?#...)>
+which are special to the RE engine generate corresponding nodes.  C<(?#...)>
 comments are ignored.  All the rest is either converted to literal strings
 to match, or is ignored (as is whitespace and C<#>-style comments if
 C<//x> is present).
@@ -1354,7 +1394,7 @@ Similarly, the C<(?{...})> is only checked for matching braces.
 
 =item Optimization of regular expressions
 
-This step is listed for compeleteness only.  Since it does not change
+This step is listed for completeness only.  Since it does not change
 semantics, details of this step are not documented and are subject
 to change.