Mimsy Were the Borogoves

Hacks: Articles about programming in Python, Perl, Swift, BASIC, and whatever else I happen to feel like hacking at.

Perl Cookbook

Jerry Stratton, September 12, 2001

When we switched from PH to LDAP, everyone else wanted to not support older e-mail readers. I grabbed the Perl Cookbook, found the simple telnet server, and quickly modified it to gateway between PH and LDAP.

At first glance, this is a huge book, with lots of ‘recipes’ inside for programming in Perl. A lot of the recipes appear to be trivially obvious--whereby “trivial”, I mean that it would take less time to write the solution myself than to find it in a 750 page book. Others are not, however, and no doubt what I consider trivial others do not, and what authors Christiansen and Torkington consider trivial, I do not. This is a lot like the kind of answers you would get posting questions to the net, except that these are (hopefully) working examples rather than created off-the-cuff with “debugging opportunity provided as an exercise.”

Sometimes it’s weird seeing familiar names in unfamiliar places. I remember Tom Christiansen from my Dungeons & Dragons days. He was one of the early crew at TSR and lived next door to Gary Gygax. He (as did I and probably a whole generation of coders) did some of his earliest programming writing character generation tools for role-playing games.

There is not, unfortunately, any of that in “Perl Cookbook”. There is just about everything else, however. I just opened it randomly to page 218, and found the “Regular Expression Grabbag”. Regular expressions for grabbing Roman numerals, grabbing dates and times, grabbing links from HTML. One of the sections I use most often is the sockets section, chapter 17. There’s “Writing a TCP Client” which implements a simple means of connecting to TCP/IP servers, and “Writing a TCP Server”, which implements a simple means of waiting for clients to connect over TCP/IP.

There is a small chapter on CGI programming, which includes recommendations to use the CGI module. But many of your CGI problems will have solutions in more specific areas. Browsing through the book (and if you’re a real Perl programmer, you’ll probably browse through this book simply for the fun of reading it; it makes better bathroom reading than most) I see in “Numbers” a recipe for generating weighted random numbers, for example “to randomly select a banner to display on a web page, given a set of relative weights saying how often each banner is to be displayed.”

There are sections on using TK to provide a more robust user interface, object-oriented programming in Perl, strings, numbers, dates and times (the same thing in Unix), arrays, hashes, pattern matching, file and directory access, subroutines, packages, databases, process management, sockets, and even web automation (automating access or updates to web pages). It provides a useful index for finding the solutions in these sections. (Just glancing through for this review, I discovered something to do with Expect in Perl, which could ease some problems I’ve been having using Expect in CGI programming.)

Something that would have been useful, which I don’t see, would be instructions on the use of CPAN to grab modules, since often the solutions presented here require or at least recommend the use of one or more non-standard modules. There’s a tiny section on installing modules that you’ve already downloaded, but nothing that I can find about using the command-line CPAN interface to more easily install them from scratch. (In their favor, it may be that when this book was published in late 1998, the CPAN interface had not yet been standardized.)

“Perl Cookbook” was created as a companion to the “Programming Perl” reference and tutorial (“the camel book”). The first edition of that book had chapters on “Common Tasks” and “Real Perl Programs”. Those chapters were removed from the revised camel due to space considerations. This is a monster expansion of those original two chapters. This means that it includes some incredibly simple “common tasks” as well as some more complex “real programs”.

If you use Perl regularly, you’ll probably find this useful. This is one of a rare breed of books that I purchase two copies for, one for the office and one for home. It was developed using Perl 5.004_04, so the examples will work best with that version or later.

  1. <- Javascript Guide
  2. HTML Definitive Guide ->