Perl One-Liners

When you write a Perl script, much of the time you'll write it as you have throughout this book—putting the script into a file, and then using the Perl interpreter to run that script. But sometimes there might be a task that's just really simple, or there might be something that you only need to do once (or very infrequently). For these kinds of tasks, it's almost a waste of time to start up an editor to write an actual script. For just this reason there are Perl one-liners.

Perl one-liners are scripts that you type directly on the Perl command line. They aren't saved anywhere; if you get them wrong you'll have to type them again.

To create a Perl one-liner, use the -e option, followed by the script inside quotes, like this:

%  perl
						-e 'print "this is a one-liner
";'
this is a one-liner
%

In Windows, you'll have to use double-quotes around the entire script, and back-slash the quotes for the strings, like this:

C:> perl
						-e "print "this is a windows one-liner
";"
					

If your script contains multiple statements, put them all on the single line (in most Unix shells, you can continue a single command onto multiple lines by putting a backslash () at the end of a line). Remember, Perl doesn't care much about whitespace, so you could theoretically create an incredibly complex Perl one-liner, and Perl would have no problem executing it (in fact, this is a boast you'll commonly hear from Perl programmers—“I can do that in one line!”—of course, you can do anything in Perl in one line, as long as that line is long enough).

Here are a couple example Perl one-liners. To reverse all the lines in a file

% perl
						-e 'print reverse <>;' filename.txt
					

To print all the lines in a file, with line numbers

% perl
						-e '$i=1;while(<>){print "$i: $_";$i++} ' filename.txt
					

To remove all leading whitespace from each line in a file

% perl
						-e 'while(<>){s/^s+//g;print;} ' filename.txt
					

To print a file all in uppercase

% perl
						-e 'while(<>){print uc  $_;} '
					

Because these sorts of scripts commonly use while loops with <> and some form of print, Perl has a shortcut for that. The -p option allows you to omit the while (<>) part. It will also print $_ for each line. So, for example, that uppercase example could be written like this instead (whether this example is better or not is up to you):

% perl
						-p
						-e '$_ = uc $_;' test.txt
					

This is equivalent to the code

while (<>) {
   $_ = uc $_;
   print;
}

You can also collapse the options into a single dash, as long as the e comes last:

% perl
						-pe '$_ = uc $_;' test.txt
					

Note

-p isn't the only Perl option designed to optimize your typing in one-liners. The -n option does the same thing as -p, minus the print part (all it gives you is the while (<>) loop). The -l option, when used with either -p or -n, will automatically chomp the newlines off of each line for you, and then put it back when you print the line. (Or, to be more specific, it sets the value of the $ variable, the output record separator, to be $/, the input record separator, usually a newline.) You can also give -l an octal argument representing the character you want to use as the output record separator.


Finally, Perl one-liners can be extremely powerful when used in conjunction with the -i option. For example, say you had written a novel and had it stored in a series of files ending with a .txt extension. You want to change all instances of the name “Steve” with the name “Fred.” Here's a Perl one-liner that modifies all the original files and creates a backup copy of each:

% perl
						-p
						-i.bak
						-e 's/Steve/Fred/g' *.txt
					

The -i option changes your original files in place, so that the new versions will have the same filenames as the originals. The old versions of the files (the ones with Steve in them) will be saved off to filenames with the extension .txt.bak. This way, if you find that it wasn't Steve you wanted to change, it was Albert, you can get your original files back. Be careful when you use this Perl command—try it on a single file without modifying it, first, to make sure your one-liner works right. Otherwise you could end up moving a lot of .bak files back to where they belong!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.100.202