7 of the most useful Perl command line options

Perl has a lot of command line options. Some of them can be used to do very useful things, others are more esoteric.
In the Perl tutorial we already saw how to use Perl on the command line,
let's now extend that list.

-v to get the Perl version

This options just shows the version number of the Perl interpreter and the Copyright information:

$ perl -v
This is perl 5, version 20, subversion 1 (v5.20.1) built for darwin-thread-multi-2level
Copyright 1987-2014, Larry Wall
Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.
Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl". If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.

-V to get compiler details

Besides the version number, this command line option will tell Perl to print
all the information about the platform we are currently running on (In the following
example osname=darwin means Mac OSX.

It also provides the configuration parameters - the way our version of
Perl was compiled. The
same information is available via the Config module during the runtime
of our code.

The %ENV part, if available, shows the Perl-related environment variables that
are currently in effect. These environment variables can be set in the Unix/Linux
shell or the Windows Command Window, before running Perl.

Finally it lists the content of the @INC array, that controls where Perl will look for modules
to load. One can change @INC in several ways.

-e execute code on the command line

For one-off tasks it can be very useful to be able to run a piece of Perl code without creating a file.
The code itself needs to be between quotes. Due to differences between the Unix/Linux shell
and the MS Windows Command prompt we need to use different quotes around our code.

On Unix/Linux systsem (including Mac OSX) it is recommended to put our code in single quotes as in the following
example:

$ perl -e 'print qq{Hello World\n}'
Hello World

On MS Windows we must use double quotes around our code.

$ perl -e "print qq{Hello World\n}"
Hello World

Internally, it is probably the best to use q and qq
instead of single-quote and double-quote, respectively. That might help reduce the confusion caused by
the behavior of the shell and command prompt.

-E execute code on the command line with all the latest features enabled

Since version 5.10 of Perl has been released, Perl includes some additional
keywords (called features) in the language. For improved backward compatibility these keywords are only
enabled if the user explicitly ask for them with use feature .... For example by writing use feature qw(say);,
or by declaring a minimal version of Perl with use 5.010;.

On the command line we can achieve the same by using -E instead of -e.
It will turn on all the features of the version of Perl we are currently running.

For me the most important of all these features, at least in one-liners is the
say keyword introduced in perl 5.10.
It is just print with a trailing newline added. Nothing fancy, but makes the one-liners even shorter.

The above examples would look like these:

Unix/Linux:

$ perl -E 'say q{Hello World}'
Hello World

MS Windows:

$ perl -E "say q{Hello World}"
Hello World

You can notice the change from qq to q. As we don't need to include a newline \n in our
strings we could switch from qq to q.

-n wrap the -e/-E code in a while loop

If we provide the -n command line option it will wrap our code provided using either the -e
or the -E options in a while with a diamond operator.

So

perl -n -E 'say if /code/' file.txt

is the same as

while (<>) {
say if /code/;
}

That will go over all the lines of all the files provided on the command line
(in this case it is file.txt) and print out every line that matches the /code/ regex.

-p is like -n with print $_

The -p option is very similar to the -n flag, but it also prints the content of $_
at the end of each iteration.

So we could write:

perl -p -E 's/code/foobar/' file.txt

which would become

while (<>) {
s/code/foobar/
print;
}

That will print the result to the screen.

-i for in-place editing

The most common use of -p is together with the -i option that provides "in-place editing".
It means that instead of printing to the screen, all the output generated by our one-liner will be written back to the same file it was taken from.

So this one-liner will replace the first appearance of the string "code" by "foobar" in every line of the file "file.txt".

perl -i -p -E 's/code/foobar/' file.txt

Screencasts

There are a number of screencasts showing some of the command line options: