Supplemental files and examples for this book can be found at http://examples.oreilly.com/9780596004781/. Please use a standard desktop web browser to access these files, as they may not be accessible from all ereader devices.
All code files or examples referenced in the book will be available online. For physical books that ship with an accompanying disc, whenever possible, weve posted all CD/DVD content. Note that while we provide as much of the media content as we are able via free download, we are sometimes limited by licensing restrictions. Please direct any questions or concerns to .
Foreword
Perls object-oriented mechanism is classicprestidigitation. It takes a collection of Perlsexisting non-OO features such as packages, references, hashes,arrays, subroutines, and modules, and thenwith nothing up itssleevemanages to conjure up fully functional objects, classes,and methods. Seemingly out of nowhere.
Thats a great trick. It means you can build on yourexisting Perl knowledge and ease your way into OO Perl development,without first needing to conquer a mountain of new syntax or navigatean ocean of new techniques. It also means you can progressivelyfine-tune OO Perl to meet your own needs, by selecting from theexisting constructs the one that best suits your task.
But theres a problem. Since Perl co-opts packages,references, hashes, arrays, subroutines, and modules as the basis ofits OO mechanism, to use OO Perl you already need to understandpackages, references, hashes, arrays, subroutines, and modules.
And theres the rub. The learning curvehasnt been eliminated; its merelybeen pushed back half a dozen steps.
So then: how are you going to learn everything you need to know aboutnon-OO Perl so you can start to learn everything you need to knowabout OO Perl?
This book is the answer. In the following pages, Randal draws on twodecades of using Perl, and four decades of watching Gilligans Island and Mr. Ed , to explain each of the components ofPerl that collectively underpin its OO features. And, better still,he then goes on to show exactly how to combine those components tocreate useful classes and objects.
So if you still feel like Gilligan when it comes toPerls objects, references, and modules, this bookis just what the Professor ordered.
And thats straight from thehorses mouth.
Damian Conway, May 2003
Preface
Ten years ago, I wrote the first edition of LearningPerl . In the intervening years, Perl itself has grownsubstantially from a coolscripting language used primarily by Unix system administrators to arobust object-oriented programming language that runs on practicallyevery computing platform known to mankind.
Throughout its three editions, Learning Perl remained the same size (about 300 pages) and continued to cover muchof the same material to remain compact and accessible to thebeginning programmer. But there is much more to learn about Perl thanthere 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 LearningPerl leaves off. It shows how to use Perl to write largerprograms.
As in Learning Perl , each chapter in this bookis designed to be small enough to read in just an hour or two. Eachchapter ends with a series of exercises to help you practice whatyouve just learned, with the answers in theAppendix for your reference. And like LearningPerl , the material in this book was developed for ateaching environment and used in that setting, including for our ownuse at Stonehenge Consulting Services as we present onsite andopen-enrollment trainings.
You dont have to be a Unix guru, or even a Unixuser, to benefit from this book. Unless otherwise noted, everythingin this book applies equally well to Windows ActivePerl fromActiveState, and all other modern implementations of Perl. To usethis book, you just need to be familiar with the material in Learning Perl and have the ambition to gofurther.
Structure of This Book
Its a good idea to read this book from front toback, stopping to do the exercises. Each chapter builds on precedingchapters. Youve been warned.
An introduction to the material.
How to bring code in from separate files so you can have others dosome of your work for you.
How to allow the same code to operate on different data structures byintroducing a level of indirection.
How Perl manages to keep track of pointers to data, and anintroduction to anonymous data structures and autovivification.
Viewing, searching, and storing nested arrays and hashes.
How to capture behavior as a value to be passed around.
Sorting complex operations, the SchwartzianTransform, and working with recursively defineddata.
Working with classes, method calls, inheritance, and overriding.
Adding per-instance data, including constructors, getters, andsetters.
Adding behavior to an object that is going away, including objectpersistence.
Multiple inheritance, automatic methods, and references tofilehandles.
How use
works, from the usersand authors perspectives.
Packaging up a module for sharing, including portable installationinstructions.
Providing unit and integration tests with your distribution.
Submitting your module to the CPAN.
Where to go to get answers.
Conventions Used in This Book
The following typographic conventions are used in this book:
Constant
width
Used for function names, module names, filenames, environmentvariables, code snippets, and other literal text
ItalicsUsed for emphasis and for new terms where they are defined
Comments and Questions
Please address comments and questions concerning this book to thepublisher:
OReilly & 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, orany additional information. You can access this page at:
http://www.oreilly.com/catalog/lrnperlorm |
To comment or ask technical questions about this book, send email to:
For more information about books, conferences, Resource Centers, andthe OReilly Network, see theOReilly web site at:
Acknowledgments
In the preface of the first edition of LearningPerl , I acknowledged the BeavertonMcMenamins Cedar Hills Pub just down the streetfrom my house for the rent-free booth-officespace while I wrote most of the draft on myPowerbook 140. Well, like wearing your lucky socks every day whenyour favorite team is in the playoffs, I wrote nearly all of thisbook (including these words) at the same brewpub, in hopes that thelight of success of the first book will shine on me twice.
This McMs has the same great local microbrew beerand greasy sandwiches, but theyve gotten rid of myfavorite pizza bread, replacing it with new items like marionberrycobbler (a local treat) and spicy jambalaya. (And they added twobooths, and put in some pool tables.) Also, instead of the Powerbook140, Im using a Titanium Powerbook, with 1,000times more disk, 500 times more memory, and a 200-times-faster CPUrunning a real Unix-based operating system (OSX) instead of thelimited MacOS. I also uploaded all of the draft sections (includingthis one) over my 144K cell-phone modem and emailed them directly tothe reviewers, instead of having to wait to rush home to my 9600-baudexternal modem and phone line. How times have changed!