The -n and -p
options wrap your script inside loops. Before looking at specific
examples, let's see what the loops look like and how they are
changed by the -a and -F
options.

The -n option causes Perl
to execute your script inside the following loop:

while (<>) {
# your script
}

The -p option uses the same
loop, but adds a continue
block so that $_ will be
printed every time through the loop. If both -n
and -p are specified on the
command line, the -p option
will take precedence. The loop looks like this:

while (<>) {
# your script
} continue {
print;
}

The -a option adds a split()
function call to the beginning of each iteration of the loop so
that the loop looks like this:

while (<>) {
@F = split(/ /);
# your script
}

The -F option lets you split
on something besides the space character. If you used -F/i+/
on the command line, the loop would look like this:

while (<>) {
@F = split(/i+/);
# your script
}

You can use BEGIN and END
blocks if you need to specify some initialization or cleanup code.
The initialization section might be used to create objects or
to open log files. The cleanup section can be used to display
statistics or close files. For example,