Languages::Perl::Command Line Options

PIC Lab



Common Command-Line Options

As with most other Unix commands, perl can be run with multiple command-line switches. In this section, we'll talk only about the six most common switches. You can always run perl -h or man perlrun to find out more detailed information about all of perl's command-line options.

 
But first, some notes...
 

Before You Read This (Kind Of)
Although Perl and Unix newcomers should be able to read this article without any major difficulties, it may be useful to get acquainted with some common Unix utilities and basic Perl syntax. For example, you should know that the cat utility basically prints out the contents of a file, that find searches for files that match a given expression, and that sort sorts its input alphabetically. Also, you might like to be familiar with the idea of shell commands, command-line switches, Perl input/output, flow control (e.g., loops), and (very) basic regular expressions.

Perl vs. perl
Because Perl is an interpreted language, the programs you write must be interpreted. There is a program (typically in /usr/bin or /usr/local/bin) called perl, which does the interpreting of your scripts. To distinguish between the programming language Perl and the interpreter perl, we capitalize the P in Perl the programming language (i.e., Perl), and use a lowercase p for the interpreter (i.e., perl).

Do You Write Programs or Scripts in Perl?
A common belief among programmers is that if source code must be compiled into a binary before it can run, then it's called a program; and if the source is interpreted at runtime, then it's a script. The trend within the Perl community is headed towards interchanging the use of "program" and "script," so I make no distinction here between the two.

 

Command Switches

 

-e commandline

 

Because you can get so much done in a single line of Perl, perl includes an -e switch that allows you to execute a single Perl command from the command-line. It's most common to surround your one-liner with single-quotes so that your double-quotes don't have to be escaped.

Examples:

$ perl -e 'print sort <>'
$ perl -e 'print time, "\n"'

 

-i [extension]

 

This switch assumes that all files processed with <> are to be edited in place. That is, any changes made to STDIN will change the contents of the original file.

If specified, extension tells perl to make a back-up of the original file, using extension to modify the name of the backed-up file. If extension contains no asterisks, then extension is appended to the original file name and used as the name for the back-up file. If one or more asterisks are specified, they are replaced with the original name of the file.

Example:

$ perl -pi'.bak' -e 's/\bteh\b/the/g' resume.txt

This one-liner instructs perl to open the file called resume.txt, copy it to a back-up file called resume.txt.bak, then read through the contents of the original file and correct misspellings of the word "the." The changes made are saved to the original file. Note that there is no space between the -i switch and its argument, '.bak'.

 

-I directory

 

The -I option is functionally equivalent to the Perl statement:

use lib 'directory';

which is roughly equivalent to:

BEGIN {
    unshift @INC, 'directory';
}

Recall that the automatic @INC array is used by Perl to determine where to search for Perl modules and required scripts. By default, the current working directory is searched, as are some system directories; by adding to @INC or using the -I command-line switch, you can customize perl's search for include files.

Example:

$ perl -I'./lib' -MCPAN -e 'shell'

This command causes the ./lib directory to be searched when including the module CPAN.pm. Once the module is found and used, the shell function is called, which starts up the CPAN shell program.

If you don't understand what include directories are, or why you would ever want to import Perl modules, then don't worry about understanding the -I switch :-)

 

-n

 

The -n switch wraps your script or one-liner inside a while loop that looks like this:

while(<>) {
    ... # your program here
}

so that you can process input from STDIN. Recall that the above construct for looping through each line of input is Perl shorthand for:

while(defined($_ = <STDIN>)) {
    ...
}

so each line of input to your script gets stored in Perl's special $_ variable.

This type of loop is particularly useful when combined with Unix pipes to pass the output of one program to another:

Examples:

$ cat h/wds | perl -ne 'print if /ing$/i'

This example pipes the output of cat (which is the contents of the file h/wds) to your small Perl script. The -n switch tells perl to loop through each line that gets returned by cat and store it in $_. Then the Perl script displays only the lines of cat's output that match the regular expression /ing$/i. Pretty nifty.

$ cat books.txt | perl -n get_title.pl

In this second example, where perl's argument is a file name, the file get_title.pl is executed for each line of cat's output.

 

-p

 

The -p switch is simliar to the -n switch, except that -p assumes the following loop around your script:

while(<>) {
    ... # your program here
} continue {
    print or die "-p destination: $!\n";
}

This may seem a little cryptic at first, especially with the print or die "-p destination: $!\n" section within the continue block. Don't be worried. The continue block in Perl is a piece of code that gets executed at the end of each iteration through a loop. And the print or die ... part simply prints the value stored in $_, which is the current line of input from STDIN. If it cannot print -- that is, print returns a false value -- then program execution stops and an error is displayed.

With -p as well as with -n, remember that if you wish to use the -e switch simultaneously, you must place the -e switch last. This is because of standard argument passing syntax employed by shell commands like perl.

Examples:

$ find . -name "*.mp3" | perl -pe 's/\.\/(\w+)-\w+/$1/'

This example uses the find utility to find all MP3s in the current directory and pipe their names into a Perl script that replaces the name of the file with the artist name (presuming that the file name conforms to the 'artist_name-song_name.mp3' format). Since the s/// operator alone acts on the $_ variable, each line of input (which is stored in $_) is changed.

$ cat log.txt | perl -pe 's/(\d+)\.\d+\.\d+\.\d+/$1/' | sort | uniq

That last example assumes that log.txt contains a list of remote IP addresses. The file's contents are piped into a Perl program that replaces the entire address with the first octet of the address. To be more useful, the output of the Perl program is piped into a standard Unix utility, sort, which sorts its input. Finally, the resulting output from sort is piped into uniq, which removes duplicate lines of input, displaying only unique lines.

 

-w

 

Issuing -w from the command-line is equivalent to placing the -w switch at the end of a shebang line in a Perl script:

#!/usr/local/bin/perl -w

This tells perl to use warnings, causing compile-time and runtime warnings to be issued should any fishy things appear in your code.

Examples of when perl might complain (i.e., issue a warning) are when you use a variable only once, use a variable in void context, or use a lowercase literal that might conflict with a future reserved word.


Feedback
PIC Lab home
up one level
UCLA Department of Mathematics
Program in Computing - PIC Lab
2817 Boelter Hall / 310-825-7267
This page last updated: October 15, 2001