Perl Cookbook

James Lee

Issue #60, April 1999

In this book, Tom Christiansen and Nathan Torkington cover many topics providing numerous code examples known as recipes (it is a “cookbook”, after all).

  • Authors: Tom Christiansen and Nathan Torkington

  • Publisher: O'Reilly & Associates

  • E-mail: info@oreilly.com

  • URL: http://www.oreilly.com/

  • Price: $39.95 US

  • ISBN: 1-56592-243-3

  • Reviewer: James Lee

Having written programs in Perl for a number of years, I fondly remember spending many hours leafing through the first edition of O'Reilly's Programming Perl (also known as “the Camel Book”), an excellent book covering the Perl programming language up to version 4.0.36. That book was well-written, technically correct and very witty. Two chapters of the first edition of Programming Perl I was particularly fond of, “Common Tasks with Perl” and “Real Perl Programs” contained numerous common tasks and real programs using Perl.

In 1996, O'Reilly published the second edition of Programming Perl, an excellent book covering all features of Perl including those features new to Perl version 5. The size of the book expanded from 465 pages in the first edition to a whopping 645 pages in the second. Many topics new to Perl 5 were added to the second edition, including a discussion of references and object-oriented programming. However, due to its size, portions of the first edition could not be covered in the second. Therefore, parts of the first edition were deleted, including my favorite “Common Tasks with Perl” and “Real Perl Programs” chapters.

Perl Cookbook (also known as “the Ram Book”) is a companion to Programming Perl, expanding the chapters of real Perl programs in the first edition of the Camel book to a massive 757 pages of thoroughly explained, useful Perl code. In this book, Tom Christiansen and Nathan Torkington cover many topics providing numerous code examples known as recipes (it is a “cookbook”, after all). The introduction claims it is not a tutorial nor a reference, but rather states “this is a book for learning more Perl”. And so it is.

This is a book for all types of Perl programmers from beginner to expert. If you are looking for a book with hundreds of examples to cut and paste, or something that explains in detail how to do many different things using Perl, this book is for you.

The book starts with recipes to handle Perl's basic data types. Each of the following five topics gets its own chapter: strings, numbers, dates and times, arrays and hashes.

Chapter 6 discusses pattern matching and goes into great detail on using Perl's regular expressions.

Chapters 7, 8 and 9 deal with file access, file contents and directories. Chapter 10 discusses subroutines (check out the nifty programs to sort e-mail at the end of this chapter).

Chapters 11, 12 and 13 discuss references, packages, libraries, modules, classes, object and ties. These topics are traditionally difficult to master with Perl, yet I found these chapters to be well-written and understandable. I was particularly impressed with the section on using tie at the end of Chapter 13.

Chapter 14 covers database access, including UNIX DBM databases and the DBI module. Chapter 15 discusses user interfaces from the terminal, the Tk interface and the Expect programming language.

The remaining five chapters discuss networking with Perl. Topics include process management and communication, sockets, Internet services, CGI programming and web automation.

I have found this book to be extremely helpful, technically excellent and loaded with useful and usable source code. For example, I recently wrote a CGI program that accepts a floating-point number from the user, and I wanted to use a regular expression to verify that the number entered was valid. I thought to myself, “Hmm, I can either create the regular expression myself, or see if this issue is covered in the Perl Cookbook.” Noting that “Laziness” is one of the three primary virtues of a Perl programmer, I decided to look in the book. In the table of contents, I immediately found a recipe titled “Checking Whether a String Is a Valid Number”. Sensing I was on to something, I turned to page 44 and found the following regular expression to verify correct C-style floating point numbers:

/^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/

I suppose that, with a bit of time and a lot of testing, I could have come up with a working regular expression. But I didn't need to—the answer was found in the Perl Cookbook.

The Perl Cookbook has the answers to many common Perl questions. Need to find all unique entries in a list? Try page 102. Want to create a hash, but retrieve the key/value pairs from the hash in the order entered? Turn to page 139. Want to POP your e-mail from your server? See page 656.

I especially liked Chapter 20, entitled “Web Automation”. Did you know it takes only five lines of Perl code to submit a form to a CGI program using the GET method? This example is from page 710:

use LWP::Simple;
use URI::URL;
my $url =
url('http://www.perl.com/cgi-bin/cpan_mod');
$url->query_form(module => 'DB_File', readme => 1);
$content = get($url);

Mirroring a web page takes only two lines of code (from page 724):

use LWP::Simple;
mirror($URL, $local_filename);
Code like this makes Perl the world's most useful programming language. Examples like these make the Perl Cookbook the most useful Perl book I own.

The book contains a mountain of source code, all of which is available from the O'Reilly FTP site (ftp://ftp.oreilly.com/published/oreilly/perl/cookbook/). There, you can find over 130 full-length programs as well as all of the code snippets from the book.

Since this is a cookbook covering a number of topics, readers looking for in-depth discussions of the topics mentioned above should be aware of the fact that they are not covered in minute detail. For instance, the chapter on CGI programming is a brief description that will be excellent if you have some knowledge of CGI, but if you are looking for in-depth discussion, you should check out a CGI book. This makes sense—cookbooks are not tutorials. They present recipes, with the assumption that you know the basics.

In summary, if you are a Perl programmer, this book is essential. If you are new to Perl and want to become a Perl programmer, this book is highly recommended as a tool to learn more Perl. If you are wondering what Perl is all about and what Perl can do for you, this book deserves a look. If you haven't heard of Perl, where have you been?

James Lee is the president and founder of Onsight (http://www.onsight.com/). When he is not teaching Perl classes or writing Perl code on his Linux machine, he likes to spend time with his kids, root for the Northwestern Wildcats (it was a long season), and daydream about his next climbing trip. He also likes to receive e-mail at james@onsight.com.