listed from highest precedence to lowest. Note that all operators
borrowed from C keep the same precedence relationship with each other,
even where C's precedence is slightly screwy. (This makes learning
-Perl easier for C folks.)
+Perl easier for C folks.) With very few exceptions, these all
+operate on scalar values only, not array values.
left terms and list operators (leftward)
left ->
right = += -= *= etc.
left , =>
nonassoc list operators (rightward)
- left not
+ right not
left and
left or xor
In the following sections, these operators are covered in precedence order.
-=head1 DESCRIPTIONS
+=head1 DESCRIPTION
=head2 Terms and List Operators (Leftward)
constructors C<[]> and C<{}>.
See also L<Quote and Quotelike Operators> toward the end of this section,
-as well as L<I/O Operators>.
+as well as L<"I/O Operators">.
=head2 The Arrow Operator
=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
=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 a scalar 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.
@foo = @foo[$#foo-4 .. $#foo]; # slice last 5 items
The range operator (in a list context) makes use of the magical
-autoincrement algorithm if the operaands are strings. You
+autoincrement algorithm if the operands are strings. You
can say
@alphabet = ('A' .. 'Z');
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.
In a list context, it's just the list argument separator, and inserts
both its arguments into the list.
-The => digraph is simply a synonym for the comma operator. It's useful
-for documenting arguments that come in pairs.
+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)
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
\n newline
\r return
\f form feed
- \v vertical tab, whatever that is
\b backspace
\a alarm (bell)
\e escape
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?
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
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:
# 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
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
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
element. It's easy to make a I<LARGE> data space this way, so use with
care.
-The null filehandle <> is special and can be used to emulate the
-behavior of B<sed> and B<awk>. Input from <> comes either from
+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
-how it works: the first time <> is evaluated, the @ARGV array is
+how it works: the first time E<lt>E<gt> is evaluated, the @ARGV array is
checked, and if it is null, C<$ARGV[0]> is set to "-", which when opened
gives you standard input. The @ARGV array is then processed as a list
of filenames. The loop
except that it isn't so cumbersome to say, and will actually work. It
really does shift array @ARGV and put the current filename into variable
-$ARGV. It also uses filehandle I<ARGV> internally--<> is just a synonym
+$ARGV. It also uses filehandle I<ARGV> internally--E<lt>E<gt> is just a synonym
for <ARGV>, which is magical. (The pseudo code above doesn't work
because it treats <ARGV> as non-magical.)
-You can modify @ARGV before the first <> as long as the array ends up
+You can modify @ARGV before the first E<lt>E<gt> as long as the array ends up
containing the list of filenames you really want. Line numbers (C<$.>)
continue as if the input were one big happy file. (But see example
under eof() for how to reset line numbers on each file.)
... # code for each line
}
-The <> symbol will return FALSE only once. If you call it again after
+The E<lt>E<gt> symbol will return FALSE only once. If you call it again after
this it will assume you are processing another @ARGV list, and if you
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 considered 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, $_;
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