Perl Command Line

As many of you know, Perl is a useful scripting language. However, you can also write many useful one liners with it - there are plenty of options available to modify its behaviour.

The standard way of doing things as a Perl one-liner is as follows:

     $ perl -e 'print "Hello World\n";'

As you can see, you simply put the perl code between quotes - this is useful for testing out perl idioms, or for simple code. A slightly more advanced one-liner for crypting passwords would be:

     $ perl -e 'print crypt("password", "salt"),"\n"'

Note that this will be put in any shell history files, and be viewable from the process list, so don't run this with any sensitive passwords. A better way to do this would be as follows:

     $ perl
     print crypt("password", "salt"),"\n";

This will output the crypted password on STDOUT. This is useful in many other cases for testing code.

Perl also has syntax checking, which is useful when combined with use strict and #!/usr/bin/perl -w - which is important to use on all scripts. To test out syntax, try the following:

     $ perl -c

This is useful when debugging code, as it shows you syntax errors. It also doesn't actually execute the code.

The next useful idiom that command options gives you is options that let you loop over a given file. This can be useful for editing a file, and taking actions based on the contents, or even editing it in place. There are many variants on this, as follows.

The basic option to do this is -p. It is equivalent to the following code, and makes perl operate a little like sed.

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

For those of you who don't know perl, <> is effectivelly a loop over the file, putting each line into a variable available for processing.

This is useful when you want to make a change, and print each line after processing it. An example of this would be:

     The cat sat on the mat.

     $ perl -pe 's/cat/dog/' file
     The dog sat on the mat.

Slightly different to this is -n - this is almost the same as -p, but it doesn't print every line. This is equivalent to the following code.

while (<>) {
     ...          # your script goes here

Another useful modifier is -i. This allows you to either edit the files in place, or back them up. As in the previous example, you can edit the file in place.

     $ perl -pi -e 's/cat/dog/' file

This edits the file in place - this can be useful for global search and replacements. If you're cautious, however, you can leave a backup by adding an extension, such as -i.bak. This will leave a backup file with the contents unmodified.

For even more fun, you can use -a, which turns on autosplit mode. This modifier to -n or -p adds an implicit split inside the while loop, and is useful for columned data. By default, it splits on space, but you can change it with -F, which specifies a regular expression to split on. A useful example of parsing password files would be:

     $ perl -an -F: -e 'if ($F[2] > 100) { print $F[0],"\n"; }' /etc/passwd

This splits on a colon, and puts the result into @F. This can be useful for finding out information based on context.

To include modules in these scripts, use the -M option. For example:

     $ perl -Mfoo -e '....';

This is equivalent to:

     use foo;

One thing to note however, is that perl one-liners are rarely run with use strict or -w. If you find yourself using these, it might be time to consider a script. It is important to known when to keep with one-liners, and when to use a script. Additionally, remember that you can use any of these options in a script.

As you can see you can get quite a lot of functionality for very little code, and can save yourself a lot of time, both in script development time, and in finding out information from files. These options allow you to use the functionality of perl, without having to write a full blown script by giving you well tested, well known code. For more information about these options and more, see both perl(1p) and perlrun(1p).