<P>Perl operators have the following associativity and precedence,
listed from highest precedence to lowest. 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.) With very few exceptions, these all operate on scalar
values only, not array values.</P>
<PRE>
left terms and list operators (leftward)
left ->
nonassoc ++ --
right **
right ! ~ \ and unary + and -
left =~ !~
left * / % x
left + - .
left << >>
nonassoc named unary operators
nonassoc < > <= >= lt gt le ge
nonassoc == != <=> eq ne cmp
left &
left | ^
left &&
left ||
nonassoc .. ...
right ?:
right = += -= *= etc.
left , =>
nonassoc list operators (rightward)
right not
left and
left or xor</PRE>
<P>In the following sections, these operators are covered in precedence order.</P>
<P>Many operators can be overloaded for objects. See <A HREF="../../lib/overload.html">the overload manpage</A>.</P>
<P>
<HR>
<H1><A NAME="description">DESCRIPTION</A></H1>
<P>
<H2><A NAME="terms and list operators (leftward)">Terms and List Operators (Leftward)</A></H2>
<P>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
operators behaving as functions because you put parentheses around
the arguments. These are all documented in <A HREF="../../lib/Pod/perlfunc.html">the perlfunc manpage</A>.</P>
<P>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
arguments within parentheses are taken to be of highest precedence,
just like a normal function call.</P>
<P>In the absence of parentheses, the precedence of list operators such as
<A HREF="../../lib/Pod/perlfunc.html#item_print"><CODE>print</CODE></A>, <A HREF="../../lib/Pod/perlfunc.html#item_sort"><CODE>sort</CODE></A>, or <A HREF="../../lib/Pod/perlfunc.html#item_chmod"><CODE>chmod</CODE></A> is either very high or very low depending on
whether you are looking at the left side or the right side of the operator.
For example, in</P>
<PRE>
@ary = (1, 3, sort 4, 2);
print @ary; # prints 1324</PRE>
<P>the commas on the right of the sort are evaluated before the sort,
but the commas on the left are evaluated after. In other words,
list operators tend to gobble up all arguments that follow, and
then act like a simple TERM with regard to the preceding expression.
Be careful with parentheses:</P>
<PRE>
# These evaluate exit before doing the print:
print($foo, exit); # Obviously not what you want.
print $foo, exit; # Nor is this.</PRE>
<PRE>
# These do the print before evaluating exit:
(print $foo), exit; # This is what you want.
print($foo), exit; # Or this.
print ($foo), exit; # Or even this.</PRE>
<P>Also note that</P>
<PRE>
print ($foo & 255) + 1, "\n";</PRE>
<P>probably doesn't do what you expect at first glance. See
<A HREF="#named unary operators">Named Unary Operators</A> for more discussion of this.</P>
<P>Also parsed as terms are the <A HREF="../../lib/Pod/perlfunc.html#item_do"><CODE>do {}</CODE></A> and <A HREF="../../lib/Pod/perlfunc.html#item_eval"><CODE>eval {}</CODE></A> constructs, as
well as subroutine and method calls, and the anonymous
constructors <CODE>[]</CODE> and <CODE>{}</CODE>.</P>
<P>See also <A HREF="#quote and quotelike operators">Quote and Quote-like Operators</A> toward the end of this section,
as well as <A HREF="#i/o operators">I/O Operators</A>.</P>
Searches a string for a pattern match, and in scalar context returns
true if it succeeds, false if it fails. If no string is specified
via the <CODE>=~</CODE> or <CODE>!~</CODE> operator, the $_ string is searched. (The
string specified with <CODE>=~</CODE> need not be an lvalue--it may be the
result of an expression evaluation, but remember the <CODE>=~</CODE> binds
rather tightly.) See also <A HREF="../../lib/Pod/perlre.html">the perlre manpage</A>. See <A HREF="../../lib/Pod/perllocale.html">the perllocale manpage</A> for
discussion of additional considerations that apply when <CODE>use locale</CODE>
is in effect.
<P>Options are:</P>
<PRE>
c Do not reset search position on a failed match when /g is in effect.
g Match globally, i.e., find all occurrences.
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.</PRE>
<P>If ``/'' is the delimiter then the initial <A HREF="../../lib/Pod/perlfunc.html#item_m"><CODE>m</CODE></A> is optional. With the <A HREF="../../lib/Pod/perlfunc.html#item_m"><CODE>m</CODE></A>
you can use any pair of non-alphanumeric, non-whitespace characters
as delimiters. This is particularly useful for matching path names
that contain ``/'', to avoid LTS (leaning toothpick syndrome). If ``?'' is
the delimiter, then the match-only-once rule of <A HREF="#item_%3FPATTERN%3F"><CODE>?PATTERN?</CODE></A> applies.
If ``''' is the delimiter, no interpolation is performed on the PATTERN.</P>
<P>PATTERN may contain variables, which will be interpolated (and the
pattern recompiled) every time the pattern search is evaluated, except
for when the delimiter is a single quote. (Note that <CODE>$)</CODE> and <CODE>$|</CODE>
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 <CODE>/o</CODE> 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 <CODE>/o</CODE> constitutes a promise
that you won't change the variables in the pattern. If you change them,
Perl won't even notice. See also <A HREF="#qr//">qr//</A>.</P>
<P>If the PATTERN evaluates to the empty string, the last
<EM>successfully</EM> matched regular expression is used instead.</P>
<P>If the <CODE>/g</CODE> option is not used, <A HREF="#item_m/"><CODE>m//</CODE></A> in list context returns a
list consisting of the subexpressions matched by the parentheses in the
pattern, i.e., (<CODE>$1</CODE>, <CODE>$2</CODE>, <CODE>$3</CODE>...). (Note that here <CODE>$1</CODE> 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 <CODE>(1)</CODE> for
success. With or without parentheses, an empty list is returned upon
failure.</P>
<P>Examples:</P>
<PRE>
open(TTY, '/dev/tty');
<TTY> =~ /^y/i && foo(); # do foo if desired</PRE>
<PRE>
if (/Version: *([0-9.]*)/) { $version = $1; }</PRE>
<PRE>
next if m#^/usr/spool/uucp#;</PRE>
<PRE>
# poor man's grep
$arg = shift;
while (<>) {
print if /$arg/o; # compile only once
}</PRE>
<PRE>
if (($F1, $F2, $Etc) = ($foo =~ /^(\S+)\s+(\S+)\s*(.*)/))</PRE>
<P>This last example splits $foo into the first two words and the
remainder of the line, and assigns those three fields to $F1, $F2, and
$Etc. The conditional is true if any variables were assigned, i.e., if
the pattern matched.</P>
<P>The <CODE>/g</CODE> modifier specifies global pattern matching--that is,
matching as many times as possible within the string. How it behaves
depends on the context. In list context, it returns a list of the
substrings matched by any capturing 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.</P>
<P>In scalar context, each execution of <A HREF="#item_m/"><CODE>m//g</CODE></A> 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 <A HREF="../../lib/Pod/perlfunc.html#item_pos"><CODE>pos()</CODE></A>
function; see <A HREF="../../lib/Pod/perlfunc.html#pos">pos in the perlfunc manpage</A>. A failed match normally resets the
search position to the beginning of the string, but you can avoid that
by adding the <CODE>/c</CODE> modifier (e.g. <A HREF="#item_m/"><CODE>m//gc</CODE></A>). Modifying the target
string also resets the search position.</P>
<P>You can intermix <A HREF="#item_m/"><CODE>m//g</CODE></A> matches with <A HREF="#item_m/"><CODE>m/\G.../g</CODE></A>, where <CODE>\G</CODE> is a
zero-width assertion that matches the exact position where the previous
<A HREF="#item_m/"><CODE>m//g</CODE></A>, if any, left off. The <CODE>\G</CODE> assertion is not supported without
the <CODE>/g</CODE> modifier. (Currently, without <CODE>/g</CODE>, <CODE>\G</CODE> behaves just like
<CODE>\A</CODE>, but that's accidental and may change in the future.)</P>
<P>Using single-quote as a delimiter protects the command from Perl's
double-quote interpolation, passing it on to the shell instead:</P>
<PRE>
$perl_info = qx(ps $$); # that's Perl's $$
$shell_info = qx'ps $$'; # that's the new shell's $$</PRE>
<P>How that string gets evaluated is entirely subject to the command
interpreter on your system. On most platforms, you will have to protect
shell metacharacters if you want them treated literally. This is in
practice difficult to do, as it's unclear how to escape which characters.
See <A HREF="../../lib/Pod/perlsec.html">the perlsec manpage</A> for a clean and safe example of a manual <A HREF="../../lib/Pod/perlfunc.html#item_fork"><CODE>fork()</CODE></A> and <A HREF="../../lib/Pod/perlfunc.html#item_exec"><CODE>exec()</CODE></A>
to emulate backticks safely.</P>
<P>On some platforms (notably DOS-like ones), the shell may not be
capable of dealing with multiline commands, so putting newlines in
the string may not get you what you want. You may be able to evaluate
multiple commands in a single line by separating them with the command
separator character, if your shell supports that (e.g. <CODE>;</CODE> on many Unix
shells; <CODE>&</CODE> on the Windows NT <CODE>cmd</CODE> shell).</P>
<P>Beginning with v5.6.0, Perl will attempt to flush all files opened for
output before starting the child process, but this may not be supported
on some platforms (see <A HREF="../../lib/Pod/perlport.html">the perlport manpage</A>). To be safe, you may need to set
<CODE>$|</CODE> ($AUTOFLUSH in English) or call the <A HREF="../../lib/Pod/perlvar.html#item_autoflush"><CODE>autoflush()</CODE></A> method of
<CODE>IO::Handle</CODE> on any open handles.</P>
<P>Beware that some command shells may place restrictions on the length
of the command line. You must ensure your strings don't exceed this
limit after any necessary interpolations. See the platform-specific
release notes for more details about your particular environment.</P>
<P>Using this operator can lead to programs that are difficult to port,
because the shell commands called vary between systems, and may in
fact not be present at all. As one example, the <CODE>type</CODE> command under
the POSIX shell is very different from the <CODE>type</CODE> command under DOS.
That doesn't mean you should go out of your way to avoid backticks
when they're the right way to get something done. Perl was made to be
a glue language, and one of the things it glues together is commands.
Just understand what you're getting yourself into.</P>
<P>See <A HREF="#i/o operators">I/O Operators</A> for more discussion.</P>
Transliterates all occurrences of the characters found in the search list
with the corresponding character in the replacement list. It returns
the number of characters replaced or deleted. If no string is
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.)
<P>A character range may be specified with a hyphen, so <A HREF="#item_tr/"><CODE>tr/A-J/0-9/</CODE></A>
does the same replacement as <A HREF="#item_tr/"><CODE>tr/ACEGIBDFHJ/0246813579/</CODE></A>.
For <STRONG>sed</STRONG> devotees, <A HREF="../../lib/Pod/perlfunc.html#item_y"><CODE>y</CODE></A> is provided as a synonym for <A HREF="../../lib/Pod/perlfunc.html#item_tr"><CODE>tr</CODE></A>. If the
SEARCHLIST is delimited by bracketing quotes, the REPLACEMENTLIST has
its own pair of quotes, which may or may not be bracketing quotes,
e.g., <CODE>tr[A-Z][a-z]</CODE> or <A HREF="../../lib/Pod/perlfunc.html#item_tr"><CODE>tr(+\-*/)/ABCD/</CODE></A>.</P>
<P>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.</P>
<P>Options:</P>
<PRE>
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).</PRE>
<P>If the <CODE>/c</CODE> modifier is specified, the SEARCHLIST character set
is complemented. If the <CODE>/d</CODE> modifier is specified, any characters
specified by SEARCHLIST not found in REPLACEMENTLIST are deleted.
(Note that this is slightly more flexible than the behavior of some
<STRONG>tr</STRONG> programs, which delete anything they find in the SEARCHLIST,
period.) If the <CODE>/s</CODE> modifier is specified, sequences of characters
that were transliterated to the same character are squashed down
to a single instance of the character.</P>
<P>If the <CODE>/d</CODE> modifier is used, the REPLACEMENTLIST is always interpreted
exactly as specified. Otherwise, if the REPLACEMENTLIST is shorter
than the SEARCHLIST, the final character is replicated till it is long
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.</P>
<P>The first <CODE>/U</CODE> or <CODE>/C</CODE> 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.</P>
<P>Examples:</P>
<PRE>
$ARGV[1] =~ tr/A-Z/a-z/; # canonicalize to lower case</PRE>
<PRE>
$cnt = tr/*/*/; # count the stars in $_</PRE>
<PRE>
$cnt = $sky =~ tr/*/*/; # count the stars in $sky</PRE>
<PRE>
$cnt = tr/0-9//; # count the digits in $_</PRE>
<PRE>
tr/a-zA-Z//s; # bookkeeper -> bokeper</PRE>
<PRE>
($HOST = $host) =~ tr/a-z/A-Z/;</PRE>
<PRE>
tr/a-zA-Z/ /cs; # change non-alphas to single space</PRE>
<PRE>
tr [\200-\377]
[\000-\177]; # delete 8th bit</PRE>
<PRE>
tr/\0-\xFF//CU; # change Latin-1 to Unicode
tr/\0-\x{FF}//UC; # change Unicode to Latin-1</PRE>
<P>If multiple transliterations are given for a character, only the
first one is used:</P>
<PRE>
tr/AAA/XYZ/</PRE>
<P>will transliterate any A to X.</P>
<P>Because the transliteration 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 an eval():</P>
<PRE>
eval "tr/$oldlist/$newlist/";
die $@ if $@;</PRE>
<PRE>
eval "tr/$oldlist/$newlist/, 1" or die $@;</PRE>
<P></P></DL>
<P>
<H2><A NAME="gory details of parsing quoted constructs">Gory details of parsing quoted constructs</A></H2>
<P>When presented with something that might have several different
interpretations, Perl uses the <STRONG>DWIM</STRONG> (that's ``Do What I Mean'')
principle to pick the most probable interpretation. This strategy
is so successful that Perl programmers often do not suspect the
ambivalence of what they write. But from time to time, Perl's
notions differ substantially from what the author honestly meant.</P>
<P>This section hopes to clarify how Perl handles quoted constructs.
Although the most common reason to learn this is to unravel labyrinthine
regular expressions, because the initial steps of parsing are the
same for all quoting operators, they are all discussed together.</P>
<P>The most important Perl parsing rule is the first one discussed
below: when processing a quoted construct, Perl first finds the end
of that construct, then interprets its contents. If you understand
this rule, you may skip the rest of this section on the first
reading. The other rules are likely to contradict the user's
expectations much less frequently than this first one.</P>
<P>Some passes discussed below are performed concurrently, but because
their results are the same, we consider them individually. For different
quoting constructs, Perl performs different numbers of passes, from
one to five, but these passes are always performed in the same order.</P>
<DL>
<DT><STRONG><A NAME="item_Finding_the_end">Finding the end</A></STRONG><BR>
<DD>
The first pass is finding the end of the quoted construct, whether
it be a multicharacter delimiter <CODE>"\nEOF\n"</CODE> in the <CODE><<EOF</CODE>
construct, a <CODE>/</CODE> that terminates a <A HREF="#item_qq/"><CODE>qq//</CODE></A> construct, a <CODE>]</CODE> which
terminates <CODE>qq[]</CODE> construct, or a <CODE>></CODE> which terminates a
fileglob started with <CODE><</CODE>.
<P>When searching for single-character non-pairing delimiters, such
as <CODE>/</CODE>, combinations of <CODE>\\</CODE> and <CODE>\/</CODE> are skipped. However,
when searching for single-character pairing delimiter like <CODE>[</CODE>,
combinations of <CODE>\\</CODE>, <CODE>\]</CODE>, and <CODE>\[</CODE> are all skipped, and nested
<CODE>[</CODE>, <CODE>]</CODE> are skipped as well. When searching for multicharacter
delimiters, nothing is skipped.</P>
<P>For constructs with three-part delimiters (<A HREF="#item_s/"><CODE>s///</CODE></A>, <A HREF="#item_y/"><CODE>y///</CODE></A>, and
<A HREF="#item_tr/"><CODE>tr///</CODE></A>), the search is repeated once more.</P>
<P>During this search no attention is paid to the semantics of the construct.
Thus:</P>
<PRE>
"$hash{"$foo/$bar"}"</PRE>
<P>or:</P>
<PRE>
m/
bar # NOT a comment, this slash / terminated m//!
/x</PRE>
<P>do not form legal quoted expressions. The quoted part ends on the
first <CODE>"</CODE> and <CODE>/</CODE>, and the rest happens to be a syntax error.
Because the slash that terminated <A HREF="#item_m/"><CODE>m//</CODE></A> was followed by a <CODE>SPACE</CODE>,
the example above is not <A HREF="#item_m/"><CODE>m//x</CODE></A>, but rather <A HREF="#item_m/"><CODE>m//</CODE></A> with no <CODE>/x</CODE>
modifier. So the embedded <CODE>#</CODE> is interpreted as a literal <CODE>#</CODE>.</P>
<P></P>
<DT><STRONG><A NAME="item_Removal_of_backslashes_before_delimiters">Removal of backslashes before delimiters</A></STRONG><BR>
<DD>
During the second pass, text between the starting and ending
delimiters is copied to a safe location, and the <CODE>\</CODE> is removed
from combinations consisting of <CODE>\</CODE> and delimiter--or delimiters,
meaning both starting and ending delimiters will should these differ.
This removal does not happen for multi-character delimiters.
Note that the combination <CODE>\\</CODE> is left intact, just as it was.
<P>Starting from this step no information about the delimiters is
<P>There are several I/O operators you should know about.</P>
<P>A string enclosed by backticks (grave accents) first undergoes
double-quote interpolation. It is then interpreted as an external
command, and the output of that command is the value of the
pseudo-literal, j
string consisting of all output is returned. In list context, a
list of values is returned, one per line of output. (You can set
<CODE>$/</CODE> to use a different line terminator.) The command is executed
each time the pseudo-literal is evaluated. The status value of the
command is returned in <CODE>$?</CODE> (see <A HREF="../../lib/Pod/perlvar.html">the perlvar manpage</A> for the interpretation
of <CODE>$?</CODE>). Unlike in <STRONG>csh</STRONG>, 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 literal dollar-sign through to the shell you need to hide
it with a backslash. The generalized form of backticks is <A HREF="#item_qx/"><CODE>qx//</CODE></A>.
(Because backticks always undergo shell expansion as well, see
<A HREF="../../lib/Pod/perlsec.html">the perlsec manpage</A> for security concerns.)</P>
<P>In scalar context, evaluating a filehandle in angle brackets yields
the next line from that file (the newline, if any, included), or
<A HREF="../../lib/Pod/perlfunc.html#item_undef"><CODE>undef</CODE></A> at end-of-file or on error. When <CODE>$/</CODE> is set to <A HREF="../../lib/Pod/perlfunc.html#item_undef"><CODE>undef</CODE></A>
(sometimes known as file-slurp mode) and the file is empty, it
returns <CODE>''</CODE> the first time, followed by <A HREF="../../lib/Pod/perlfunc.html#item_undef"><CODE>undef</CODE></A> subsequently.</P>
<P>Ordinarily you must assign the returned value to a variable, but
there is one situation where an automatic assignment happens. If
and only if the input symbol is the only thing inside the conditional
of a <CODE>while</CODE> statement (even if disguised as a <CODE>for(;;)</CODE> loop),
the value is automatically assigned to the global variable $_,
destroying whatever was there previously. (This may seem like an
odd thing to you, but you'll use the construct in almost every Perl
script you write.) The $_ variables is not implicitly localized.
You'll have to put a <CODE>local $_;</CODE> before the loop if you want that
to happen.</P>
<P>The following lines are equivalent:</P>
<PRE>
while (defined($_ = <STDIN>)) { print; }
while ($_ = <STDIN>) { print; }
while (<STDIN>) { print; }
for (;<STDIN>;) { print; }
print while defined($_ = <STDIN>);
print while ($_ = <STDIN>);
print while <STDIN>;</PRE>
<P>This also behaves similarly, but avoids $_ :</P>
<PRE>
while (my $line = <STDIN>) { print $line }</PRE>
<P>In these loop constructs, the assigned value (whether assignment
is automatic or explicit) is then tested to see whether it is
defined. The defined test avoids problems where line has a string
value that would be treated as false by Perl, for example a ``'' or
a ``0'' with no trailing newline. If you really mean for such values
to terminate the loop, they should be tested for explicitly:</P>
<PRE>
while (($_ = <STDIN>) ne '0') { ... }
while (<STDIN>) { last unless $_; ... }</PRE>
<P>In other boolean contexts, <CODE><filehandle></CODE> without an
explicit <A HREF="../../lib/Pod/perlfunc.html#item_defined"><CODE>defined</CODE></A> test or comparison elicit a warning if the
<CODE>use warnings</CODE> pragma or the <STRONG>-w</STRONG>
command-line switch (the <CODE>$^W</CODE> variable) is in effect.</P>
<P>The filehandles STDIN, STDOUT, and STDERR are predefined. (The
filehandles <CODE>stdin</CODE>, <CODE>stdout</CODE>, and <CODE>stderr</CODE> will also work except
in packages, where they would be interpreted as local identifiers
rather than global.) Additional filehandles may be created with
the <A HREF="../../lib/Pod/perlfunc.html#item_open"><CODE>open()</CODE></A> function, amongst others. See <A HREF="../../lib/Pod/perlopentut.html">the perlopentut manpage</A> and
<A HREF="../../lib/Pod/perlfunc.html#open">open in the perlfunc manpage</A> for details on this.</P>
<P>If a <FILEHANDLE> is used in a context that is looking for
a list, a list comprising all input lines is returned, one line per
list element. It's easy to grow to a rather large data space this
way, so use with care.</P>
<P><FILEHANDLE> may also be spelled <A HREF="../../lib/Pod/perlfunc.html#item_readline"><CODE>readline(*FILEHANDLE)</CODE></A>.
See <A HREF="../../lib/Pod/perlfunc.html#readline">readline in the perlfunc manpage</A>.</P>
<P>The null filehandle <> is special: it can be used to emulate the
behavior of <STRONG>sed</STRONG> and <STRONG>awk</STRONG>. Input from <> 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
checked, and if it is empty, <CODE>$ARGV[0]</CODE> is set to ``-'', which when opened
gives you standard input. The @ARGV array is then processed as a list
of filenames. The loop</P>
<PRE>
while (<>) {
... # code for each line
}</PRE>
<P>is equivalent to the following Perl-like pseudo code:</P>
<PRE>
unshift(@ARGV, '-') unless @ARGV;
while ($ARGV = shift) {
open(ARGV, $ARGV);
while (<ARGV>) {
... # code for each line
}
}</PRE>
<P>except that it isn't so cumbersome to say, and will actually work.
It really does shift the @ARGV array and put the current filename
into the $ARGV variable. It also uses filehandle <EM>ARGV</EM>
internally--<> is just a synonym for <ARGV>, which
is magical. (The pseudo code above doesn't work because it treats
<ARGV> as non-magical.)</P>
<P>You can modify @ARGV before the first <> as long as the array ends up
containing the list of filenames you really want. Line numbers (<CODE>$.</CODE>)
continue as though the input were one big happy file. See the example
in <A HREF="../../lib/Pod/perlfunc.html#eof">eof in the perlfunc manpage</A> for how to reset line numbers on each file.</P>
<P>If you want to set @ARGV to your own list of files, go right ahead.
This sets @ARGV to all plain text files if no @ARGV was given:</P>
<P>By default, Perl assumes that it must do most of its arithmetic in
floating point. But by saying</P>
<PRE>
use integer;</PRE>
<P>you may tell the compiler that it's okay to use integer operations
(if it feels like it) from here to the end of the enclosing BLOCK.
An inner BLOCK may countermand this by saying</P>
<PRE>
no integer;</PRE>
<P>which lasts until the end of that BLOCK. Note that this doesn't
mean everything is only an integer, merely that Perl may use integer
operations if it is so inclined. For example, even under <CODE>use
integer</CODE>, if you take the <A HREF="../../lib/Pod/perlfunc.html#item_sqrt"><CODE>sqrt(2)</CODE></A>, you'll still get <CODE>1.4142135623731</CODE>
or so.</P>
<P>Used on numbers, the bitwise operators (``&'', ``|'', ``^'', ``~'', ``<<'',
and ``>>'') always produce integral results. (But see also <A HREF="#bitwise string operators">Bitwise String Operators</A>.) However, <CODE>use integer</CODE> still has meaning for
them. By default, their results are interpreted as unsigned integers, but
if <CODE>use integer</CODE> is in effect, their results are interpreted
as signed integers. For example, <CODE>~0</CODE> usually evaluates to a large
integral value. However, <CODE>use integer; ~0</CODE> is <CODE>-1</CODE> on twos-complement
<P>While <CODE>use integer</CODE> provides integer-only arithmetic, there is no
analogous mechanism to provide automatic rounding or truncation to a
certain number of decimal places. For rounding to a certain number
of digits, <A HREF="../../lib/Pod/perlfunc.html#item_sprintf"><CODE>sprintf()</CODE></A> or <A HREF="../../lib/Pod/perlfunc.html#item_printf"><CODE>printf()</CODE></A> is usually the easiest route.
See <A HREF="../../lib/Pod/perlfaq4.html">the perlfaq4 manpage</A>.</P>
<P>Floating-point numbers are only approximations to what a mathematician
would call real numbers. There are infinitely more reals than floats,
so some corners must be cut. For example:</P>
<PRE>
printf "%.20g\n", 123456789123456789;
# produces 123456789123456784</PRE>
<P>Testing for exact equality of floating-point equality or inequality is
not a good idea. Here's a (relatively expensive) work-around to compare
whether two floating-point numbers are equal to a particular number of
decimal places. See Knuth, volume II, for a more robust treatment of
this topic.</P>
<PRE>
sub fp_equal {
my ($X, $Y, $POINTS) = @_;
my ($tX, $tY);
$tX = sprintf("%.${POINTS}g", $X);
$tY = sprintf("%.${POINTS}g", $Y);
return $tX eq $tY;
}</PRE>
<P>The POSIX module (part of the standard perl distribution) implements
ceil(), floor(), and other mathematical and trigonometric functions.
The Math::Complex module (part of the standard perl distribution)
defines mathematical functions that work on both the reals and the
imaginary numbers. Math::Complex not as efficient as POSIX, but
POSIX can't work with complex numbers.</P>
<P>Rounding in financial applications can have serious implications, and
the rounding method used should be specified precisely. In these
cases, it probably pays not to trust whichever system rounding is
being used by Perl, but to instead implement the rounding function you