%				-*- Mode: LaTeX; Mode: Auto-fill -*-
\documentclass{article}
%\usepackage{alltt}
\title{Perl Programming, Part 1}
\author{Andrew Arensburger}

\newcommand{\code}[1]{\texttt{#1}}
% Make curly braces come out right.
\renewcommand{\{}{\symbol{"7B}}
\renewcommand{\}}{\symbol{"7D}}
\newcommand{\bs}{\texttt{\symbol{"5C}}}	% Backslash
\renewcommand{\tilde}{\symbol{"7E}}	% Tilde
\newcommand{\amp}{\symbol{"26}}		% Ampersand
\newcommand{\vs}{\texttt{\char`\ }}	% Visible space
\newcommand{\perl}{Perl}		% XXX - Do something special?

\newcommand{\alttwo}[2]{%
\item[\vbox{\hbox to0pt{\hss#1}%
            \hbox to0pt{\hss#2}}]%
}
\newcommand{\altthree}[3]{%
\item[\vbox{\hbox to0pt{\hss#1}%
            \hbox to0pt{\hss#2}%
            \hbox to0pt{\hss#3}}]%
}

% Like \item, but for long items: the \vbox creates two boxes one on
% top of the other. The bottom one is empty, and is just there to
% align with the text of the item.
%
% The top box begins by skipping left to the margin, then has the text
% of the item, followed by \hss to stop complaints about overfull boxes.
\newcommand{\bigitem}[1]{%
\item[\vbox{\hbox to\leftmargin{\hskip-\leftmargini#1\hss}
	    \hbox to0pt{\hss}}]%
}

\newcommand{\XXX}[1]{		% For debugging
  \marginpar{\raggedright
	[\textsl{#1}]}}

\begin{document}

% Good split:? Part 1: the language. Part 2: applied stuff.

\maketitle

\tableofcontents

\newpage
\part{}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Introduction}

	\perl\ has been called ``the Swiss army chainsaw of scripting
languages,'' and rightfully so. Anything you want to do, you can do
with \perl. If it's not already in there, you can add it.

	\perl\ has a reputation for being needlessly cryptic, with a
byzantine syntax. To a great extent, this is true. Fortunately, you
don't need to know all---or even a lot---of \perl\ in order to write
\perl\ programs.%
\footnote{It is a topic of some discussion as to whether \perl\
programs should be called ``scripts'' or ``programs.'' I don't really
care. I'll use the two terms interchangeably.}
So if you doze off in the middle of this talk, that's okay. Just
lean your head forward so that you don't snore.

	Secondly, a lot of features are shortcuts were added in order
to make certain types of things easier to do. From my own experience,
I can say that fully 90\% of all \perl\ scripts that I write are
one-shots: most of them I just enter on the command line, the rest I
put in \code{/tmp}, run once, and then forget about. When you're
writing a short throwaway, you really get to appreciate all the
shortcuts that \perl\ has to offer.

	\perl\ goes to great lengths to make things do what you expect
them to. My job is simply to tell you what it is that you expect.

	A note about versions: \perl\ version 5 introduced a lot of
new features, and generally made \perl\ even more powerful than it was
before. Since version 5 has been out for years, I will be talking
primarily about version 5. \perl\ 4 scripts will mostly run okay under
\perl\ 5, but not vice-versa. I might occasionally remember to mention
that such-and-such feature doesn't exist under \perl\ 4, but if you
try something and it doesn't work, make sure that you're using \perl\
5.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Variables}

	\perl, like any other programming language, has
variables. There are three basic types of variables: \emph{scalars},
\emph{arrays} and \emph{hashes}. In addition, \perl\ supports
\emph{references}, which allow one to do some rather interesting
things.

	Variables need not be declared in advance; if you refer to a
new variable, \perl\ will create one on the spot; the way variables
are used contains enough information for it to figure out what kind of
variable you're talking about. Bear in mind, this also means that this
can also cause hard-to-detect problems: if you make a typo, \perl\
will happily use the misspelled variable name and not tell you about
it.%
\footnote{Actually, the \code{-w} command-line option will warn you
about identifiers that have only been used once, and may therefore be
typos. But woe betide you, should you make the same typo twice.}%

	\perl\ is case-sensitive: \code{\$foo}, \code{\$Foo} and
\code{\$FOO} are all different.

	Variable names may consist of any sequence of letters, numbers
and undeerscores, but must begin with a letter or an underscore.
Actually, there are a few more exotic characters that you can use, but
most of them are already taken.

\subsection{Scalars}

	A scalar is the simplest type of variable. It holds a single
value, which can be a string or a number. Internally, \perl\ stores
scalars as strings, which means that it is not terribly well-suited
for number-crunching, since it has to keep converting strings to
numbers and back all the time.

	To assign a value to a variable, use
\begin{quote}
\code{\$var =} \textsl{value}
\end{quote}
and to refer to the value, use
\begin{quote}
\code{\$var}
\end{quote}

	You can assign several variables in parallel, e.g.,
%
\begin{quote}
\begin{verbatim}
($a, $b, $c) = (1, 2, 3);
($a, $b, $c) = ($c, $b, $a);
\end{verbatim}%$
\end{quote}
%
The second line illustrates that values on the right are determined
before the assignments happen on the left, so you can use this to swap
two variables without a temporary.

	You won't use parallel assignment in this way very often,
though: usually, you'll use it to assign an array into several
variables, or vice-versa.

\subsection{Arrays}

	Arrays are ordered lists of values. The name ``array'' is
traditional, although they're more akin to linked lists or deques.
Use an array any time you need a list of things.

\begin{quote}
\begin{verbatim}
@array = @otherarray;
@mixed = ( "arensb", 2072, 10 );
@empty = ();
\end{verbatim}
\end{quote}

	Array indices go from 0 to $n-1$ , just as in C. Unlike C,
however, you do not need to declare the size of an array in advance:
if you assign to an array index beyond the end of the array, the array
will be resized to accomodate this (any values in between will be
assigned the undefined value).

	Square brackets indicate subscripting:
%
\begin{quote}
\begin{verbatim}
$foo      = $array[9];
$array[3] = $bar;
@other    = @array[2,0,1];
@baz      = @array[4..8];
($a, $b, @rest) = @array;
\end{verbatim}
\end{quote}

	Note that in the last three examples, I used \code{@array}
instead of \code{\$array}. The \code{@} indicates that the expression
should return an array, rather than a scalar. The square brackets tell
\perl\ that \code{array} is an array. This is because \code{\$foo} and
\code{@foo} are two different variables: the first is a scalar, the
second is an array. This is perfectly legal.

	\code{@array[2,0,1]} returns an array consisting of elements
2, 0 and 1 of \code{@array}, in that order, and \code{@array[4..8]}
returns an array consisting of elements 4 through 8 inclusive of
\code{@array}.

	You can subscript anything that has an array value,
\textit{e.g.}, literal arrays:
%
\begin{quote}
\begin{verbatim}
$foo = ("a", "b", "c")[1];
\end{verbatim}%$
\end{quote}
%
though this is more useful with functions returning arrays.

	There are two ways of determining the size of an array:
%
\begin{quote}
\begin{verbatim}
$size = $#array + 1;
$size = @array;
$size = scalar(@array);
\end{verbatim}%$
\end{quote}
%
The \code{\$\#}\textit{name} construct returns the index of the last
element in the array. Since indices begin at 0, this returns the size
of the array, minus 1. The last two lines are equivalent, but the
third line makes the scalar context explicit (we'll cover contexts in
a little bit).

	You can also assign to \code{\$\#array}. This has the effect
of growing or shrinking the array, as necessary. Note that if you
shrink an array, any values past the new end are lost forever: you
cannot recover them by growing the array again.

	I mentioned that arrays in \perl\ are more akin to deques than
to C arrays. The four deque-related functions are:
%
\begin{itemize}
\addtolength{\labelsep}{1em}%
\item[\code{push} \textit{array}\code{,} \textit{value\ldots}]
	Appends \textit{value} to the end of \textit{array}.
\item[\code{pop} \textit{array}]
	Pops the last value off of the end of \textit{array}
	(shortening it by one), and returns it.
\item[\code{shift} \textit{array}]
	Removes the first element from the beginning of \textit{array}
	(shortening it by one) and returns it.
\item[\code{unshift} \textit{array}\code{,} \textit{value\ldots}]
	Prepends \textit{value} to the beginning of \textit{array}.
\end{itemize}

	If you omit \textit{array}, \code{pop} and \code{shift} will
use \code{@ARGV}, the list of command-line arguments (or \code{@\_},
inside of a function).

\subsection{Hashes}

	The third basic type in \perl\ is the \emph{hash table}, also
known as an \emph{associative array} or \emph{hash}. Like arrays, a
hash is a collection of values. However, whereas array indices are
integers, hash indices can be any string. You can think of arrays as
being lists, and hashes as being look-up tables.

\begin{quote}
\begin{verbatim}
%user2group = (
        "arensb" => 10,
        "root"   => 0,
        "bin"    => 3,
);
$user2group{"arnie"} = 199;
%empty = ()
\end{verbatim}%$
\end{quote}

	The token \code{=>} is equivalent to a comma, and is intended
as syntactic sugar when you're defining hashes.

	Extracting values from hashes works much the same way as for
arrays, except that you use curly braces instead of square brackets
for the subscripts:
%
\begin{quote}
\begin{verbatim}
$group  = $user2group{"arensb"};
@groups = @user2group{"root", "bin"};
\end{verbatim}
\end{quote}
%
Again, note that \code{\$} indicates that the expression should return
a scalar value, \code{@} indicates that it should return an array
value, and the curly braces tell \perl\ that \code{user2group} is a
hash.

	If you need to get all of the values in a hash, you can use
the \code{keys} and \code{values} functions. \code{keys~\%hash}
returns an array of all of the keys (indices) in \code{\%hash}, in no
particular order, and \code{values~\%hash} returns a list of all of
the values.

	Often, though, you don't care what the keys are, you just want
to do something to all of the elements in a hash. The \code{each}
function returns an array of two elements: a key and a value. Each
time you call \code{each~\%hash}, it returns the next key-value pair
in \code{\%hash}.

	To find out whether a hash contains a given value, you can use
the \code{defined} function:
%
\begin{quote}
\code{if~(defined(\$user2group\{"arnie"\}))}\\
\ldots
\end{quote}

	To remove a hash entry, use \code{delete}:
%
\begin{quote}
\begin{verbatim}
delete($user2group{"arnie"});
\end{verbatim}%$
\end{quote}

	By the way, there's nothing that says that the value part of a
hash entry has to represent anything. One common trick is to use a
hash as an unordered set:
%
\begin{quote}
\begin{verbatim}
%isastaffer = (
        arensb  => 1,
        root    => 1,
        bin     => 1,
);

...
if ($isastaffer{$user}) {
        ...
}
\end{verbatim}
\end{quote}

\subsection{Barewords}

	A word by itself that has no other interpretation is known as
a ``bareword,'' and is treated as if it were a double-quoted
string. Avoid barewords, since they only lead to trouble.

\subsection{Numbers}

	This is a brief section, since numbers in \perl\ work pretty
much the way you'd expect them to. The following are all legal
numbers:
%
\begin{quote}
\begin{verbatim}
  123
 0456           # Octal
0x8a0           # Hex
0.998
 .998
9.98e-1
1_234_567_890
\end{verbatim}
\end{quote}
%
The underscores in the last example are just for legibility:
\texttt{1\_234} is equivalent to \texttt{1234}.

\subsection{Context}
\label{context}

	Every expression is evaluated in a particular
\emph{context}. The two major ones are the \emph{scalar} and
\emph{array} contexts. Intuitively enough, if an expression is
evaluated in a scalar context, it yields a scalar, and if it is
evaluated in an array context, it yields an array.

	We've already seen an example of scalar vs. array context:
%
\begin{quote}
\begin{verbatim}
@array = ("a", "b", "c");
 $foo  = @array;    # Set $foo to size of @array
($bar) = @array;    # Set $bar to first element of @array
\end{verbatim}
\end{quote}
%
You can use \code{scalar(}\textit{expression}\code{)} to force
\textit{expression} to be evaluated in a scalar context. There is no
equivalent function to force an array context, though you can use
parentheses to good effect.

	Some functions return different values depending on the
context in which they're evaluated. The \code{localtime} function, for
instance, returns either the time and date in human-readable form, or
an array giving the year, month, day, hours, minutes and seconds of
the current time:
%
\begin{quote}
\begin{verbatim}
$time = localtime;      # Returns "Sat Jun 13 14:19:35 1998"
@time = localtime;      # Returns (35 19 14 13 5 98 6 163 1)
\end{verbatim}%$
\end{quote}

	If you want to write functions that behave this way, you'll
want to use the \code{wantarray} function, which returns true if the
function is being evaluated in an array context.

\subsection{Special Variables}

	\perl\ has a whole slew of special variables, most of which
have funny names like \code{\$\_}, which contributes to \perl's
reputation for looking like line noise.

	If this bothers you, you can put
%
\begin{quote}
\begin{verbatim}
use English;
\end{verbatim}
\end{quote}
%
at the top of your program. This will allow you to use either the
``traditional'' names, or English (or \textsl{awk}) equivalents.

	For the most part, special variables will be listed next to
the section to which they are pertinent. A few don't fall into
convenient categories, however, so they're listed here (along with
their alternate names):

\begin{itemize}
\addtolength{\labelsep}{1em}%
\alttwo	{\code{\$ARG}}
	{\code{\$\_}}
	The root of all being. \code{\$\_} is used everywhere, usually
	as a default argument. If you read a line and don't specify
	where to put it, it'll go into \code{\$\_}. If you want to see
	if a string matches a certain pattern, and don't specify which
	string, \perl\ will use \code{\$\_}. Even some mathematical
	functions use \code{\$\_} as their default argument.
\alttwo	{\code{\$CHILD\_ERROR}}
	{\code{\$?}}
	The status returned by the last pipe close, backtick command,
	or \code{system} call.
\altthree{\code{\$OS\_ERROR}}
	 {\code{\$ERRNO}}
	 {\code{\$!}}
	The error returned by the last system call. If you use it in a
	string context (see section \ref{context}), its value is an
	error message (what \code{perror()} would have printed). In a
	numeric context, its value is the current value of
	\code{errno}.
\altthree{\code{\$PROCESS\_ID}}
	 {\code{\$PID}}
	 {\code{\$\$}}
	The process ID of the \perl\ process running this script.
\altthree{\code{\$REAL\_USER\_ID}}
	 {\code{\$UID}}
	 {\code{\$<}}
	The real uid of this process.
\altthree{\code{\$EFFECTIVE\_USER\_ID}}
	 {\code{\$EUID}}
	 {\code{\$>}}
	The effective uid of this process.
\altthree{\code{\$REAL\_GROUP\_ID}}
	 {\code{\$GID}}
	 {\code{\$(}}
	The real gid of this process.
\altthree{\code{\$EFFECTIVE\_GROUP\_ID}}
	 {\code{\$EGID}}
	 {\code{\$)}}
	The effective gid of this process.
\item[\code{@ARGV}]
	The list of command-line options passed to the script (not to
	the \perl\ interpreter). Unlike C, \code{\$ARGV[0]} is not the
	name of the script, but rather the first command-line
	argument.
\alttwo	{\code{\$PROGRAM\_NAME}}
	{\code{\$0}}
	The file containing the \perl\ script being executed, like
	\code{argv[0]} in C.
\item[\code{\%ENV}]
	Contains the current values of environment variables. This is
	similar to \code{getenv} in C, except that you can manipulate
	the environment in a more intuitive fashion.

	If you change a value in \code{\%ENV}, the new value will be
	passed down to subprocesses.
\item[\code{\%SIG}]

	This hash is used to manipulate signal handlers. If you say
%
	\begin{quote}
	\begin{verbatim}
	$SIG{"QUIT"} = handler;
	\end{verbatim}%$
	\end{quote}
%
	or
%
	\begin{quote}
	\begin{verbatim}
	$SIG{"QUIT"} = \&handler;
	\end{verbatim}%$
	\end{quote}
%
	(the latter is preferable), \perl\ will call the function
	\code{handler} when it receives a SIGQUIT.

	If you want to restore the default handler for a signal, use
%
	\begin{quote}
	\begin{verbatim}
	$SIG{"HUP"} = 'DEFAULT';
	\end{verbatim}%$
	\end{quote}
%
	Or, if you want to ignore the signal altogether, use
%
	\begin{quote}
	\begin{verbatim}
	$SIG{"HUP"} = 'IGNORE';
	\end{verbatim}%$
	\end{quote}

\item[\code{@INC}]
	A list of directories in which to look for included files. You
	can specify additional directories by passing the
	\code{-I}\textit{directory} option to the \perl\ interpreter
	(not to your program, since it probably wouldn't know what to
	do with it).
\item[\code{\%INC}]
	Contains an entry for each file that has been included using
	\code{do} or \code{require} (we'll talk about those later).
\alttwo	{\code{\$PERL\_VERSION}}
	{\code{\$]}}
	The version of \perl\ you're using.
\alttwo	{\code{\$OSNAME}}
	{\code{\$\^{}O}}
	The name of the operating system under which this version of
	\perl\ was built.

	Actually, if you need to look at this stuff, you may want to
	use the \code{Config} module, which contains lots of other
	juicy details about the local setup.
\alttwo	{\code{\$EXECUTABLE\_NAME}}
	{\code{\$\^{}X}}
	The name of the \perl\ executable itself.
\alttwo	{\code{\$BASETIME}}
	{\code{\$\^{}T}}
	The time at which the script began running, in seconds since
	the epoch.
\alttwo	{\code{\$WARNING}}
	{\code{\$\^{}W}}
	Whether or not this script is running with the \code{-w}
	(warning) command-line option.
\end{itemize}

	It can be daunting trying to remember which variable is which,
so the perlvar(1) manual page lists a mnemonic for almost every
variable.

	Some variable names consist of a caret followed by a
letter. Rest assured, that really is a caret, and not a control
character!

\subsection{Quoting}

	\perl\ has several methods of quoting strings, analogous to
those in the Bourne shell.

\begin{itemize}
\addtolength{\labelsep}{1em}%
\alttwo	{\code{'}\ldots\code{'}}
	{\code{q\{}\ldots\code{\}}}
	Single quotes simply quote what's between them. What you type
	is what you get. The only exception to this is that you can
	use a backslash to escape a single quote (\code{\bs'}) or
	another backslash (\code{\bs\bs}).
\alttwo	{\code{"}\ldots\code{"}}
	{\code{qq\{}\ldots\code{\}}}
	Double quotes differ from single quotes mainly in that if you
	have a variable (either a scalar or an array) in
	double-quotes, its value will be expanded and interpolated
	into the string at that point. The same is true of back-quoted
	expressions (see below).
\alttwo	{\code{`}\ldots\code{`}}
	{\code{qx\{}\ldots\code{\}}}
	An expression inside backquotes is passed to the shell as a
	command; its value is whatever the command prints to
	\textit{stdout}. As with double quotes, scalar and array
	values are interpolated.
\item[\code{qw\{}\ldots\code{\}}\footnotemark]
\footnotetext{The Camel Book says that this is equivalent to
\code{(}\ldots\code{)}, but it doesn't seem to be.}%
	This is sort of a single-quote for lists: this allows you to
	create a list of elements without having to single-quote each
	one. The words are separated by whitespace instead of commas.
%
	\begin{quote}
        \begin{verbatim}
	@array = qw( sys$disk  It's!  `quot"ing' );
        \end{verbatim}%$
	\end{quote}
\item[\code{<<}\textit{word}]
	Also known as a ``here'' document, a \code{<<}\textit{word}
	string starts at the next line, and continues until
	\textit{word} appears on a line by itself.
\end{itemize}

	When an array value is interpolated into a string, \perl\
inserts the value of the special variable \code{\$"} between each
element.

	``Here'' strings are very handy for multi-line strings, but
are somewhat error-prone. Remember that \code{<<}\textit{word} behaves
as if you had just inserted a string at that point on the line, even
though the body of the string hasn't started yet. In particular, don't
forget the semicolon at the end of the command!

	Wrong:
%
	\begin{quote}
	\begin{verbatim}
	print <<EOT
	This is a string.
	EOT;
	\end{verbatim}
	\end{quote}

	Right:
%
	\begin{quote}
	\begin{verbatim}
	print <<EOT, " and also ", <<EndOfSecondString;
	This is a string.
	EOT
	this is another.
	EndOfSecondString
	\end{verbatim}
	\end{quote}

	As this example illustrates, you can have multiple ``here''
documents on one line. They are read in the order in which they appear
on the line (for obvious reasons, I think).

	By default, \code{<<}\textit{word} behaves like a
double-quoted string. However, you can enclose \textit{word} in the
quotes of your choice (just the \textit{word} after the \code{<<}, not
the terminating one), and the string will behave as a string of that
type.

	The \code{q}* style of quoting allows you to use any character
you like: \code{q/abc/} is equivalent to \code{q\#abc\#}.
Alternatively, you can use one of the three symmetrical delimeters:
\code{q\{abc\}}, \code{q(abc)}, \code{q<abc>}. Pick whatever seems
most readable.

\subsection{Operators}

	Like any respectable programming language, \perl\ has a full
complement of operators. They are:

\begin{itemize}
\item[Math]
	\ \\
	\begin{itemize}
	\addtolength{\labelsep}{1em}%
	\item[\code{+}, \code{-}, \code{*}, \code{/}]
		The usual arithmetic operators.
	\item[\code{\%}]
		Modulus.
	\item[\code{**}]
		Exponentiation.
	\item[\code{\amp}, \code{|}, \code{\^}, \code{\tilde}]
		Bit-wise and, or and exclusive-or, and not.
	\item[\code{<<}, \code{>>}]
		Left and right bit-shift.
	\item[\code{+=}, \code{-=}, etc.]
		\textit{operator}-and-assign. Works just as in C, with
		all of the operators above.
	\item[\code{++}, \code{--}]
		\perl\ supports both pre- and post-increment and
		-decrement. They work the same way as in C, with the
		following flourish: if you have a variable whose value
		is a string of letters followed by a string of
		numbers, \textit{var}\code{++} will increment the
		value anyway: \code{e012} will become \code{e013},
		\code{aaa} will become \code{aab}, and so forth. This
		is useful for programs that need to generate
		identifiers automatically.

		Note that \code{--} is not magical.
	\item[\code{<}, \code{<=}, \code{==}, \code{>=}, \code{>}, \code{!=}]
		Numeric comparison. A string that isn't a proper
		number has a numeric value of 0.
	\item[\code{<=>}]
		A generalized comparison operator: \code{\$a <=> \$b}
		returns -1 if \code{\$a} is less than \code{\$b}, 1 if
		\code{\$a} is greater than \code{\$b}, or 0 if they
		are equal. This is typically used in sorting
		functions.
	\end{itemize}
\item[Strings]
	\ \\
	\begin{itemize}
	\addtolength{\labelsep}{1em}%
	\item[\code{.} (dot)]
		Concatenates two strings.
	\item[\code{.=}]
		Concatenate and assign.
	\item[\code{x}]
		String multiplier:
		\textit{string}~\code{x}~\textit{number} yields
		\textit{number} copies of \textit{string},
		concatenated together. Thus, \code{"ab" x 3} yields
		\code{"ababab"}.
	\item[\code{x=}]
		Multiply and assign.
	\item[\code{lt}, \code{le}, \code{eq}, \code{ge}, \code{gt}, \code{ne}]
		String comparison operators, similar to the ones in
		the test(1) utility. They compare strings according to
		(case-sensitive) dictionary order. Thus,
		\code{"bar"~le~"baz"}. If a string happens to be a
		number, it will be treated as a string:
		\code{"100"~lt~"2"}.

		(NB: the ``not equal'' operator is \code{ne}, not
		\code{neq}.)
	\item[\code{cmp}]
		The generalized string comparison
		operator. \code{\$a~cmp~\$b} returns -1 if \code{\$a}
		is less than \code{\$b}, 1 if \code{\$a} is greater
		than \code{\$b}, and 0 if they are equal.
	\end{itemize}
\item[Files]
	\perl\ has an inordinate number of file-testing operators,
	written as \code{-}\textit{X}~\textit{expr}, where
	\textit{expr} is either a filehandle, or an expression giving
	the name of a file.

	These operators may appear strange at first, but they harken
	back to the test(1) utility that the Bourne shell uses.

	\begin{itemize}
	\addtolength{\labelsep}{1em}%
	\item[\code{-r}, \code{-w}, \code{-x}, \code{-o}]
		File is readable, writable, executable or owned by the
		effective uid.
	\item[\code{-R}, \code{-W}, \code{-X}, \code{-O}]
		File is readable, writable, executable or owned by the
		real uid.
	\item[\code{-e}]
		File exists.
	\item[\code{-z}]
		File has zero size.
	\item[\code{-s}]
		Size of the file (so this functions as a ``file has
		non-zero size'' test).
	\item[\code{-M}, \code{-A}, \code{-C}]
		Returns the time, in days, since the file's last
		modification time (mtime), access time (atime) or
		inode modification time (ctime).
	\item[\code{-f}]
		File is a plain file.
	\item[\code{-d}]
		File is a directory.
	\item[\code{-l}]
		File is a symbolic link.
	\item[\code{-p}]
		File is a named pipe.
	\item[\code{-S}]
		File is a socket.
	\item[\code{-b}]
		File is a block special file.
	\item[\code{-c}]
		File is a character special file.
	\item[\code{-t}]
		File is a filehandle opened to a terminal (this is
		\perl's version of \code{isatty()}).
	\item[\code{-u}]
		File has the setuid bit set.
	\item[\code{-g}]
		File has the setgid bit set.
	\item[\code{-k}]
		File has the sticky bit set.
	\item[\code{-T}]
		File is a text file.
	\item[\code{-B}]
		File is a binary file.
	\end{itemize}

	You can also pass any of these operators a special filehandle,
	called \code{\_} (underscore). This causes the operator to
	reuse the results from the last \code{stat()} call:
%
	\begin{quote}
	\begin{verbatim}
	if (-u $filename || -g $filename)
	\end{verbatim}
	\end{quote}
%
	calls \code{stat()} twice, whereas
%
	\begin{quote}
	\begin{verbatim}
	if (-u $filename || -g _)
	\end{verbatim}%$
	\end{quote}
%
	only calls it once.

\item[Others]
	\ \\
	\begin{itemize}
	\addtolength{\labelsep}{1em}%
	\item[\code{=\tilde}, \code{!\tilde}]
		Certain functions, notably the string-match and
		string-replace functions, need a string as their
		argument. By default, they use \code{\$\_}. The
		\code{=\tilde} makes them work on some other string,
		e.g., \code{\$foo~=\tilde~m/abc/}.

		\code{!\tilde} works the same way as \code{=\tilde},
		but negates the result of the operation.
	\item[\code{\amp\amp}, \code{||}]
		Logical and, or. These work like C's \code{\amp\amp}
		and \code{||}, and perform short-circuit evaluation.

		One difference, though: in \perl, these operators
		return not 0 or 1, as in C, but rather the last value
		seen. Thus, if your program needs to run the user's
		favorite editor, a good way to do that is to use
		\begin{verbatim}
		$EDITOR = $ENV{VISUAL} ||
		          $ENV{EDITOR} ||
		          "/usr/ucb/vi";
		\end{verbatim}%$
	\item[\code{\amp}, \code{|}, \code{\^}, \code{\tilde}]
		Bitwise and, or, xor; one's complement.
	\item[\code{!}]
		Not.
	\item[\code{?:}]
		An in-line \code{if}
		statement.\\
		\textit{expression}~\code{?}~\textit{if-expr}~\code{:}~\textit{else-expr}\\
		works the same way as in C.
	\item[\code{..} (dot dot)]
		The range operator is rather magical. It's one of my
		favorites.

		In a list context, \textit{num1}\code{..}\textit{num2}
		returns the list of numbers from \textit{num1} to
		\textit{num2}. This is handy for taking array slices
		(\code{@array[3..10]}) or for repeating a loop a fixed
		number of times (\code{for (1..50)}\ldots). Be aware,
		though, that this does generate a temporary array, so
		you can waste a lot of memory by using this with a
		large range.

		In a scalar context,
		\textit{expr1}~\code{..}~\textit{expr2} acts as a
		``flip-flop.'' It starts out false. Then, once the
		left-hand expression becomes true, \code{..} returns
		true and starts evaluating its right-hand expression,
		until that becomes true. \textit{After} that,
		\code{..} flip-flops back to being false.

		This is useful for things like finding text between
		delimiters in a file:
%
		\begin{verbatim}
		perl5 -ne 'print if /BEGIN/../END/'
		\end{verbatim}
%
		will read standard input and print all lines between
		lines delimited by \code{BEGIN} and \code{END}.

		There is also a \code{...} (dot dot dot) operator,
		related to \code{..}, but I won't cover it here.
	\item[\code{and}, \code{or}, \code{xor}, \code{not}]

		These work just like \code{\amp\amp}, \code{||},
		\code{\^} and \code{!}, but have a much lower
		precedence.

		The motivation for this is that, since most functions
		return some value that tells whether it succeeded, it
		is rather common to write
		\textit{expr1}~\code{\amp\amp}~\textit{expr2} as a way
		of saying, ``do \textit{expr1}, and if that succeeds,
		do \textit{expr2}.''

		Unfortunately, depending on what operators
		\textit{expr1} and \textit{expr2} might contain,
		\perl\ might not parse things the way you want it
		to. \code{and}, \code{or} and \code{not} have the
		lowest precedence, so your code will always be parsed
		as
		\code{(}\textit{expr1}\code{)~and~(}\textit{expr2}\code{)}.
	\end{itemize}
\end{itemize}

	There is a hierarchy of precedence to these operators, but
don't memorize it (aside from the rule about \code{and}, \code{or},
\code{xor} and \code{not}). Use parentheses to make explicit how you
want expressions to be evaluated, and your code will be more readable
for it.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Flow Control}

	\perl\ has mostly the same flow-control operators as C does,
with just a few flourishes of its own.

\subsection{\code{if} and \code{unless}}

\begin{quote}
\begin{tabbing}
\code{if} \= \code{(}\textit{condition}\code{) \{}\\
	\> \textit{commands}\\
\code{\}}\\
$[$\code{elsif (}\textit{condition}\code{) \{}\\
	\> \textit{commands}\\
\code{\}}$\ldots]$\\
$[$\code{else \{}\\
	\> \textit{commands}\\
\code{\}} $]$
\end{tabbing}
\end{quote}

	\perl's \code{if} statement is reminiscent of both C and the
Bourne shell, except that \textsl{sh}'s \code{elif} has mutated into
\code{elsif}.

	One potential pitfall for C programmers is the fact that in
\perl, braces are mandatory, even if you only have one statement. This
avoids ambiguity caused by nested \code{if} statements.

	The \code{unless} construct is similar to \code{if}.
%
\begin{quote}
\code{unless (\$i < 100)}\ldots
\end{quote}
%
is equivalent to
%
\begin{quote}
\code{if (not \$i < 100)}\ldots
\end{quote}

	I won't tell you how \code{unless} affects \code{elif} and
\code{else} blocks, because such constructs are confusing and should
be avoided. \code{unless} is best suited for one-line postfix
conditions.

\subsection{Truth And Other Booleans}

	At this point, it might be useful to digress for a moment to
expore the nature of truth. Actually, it's easier to define what is
false: empty arrays and hashes, and nonexistent variables, are
false. That's the easy bit.

	For scalars, 0 is false, as is the empty string. Actually,
there are two varieties of empty string: the first is \code{""}, the
second is the undefined value.

	The undefined value is what you get if, say, you try to get a
hash element that doesn't exist. It is similar to the \code{NULL}
pointer in C. You can find out whether a variable has the undefined
value by calling \code{defined(}\textit{variable}\code{)} (or
\code{!defined(}\textit{variable}\code{)}, as the case may be).

	By the way, you can explicitly set a variable to the undefined
value by using \code{undef}:
%
\begin{quote}
\begin{verbatim}
$var = undef;
\end{verbatim}%$
\end{quote}

	That's it for falsehood. Anything that isn't false is true.

\subsection{\code{while} and \code{until}}

\begin{quote}
\begin{tabbing}
\code{while} \= \code{(}\textit{condition}\code{) \{}\\
	\> \textit{commands}\\
\code{\}}\\
$[$\code{continue \{}\\
	\> \textit{commands}\\
\code{\}} $]$
\end{tabbing}
\end{quote}

	The \code{while} loop should also look fairly familiar to C
programmers. Again, as with \code{if}, the braces are mandatory.

	Note, however, the optional \code{continue} block. The
statements in the \code{continue} block will be executed every time
the loop repeats, whether by falling off the end of the \code{while}
block, or because of an explicit loop-control construct (which we'll
see in a bit). It allows you to make sure that a particular piece of
code gets executed every time you iterate through a loop. It's not
used often in practice, but it's there if you need it.

	\code{until} works just like \code{while}, except that the
test is negated.

\subsection{Postfix Conditionals}

	\code{if}, \code{unless}, \code{while} and \code{until} also
come in postfix versions. That is,
%
\begin{quote}
\begin{verbatim}
if ($foo eq "abc")
{
        print "Foo is abc\n";
}
\end{verbatim}%$
\end{quote}
%
is equivalent to
%
\begin{quote}
\begin{verbatim}
print "Foo is abc\n" if $foo eq "abc";
\end{verbatim}%$
\end{quote}
%
Likewise for the other postfix conditionals.

	Note that in the postfix version, parentheses are not required
around the condition, since there is no ambiguity as to where the
condition begins and ends.

	One caveat: if you have a \code{do} block followed by a
postfix \code{while} or \code{until}, the \code{do} block will execute
at least once. This is so that
%
\begin{quote}
\begin{verbatim}
open FILE, "$filename";
do {
        $line = <FILE>;
        print $line;
} until $line =~ /END/;
close FILE;
\end{verbatim}
\end{quote}
%
will work as expected.

\subsection{\code{for}}

\begin{quote}
\begin{tabbing}
\code{for} \= \code{(}\textit{init}\code{;}
		\textit{condition}\code{;}
		\textit{continue}\code{) \{}\\
	\> \textit{commands}\\
\code{\}}
\end{tabbing}
\end{quote}

	This looks a lot like C's \code{for} statement, doesn't it? No
surprises here. This is equivalent%
\footnote{In fact, Larry added the \code{continue} block so that
\code{for} could be defined precisely this way.}
to
%
\begin{quote}
\begin{tabbing}
\textit{init}\code{;}\\
\code{while} \= \code{(}\textit{condition}\code{) \{}\\
	\> \textit{commands}\\
\code{\} continue \{}\\
	\> \textit{continue}\\
\code{\}}
\end{tabbing}
\end{quote}

\subsection{The other \code{for}, and \code{foreach}}

\begin{quote}
\begin{tabbing}
\code{for} \= $[$\textit{var}$]$ \code{(}\textit{list}\code{) \{}\\
	\> \textit{commands}\\
\code{\}}
\end{tabbing}
\end{quote}

	\perl's \code{for} and \code{foreach} loops (the two are
synonymous) behave much like \textsl{sh}'s \code{for} and
\textsl{csh}'s \code{foreach} loops. They iterate over \textit{list},
setting \textit{var} to each element in turn. If \textit{var} is
omitted, \code{\$\_} is used.

	Again, the parens and curly braces are mandatory.

\subsection{Bare blocks}

	Technically, a bare block---that is, a pair of curly braces
with statements between them---is a loop that executes exactly once. I
bring this up because you do a few interesting things by treating a
block as a loop, as we'll see later on.

	Oh, and by the way: you can omit the semicolon after the last
statement in a block.

\subsection{do}

	\code{do \{}~\textit{BLOCK}~\code{\}} executes the commands in
\textit{BLOCK} and returns the value returned by the last
statement. Useful when you can't use a bare block, or when you'd like
to write something as one statement, but goshdarnit, you need two or
more:
%
\begin{quote}
\begin{verbatim}
open INFILE, "/my/file" or do {
                print STDERR "I don't know what to do.\n"
                exit 1;
        };
\end{verbatim}
\end{quote}

\subsection{Loop-control commands}

	There are times when you don't want to finish executing the
body of the loop you're in. For these cases, \perl\ provides not one,
not two, but three special commands:

	\code{last}: this is similar to C's \code{break} statement: it
exits the loop immediately (without passing through the
\code{continue} block, if any), causing control to continue with the
next statement after the closing brace.

	\code{next}: this is similar to C's \code{continue} statement:
control passes through the \code{continue} block, if any, then back up
to the condition at the top of the loop.

	\code{redo} has no equivalent in C. It causes control to go
back to the top of the \code{while} block, to the first statement
after the opening brace. The \code{continue} block is not executed,
and the condition is not evaluated.

\subsection{Block labels}

	But wait, there's more! You can also put labels in front of
blocks and loop commands:

\begin{quote}
\begin{verbatim}
OUTER: for ($i = 0; $i < 100; $i++)
{
    INNER: for ($j = 0; $j < 10; $j++)
    {
        if (&an_error_has_occurred)
        {
            last OUTER;
        }
    }
}
\end{verbatim}
\end{quote}

	By default, \code{last}, \code{next} and \code{redo} act on
the innermost loop that they're in. If you give them a label, they
apply to the innermost enclosing block that has that label.

\subsection{Gotos}

	\perl\ has three \code{goto} invocations, two of which are
highly magical, but you'll have to look them up yourselves, because
they're evil and, with all the loop-control stuff we've just seen,
unnecessary.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{I/O}

	Since \perl\ was originally written as a report generator,
it's not surprising that it can perform various I/O operations. Since
I/O is such an important function of \perl's, it has more magic than
most other parts.

\subsection{Files}

	Most of the time, when you need to read a file, you'll do the
following:
%
\begin{quote}
\begin{verbatim}
open INFILE, "/my/file";
while ($line = <INFILE>)
{
        # Do something
}
close INFILE;
\end{verbatim}%$
\end{quote}

	The \code{open} function opens a file, naturally enough. The
first word, \code{INFILE} is the \emph{filehandle} (by convention,
filehandles are in all-caps, to set them apart). The second argument
specifies the filename. By default, the file is opened for reading.

	The special filehandles \code{STDIN}, \code{STDOUT} and
\code{STDERR} come pre-opened, so you don't even need to open them to
use them.

	Above, we used \code{open INFILE, "/my/file"}. We could also
have said \code{open INFILE, "</my/file"}, to explicitly say that it's
being opened for reading. Similarly, \code{open OUTFILE, ">/my/file"}
opens /my/file for writing, and \code{open OUTFILE, ">>/my/file"} for
appending.%
\footnote{If you're dealing with user-supplied filenames, don't use
\code{open FILE, "\$filename"} since if \code{\$filename} contains
\code{<} or \code{>}, it will be interpreted as part of the open mode
specification. Similarly, don't use \code{open FILE, ">\$filename"},
since if \code{\$filename} begins with \code{>}, you will append to
the file instead of zeroing it first. Use \code{open FILE,
">~\$filename"} instead.\footnotemark}%
\footnotetext{You can't use \code{open} to open a file whose name
begins with a space. Use \code{sysopen} instead.}%
. If you put a \code{+} in front of the \code{<} or \code{>}, you'll
get both read and write access to the file.%
\footnote{\code{+>} zeroes the file first; \code{+<} doesn't.}

	\code{open~CMD,~"|}\textit{command}\code{"} will invoke the
shell and run \textit{command}; if you write to the \code{CMD}
filehandle, your text will be fed to the standard input of
\textit{command}. Likewise,
\code{open~CMD,~"}\textit{command}\code{|"} will run
\textit{command}, and reading from \code{CMD} will read from its
standard output. You can't put a pipe at both ends of a command this
way. You have to jump through a few hoops to do that.

	The \code{close} statement closes the file, naturally enough.

	The expression \code{<}\textit{FILEHANDLE}\code{>} reads the
next line from \textit{FILEHANDLE} and returns it. When it reaches the
end of the file, \code{<}\textit{FILEHANDLE}\code{>} will return the
undefined value, which is false, as we've seen above, and the
\code{while} loop will terminate.

	In an array context, however, the angle operator will read
every line in \textit{FILEHANDLE}, put them into an array, and return
it. This is a quick and easy way to read in an entire file (and
potentially waste tons of memory).

	The angle operator has a bit of magic built in: if it's the
only thing in a \code{while} loop condition, it'll read the next line
and assign it to the \code{\$\_} variable. The following:
%
\begin{quote}
\begin{verbatim}
while ($_ = <FILE>)
{
        print $_;
}
\end{verbatim}
\end{quote}
%
is equivalent to
%
\begin{quote}
\begin{verbatim}
while (<FILE>)
{
        print $_;
}
\end{verbatim}%$
\end{quote}

	And as this example shows, you write using the \code{print}
function. The syntax for \code{print} is
%
\begin{quote}
\code{print} \textit{[FILEHANDLE]} \textit{[expression]}
\end{quote}
%
(Note that there is no comma between \textit{FILEHANDLE} and
\textit{expression})

	If you omit \textit{FILEHANDLE}, it defaults to
\code{STDOUT}. If you omit \textit{expression}, it defaults to
\code{\$\_}.

	Unlike some other languages, \perl\ does not take care of the
ends of lines for you: when you \code{print} a line, you need to
include the \code{\bs n} at the end. Similarly, when you read a line
with \code{<}\textit{FILEHANDLE}\code{>}, it still has the newline at
the end.

	Since trailing newlines usually get in the way of what you
want to do, \perl\ provides the \code{chop} and \code{chomp}
functions. \code{chop \$}\textit{var} chops off the last character of
\code{\$}\textit{var} (and returns it). \code{chomp \$}\textit{var}
looks for a newline (or whatever the record separator is set to) at
the end of \code{\$}\textit{var}, and removes it if it is there (and
returns the number of characters it removed).

\subsection{\code{<>}}

	You might think that since
%
\begin{quote}
\begin{verbatim}
print "Hello, world!\n";
\end{verbatim}
\end{quote}
%
is equivalent to
%
\begin{quote}
\begin{verbatim}
print STDOUT "Hello, world!\n";
\end{verbatim}
\end{quote}
%
that \code{<>} would be equivalent to \code{<STDIN>}. Well, not
quite. \code{<>}, sometimes called the diamond operator, has magic of
its own.

	Most Unix filters, like \textsl{grep}, \textsl{sed},
\textsl{awk} etc., will read the files named on the command line, or
standard input if there aren't any, or if you specify \code{-} (dash)
as a filename. \code{<>} does all this for you.

	When you use \code{<>}, it looks at \code{@ARGV}, the array of
command-line options. It will remove the first argument from
\code{@ARGV}, and open the file that it names. Once it has finished
reading that file, it will close it, grab the next filename from
\code{@ARGV}, and so forth.

	If there weren't any filenames in \code{@ARGV} to begin with,
\code{<>} will first set \code{@ARGV} to \code{("-")}, then proceed as
above. That way, it will open the filename \code{-} (dash), which is
special, and gives you \code{STDIN} when it's opened for reading, and
\code{STDOUT} when it's opened for writing.

	So to answer the question posed at the beginning of this
section, if you want to be sure of reading from \code{STDIN}, you need
to explicitly say \code{<STDIN>}.

	In fact, \code{while~(<>)} loops are so common that \perl\
provides not one, but two command-line options that provide one
automatically. They are primarily intended to be used with the
\code{-e} option, which says that the next command-line argment is the
script.

	\code{perl5~-ne}~\textit{script} is equivalent to
%
\begin{quote}
\code{while (<>)}\\
\code{\{}\\
\hbox to4em{}\textit{script}\\
\code{\}}
\end{quote}
%
and \code{perl5~-pe}~\textit{script} is (almost) equivalent to
%
\begin{quote}
\code{while (<>)}\\
\code{\{}\\
\hbox to4em{}\textit{script}\\
\code{\} continue \{}\\
\hbox to4em{}\code{print}\\
\code{\}}
\end{quote}

	You may think that \perl\ I/O is needlessly burdened with
special cases and exceptions, but it does simplify many short
scripts. The simplest way to write \textsl{cat} in \perl\ is
%
\begin{quote}
\code{perl5 -pe ''} \textit{filename\ldots}
\end{quote}
%
and \textsl{grep} becomes
%
\begin{quote}
\code{perl5 -ne 'print if /}\textit{pattern}\code{/'} \textit{filename\ldots}
\end{quote}

	If you were to spell it out, without any magic,%
\footnote{Well, hardly any.}
this last example would become
%
\begin{quote}
\begin{verbatim}
@ARGV = ('-') unless @ARGV;
while ($ARGV = shift @ARGV) {
        open FILE, $ARGV or die("Can't open $ARGV: $!\n");
        while ($_ = <FILE>)
        {
                print STDOUT, $_ if /pattern/;
        }
        close FILE;
}
\end{verbatim}%$
\end{quote}

	The \code{-i}\textit{extension} command-line option turns on
in-place editing for \code{<>}. That is, if you specify \code{-i.bak},
then when \code{<>} opens a file \code{foo}, it will rename it as
\code{foo.bak}, and also open \code{foo} for writing and make this the
default filehandle for \code{print} statements. Thus,
%
\begin{quote}
\begin{verbatim}
perl5 -i.bak -pe 'tr/a-z/A-Z/' foo bar baz
\end{verbatim}
\end{quote}
%
will translate the contents of files \code{foo}, \code{bar} and
\code{baz} to upper case, and leave backup copies in \code{foo.bak},
\code{bar.bak} and \code{baz.bak}.

	Note that \code{<>} really does use \code{@ARGV}, so it's
perfectly legal to say
%
\begin{quote}
\begin{verbatim}
@ARGV = qw( foo bar baz );
while (<>)
{
        # Do something
}
\end{verbatim}
\end{quote}

\subsection{\code{<}\ldots\code{>} the Globber}

	One last thing about \code{<}\ldots\code{>}: if you put
something other than a filehandle between the angle brackets, they
become something else: the filename globbing operator. The expression
between brackets will be interpreted as a pattern. The globbing
operator will return every filename that matches the pattern. In a
list context, it'll return them all at once; in a scalar context,
it'll return one at a time.
%
\begin{quote}
\begin{verbatim}
@etcfiles = </etc/*>;

while (</tmp/*>)
{
        print "$_\n"
}
\end{verbatim}%$
\end{quote}

\subsection{End Of File}

	The \code{eof} function tests end-of-file status. Normally, it
is invoked as \code{eof}~\textit{FILEHANDLE}, which returns true if
\textit{FILEHANDLE} is currently at the end of file (\textit{i.e.}, if
the next read would return the undefined value).

	If you omit the \textit{FILEHANDLE} argument, \code{eof} tests
the last filehandle that was read from.

	You can also say \code{eof()}, which tests the pseudo-file
that \code{<>} uses, that composed of all of the files listed on the
command line. In other words, \code{eof()} will tell you whether
you've reached the end of the last file listed on the command line.

	If you're using \code{<>} and want to detect the end of each
file, you can either use \code{eof} without any arguments (assuming
you haven't read from any other files), or use the special filehandle
\code{ARGV}:%
\footnote{Yes, \code{ARGV} is magical. Are you surprised?}
\code{eof(ARGV)}.

\subsection{Formats}

	Formats are \perl's way of letting you print pretty formatted
reports. The way it does this is rather nice: you just draw a picture
of how you want the data to come out, fill in the blanks, and
\code{write}.

	That's \code{write}, not \code{print}. \code{print} just does
plain, ordinary printing. \code{write} outputs the next record for the
format you're using.

	Perhaps the easiest way to show what formats are all about is
with an example:
%
\begin{verbatim}
format NEWHOST =
Name:   @>>>>>>>>>  IP: @<<<<<<<<<<<<<< Ether: @|||||||||||||||||
        $hostname,      $ip,            $ether
Domain:    @>>>>>>          CPU Tag: @<<<<<<<<<<<<<<<<<<<<
        $domain,        $cpu_tag,
                        Monitor Tag: @<<<<<<<<<<<<<<<<<<<<
                        $mon_tag
Problems with the installation:
    ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        $problems
~   ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        $problems
~   ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        $problems
~   ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<...
        $problems
# Should there be more than four lines for problems?

Comments:
@*
$comments
.
\end{verbatim}%$

	As you see, a format declaration begins with
\code{format}~\textit{formatname}~\code{=}, and continues up to a dot
on a line by itself.

	The text just prints the way it's laid out. The things that
look like \code{@<<<<<} and such are \emph{picture fields}. They
specify where the data should go on the line. \code{@>>>>} means that
the data should be flushed right, \code{@<<<<<} means it should be
flushed left, and \code{@|||||} means it should be centered within the
field.%
\footnote{The \code{@} is part of the picture field, and does count
toward its width. One consequence of this is that if you have a
one-character field, you cannot specify whether it is to be flushed
left or right, or centered. This is not considered a problem.}

	You can also have fields of the form \code{@\#\#\#\#\#} or
\code{@\#\#\#.\#\#}, for specifying numeric values. If the field includes a
dot, the decimal will line up with it.

	Underneath each picture field is a variable name. This is,
intuitively enough, the variable whose value will be plugged into the
field. The variables are separated by commas. And they don't have to
line up with the fields, they just have to be in the same
order. Lining them up just makes it clearer what belongs with what.

	A \code{\#} at the beginning of a line marks that line as
being a comment.

	You'll note that some of the fields begin with \code{\^{}},
and some lines have a \code{\tilde}. Normally, if a value is too wide
to fit in a field, the end is chopped off, and you never see it. If,
however, the field begins with \code{\^{}}, \perl\ will fit as much of
the value as it can into the field, chop that off,%
\footnote{This \emph{does} change your variable, so make sure it's not
something you're going to need later.}
%
and save the rest until the next time that variable is used.

	If all of the picture fields on a line are blank, and there's
a \code{\tilde} anywhere on the line, that line will not be printed
(if it is printed, though, the \code{\tilde} will be turned into a
blank).

	Thus, in the above example, the variable \code{\$problems} can
take up to four lines of text. The dots at the end of the fourth line
are just there in case that's not enough, to let the reader know that
there was more text.

	\perl\ tries to be reasonably smart about splitting lines this
way: by default, it'll break only on whitespace or a dash, though you
can change this by setting the \code{\$:} variable.

	Finally, we get to the special field \code{@*}. This just
inserts the value of its variable as-is, without any splitting, just
the way it appears, for as long as it takes.

	To print using this format, use:
%
\begin{quote}
\begin{verbatim}
$~ = NEWHOST;

$hostname = "glitnir";
$domain   = "CfAR";
$ip       = "128.8.132.40";
$ether    = "00:40:05:4a:c0:0a";
$cpu_tag  = "12345";
$mon_tag  = "67890";
$problems = "None";
$comments = `cat /tmp/long.rant`;

write;
\end{verbatim}%$
\end{quote}

	Each filehandle has a default format that has the same name as
the filehandle (note, however, that the two are not otherwise related;
one is a format, the other is a filehandle). In this case, however,
we're using a format called \code{NEWHOST}. Rather than open a new
file called \code{NEWHOST}, let's simply set \code{\$\tilde} to
associate the format \code{NEWHOST} with the default filehandle, which
is currently \code{STDOUT}.%
\footnote{You can specify which filehandle is the default for
\code{print} and \code{write} with
\code{select}~\textit{filehandle}.\footnotemark}%
\footnotetext{Note that this is different from the \code{select()}
system call, which is also available in \perl, and is also called
\code{select}.}%

	We then assign values to each of the variables that appear in
the format, and call \code{write}. \code{write} also takes an optional
filehandle as an argument, in case you don't want to \code{write} to
the current default filehandle.

	Each format also has a top-of-form format that gets printed
every time \code{write} begins a new page. By default, the top-of-form
format has the same name as the default format for that filehandle,
but with \code{\_TOP} at the end. You can use this to print page
numbers (available in \code{\$\%}), or to print column headings,
\textit{e.g.}:
%
\begin{verbatim}
format HOSTINFO_TOP =
Host information                                       Page @<<
                                                            $%

Keep this info up to date, or feel the wrath of Theresa!

Name            Room    Type    Model   Comment
.

@<<<<<<<<<<<<<  @<<<<   @<<<<<  @<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<
$hostname,      $room,  $type   $model  $comments
~                                       ^<<<<<<<<<<<<<<<<<<<<<<<
                                        $comments
.
\end{verbatim}%$

	You can change the top-of-form format by setting the
\code{\$\^{}} variable.

\subsection{I/O Special Variables}

	As you might expect, there are a number of special variables
associated with I/O. They are:
% They are listed below. The single-character names
%are the ``traditional'' ones; you can use the long, readable names if
%you put \code{use English;} at the top of your program.

\begingroup
% Add to the left margin, because we've got some pretty long items here.
\addtolength{\leftmargini}{2em}%
\begin{itemize}
\addtolength{\labelsep}{1em}%
\item[\code{\$\_}]
	\code{while (<}\textit{FILEHANDLE}\code{>)} reads the next
	line into \code{\$\_} by default.
\item[\code{\$ARGV}]
	The name of the current file when reading with \code{<>}.
\altthree{\code{\$INPUT\_RECORD\_SEPARATOR}}
	 {\code{\$RS}}
	 {\code{\$/}}
	The input record separator. This is a newline (\code{"\bs n"})
	by default.

	Note that this is magical: if you set it to the empty string
	(\code{""}), it will behave as if you had set it to two
	newlines (\code{"\bs n\bs n"}), with this exception: two or
	more blank lines in a row will be compressed into one blank
	line. This makes it easy to read files one paragraph at a
	time.
\alttwo	 {\code{\$OUTPUT\_AUTOFLUSH}}
	 {\code{\$|}}
	If set to a nonzero value, forces a flush every time you write
	to the currently-selected filehandle.
\altthree{\code{\$OUTPUT\_FIELD\_SEPARATOR}}
	 {\code{\$OFS}}
	 {\code{\$,}}
	Output field separator. When you \code{print} several items,
	separated by commas, \perl\ inserts the value of \code{\$,}
	between each item.
\alttwo	 {\code{\$LIST\_SEPARATOR}}
	 {\code{\$"}}
	Like \code{\$,}, but applies to arrays interpolated into a
	double-quoted string.
\altthree{\code{\$INPUT\_LINE\_NUMBER}}
	 {\code{\$NR}}
	 {\code{\$.}}
	The current input line number for the last filehandle that was
	read from.
\alttwo	 {\code{\$FORMAT\_LINES\_PER\_PAGE}}
	 {\code{\$=}}
	The number of lines per page on the currently-selected output
	channel.
\alttwo	 {\code{\$FORMAT\_LINES\_LEFT}}
	 {\code{\$-}}
	The number of lines left on the current output page.
\alttwo	 {\code{\$FORMAT\_PAGE\_NUMBER}}
	 {\code{\$\%}}
	The current page number of the currently-selected output
	channel.
\alttwo	 {\code{\$FORMAT\_NAME}}
	 {\code{\$\tilde}}
	The name of the current format for the currently-selected
	output channel.
\alttwo	 {\code{\$FORMAT\_TOP\_NAME}}
	 {\code{\$\^}}
	The name of the current top-of-page format for the
	currently-selected output channel.
\alttwo	 {\code{\$FORMAT\_LINE\_BREAK\_CHARACTERS}}
	 {\code{\$:}}
	A string containing the characters after which it is okay to
	break a long line in a format, and start filling in
	continuation (\code{\^}) fields. This is \code{"\bs n-"} by
	default.
\alttwo	 {\code{\$FORMAT\_FORMFEED}}
	 {\code{\$\^{}L}}
	The string that formats should output to produce a form
	feed. This is \code{"\bs f"} by default.
\alttwo	 {\code{\$ACCUMULATOR}}
	 {\code{\$\^{}A}}
	The current value of the \code{write} accumulator for format
	lines. See perlform(1) and perlfunc(1) for details.
\alttwo	 {\code{\$INPLACE\_EDIT}}
	 {\code{\$\^{}I}}
	The current value of the inplace-edit extension. If \perl\ is
	running with the \code{-i} command-line option, but no backup
	extension specified, \code{\$\^{}I} will be the empty
	string. If the \code{-i} option was not specified,
	\code{\$\^{}I} has the undefined value.
\end{itemize}
\endgroup

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Regular Expressions}

	Regular expressions, often abbreviated ``regexps'', ``REs'',
or simply ``patterns'' are a major part of \perl, simply because
\perl\ is so good at them.

	There's a mathematical definition of ``regular expression''
that you may have run into if you've taken compiler design. However,
\perl\ has added so much on top of that that it's nearly useless, so
we'll ignore it. Suffice it to say that a regular expression is a
pattern of characters. For instance, ``abc'' is a regular expression,
and
%
\begin{quote}
\begin{verbatim}
m/abc/
\end{verbatim}
\end{quote}
%
will return a true value if the current line contains an `a' followed
by a `b' followed by a `c'.

	Any ordinary character is a regular expression that matches
itself. Several regular expressions in a row mean that the string must
match the first one, then immediately the second one, then the next,
and so forth.

	There are also several special characters that have special
meanings within regexps:
%
\begin{itemize}
\addtolength{\labelsep}{1em}%
\item[\code{\^{}}]
	Matches the beginning of a line: \code{foobar} matches
	\code{m/bar/}, but not \code{m/\^{}bar/}.
\item[\code{\$}]
	Matches the end of a line: \code{foobar} matches \code{m/foo/},
	but not \code{m/foo\$/}.
\end{itemize}
%
Using \code{\^{}} or \code{\$} is known as \emph{anchoring} a pattern:
you're saying that one end or the other of the pattern (or both) has
to be at the beginning or end of the string.

	One thing to watch out for: \code{\$} normally precedes a
variable name, and variables \emph{are} interpolated into patterns. So
if you say
%
\begin{quote}
\begin{verbatim}
m/foo$bar/
\end{verbatim}%$
\end{quote}
%
This does not mean \code{foo}, followed by a newline, followed by
\code{bar}. \perl\ will first expand the variable \code{\$bar}, an
treat the whole thing as a pattern.

	Since the end of a line normally only occurs at the end of a
pattern, \perl\ is usually smart enough to figure out what you meant,
but it's still something to bear in mind. And if you \emph{did} mean
to say ``\code{foo}, followed by newline, followed by \code{bar},''
there are ways of doing that, which we'll cover in a bit.

\begin{itemize}
\addtolength{\labelsep}{1em}%
\item[\code{.} (dot)]
	Matches any character except newline.
\item[\textit{pat1}\code{|}\textit{pat2}]
	Matches either pattern \textit{pat1} or \textit{pat2}.
\item[\code{[}\textit{range}\code{]}]
	Matches any character in the range \textit{range}. For
	instance, \code{m/[abc]/} will match either \code{a},
	\code{b} or \code{c}. For longer ranges, you can use a dash:
	\code{m/[0-9a-f]/} will match any (lower case) hex digit. (If
	you want to include a dash in your range, make it the first
	character in the range: \code{m/[-a-z]/} will match any
	letter, or a dash.)

	You can also negate a range by putting \code{\^{}} at the
	beginning of the range: \code{m/[\^{}a-z]/} will match any
	character except a lower case letter.
\item[\code{(}\textit{pattern}\code{)}]
	Parentheses perform grouping. They also make individual parts
	of the matched string available through the
	\code{\$}\textit{digit} variables (see below).
\item[\code{\bs}]
	A backslash escapes the next character, so that it loses any
	special meaning that it might have. Thus, \code{\bs(} matches
	a literal open parenthesis.%
	\footnote{This is in contrast to some other regular expression
	implementations: in \textsl{sed}, for example, you have to use
	\code{\bs(}\ldots\code{\bs)} to create a backreference.}
\item[\textit{pattern}\code{*}]
	Matches zero or more occurrences of \textit{pattern}.
\item[\textit{pattern}\code{+}]
	Matches one or more occurrences of \textit{pattern}.
\item[\textit{pattern}\code{?}]
	Matches zero or one occurrences of \textit{pattern}.
	(\textit{i.e.}, \textit{pattern} is optional)
\item[\textit{pattern}\code{\{}\textit{n}\code{\}}]
	Matches exactly \textit{n} occurrences of \textit{pattern}.
\item[\textit{pattern}\code{\{}\textit{n}\code{,\}}]
	Matches at least \textit{n} occurrences of \textit{pattern}.
\item[\textit{pattern}\code{\{}\textit{n}\code{,}\textit{m}\code{\}}]
	Matches at least \textit{n}, but no more than \textit{m}
	occurrences of \textit{pattern}.
\end{itemize}

	Watch out when you use \textit{pattern}\code{*}: remember that
it does match zero occurrences of the pattern. Thus, if you want to
see if a string has more than one word in it, you might be tempted to
write
%
\begin{quote}
\begin{verbatim}
m/[a-z]\s*[a-z]/
\end{verbatim}
\end{quote}
%
However, the string \code{ab} would match, since it consists of a
letter, followed by zero spaces, followed by another letter. In this
case, you'd need to use \code{\bs s+}, to make sure that there was at
least one space between the two letters.

	By default, \perl's regular expressions are ``greedy,''
\textit{i.e.}, they try to match as much as possible:
%
\begin{quote}
\begin{verbatim}
"one two three" =~ /(.*)\s(.*)/;
\end{verbatim}
\end{quote}
%
will set \code{\$1} to \code{one two} and \code{\$2} to
\code{three}. If this is not what you want, you can append a \code{?}
to the standard numeric modifiers to change the greediness. Thus,
\textit{pattern}\code{*?} will match zero or more instances of
\textit{pattern} (but as few as possible), \textit{pattern}\code{+?}
will match one or more instances (but as few as possible), and
\textit{pattern}\code{??} will match zero or one instances (but
preferably zero). Thus,
%
\begin{quote}
\begin{verbatim}
"one two three" =~ /(.*?)\s(.*)/;
\end{verbatim}
\end{quote}
%
will set \code{\$1} to \code{one} and \code{\$2} to \code{two three}, and
%
\begin{quote}
\begin{verbatim}
"one two three" =~ /(.*?)\s(.*?)/;
\end{verbatim}
\end{quote}
%
will set \code{\$1} to \code{one} and \code{\$2} to the empty string.%
\footnote{Actually, if you think about it some more, you'd expect the
last example to set both \code{\$1} and \code{\$2} to the empty
string. For an explanation of why not, see the perlre(1) man page.}

	The following parenthesized expressions, of the form
\code{(?}\ldots\code{)} go a long way toward making \perl\ regular
expressions not merely powerful, but obscenely powerful:
%
\begin{itemize}
\addtolength{\labelsep}{1em}%
\item[\code{(?\#}\textit{text}\code{)}]
	A comment. \textit{text} is ignored.
\item[\code{(?:}\textit{pattern}\code{)}]
	Groups a regular expression like \code{( )}, but doesn't make
	a backreference (\textit{i.e.}, doesn't set \code{\$1}).
\item[\code{(?=}\textit{pattern}\code{)}]
	A zero-width, positive lookahead assertion. That is,
%
	\begin{quote}
	\begin{verbatim}
	/foo(?=bar)/
	\end{verbatim}
	\end{quote}
%
	matches \code{foo}, but only if it is followed by
	\code{bar}. However, \code{\$\&} will only contain \code{foo},
	not \code{bar}.

\item[\code{(?!}\textit{pattern}\code{)}]
	A zero-width, negative lookahead assertion.
%
	\begin{quote}
	\begin{verbatim}
	/foo(?!bar)/
	\end{verbatim}
	\end{quote}
%
	will match \code{foo}, unless it is followed by \code{bar}.

	Note that this can sometimes be nonintuitive. For instance,
	\code{"aaab"} matches the pattern \code{/a+(?!b)/}, since
	\code{"aa"} \emph{is} a string of \code{a}s that isn't
	followed by a \code{b}: it's followed by another \code{a}!
\end{itemize}

	There are certain ranges that occur over and over, so \perl\
has predefined shorthand for them:
%
\begin{itemize}
\addtolength{\labelsep}{1em}%
\item[\code{\bs w}]
	Matches any alphanumeric (``word'') character, or an
	underscore (\code{\_}).
\item[\code{\bs W}]
	Matches anything but a ``word'' character (\textit{i.e.},
	anything that \code{\bs w} doesn't match.
\item[\code{\bs s}]
	Matches a whitespace character.
\item[\code{\bs S}]
	Matches a non-whitespace character.
\item[\code{\bs d}]
	Matches a digit (0--9).
\item[\code{\bs D}]
	Matches a non-digit character.
\item[\code{\bs b}]
	Matches a word break. This doesn't match any actual
	characters. Rather, it matches the place between a word
	character (\code{\bs w}) and a non-word character (\code{\bs
	W}).
\item[\code{\bs B}]
	Matches a non-word break.
\item[\code{\bs t}]
	Matches a tab.
\item[\code{\bs n}]
	Matches a newline.
\item[\code{\bs r}]
	Matches a carriage return.
\item[\code{\bs f}]
	Matches a form-feed.
\item[\code{\bs a}]
	Matches a bell character.
\item[\code{\bs e}]
	Matches the escape character.
\item[\code{\bs}\textit{n}]
	Matches the \textit{n}th parenthesized expression (where
	\textit{n} is a digit) within a pattern. Thus,
	\code{"foo-foo"} will match the pattern \code{(.*)-\bs1}, but
	\code{"foo-bar"} won't.
\item[\code{\bs 0}\textit{nn}]
	Matches the character whose ASCII code, in octal, is
	\textit{nn}. Note: this is not the same as the same as the
	``match a previous substring'' escape; the leading \code{0}
	distinguishes the two cases.
\item[\code{\bs x}\textit{nn}]
	Matches the character whose ASCII code, in hex, is
	\textit{nn}.  Note: this, too, is not the same as the same as
	the ``match a previous substring'' escape; the leading
	\code{x} distinguishes the two cases.
\end{itemize}

	These next few escapes actually apply to strings in general,
but we might as well mention them here:

\begin{itemize}
\addtolength{\labelsep}{1em}%
\item[\code{\bs l}]
	Convert the next character to lower case.
\item[\code{\bs u}]
	Convert the next character to upper case.
\item[\code{\bs L}]
	Convert to lower case until \code{\bs E}.
\item[\code{\bs U}]
	Convert to upper case until \code{\bs E}.
\item[\code{\bs Q}]
	Quote regexp metacharacters until \code{\bs E}.
\end{itemize}

\subsection{Extended Regular Expressions}

	With all of this stuff going on, you may have gotten the
impression that \perl\ regular expressions are a write-only
language. Unfortunately, there's a lot of truth in this. If you have
an error in a particularly hairy regexp, it may be easier to just
rewrite it from scratch than to try to fix it.

	However, you can use the \code{/x} option to \code{m//} and
\code{s///} to enable extended regular expressions. In an extended
regular expression, all whitespace is ignored (unless it's escaped or
in a character range), so you can split it up into lines, and indent
it for legibility. Also, you can use \code{\#} to introduce comments.

	Let's look at a fairly hairy regular expression:
\begin{quote}
\begin{verbatim}
m/^                                 # Anchor beginning

 # Start with the day
 (mon|tue|wed|thu|fri|sat|sun)      # Day of the week
 \.?                                # Optional dot
 ,\s+                               # Comma, whitespace

 # Now try to match the date
 ((jan|mar|may|jul|aug|oct|dec)     # The 31-day months
        \s+                         # Whitespace
        (0?[1-9]        |           # 1-9 (01 also allowed)
         [12][0-9]      |           # 10-29
         3[01]                      # 30 and 31
        )
  | (apr|jun|sep|nov)               # The 30-day months
        \s+
        (0?[1-9]        |           # 1-9 (01 also allowed)
         [12][0-9]      |           # 10-29
         30                         # 30
        )
  | feb                             # February: we don't
                                    # allow leap years
        \s+
        (0?[1-9]        |           # 1-9 (01 also allowed)
         1[0-9]         |           # 10-19
         2[0-8]                     # 20-28
        )
 )

 # Finally, get the year
 (19)?\d{2}

$                                   # Anchor end
/xi
\end{verbatim}%$
\end{quote}

	This pattern matches a date of the form, \code{Mon., Jun 4,
1998}. The complexity comes from the fact that it only maches valid
dates (\textit{i.e.}, it doesn't match \code{Feb. 44}).

	Granted, this is still a mess, but it's better than
\begin{quote}
\begin{verbatim}
m/^(mon|tue|wed|thu|fri|sat|sun)\.?,\s+((jan|mar|may|jul|aug|
oct|dec)\s+(0?[1-9]|[12][0-9]|3[01])|(apr|jun|sep|nov)\s+(0?
[1-9]|[12][0-9]|30)|feb\s+(0?[1-9]|1[0-9]|2[0-8]))(19)?\d{2}
$/i
\end{verbatim}%$
\end{quote}

\subsection{Special Variables}

	\perl\ has a number of special variables associated with
regular expressions.
%
\begin{itemize}
\addtolength{\labelsep}{1em}%
\item[\code{\$\_}]
	The default variable to match.

	If you just say \code{m/abc/}, \perl\ will see if \code{\$\_}
	contains \code{abc}. If you want to see if some other string
	matches the pattern, you need to use \code{\$var =\~{} m/abc/}
	(actually, you don't need to use a variable. You can use any
	string).
\item[\code{\$}\textit{number}]
	Matches the \textit{number}th parenthesized expression.

	As I mentioned earlier, parentheses perform grouping in a
	regular expression. They also indicate to \perl\ that you're
	interested in that part of the string, so they make it
	available through the variables \code{\$1}, \code{\$2}, etc.

	The part of the string that matches the first set of
	parentheses will be placed in \code{\$1}, the part that
	matches the second set of parentheses will be put in
	\code{\$2}, and so forth. So if you have
%
	\begin{quote}
	\begin{verbatim}
	$_ = "name = arensb   uid = 2072";
	m/name = (\w+)\s+uid = (\d+)/;
	\end{verbatim}%$
	\end{quote}
%
	\code{\$1} will be set to \code{arensb} and \code{\$2} will be
	set to \code{2072}.

	Remember, parenthesized expressions can nest. To get the
	number of a parenthesized expression, just count the
	open-parens from the left:
%
	\begin{quote}
	\begin{verbatim}
	$_ = "user n arensb";
	m/user (n (\w+)|# (\d+))/;

	$_ = "user # 2072";
	m/user (n (\w+)|# (\d+))/;
	\end{verbatim}
	\end{quote}
%
	In the first case, \code{\$1} will be set to \code{n arensb},
	\code{\$2} will be set to \code{arensb}, and \code{\$3} will
	have the undefined value.

	In the second case, \code{\$1} will be set to \code{\# 2072},
	\code{\$2} will have the undefined value, and \code{\$3} will be set
	to \code{2072}.

\alttwo {\code{\$MATCH}}
	{\code{\$\&}}
	Gets set to the text that matched the pattern.
\alttwo	{\code{\$PREMATCH}}
	{\code{\$`}}
	Gets set to the text preceding the text that matched the last
	pattern.
\alttwo	{\code{\$POSTMATCH}}
	{\code{\$'}}
	Gets set to the text following the text that matched the last
	pattern.
\alttwo	{\code{\$LAST\_PAREN\_MATCH}}
	{\code{\$+}}
	Gets set to the text that matched the last parenthesized
	expression that matched something. For instance:
%
	\begin{quote}
	\begin{verbatim}
	m/Username: (\w+)|UID: (\d+)/;
	\end{verbatim}
	\end{quote}
\end{itemize}

\subsection{Operations}

	Okay, now that you know what a regular expression is, what can
you do with one? First of all, you can see if a string matches it:

\subsubsection{\code{m//}}

\begin{quote}
\code{m/}\textit{pattern}\code{/}\textit{[options]}
\end{quote}
%
\code{m//} returns a true value if a string matches \textit{pattern},
and a false value otherwise.

Note that you can use any character%
\footnote{Any non-alphanumeric, non-whitespace character, that
is. Otherwise, \code{magenta} would be a valid \perl\ program, which
would be confusing.}
as the pattern delimiter, instead of slashes. This is especially
useful when you're matching a filename and don't want to write
\code{\bs/} all the time.

	Again, as with the \code{q}*-style quoting operators, you can
use the symmetrical delimiters: \code{m\{}\ldots\code{\}},
\code{m(}\ldots{)}, \code{m<}\ldots\code{>}.

	If, however, you choose to use slashes, then you don't need
the \code{m} at the beginning. In addition, if you don't say
otherwise, \perl\ will match \code{\$\_} against the pattern. That's
why you'll often see
%
\begin{quote}
\code{if (/\^{}user.*/)}\\
\code{\{}\\
\hbox{}\hspace{4em}\ldots\\
\code{\}}\\
\end{quote}
%
Otherwise, you can specify
%
\begin{quote}
\code{\$var =\~{} /}\textit{pattern}\code{/}
\end{quote}
%
or
\begin{quote}
\code{\$var !\~{} /}\textit{pattern}\code{/}
\end{quote}
%
to say ``\code{\$var} matches \textit{pattern}'' or ``\code{\$var}
doesn't match \textit{pattern},'' respectively.

	\code{m//} takes a number of options:
%
\begin{itemize}
\addtolength{\labelsep}{1em}%
\item[\code{i}]
	Do case-insensitive pattern-matching.
\item[\code{g}]

	Do a global pattern match. In a list context, \code{m//g}
	returns a list of all of the patterns that it found in the
	string. In a scalar context, it finds the first match and
	returns true; then, if you match the same pattern, \perl\ will
	remember where it left off and start from there. Thus,
%
	\begin{quote}
	\begin{verbatim}
	$_ = "a1b2c3";
	@numbers = /\d/g;
	\end{verbatim}%$
	\end{quote}
%
	will set \code{@numbers} to \code{(1, 2, 3)}, and
%
	\begin{quote}
	\begin{verbatim}
	while ("a1b2c3" =~ /\d/g)
	{
	        print "$&\n";
	}
	\end{verbatim}%$
	\end{quote}
%
	will print
%
	\begin{quote}
	\begin{verbatim}
	1
	2
	3
	\end{verbatim}
	\end{quote}

		If you're using \code{m//g}, you can use \code{\bs G}
	in the pattern, to match the place where the last match left
	off. This acts as a weak \code{\^{}}. For instance,
%
	\begin{quote}
	\begin{verbatim}
	$_ = "abc123def456";
	m/\d/g;
	print "$&\n";
	while (/\G\d/g)
	{
	        print "$&\n";
	}
	\end{verbatim}%$
	\end{quote}
%
	will print
%
	\begin{quote}
	\begin{verbatim}
	1
	2
	3
	\end{verbatim}
	\end{quote}

\item[\code{c}]
	Normally, when \code{m//g} doesn't match, it resets the search
	position to the beginning of the string. \code{m//gc} prevents
	this.
\item[\code{m}]
	This is a multi-line pattern. That is, \code{\^{}} will match
	the beginning of every line, and \code{\$} will match the end
	of every line in the string.

		Inside of \code{m//m}, you can use \code{\bs A} and
	\code{\bs Z}. \code{\bs A} matches only the very beginning of
	the string, and \code{\bs Z} matches only the very end.
\item[\code{o}]
	Only compile the pattern once.

	If you have variables in a pattern, \perl\ will interpolate
	them, then compile the pattern. It'll do this every time it
	sees the pattern. This can be expensive, so if you specify the
	\code{/o} option, \perl\ will compile the pattern only
	once. Of course, if the variables in your pattern change
	value, this won't work.
\item[\code{s}]
	Treat the string as a single line: \code{.} will match newlines.
\item[\code{x}]
	Enable extended regular expressions. These get a section of
	their own.
\end{itemize}

	(Are you overwhelmed yet?)

\subsubsection{\code{s///}}

	The next really useful thing you can do with regular
expressions is string replacement. This is done with
%
\begin{quote}
\code{s/}\textit{pattern}\code{/}\textit{replacement}\code{/}\textit{[options]}
\end{quote}

	This replaces the text matched by \textit{pattern} with the
\textit{replacement} text, and returns the number of substitutions
made. Note that \textit{replacement} is a string, not a pattern.

	Again, just as with \code{m//}, \code{s///} will work on
\code{\$\_} by default, or you can use \code{=\~{}} to have it work on
some other variable. Likewise, you can use a delimiter other than
slashes, if you like.

	Since \textit{pattern} is a regular expression, all of the
special variables are available on the right, so
%
\begin{quote}
\begin{verbatim}
$string = "ABCdef";
$string =~ s/^(...)/$1($1)/;
\end{verbatim}
\end{quote}
%
will set \code{\$string} to \code{ABC(ABC)def}.

	\code{s///} takes the same options as \code{m//}, with the
addition of
\begin{itemize}
\addtolength{\labelsep}{1em}%
\item[\code{e}]
	Treat \textit{replacement} as an expression to be evaluated.

	When you have the \code{/e} option, \code{s///} will match the
	pattern on the left, then evaluate the string on the right as
	a \perl\ expression, and replaces \textit{pattern} with
	whatever the expression returns. For example:
%
	\begin{quote}
	\begin{verbatim}
	s/\d+/(getpwuid($&))[0]/eg;
	\end{verbatim}%$
	\end{quote}
%
	replaces any integer in \code{\$\_} with the name of the user
	with that uid.

	A word of caution: \perl\ doesn't compile the replacement
	expression until it needs to, so if it contains a syntax
	error, you won't see any error messages about it until it is
	encountered at runtime.
\end{itemize}

\subsubsection{\code{tr///} and \code{y///}}

\begin{quote}
\code{tr/}\textit{searchlist}\code{/}\textit{replacementlist}\code{/}\textit{[options]}
\end{quote}

	\code{tr///} replaces the characters on the left with the
corresponding characters on the right, much as the \textsl{tr} program
does (\code{y} is a synonym for \code{tr}). Thus,
%
\begin{quote}
\begin{verbatim}
tr/A-Z/a-z/
\end{verbatim}
\end{quote}
%
converts all upper case letters to lower case, and leaves everything
else alone.

	Note that \textit{searchlist} and \textit{replacementlist} are
strings, not full-fledged regular expressions (except that you can
have ranges), but this seemed like the right place to talk about this.

	As you're probably getting used to by now, \code{tr} operates
on \code{\$\_} by default, or you can use \code{=\~{}} to make it work
on another variable.

	\code{tr///} only takes a few options:
%
\begin{itemize}
\addtolength{\labelsep}{1em}%
\item[\code{c}]
	Complement the search list: anything that doesn't match will
	be replaced by the last character of \textit{replacementlist}
\item[\code{d}]
	Delete any characters that were found in the search list, but
	don't have a corresponding replacement. Normally, they are
	replaced by the last character in the replacement list.
\item[\code{s}]
	Squash identical characters in the output:
	%
	\begin{quote}
	\begin{verbatim}
	s/ \t/_/s
	\end{verbatim}
	\end{quote}
	%
	converts \code{a\  b\  \  \  \  c} to \code{a\_b\_c}.
\end{itemize}

\subsubsection{\code{split} and \code{join}}

\begin{quote}
\code{split /}\textit{pattern}\code{/,} \textit{[string}\code{,} \textit{[limit]]}\\
\code{join }\textit{string}\code{,} \textit{array}
\end{quote}

	\code{split} looks for instances of \textit{pattern} in the
string, and returns an array consisting of everything else. Thus,
%
\begin{quote}
\begin{verbatim}
split /:/, "staff:*:10:arensb, arnie"
\end{verbatim}
\end{quote}
%
will return \code{("staff", "*", "10", "arensb, arnie")}.

	If you omit the string, \code{split} will use---you guessed
it---\code{\$\_}.

	If you also omit the pattern, \code{split} will split on
whitespace, and will also strip leading whitespace so that you don't
get an empty first element.

	If you specify a number as \textit{limit}, then \code{split}
will split the \textit{string} into no more than that many parts.

	\code{join} is the converse of \code{split}: it returns a
string, consisting of all of the elements in \textit{array}, with
\textit{string} in between.

	One thing to watch out for: it \emph{is} legal to split on an
empty pattern (\textit{e.g.}, \code{split~//~"abc"}), but this usually
isn't what you want: this will return an array of every character in
the string. Another common mistake is, as with \code{m//}, using
\code{*} instead of \code{+}: \code{split~/:*/} will split
\code{ab::c} into \code{("a", "b", "c")}.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Functions}

	Of course, a programming language wouldn't be much good if it
didn't allow you to define your own functions, now would it?

	To define your own function, use
%
\begin{quote}
\code{sub} \textit{name}\\
\code{\{}\\
\hbox{}\hspace{4em}\ldots\\
\code{\}}
\end{quote}
%
and call it using
%
\begin{quote}
\code{\amp}\textit{name}\code{(}\textit{arg\ldots}\code{);}
\end{quote}

	The body of the function can contain the same sorts of things
that you can do in the main program: you can manipulate variables and
use all of the flow-control constructs, as you'd expect. But you can
do just about anything you like, including switching packages (we'll
talk about packages later), and even define new functions.

	You can define functions wherever you like: the \perl\
compiler will find them during the compilation phase, and make them
available to your code by the time the body of the program is
executed. You don't have to worry about defining functions before
calling them.%
\footnote{In fact, since you can define functions at runtime, you can
even put in calls to functions that don't yet exist. Of course, you
shouldn't do this without good reason.}

%\subsection{\code{local} vs. \code{my} Variables}
\subsection{Local Variables}

	The next question is, what does the following code do?
%
\begin{quote}
\begin{verbatim}
$var = 1;
&myfunc;
print $var;

sub myfunc {
        # ...
        $var = 2;
}
\end{verbatim}%$
\end{quote}

	The answer depends, of course, on what the \code{\#...} is.
Normally, this will set the global variable \code{\$var} to 2. Unlike
the Bourne shell, variables inside \perl\ functions are not
automaticaly local. So if you don't say anything in the \code{\#...}
above, the \code{myfunc} function will set the global variable
\code{\$var} to 2, and \code{\$var} will retain this value when
\code{myfunc} exits.

	Of course, it can be extremely handy to have local variables
inside of a function. To do so, simply put
%
\begin{quote}
\begin{verbatim}
my $var;
\end{verbatim}%$
\end{quote}
%
or
%
\begin{quote}
\begin{verbatim}
my $var = 2;
\end{verbatim}%$
\end{quote}
%
at the beginning of your function body, and everything will work as
you expect it to.%
\footnote{You may also see scripts that use \code{local} instead of
\code{my}. Unless you know what you're doing, you should use
\code{my}, and I'll talk about why later on. In the meantime, if
you're impatient, I'll tell you that \code{my} uses lexical scoping,
whereas \code{local} uses dynamic scoping.}
%
\code{my} variables do not get propagated outside of the enclosing
block, so if \code{myfunc} had a \code{my \$var} at the top, then
\code{\$var} would still be 1 back in the main program.

\subsection{Function Arguments}

	Function arguments are passed to a function through the
\code{@\_} array. Thus, you'll often see this sort of thing:
%
\begin{quote}
\begin{verbatim}
sub myfunc {
        my $arg  = shift;
        my @rest = @_;
	my ($num1, $num2) = (3, 98);
\end{verbatim}%$
\end{quote}
%
As was briefly mentioned in the section on arrays, the \code{shift}
function removes the first element from an array, and returns it. If
you don't specify which array to do this to, it'll use \code{@ARGV} in
the main program, or \code{@\_} in functions.

	So here, the line \code{my \$arg = shift} declares
\code{\$arg} as being local to the function, and also initializes it
to the first argument.

	The next line declares the array \code{@rest} to be local to
\code{myfunc}, and assigns it all of the other arguments that were
passed to the function.

	Finally, note how to place the parentheses if you want to
declare several \code{my} variables on one line. In my opinion,
however, you should only declare one \code{my} variable per line,
since it makes your code more readable.

\subsection{Other tricks}

	Actually, there's another way to call functions: if the
function is declared \emph{before} you call it, the compiler already
knows about it, so you don't need the \code{\amp} to tell it, ``this
is a function call.'' Thus, you can say
%
\begin{quote}
\begin{verbatim}
sub japh {
	my $language = shift;
        print "Just another $language hacker\n";
}

japh "Perl";
\end{verbatim}
\end{quote}
%
And as you can see, you can also omit the parentheses around the
arguments. This allows you to make your functions look like the
built-in ones.

	You can also have a stand-alone declaration in one place, and
a definition someplace else.%
\footnote{A \emph{declaration} merely says that the function exists,
or will exist at some point. The definition specifies the body of the
function, \textit{i.e.}, which commands it executes.}
%
As long as the declaration comes before the call, you can safely omit
the \code{\amp}:
%
\begin{quote}
\begin{verbatim}
sub japh;

japh "Perl";

sub japh {
	my $language = shift;
        print "Just another $language hacker\n";
}
\end{verbatim}
\end{quote}

	If you call a function as
%
\begin{quote}
\begin{verbatim}
&myfunc;
\end{verbatim}
\end{quote}
%
\textit{i.e.}, if you leave off the arguments \emph{and} parentheses,
\code{myfunc} will be called with the current value of
\code{@\_}. This means that, if you were so inclined, you could write
a function that manipulated its argument list, then passed it off to
another function to do the real work. If the argument list is long,
this can avoid copying arrays needlessly.

	If you want to explicitly call a function with no arguments, use
%
\begin{quote}
\begin{verbatim}
&myfunc();
\end{verbatim}
\end{quote}

\subsection{Return values}

	To return a value, use
%
\begin{quote}
\code{return} \textit{value}\code{;}
\end{quote}
%
And that's about it. \textit{value} can be a scalar, array, or hash
variable, or a literal value.

\subsection{Function Prototypes}

	As you can tell from the above, all functions in \perl\ take a
variable number of arguments, so you can call a function any which
way, and it'll work. There are times, however, when you \emph{do} want
to be told that a certain function requires two arguments, and you're
only passing it one. For this, \perl\ has function prototypes.

	A function prototype looks like this:
%
\begin{quote}
\begin{verbatim}
sub myfunc ($$@) {
        my $a = shift;
        my $b = shift;
        my @c = @_;

        ...
}
\end{verbatim}%$
\end{quote}
%
It's like a little picture of the way the function should be called. A
\code{\$} in the prototype means that the argument is a scalar; a
\code{@} means that the argument is an array, and \code{\%} means that
it's a hash. Thus, \code{myfunc} above takes two scalars and an array.

	A semicolon (\code{;}) separates mandatory arguments from
optional ones:
%
\begin{quote}
\begin{verbatim}
sub settime ($$;$)
\end{verbatim}%$
\end{quote}
%
Here, the function \code{settime} takes either two or three arguments,
so it can be called as
%
\begin{quote}
\begin{verbatim}
settime 19, 30;
\end{verbatim}
\end{quote}
%
or
%
\begin{quote}
\begin{verbatim}
settime 19, 30, 59;
\end{verbatim}
\end{quote}
%
but not
%
\begin{quote}
\begin{verbatim}
settime 19, 30, 59, 30;
\end{verbatim}
\end{quote}

	A star (\code{*}) indicates a glob, and is usually used for
filehandles. We haven't covered this yet, but don't worry: it won't
make much more sense after we do.

	A backslash (\code{\bs}) in front of a character indicates
that that argument \emph{must} begin with that character. That is, if
you have
%
\begin{quote}
\begin{verbatim}
sub sort_list (\@)
\end{verbatim}
\end{quote}
%
it can be called as
%
\begin{quote}
\begin{verbatim}
sort_list @my_array;
\end{verbatim}
\end{quote}
%
but not
%
\begin{quote}
\begin{verbatim}
sort_list 19, 101, 38, 54, "hike!";
\end{verbatim}
\end{quote}
%
This will make more sense when we get to references. Trust me.

	A function prototype \emph{does} count as a declaration if you
don't want to put the \code{\amp} in front of your function calls.

	Having said all this, I must confess that prototypes aren't
quite as useful as one might hope. As I mentioned, the prototype must
come before the function call for it to have any effect. This doesn't
mean that you should put all of your function definitions at the top:
you can also have a stand-alone prototype at the top:
%
\begin{quote}
\begin{verbatim}
sub myfunc ($$@);
\end{verbatim}
\end{quote}
%
However, if you do this, you also need to include a prototype when you
define the function later on.

	Of course, if your function is inside of a module, the module
will typically be included at the top of the main program, so you only
have to maintain one prototype, which simplifies everything.

	In addition, object methods (which we haven't covered yet, but
which are special types of functions) aren't affected by prototypes;
also, if you use \code{\amp}\textit{func}, prototypes have no
effect. The intent is to allow you to write functions that look like
the built-in functions; if you stray too far from that, you don't get
their benefits.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Built-In Functions}

	\perl\ includs a whole slew of built-in functions, just to
start you off. I won't go into a lot of detail about them; this list
is just to give you an idea of the sorts of things you can do, and the
wheels you don't have to reinvent.

\subsection{Scalar Functions}

\begingroup
\addtolength{\leftmargini}{2em}%
\begin{itemize}
\addtolength{\labelsep}{1em}%
\item[\code{chr} \textit{num}]
	Returns the character whose value is \textit{num} in the
	character set you're using.
\item[\code{ord} \textit{expr}]
	Returns the ASCII value of the first character in
	\textit{expr}.
\item[\code{chmod} \textit{nnn}\code{,} \textit{files\ldots}]
	Changes the mode of \textit{files} to \textit{nnn}.
\item[\code{chown} \textit{uuu}\code{,} \textit{ggg}\code{,} \textit{files\ldots}]
	Changes the owner of \textit{files} to user \textit{uuu} and
	group \textit{ggg}.
\item[\code{die} \textit{message}]

	Prints the value of \textit{message}, and exits with the
	current value of \code{\$!}. If \textit{message} does not end
	with a newline, appends
	\code{at}~\textit{FILE}~\code{line}~\textit{LINE}.

	Inside of an \code{eval}, however, \code{die} makes the
	\code{eval} exit with the undefined value, and sets \code{\$@}
	to the value of \textit{message}. This allows you to implement
	exception-catching \textit{{\`a} la} C++ or Java.

	You can also catch \code{die}s by setting up a handler for the
	pseudo-signal \code{\$SIG\{\_\_DIE\_\_\}}. It will be passed
	the error message as its argument. If it calls \code{die}
	again, the second error message will be printed.
\item[\code{warn} \textit{message}]
	Prints \textit{message} to standard error, just like
	\code{die}, but doesn't exit.

	Like \code{die}, you can install a handler for the
	pseudo-signal \code{\$SIG\{\_\_WARN\_\_\}}. However, if you do
	so, it is your responsibility to take any appropriate action
	(including printing an error message), since \perl\ will
	assume that you know what you're doing when you install such a
	handler.

	If you want to get the default behavior of \code{warn} inside
	the handler, just call \code{warn} again. The hook will not be
	invoked recursively.
\item[\code{exit} \textit{code}]
	Exits the program immediately and returns an exit code of
	\textit{code} to the caller.
\item[\code{exec} \textit{prog}\textit{[}\code{,} \textit{arg\ldots]}]
	Executes the program \textit{prog} and replaces the current
	program with it. That is, this function never returns.

	\code{exec} will either pass the program to the shell, or call
	\code{execvp()} directly, depending on whether it looks as if
	you're passing it a shell expression or an \code{argv[]} list.
\item[\code{system} \textit{list}]
	Executes a program in the same way as \code{exec}, except that
	it \code{fork}s first, and waits for the program to exit. The
	return value is the program's exit code, times 256.
\item[\code{grep} \textit{expr}\code{,} \textit{list}]
	\code{grep} evaluates \textit{expr} (often just a
	pattern-match) for each element in \textit{list}, and returns
	the list of those for which \textit{expr} returned a
	true value. Inside \textit{expr}, \code{\$\_} is set to
	the current element.
\item[\code{map} \textit{expr}\code{,} \textit{list}]
	Evaluates \textit{expr} for each element of
	\textit{list}, and returns the list of values returned by
	\textit{expr}.
\item[\code{sort} \textit{expr} \textit{list}]
	Sorts the elements of \textit{list}, and returns the sorted
	list. \textit{expr} can be either the name of a function, or a
	block of code.%
	\footnote{Which is actually an anonymous inline function}

	\textit{expr}, be it a function or a block, is a comparison
	function which will have available the variables \code{\$a}
	and \code{\$b}. It should return -1 if \code{\$a} comes before
	\code{\$b}, 1 if \code{\$a} comes after \code{\$b}, or 0 if
	they're equal. The \code{<=>} and \code{cmp} operators come in
	really handy here.
%\item[\code{splice} \textit{array}\code{,} \textit{offset}\code{,} \textit{[length}\code{,} \textit{list]]}]
\bigitem{\code{splice} \textit{array}\code{,} \textit{offset}\code{,} \textit{[length}\code{,} \textit{list]]}}
	Generalized list-substitution function. Replaces
	\textit{length} elements of \textit{array}, starting at
	\textit{offset}, with \textit{list}. See perlfunc(1) for the
	details.
\item[\code{lc} \textit{expr}]
	Converts \textit{expr} to lower case.
\item[\code{lcfirst} \textit{expr}]
	Converts the first character of \textit{expr} to lower case.
\item[\code{uc} \textit{expr}]
	Converts \textit{expr} to upper case.
\item[\code{ucfirst} \textit{expr}]
	Converts the first character of \textit{expr} to upper case.
\item[\code{int} \textit{expr}]
	Looks for an integer at the beginning of \textit{expr} and
	returns its value.
\item[\code{hex} \textit{expr}]
	Interprets \textit{expr} as a hex number (with an optional
	leading \code{0x}), and returns the corresponding value.
\item[\code{oct} \textit{expr}]
	Interprets \textit{expr} as an octal number, and returns its
	value. Oh, and if \textit{expr} begins with \code{0x}, it'll
	interpret it as a hex number.
\item[\code{pack} \textit{template}\code{,} \textit{list}]
	Takes a list of values and converts them to a binary
	string. You can use this to write binary files that will be
	read by some other program.

	\textit{template} is too complex to describe here. See
	perlfunc(1) for the gory details.
\item[\code{unpack} \textit{template}\code{,} \textit{expr}]
	The reverse of \code{pack}. Takes \textit{expr}, parses it
	according to \textit{template}, and returns the corresponding
	list of values.

	You can use this to read binary files.
\item[\code{length} \textit{expr}]
	Returns the string length of \textit{expr}.
\bigitem{\code{printf} \textit{[FILEHANDLE]} \textit{format}\code{,} \textit{list}}
	C's \code{printf()}, in case you need to, say, print a
	floating-point number with a certain number of decimals. In
	general, however, you should prefer \code{print} over
	\code{printf}.
\bigitem{\code{sprintf} \textit{[FILEHANDLE]} \textit{format}\code{,} \textit{list}}
	Like C's \code{sprintf()}, except that it returns the
	resulting value.
\item[\code{reverse} \textit{list}]
	Reverses an array or string.
\item[\code{select} \textit{[FILEHANDLE]}]
	Sets or returns the default filehandle. This is handy for
	setting \code{\$\tilde} and such.
\item[\code{substr} \textit{expr}\code{,} \textit{offset}\code{,} \textit{[len]}]
	Returns the \textit{len}-length substring of \textit{expr}
	that begins at \textit{offset}. If \textit{offset} is
	negative, it refers to the position that far from the end of
	\textit{expr}.

% ``useless'' functions
\bigitem{\code{sysopen} \textit{filehandle}\code{,} \textit{filename}\code{,} \textit{mode}\code{,} \textit{[perm]}}
	Opens a file, but gives you all the power of open(2).
\bigitem{\code{sysread} \textit{filehandle}\code{,} \textit{var}\code{,} \textit{len}\code{,} \textit{[offset]}}
	Uses read(2) to read from a file. Typically, if you need to
	use this, you'll want to feed the results to \code{unpack}.
\bigitem{\code{syswrite} \textit{filehandle}\code{,} \textit{var}\code{,} \textit{len}\code{,} \textit{[offset]}}
	Uses write(2) to write to a file. Typically, \textit{var} will
	be the output of \code{pack}.
\item[\code{study} \textit{var}]
	Makes \perl\ take extra time to study the pattern
	\textit{var}, to optimize matching it. This may or may not
	save time.
\item[\code{index} \textit{str}\code{,} \textit{substr [}\code{,} \textit{pos]}]
	Returns the position of the first occurrence of \textit{substr}
	in \textit{str} at or after \textit{pos}, or -1. If
	\textit{pos} is omitted, starts at the beginning of
	\textit{str}.
\item[\code{rindex} \textit{str}\code{,} \textit{substr [}\code{,} \textit{pos]}]
	Just like \code{index}, but returns the last occurrence of
	\textit{substr} within \textit{str}.
\item[\code{pos} \textit{var}]
	Returns the position where the last \code{m//g} over
	\textit{scalar} left off.
\item[\code{vec} \textit{expr}\code{,} \textit{offset}\code{,} \textit{bits}]
	Treats \textit{expr} as an array of bit-fields, and allows you
	to get or set values.
\item[\code{getlogin}]
	Looks up the current username in \textsl{/etc/utmp} and
	returns it.
\item[\code{glob} \textit{expr}]
	Same as \code{<}\textit{file pattern}\code{>}.
\item[\code{reset} \textit{[expr]}]
	Resets certain matches, or variables beginning with
	\textit{expr}. Deprecated.
\end{itemize}
\endgroup

% eval	% Put under objects, somewhere
%	$@ (goes with eval)
%import	% Put under packages
%new	% Put under objects
%no	% Put under modules
%package	% Put under packages
%quotemeta
%ref	% Put under references
%require	% packages
%tie	% objects
%tied	% objects
%untie	% objects
%use	% modules

\subsection{Unix Functions}

	The following functions do pretty much the same thing as the
standard Unix functions of the same name.

\begingroup
\ttfamily
\begin{tabular}{lllll}

abs		& gethostbyname		& getsockopt	& rename	& sleep \\
accept		& gethostent		& gmtime	& rewinddir	& socket \\
atan2		& getnetbyaddr		& ioctl		& rmdir		& socketpair \\
bind		& getnetbyname		& kill		& seek		& sprintf \\
chdir		& getnetent		& link		& seekdir	& sqrt \\
chroot		& getpeername		& listen	& select	& srand \\
connect		& getpgrp		& log		& semctl	& stat \\
cos		& getppid		& lstat		& semget	& symlink \\
crypt		& getpriority		& mkdir		& semop		& syscall \\
exp		& getprotobyname	& msgctl	& send		& tell \\
fcntl		& getprotobynumber	& msgget	& setpgrp	& telldir \\
fileno		& getprotoent		& msgrcv	& setpriority	& time \\
flock		& getpwent		& msgsnd	& setsockopt	& times \\
fork		& getpwnam		& opendir	& shmctl	& truncate \\
getc		& getpwuid		& pipe		& shmget	& umask \\
getgrent	& getservbyname		& rand		& shmread	& unlink \\
getgrgid	& getservbyport		& readdir	& shmwrite	& utime \\
getgrnam	& getservent		& readlink	& shutdown	& wait \\
gethostbyaddr	& getsockname		& recv		& sin		& waitpid
 
\end{tabular}
\endgroup
\end{document}
