Channels ▼

Web Development

Review: Perl Cookbook

February, 2004: Perl Cookbook

Russell is a Perl programmer, MySQL developer, and web designer living and working on a consulting basis in New Orleans. He is also an adjunct instructor at a local college where he teaches Linux and other open-source software. He can be reached at [email protected]

Perl Cookbook, Second Edition
Tom Christiansen and Nathan Torkington
O'Reilly & Associates, 2003
1000 pp., $49.95
ISBN 0-596-00313-7

As a Perl programmer, you probably own or have access to O'Reilly's Perl Cookbook. It's an essential book for the advanced development of Perl skills. Thanks to the stability of Perl, this is one computer book that doesn't become obsolete very quickly. It was first published in the Summer of 1998 and is still useful today. If your copy of the Cookbook, however, is anything like some that I've seen belonging to senior Perlists—filled with post-it notes, notes written in the margins, highlighting and dog-eared pages throughout, and looking more than a little worn—you're probably due for a replacement. Fortunately, you now have an additional excuse to buy a fresh copy of the Perl Cookbook, since the second edition is out.

The Perl Cookbook is filled with dozens of common and somewhat uncommon dilemmas that one might encounter with Perl in daily life. Scenarios are laid out in clear language, then resolved with excellent explanations. Sometimes the solutions (or recipes) are straightforward and limited. Often times, though, the authors give more than one solution depending on what they imagine the reader may be seeking or may need. The result is a deeper understanding for the reader by way of more examples, and a greater likelihood that the nuances of your particular problem are addressed.

What's Different

The first edition of the Perl Cookbook is based on Perl 5.004.04. Because of the stability and the reverse compatibility of Perl, just about all of the first edition still applies. The authors have updated the text in the second edition for Perl 5.8.1. Many of the changes to old recipes are based on the newer version of Perl. But many of the changes were made to give greater clarity through expanded discussions, and to give the reader more examples since there's always more than one way to solve a problem in Perl. The 200 additional pages in the new edition are composed of changes to more than 100 recipes, as well as the inclusion of 80 new recipes. With that many changes, I can't list them all here. However, I will highlight several of them briefly.

Math fans will be pleased to find that a new recipe on named Unicode characters (1.5) has been added, as well as a recipe on normalizing similar Unicode characters (1.9) and treating them as octets (1.10). There's a new recipe to format text as title case (1.14). The recipes for trimming blank spaces from the end of strings (1.19), as well as the one for parsing comma-separated data (1.20) were expanded. Chapter 2 on numbers has some recipes that have been reworded and reworked, as well—including 2.2 on rounding floating-point numbers and 2.3 on comparing them. The recipes on converting binary, octal, and hexadecimal numbers have been rewritten and combined into one lengthy recipe (2.15).

There are new recipes in Chapter 8 on dealing with a file's contents: treating a file as an array (8.18); setting the default I/O layers (8.19); converting Microsoft text files into Unicode (8.21); comparing the contents of files (8.22); treating strings as files (8.23); and dealing with flat file indexes (8.27). In Chapter 9 on directories, a recipe has been included on how to handle symbolic file permissions instead of their octal values (9.11). In Chapter 10 on subroutines, a recipe has been introduced for creating a switch statement using the Switch module with the case command—a very handy way to consolidate multiple if and elsif statements into a clean format.

Chapter 11 on references has a new recipe for dealing with memory problems common in self-referential data structures (11.15). There's also a new recipe on using program outlines (11.16). Chapter 12 on packages, libraries, and modules has a new recipe that provides a solution for making a function private (12.5). There's another one on customizing warnings in your own Perl module (12.15). And Chapter 13 on objects has a new recipe using the dclone() function to give the user a copy method for a class.

In Chapter 14 on database accessing, there are several new recipes: escaping embedded quotes (14.10); handling database errors (14.11); setting up database queries within a loop statement (14.12); determining the number of rows returned by a database query (14.14); and displaying data retrieved one page at a time (14.16). Chapter 15 is on user interfaces, or rather, interactivity. There are several new recipes on this topic as well: graphing data (15.18); creating thumbnails of images (15.19); and adding text to an image (15.20).

Chapter 17 on sockets has an additional recipe on handling multiple clients from within a process using an operating system's threads (17.14). There's another on managing multiple inputs from unpredictable sources (17.19). Chapter 20 on web automation has many new recipes—one using cookies (20.14), another two on retrieving password-protected pages using LWP (20.15) and https pages (20.16), and two particularly good ones on parsing HTML (20.18) and on extracting data from an HTML table (20.19).

Finally, two new chapters have been added: Chapter 21 on mod-perl contains 17 recipes from authenticating to dealing with cookies to redirection. It has recipes on Apache logs, migrating from CGI to mod-perl, and working with the HTML::Mason perl module. Chapter 22 on XML, another new chapter, includes a quick introduction to XML, as well as a few lengthy recipes on parsing XML and validating XML gracefully. It also provides advice on searching an XML tree.


In summary, the authors and editors of the Perl Cookbook have managed to retain what is good and of value in the first edition. They've managed to fine tune the existing recipes for the latest version of Perl and have added many more recipes to keep up with the developing needs of Perl programmers. This was quite an undertaking on their part, and they've succeeded nicely.


Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.