Perl Command Line Options

Here are some of the most common and most useful command line options that you can use when you're running a Perl script.

-w


This turns on warnings in Perl; for example, using this flag will cause Perl to warn you about uninitialized variables in your program. Always either use this flag, or else define use warnings; in your program.




-e [perl code]


Allows you to run a Perl program specified on the command line.


perl -e "print 'Hello'";
Hello





-i[*.orig]


This one requires quite a lot of explanation. Basically -i is intended to allow you to more easily edit a file with Perl, without going to the hassle of opening the file in your program. It can of course be used to create the famous Perl one-liners, but we'll get into that shortly. Let's take a look at how you use this flag.

perl -i.orig test.pl temp.txt




Here we're telling Perl that we want to edit a file called "temp.txt". The original, unedited version of the file will be automatically saved in a file called "temp.txt.orig" (UNIX systems often allow you to do in-place edits with the -i flag without specifying a backup file extension; Windows typically insists there must be a backup).

Let's suppose we've got the following text in "temp.txt":

one two three lion four
lion five six
seven eight
lion
nine lion ten eleven




We want to replace all occurences of "lion" with the word "orange".

We can do this with a tiny little Perl script that looks like this:

while(my $line = <>) {
    $line =~ s/lion/orange/g;
    print $line;
}




When we use the -i flag, lines from the file specified on the command line can be read using the angle bracket operators <>. Perl print() will print not to STDOUT but to the file you're reading.

So when we run the above we end up with this in "temp.txt":

one two three orange four
orange five six
seven eight
orange
nine orange ten eleven




... and the original file contents in a file called "temp.txt.orig".

-i is particularly useful when combined with -p.

-p


This flag in effect surrounds your code with the following loop:

while(<>) {
    # Your code goes here.
    print;
}





What does this mean? Since both <> and print(), and the regular expression operators implicitly use the $_ variable, in effect your script is being supplied with one line of the specified file after another and printing it out after you're done with it.

Bottom line: you can write programs like this:

s/lion/orange/g;




and run them like this:

H:>perl -p test.pl temp.txt
one two three orange four
orange five six
seven eight
orange
nine orange ten eleven




... and as you can see, the results of your program operating on each line of the file are printed out onscreen. The original file is not changed.

Furthermore, -p enables you to specify multiple files with wildcards, for example temp*.txt will catch temp1.txt, temp2.txt etc.; -- or at least, this works in UNIX-like systems. On Windows, I can't get it to work. If you know how to, please let me know.



Quickly Replacing Text In a File With -pi



You can combine -i and -p to replace text in a file using only one line of Perl, like this.

Your program:

s/lion/orange/g;




Input file:

one two three lion four
lion five six
seven eight
lion
nine lion ten eleven




Command line:

perl -pi.orig test.pl temp.txt




Original file will be changed to:

one two three orange four
orange five six
seven eight
orange
nine orange ten eleven




... and the original file is saved in "temp.txt.orig"

For reasons that I don't understand, ActivePerl in Windows seems to only let you work on one file at a time like this. UNIX-like systems on the other hand, will allow you to edit multiple files using wildcards such as this:

perl -pi.orig test.pl temp*.txt




Every "temp.txt" file is now edited, with the originals being placed in "temp.txt.orig" files.

Perl One Liners



The flags -p, -i and -e (pie!) can be combined to allow you to edit files using Perl specified right on the command line.

This command line replaced all occurences of "orange" in the file "temp.txt" with the text "banana".

perl -pi.orig -e "s/orange/banana/g" temp.txt




Pretty nifty eh? Again, Windows frustratingly complains if there are wildcards in the name (please someone tell me how to get around this!), while UNIX-like systems will happily do multiple replacements on multiple files -- which is very, very useful on occasion.

(Incidentally, if you're looking for information on how to process command line arguments from within your own Perl program, go here.)


-v


Displays the version of Perl you're invoking.

-X (capital X)


Suppress all warnings, even if -w is specified. Don't use this if you can help it -- it's much better to catch and fix the warnings. In the long run, staying away from this flag will save you time.

-h


Displays help. Always handy.

-T


Causes a fatal error in CGI scripts that do potentially unsafe things, like using CGI input to create and run system commands.

That's it -- these are the command-line options that I've found to be most useful for running Perl.

For more detailed information, you might want to check perlrun.