|
Softpanorama |
May the source be with you, but remember the KISS principle ;-)
Softpanorama Search
|
In modern Unix programming environment Perl can be used as AWK replacement even in simple scripts. Recently Perl became a standard, installed by default in all major Unixes including AIX, HP-UX, Linux and Solaris. It is usually available in /usr/bin
Default availability dramatically changed role of Perl in Unix system scripting and routine text processing. For most administrators it is much easier to use Perl that any of the older alternatives. The main consideration here is the power of the language and availability of a very good, built-in debugger. Neither bash nor AWK has built-in debugger installed by default. And that alone tips the scales in Perl favor, as each administrator has a lot of things to do to spend time guessing where he made mistake using multiple echo/print statements.
That's why Perl is gradually displacing older Unix utilities such as cut, sed, wc, and, of course, AWK. Often you can replace quite complex set of pipe stages that use classic UNIX utilities with one Perl loop. Don't interpret me wrong, pipes are a blessing and extremely powerful tool that each UNIX admins should use to the max, but if you can avoid unnecessary complexity, why stick to old practices.
Perl is also amazingly Unix-friendly language that give the programmer full access to the Unix API.
The simplest way to start is to remember -e option (execute), which instructs Perl interpreter that the next argument is a Perl statement to be compiled and run. If -e is given, Perl will not look for a script filename in the argument list. Make sure to use semicolons where you would in a normal program. For example
perl -e 'print "Hello world of Perl command line";'
Multiple -e commands may be given to build up a multi-line script.
There are several more options that can sometimes simplify writing "in-line" Perl scripts or make them more similar to traditional AWL-scripts:
while (<>) { ... # your script goes here } The lines are
not printed by default. See -p to have lines printed. If a file named
by an argument cannot be opened for some reason, Perl warns you about
it, and moves on to the next file.
while (<>) { ... # your script } continue { print or die "-p destination: $!\n";}
perl -ane 'print pop(@F), "\n";'
is equivalent to
while (<>) { @F = split(' '); print pop(@F), "\n"; }
It is important to know that an alternate delimiter for split
may be specified using option -F.
As a useful example let's look how we can combine power of perl command line with find utilitie to produce a very simple but still useful command global string/pattern replace utility for multiple files :
$ find . -type f -exec perl -i -pe 's/something/another/g' {} \;
To make a command named lower that converts all filenames in the current directory to lower case add this to your ~/.bashrc or ~/.kshrc
function lower { perl -e 'for (@ARGV) { rename $_, lc($_) unless -e lc($_); }' * ; }
Tom Christiansen posted a list of on Usenet years ago, and that list is still interesting and useful for any Perl programmer. We those and some more modern one liners on a separate page Perl One Liners which you are encouraged to browse; the old Tom Christiansen list widely available in the file tomc.txt in various places on the Internet.
As with everything excessive zeal hurts, so you need to exercise judgment and the moment when one liner became counterproductive because of excessive complexity and should be converted into a regular script.
Copyright © 1996-2009 by Dr. Nikolai Bezroukov. www.softpanorama.org was created as a service to the UN Sustainable Development Networking Programme (SDNP) in the author free time. Submit comments This document is an industrial compilation designed and created exclusively for educational use and is placed under the copyright of the Open Content License(OPL). Site uses AdSense so you need to be aware of Google privacy policy. Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.
Disclaimer:
Created: November 7 1998; Last modified: September 05, 2009