Perl Cookbook

April 1st, 1999 by James Lee in

In this book, Tom Christiansen and Nathan Torkington cover many topics providing numerous code examples known as recipes (it is a “cookbook”, after all).
Your rating: None
  • 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.

__________________________


Special Magazine Offer -- 2 Free Trial Issues!
Receive 2 free trial issues of Linux Journal as well as instant online access to current and past issues. There's NO RISK and NO OBLIGATION to buy. CLICK HERE for offer

Linux Journal: delivering readers the advice and inspiration they need to get the most out of their Linux systems since 1994.

Sorry, offer available in the US only. International orders, click here.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

linux

On October 2nd, 2007 enigma cross of changes mp3 (not verified) says:

ported Has Linux? written / XXX anyone compiled for . Best regards.

Featured Videos

Email is one of the least private and least secure forms of communication, although few people realize this. MixMaster is one way to allow secure, anonymous communication even over the very public medium of email. This tutorial will get you started with MixMaster quickly and easily.

In case you were wondering about the fun side of Linux World Expo, we thought we'd give you a peek at our shenanigans. We at Linux Journal love what we do so much, that we can't help but have a ball wherever we go.

From the Magazine

September 2008, #173

Feeling a bit like a Thermian? Never give up, never surrender! Someday, you could go from underdog to top dog. Just take a look at a few of the underdogs we highlight in this issue: Mutt, djbdns, Nginix, Gentoo, Xara and the program voted mostly likely to fail just a few years back—Firefox. If Firefox is not radical enough for you, check out Chef Marcel's column for some more alternatives. Having trouble mapping your program data to your relational database? If so, Rueven Lerner shows you some tricks in his At The Forge column.

Need to run GUI applications on your server in the next state? In his Paranoid Penguin column, Mick Bauer shows you how to do it securely. Kyle Rankin keeps hacking and slashing and shows you a few split screen secrets you may not be familiar with. Finally, we all know what happens next February, but only Doc knows what happens afterward.

Read this issue