Ten years ago, I wrote the first edition of Learning Perl. In the intervening years, Perl itself has grown substantially from a “cool” scripting language used primarily by Unix system administrators to a robust object-oriented programming language that runs on practically every computing platform known to mankind.
Throughout its three editions, Learning Perl remained the same size (about 300 pages) and continued to cover much of the same material to remain compact and accessible to the beginning programmer. But there is much more to learn about Perl than there was ten years ago.
This book may be entitled Learning Perl Objects, References, and Modules, but I like to think of it as just Learning More Perl. This is the book that picks up where Learning Perl leaves off. It shows how to use Perl to write larger programs.
As in Learning Perl, each chapter in this book is designed to be small enough to read in just an hour or two. Each chapter ends with a series of exercises to help you practice what you’ve just learned, with the answers in the Appendix for your reference. And like Learning Perl, the material in this book was developed for a teaching environment and used in that setting, including for our own use at Stonehenge Consulting Services as we present onsite and open-enrollment trainings.
You don’t have to be a Unix guru, or even a Unix user, to benefit from this book. Unless otherwise noted, everything in this book applies equally well to Windows ActivePerl from ActiveState, and all other modern implementations of Perl. To use this book, you just need to be familiar with the material in Learning Perl and have the ambition to go further.
It’s a good idea to read this book from front to back, stopping to do the exercises. Each chapter builds on preceding chapters. You’ve been warned.
- Chapter 1
An introduction to the material.
- Chapter 2
How to bring code in from separate files so you can have others do some of your work for you.
- Chapter 3
How to allow the same code to operate on different data structures by introducing a level of indirection.
- Chapter 4
How Perl manages to keep track of pointers to data, and an introduction to anonymous data structures and autovivification.
- Chapter 5
Viewing, searching, and storing nested arrays and hashes.
- Chapter 6
How to capture behavior as a value to be passed around.
- Chapter 7
Sorting complex operations, the “Schwartzian Transform,” and working with recursively defined data.
- Chapter 8
Working with classes, method calls, inheritance, and overriding.
- Chapter 9
Adding per-instance data, including constructors, getters, and setters.
- Chapter 10
Adding behavior to an object that is going away, including object persistence.
- Chapter 11
Multiple inheritance, automatic methods, and references to filehandles.
- Chapter 12
useworks, from the user’s and author’s perspectives.
- Chapter 13
Packaging up a module for sharing, including portable installation instructions.
- Chapter 14
Providing unit and integration tests with your distribution.
- Chapter 15
Submitting your module to the CPAN.
- Appendix A
Where to go to get answers.
The following typographic conventions are used in this book:
Used for function names, module names, filenames, environment variables, code snippets, and other literal text
Used for emphasis and for new terms where they are defined
Please address comments and questions concerning this book to the publisher:
|O’Reilly & Associates, Inc.|
|1005 Gravenstein Highway North|
|Sebastopol, CA 95472|
|(800) 998-9938 (in the United States or Canada)|
|(707) 829-0515 (international/local)|
|(707) 829-0104 (fax)|
There is a web page for this book, which lists errata, examples, or any additional information. You can access this page at:
To comment or ask technical questions about this book, send email to:
For more information about books, conferences, Resource Centers, and the O’Reilly Network, see the O’Reilly web site at:
In the preface of the first edition of Learning Perl, I acknowledged the Beaverton McMenamin’s Cedar Hills Pub just down the street from my house for the “rent-free booth-office space” while I wrote most of the draft on my Powerbook 140. Well, like wearing your lucky socks every day when your favorite team is in the playoffs, I wrote nearly all of this book (including these words) at the same brewpub, in hopes that the light of success of the first book will shine on me twice.
This McM’s has the same great local microbrew beer and greasy sandwiches, but they’ve gotten rid of my favorite pizza bread, replacing it with new items like marionberry cobbler (a local treat) and spicy jambalaya. (And they added two booths, and put in some pool tables.) Also, instead of the Powerbook 140, I’m using a Titanium Powerbook, with 1,000 times more disk, 500 times more memory, and a 200-times-faster CPU running a real Unix-based operating system (OSX) instead of the limited MacOS. I also uploaded all of the draft sections (including this one) over my 144K cell-phone modem and emailed them directly to the reviewers, instead of having to wait to rush home to my 9600-baud external modem and phone line. How times have changed!
So, thanks once again to the staff of the McMenamin’s Cedar Hills Pub for the booth space and hospitality.
Like the third edition of Learning Perl, I also owe much of what I’m saying here and how I’m saying it to the decade of students at Stonehenge Consulting Services who have given me immediate precise feedback (by their glazed eyes and awkwardly constructed questions) when I was exceeding the “huh?” factor threshold. With that feedback over many dozens of presentations, I was able to keep refining and refactoring the materials that paved the way for this book.
Speaking of which, those materials started as a half-day “What’s new in Perl 5?” summary commissioned by Margie Levine of Silicon Graphics, in addition to my frequently presented onsite four-day Llama course (targeted primarily for Perl Version 4 at the time). Eventually, I got the idea to beef up those notes into a full course and enlisted fellow Stonehenge presenter Joseph Hall for the task. (He’s the one that selected the universe from which the examples are drawn.) Joseph developed a two-day course for Stonehenge in parallel with his excellent Effective Perl Programming book, which we then used as the course textbook (until now).
Other Stonehenge instructors have also dabbled a bit in the
“Packages, References, Objects, and
Modules” course over the years, including Chip
“every source line of the Perl compiler
“don’t mess with my
foy, and Tad “something clever
this way comes” McClellan. But the bulk of the
recent changes has been the responsibility of my senior trainer Tom
Phoenix, who has been “Stonehenge employee of the
month” so often that I may have to finally give up
my preferred parking space. Tom manages the materials (just as Tad
manages operations) so I can focus on being the president and the
janitor of Stonehenge. And since I’m naming the
Stonehenge crew, I can’t forget my wacky party
manager and marketing consultant (and longtime friend) Bill Harp, who
at this very moment is planning yet another legendary Stonehenge
OSCON party (including the premiere of the book
you’re now reading).
Tom Phoenix contributed most exercises in this book and a timely set of review notes during my writing process, including entire paragraphs for me to just insert in place of the drivel I had written. We work well as a team, both in the classroom and in our joint writing efforts. It is for this effort that we’ve acknowledged Tom as a coauthor, but I’ll take direct blame for any parts of the book you end up hating: none of that could have possibly been Tom’s fault.
I also appreciate my technical reviewers, Mike Stok, Joe Johnston, Paul Grassie, Damian Conway, Neil Bauman, and David H. Adler, for their constructive feedback and kind words, although I really was expecting to be beat up a bit more in the comments. Maybe the time limit kept y’all nice.
And I especially appreciate and acknowledge Linda Mui of O’Reilly, who has shepherded this project through from the beginning, when Tom and I suggested at OSCON 2001 that our next book should be a sequel, and then made it so.
Of course, a book is nothing without a subject and a distribution channel, and for that I must acknowledge longtime associates Larry Wall and Tim O’Reilly. Thanks guys, for creating an industry that has paid for my toys and essentials for over a decade.
And, as always, a special thanks to Lyle and Jack for teaching me nearly everything I know about writing and convincing me that I was much more than a programmer who might learn to write: I was also a writer who happened to know how to program. Thank you.
And to you, the reader of this book, for whom I toiled away the countless hours while sipping a cold microbrew and scarfing down a piece of incredible cheesecake, trying to avoid spilling on my laptop keyboard: thank you for reading what I’ve written. I sincerely hope I’ve contributed (in at least a small way) to your Perl proficiency. If you ever meet me on the street, please say “Hi.” I’d like that. Thank you.