Because each operator acts on the thing immediately to the right of it, it helps to read
this from right to left (or bottom to top, the way it is written here).
The first thing that acts on the list is the
map operator. It transforms the list into a hash in
which the keys are the list elements and the values are the lengths of each element. This
is where you put in your code that does the transformation by which you want to sort.
The next operator is the
sort function, which sorts the list by the values.
Finally, the hash is transformed back into an array by extracting its keys. The array is now
in the desired order.
Command-Line Processing
Perl is great at parsing the output of various programs. This is a task for which many
people use tools such as
awk and sed. Perl gives you a larger vocabulary for performing
these tasks. The following example is very simple, but illustrates how you might use Perl
to chop up some output and do something with it. In the example, Perl is used to list
only those files that are larger than 10KB:
$ ls -la | perl -nae ‘print “$F[8] is $F[4]\n” if $F[4] > 10000;’
The -n switch indicates that I want the Perl code run for each line of the output. The -a
switch automatically splits the output into the @F array. The -e switch indicates that the
Perl code is going to follow on the command line.
Related Ubuntu and Linux Commands
You will use these commands and tools when using Perl with Linux:
a2p—A filter used to translate awk scripts into Perl
find2perl—A utility used to create Perl code from command lines using the find
command
pcregrep—A utility used to search data using Perl-compatible regular expressions
perlcc—A compiler for Perl programs
perldoc—A Perl utility used to read Perl documentation
s2p—A filter used to translate sed scripts into Perl
vi—The vi (actually vim) text editor
Reference
The first place to look is in the Perl documentation and Linux man pages.
Perl, all of its documentation, and millions of lines of Perl programs are all available free
on the Internet. A number of Usenet newsgroups are also devoted to Perl, as are shelves of
books and a quarterly journal.
CHAPTER 27 Using Perl
638
Books
Although your local bookstore might have dozens of titles on Perl, the following are some
of the more highly recommended of these. You might also look at the Camel Critiques
(Tom Christiansen; http://language.perl.com/critiques/index.html) for reviews of other
available Perl books.
. Advanced Perl Programming, by Sriram Srinivasan, O’Reilly & Associates
. Sams Teach Yourself Perl in 21 Days, Second Edition, by Laura Lemay, Sams Publishing
. Sams Teach Yourself Perl in 24 Hours, Second Edition, by Clinton Pierce, Sams
Publishing
. Learning Perl, Third Edition, by Randal L. Schwartz, Tom Phoenix, O’Reilly &
Associates
. Programming Perl, Third Edition, by Larry Wall, Tom Christiansen, and Jon Orwant,
O’Reilly & Associates
. Effective Perl Programming: Writing Better Programs with Perl, by Joseph Hall, Addison-
Wesley Publishing Company
. CGI Programming with Perl, Second Edition, by Gunther Birznieks, Scott Guelich,
Shishir Gundavaram, O’Reilly & Associates
. Mastering Regular Expressions, by Jeffrey Friedl, O’Reilly & Associates
Usenet
Check out the following on Usenet:
. comp.lang.perl.misc—Discusses various aspects of the Perl programming language.
Make sure that your questions are Perl specific, not generic CGI programming ques-
tions. The regulars tend to flame folks who don’t know the difference.
. comp.infosystems.www.authoring.cgi—Discusses authoring of CGI programs, so
much of the discussion is Perl specific. Make sure that your questions are related to
CGI programming, not just Perl. The regulars are very particular about staying on
topic.
WWW
Check these sites on the World Wide Web:
. http://www.perl.com—Tom Christiansen maintains the Perl language home page.
This is the place to find all sorts of information about Perl, from its history and
culture to helpful tips. This is also the place to download the Perl interpreter for
your system.
Reference
639
27

Get Ubuntu Unleashed, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.