Perl Command-Line Options
by Dave CrossAugust 10, 2004
Perl has a large number of command-line options that can help to make your programs more concise and open up many new possibilities for one-off command-line scripts using Perl. In this article we'll look at some of the most useful of these.
Safety Net Options
There are three options I like to think of as a "safety net," as they can stop you from making a fool of yourself when you're doing something particularly clever (or stupid!). And while they aren't ever necessary, it's rare that you'll find an experienced Perl programmer working without them.
The first of these is -c
.
This option compiles your program without running it.
This is a great way to ensure that you haven't introduced any syntax errors while you've been editing a program.
When I'm working on a program I never go more than a few minutes without saving the file and running:
$ perl -c <program>
Related Reading ![]() Perl in a Nutshell |
This makes sure that the program still compiles. It's far easier to fix problems when you've only made a few changes than it is to type in a couple of hundred of lines of code and then try to debug that.
The next safety net is the -w
option. This turns on warnings that Perl will then give you if it finds any of a number of problems in your code. Each of these warnings is a potential bug in your program and should be investigated. In modern versions of Perl (since 5.6.0) the -w
option has been replaced by the use warnings
pragma, which is more flexible than the command-line option so you shouldn't use -w
in new code.
The final safety net is the -T
option. This option puts Perl into "taint mode." In this mode, Perl inherently distrusts any data that it receives from outside the program's source -- for example, data passed in on the command line, read from a file, or taken from CGI parameters.
Tainted data cannot be used in an expression that interacts with the outside world -- for example, you can't use it in a call to system
or as the name of a file to open. The full list of restrictions is given in the perlsec
manual page.
In order to use this data in any of these potentially dangerous operations you need to untaint it. You do this by checking it against a regular expression. A detailed discussion of taint mode would fill an article all by itself so I won't go into any more details here, but using taint mode is a very good habit to get into -- particularly if you are writing programs (like CGI programs) that take unknown input from users.
Actually there's one other option that belongs in this set and that's -d
. This option puts you into the Perl debugger. This is also a subject that's too big for this article, but I recommend you look at "perldoc perldebug" or Richard Foley's Perl Debugger Pocket Reference.
Command-Line Programs
The next few options I want to look at make it easy to run short Perl programs on the command line. The first one, -e
, allows you to define Perl code to be executed by the compiler. For example, it's not necessary to write a "Hello World" program in Perl when you can just type this at the command line.
$ perl -e 'print "Hello World\n"'
You can have as many -e
options as you like and they will be run in the order that they appear on the command line.
$ perl -e 'print "Hello ";' -e 'print "World\n"'
Notice that like a normal Perl program, all but the last line of code needs to end with a ;
character.
Although it is possible to use a -e
option to load a module, Perl gives you the -M
option to make that easier.
$ perl -MLWP::Simple -e'print head "https://www.example.com"'
So -Mmodule
is the same as use module
. If the module has default imports you don't want imported then you can use -m
instead. Using -mmodule
is the equivalent of use module()
, which turns off any default imports. For example, the following command displays nothing as the head
function won't have been imported into your main
package:
$ perl -mLWP::Simple -e'print head "https://www.example.com"'
The -M
and -m
options implement various nice pieces of syntactic sugar to make using them as easy as possible. Any arguments you would normally pass to the use
statement can be listed following an =
sign.
$ perl -MCGI=:standard -e'print header'
This command imports the ":standard" export set from CGI.pm and therefore the header
function becomes available to your program. Multiple arguments can be listed using quotes and commas as separators.
$ perl -MCGI='header,start_html' -e'print header, start_html'
In this example we've just imported the two methods header
and start_html
as those are the only ones we are using.
Pages: 1, 2 |
