=head2 Terms and List Operators (Leftward)
-A TERM has the highest precedence in Perl. They includes variables,
+A TERM has the highest precedence in Perl. They include variables,
quote and quote-like 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
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
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:
"" qq{} Literal yes
`` qx{} Command yes (unless '' is delimiter)
qw{} Word list no
- // m{} Pattern match yes
- s{}{} Substitution yes
+ // m{} Pattern match yes (unless '' is delimiter)
+ qr{} Pattern yes (unless '' is delimiter)
+ s{}{} Substitution yes (unless '' is delimiter)
tr{}{} Transliteration no (but see below)
Note that there can be whitespace between the operator and the quoting
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)
\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
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>
you can use any pair of non-alphanumeric, non-whitespace characters
-as delimiters (if single quotes are used, no interpretation is done
-on the replacement string. Unlike Perl 4, Perl 5 treats backticks as normal
-delimiters; the replacement text is not evaluated as a command).
-This is particularly useful for matching Unix path names
-that contain "/", to avoid LTS (leaning toothpick syndrome). If "?" is
+as delimiters. This is particularly useful for matching Unix path names
+that contain "/", to avoid LTS (leaning toothpick syndrome). If "?" is
the delimiter, then the match-only-once rule of C<?PATTERN?> applies.
+If "'" is the delimiter, no variable interpolation is performed on the
+PATTERN.
PATTERN may contain variables, which will be interpolated (and the
-pattern recompiled) every time the pattern search is evaluated. (Note
-that C<$)> and C<$|> might not be interpolated because they look like
-end-of-string tests.) If you want such a pattern to be compiled only
-once, add a C</o> after the trailing delimiter. This avoids expensive
-run-time recompilations, and is useful when the value you are
-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.
+pattern recompiled) every time the pattern search is evaluated, except
+for when the delimiter is a single quote. (Note that C<$)> and C<$|>
+might not be interpolated because they look like end-of-string tests.)
+If you want such a pattern to be compiled only once, add a C</o> after
+the trailing delimiter. This avoids expensive run-time recompilations,
+and is useful when the value you are 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 the empty string, the last
I<successfully> matched regular expression is used instead.
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>, C<$2>, C<$3>...). (Note that here
-C<$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:
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
if /(tcl|rexx|python)/; # :-)
$baz = "\n"; # a one-character string
+=item qr/STRING/imosx
+
+Quote-as-a-regular-expression operator. I<STRING> is interpolated the
+same way as I<PATTERN> in C<m/PATTERN/>. If "'" is used as the
+delimiter, no variable interpolation is done. Returns a Perl value
+which may be used instead of the corresponding C</STRING/imosx> expression.
+
+For example,
+
+ $rex = qr/my.STRING/is;
+ s/$rex/foo/;
+
+is equivalent to
+
+ s/my.STRING/foo/is;
+
+The result may be used as a subpattern in a match:
+
+ $re = qr/$pattern/;
+ $string =~ /foo${re}bar/; # can be interpolated in other patterns
+ $string =~ $re; # or used standalone
+ $string =~ /$re/; # or this way
+
+Since Perl may compile the pattern at the moment of execution of qr()
+operator, using qr() may have speed advantages in I<some> situations,
+notably if the result of qr() is used standalone:
+
+ sub match {
+ my $patterns = shift;
+ my @compiled = map qr/$_/i, @$patterns;
+ grep {
+ my $success = 0;
+ foreach my $pat @compiled {
+ $success = 1, last if /$pat/;
+ }
+ $success;
+ } @_;
+ }
+
+Precompilation of the pattern into an internal representation at the
+moment of qr() avoids a need to recompile the pattern every time a
+match C</$pat/> is attempted. (Note that Perl has many other
+internal optimizations, but none would be triggered in the above
+example if we did not use qr() operator.)
+
+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.
+
+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`
=item qw/STRING/
-Returns a list of the words extracted out of STRING, using embedded
-whitespace as the word delimiters. It is exactly equivalent to
+Evaluates to a list of the words extracted out of STRING, using embedded
+whitespace as the word delimiters. It can be understood as being roughly
+equivalent to:
split(' ', q/STRING/);
-This equivalency means that if used in scalar context, you'll get split's
-(unfortunate) scalar context behavior, complete with mysterious warnings.
+the difference being that it generates a real list at compile time. So
+this expression:
+
+ qw(foo bar baz)
+
+is exactly equivalent to the list:
+
+ ('foo', 'bar', 'baz')
Some frequently seen examples:
switch produce warnings if the STRING contains the "," or the "#"
character.
+Note that under use L<locale> qw() taints because the definition of
+whitespace is tainted. See L<perlsec> for more information about
+tainting and L<perllocale> for more information about locales.
+
=item s/PATTERN/REPLACEMENT/egimosx
Searches a string for a pattern, and if found, replaces that pattern
be scalar variable, an array element, a hash element, or an assignment
to one of those, i.e., an lvalue.)
-If the delimiter chosen is single quote, no variable interpolation is
+If the delimiter chosen is a single quote, no variable interpolation is
done on either the PATTERN or the REPLACEMENT. Otherwise, if the
PATTERN contains a $ that looks like a variable rather than an
end-of-string test, the variable will be interpolated into the pattern
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
specified via the =~ or !~ operator, the $_ string is transliterated. (The
string specified with =~ must be a scalar variable, an array element, a
hash element, or an assignment to one of those, i.e., an lvalue.)
+
A character range may be specified with a hyphen, so C<tr/A-J/0-9/>
does the same replacement as C<tr/ACEGIBDFHJ/0246813579/>.
For B<sed> devotees, C<y> is provided as a synonym for C<tr>. If the
its own pair of quotes, which may or may not be bracketing quotes,
e.g., C<tr[A-Z][a-z]> or C<tr(+\-*/)/ABCD/>.
+Note also that the whole range idea is rather unportable between
+character sets--and even within character sets they may cause results
+you probably didn't expect. A sound principle is to use only ranges
+that begin from and end at either alphabets of equal case (a-e, A-E),
+or digits (0-4). Anything else is unsafe. If in doubt, spell out the
+character sets in full.
+
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
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
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/
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
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"}"
=item Interpolation
Next step is interpolation in the obtained delimiter-independent text.
-There are many different cases.
+There are four different cases.
=over
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}";
=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
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).
=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.
=head2 I/O Operators
There are several I/O operators you should know about.
+
A string enclosed by backticks (grave accents) first undergoes
variable substitution just like a double quoted string. It is then
interpreted as a command, and the output of that command is the value
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, if any, included), or C<undef> at end of file.
-Ordinarily you must assign that value to a variable, but there is one
+In a scalar context, evaluating a filehandle in angle brackets yields the
+next line from that file (newline, if any, included), or C<undef> at
+end-of-file. When C<$/> is set to C<undef> (i.e. file slurp mode),
+it returns C<''> the first time, followed by C<undef> subsequently.
+
+Ordinarily you must assign the returned 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> or
C<for(;;)> loop, the value is automatically assigned to the variable
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. See L<perlfunc/open()> for details on this.
+function. See L<perlfunc/open> for details on this.
If a E<lt>FILEHANDLEE<gt> 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
element. It's easy to make a I<LARGE> data space this way, so use with
care.
+E<lt>FILEHANDLEE<gt> may also be spelt readline(FILEHANDLE). See
+L<perlfunc/readline>.
+
The null filehandle E<lt>E<gt> is special and can be used to emulate the
behavior of B<sed> and B<awk>. Input from E<lt>E<gt> comes either from
standard input, or from each file listed on the command line. Here's
(C<~ | & ^>).
If the operands to a binary bitwise op are strings of different sizes,
-B<or> and B<xor> ops will act as if the shorter operand had additional
-zero bits on the right, while the B<and> op will act as if the longer
-operand were truncated to the length of the shorter.
+B<|> and B<^> ops will act as if the shorter operand had additional
+zero bits on the right, while the B<&> op will act as if the longer
+operand were truncated to the length of the shorter. Note that the
+granularity for such extension or truncation is one or more I<bytes>.
# ASCII-based examples
print "j p \n" ^ " a h"; # prints "JAPH\n"
$baz = 0+$foo & 0+$bar; # both ops explicitly numeric
$biz = "$foo" ^ "$bar"; # both ops explicitly stringy
+See L<perlfunc/vec> for information on how to manipulate individual bits
+in a bit vector.
+
=head2 Integer Arithmetic
By default Perl assumes that it must do most of its arithmetic in