Perl: The Programmer's Companion
In the December 2000 issue of Linux Journal, I reviewed the latest edition of the classic Perl text: Programming Perl, 3rd Edition (or The Camel for short). In that review, my advice to new Perl programmers was The Camel should be "the second Perl book they should buy. The first should be Nigel Chapman's Perl: The Programmer's Companion.
Written originally in 1997 (with the third and most recent printing in June 1998), Chapman's book is now somewhat long-in-the-tooth as Perl books go. Despite this fact, it remains one of the most readable introductions to the language. In 280 pages (organized in 11 chapters), the reader is taken on a journey that transforms them from Perl newcomer to Perl programmer.
Up first is the obligatory "why this book was written" preface, which includes details on the syntax diagrams used throughout the text to describe Perl's programming constructs. Syntax diagrams are very much a tool of computer scientists, and Nigel Chapman is proud to be one. His is a programming book for programmers. If you are new to programming, look to another book to get you started.
The journey begins with a short introductory chapter on Perl as a programming language--why Perl is useful (and worth using), how it differs from other programming languages, its relationship to CGI, its general characteristics and, of course, a discussion of why it is perfectly okay to do the same thing more than one way in Perl.
Chapter 2, "Simple Text Processing", dives right in. On page 11, Chapman treats the reader to an excellent tutorial on Perl's regular expression technology, and the tutorial runs for the next fifteen pages. Regular expressions support the use of Perl as a text processing language, and a rich collection of idioms are presented and explained in sufficient detail to give the reader a thorough grounding in all things pattern-matching. At the end of the chapter, filehandles are introduced (together with some sound words on comments and code layout). Amazingly, by the end of the first chapter, you will already know enough Perl to do some useful things.
Chapter 3, "Scalar Values and Variables", builds on the material from the previous chapter and, using some simple examples, explains the ins-and-outs of using Perl's "one of something" variable container: the scalar. Perl's in-built operators are explained, in addition to Perl's rather unique notion of "truth". Assignment, strings and variable interpolation are covered, too, and a small number of in-built subroutines typically used when working with scalars are presented. The chapter ends with two small, but complete, Perl programs that demonstrate the idioms seen thus far.
The author uses the "Flow of Control" chapter to describe the usual compound statements: if, while, until and for. The creation of user-defined subroutines, in addition to the do and eval in-built subroutines is also covered. The real-world example in this chapter involves building a small compiler/translator that was actually employed in the production of the text. This example may be a little involved for some, but it does showcase Perl and consolidate the material covered in the first four chapters.
In addition to the "one of something" scalar, Perl provides great support for "collection of something" variable containers. Chapter 5, "Lists and Hashes", covers the creation and use of Perl's in-built lists, arrays and hashes. A collection of related in-built subroutines are also described, and the chapter is littered with good examples and code snippets. With arrays covered, the author returns to subroutines and explains how arrays are used to support subroutine arguments. The foreach statement is also described, as its use is almost exclusively associated with Perl arrays.
File I/O, formats and directory access are the subject of Chapter 6, "Input, Output and Files". Again, there's plenty of code here, and some small, complete examples support the discussion of these topics. The use of pack and unpack in-built subroutines are described in relation to the processing of binary data within Perl programs.
By the end of Chapter 6, readers of this book should already be competent Perl programmers. Certainly, enough material has been covered to do quite a bit of work in Perl. The next three chapters introduce what I would typically describe as advanced features of the language. Chapter 7, "References and Data Structures", does a good job of presenting one of the most confusing parts of Perl: reference syntax. A data structure example, half way through the chapter, presents the Perl implementation of an algorithm from Donald Knuth's "The Art of Computer Programming". The code is surprisingly short and easy to follow. The latter part of the chapter covers references to subroutines and their uses. Chapter 8, "Objects", presents the Perl object-oriented features. It is not a detailed treatment, but it's enough to give a flavor of how OO works in Perl. A small class is developed, together with appropriate documentation in Perl's POD markup technology. Chapter 9, "Using Modules", describes the other major code reuse mechanism in Perl: modules. There's a light treatment of how to create a custom module from scratch, and a brief review of the Perl pragmas of most interest to module writers. However, the bulk of the chapter is dedicated to surveying a batch of third-party add-on modules to Perl. Topics covered include: configuration, code timing, date manipulation, OO filehandles and persistent hashes. The chapter concludes with a short description of the standard Perl library and the CPAN.
Chapter 10, "Perl and the World-Wide Web", presents a tutorial on CGI programming for the uninitiated. Some simple CGI's are created before the author presents a more complicated HTML forms-processing example. A very brief discussion of other networking and Internet modules rounds out the chapter.
The final chapter provides pointers on where to look on-line for more information on Perl. A number of books are also recommended. In evaluating Perl, the author describes Perl as it relates to other programming languages that came before and influenced Perl: Snobol and Awk. Perl's historical relationship to UNIX is also presented, as is a comparison to TCL and Python. In the final section, entitled "Hats Off to Larry?", the author begins the section with what has become one of my favorite sentences describing Perl: "Sometimes, it may seem as if Perl deliberately trails its coat in front of the mainstream programming languages, trying to start a fight." The author concludes with a honest assessment of Perl, warts and all.
Perl: The Programmer's Companion is rounded off with an epilogue, a set of exercises and index. The Epilogue documents a (fictitious) conversation between three programming language experts: Ed, Harry and Bernie. It is left as a exercise to the reader to work out who the three experts actually are. The Exercices, available as part of the third printing, cover chapters 2 through 11. They are of sufficient depth and difficulty to keep even the most confident Perl programmer busy for quite some time. The author suggests, rather tongue-in-cheek, that readers "re-do all the above exercises in C++ or Java" as the sole exercise for Chapter 11.
So, is this the perfect introduction to Perl? To be honest, it is not far off. If I was to get picky, I'd have to say the authors use of the use English pragma throughout all of his code annoys me, but that's a personal preference. If Perl has taught me anything, it's that it is quite alright to do things in Perl in whatever way suits you. It would also be remiss of me not to mention that Perl (the programming language) has changed a little since Nigel Chapman wrote his book. Some of the newest features are (obviously) not covered in this text. It is one of my secret hopes that Mr. Chapman revise Perl: The Programmer's Companion when Perl 6 is released (hopefully, sometime next year).
I first read Perl: The Programmer's Companion in 1999. I credit this book as being the one that really turned me onto Perl and made me a Perl programmer. As part of my courses at The Institute of Technology, Carlow (Ireland), I now include a whistle-stop tour of Perl, and Chapmans's is the book I tell my students to buy (first). I've gone on to read more Perl books than I care to mention, but, despite this, I often find myself returning and referring to Perl: The Programmer's Companion.
But don't take my word for it. The authors of Programming Perl (O'Reilly 2000) and Perl Cookbook (O'Reilly 1998)--two of the most revered titles in the Perl universe--include Nigel Chapman's book in their list of recommended texts for newcomers. Tom Christiansen refers to the book as nothing short of "delightful". This is high praise indeed from Perl's inner circle.
Paul Barry (firstname.lastname@example.org) lectures in computer networking at The Institute of Technology, Carlow, in Ireland (www.itcarlow.ie). He is the author of Programming the Network with Perl, which will be published by John Wiley & Sons.
Practical books for the most technical people on the planet. Newly available books include:
- Agile Product Development by Ted Schmidt
- Improve Business Processes with an Enterprise Job Scheduler by Mike Diehl
- Finding Your Way: Mapping Your Network to Improve Manageability by Bill Childers
- DIY Commerce Site by Reven Lerner
Plus many more.