Preface

Welcome to the third edition of Learning Perl.

If you’re looking for the best way to spend your first 30 to 45 hours with the Perl programming language, look no further. In the pages that follow, you’ll find a carefully paced introduction to the language that remains the workhorse of the Internet, as well as the language of choice for system administrators, web hackers, and casual programmers around the world.

We can’t give you all of Perl in just a few hours. The books that promise that are probably fibbing a bit. Instead, we’ve carefully selected a complete and useful subset of Perl for you to learn, good for programs from one to 128 lines long, which end up being about 90% of the programs in use out there. And when you’re ready to go on, we’ve included a number of pointers for further education.

Each chapter is small enough to be read in 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 Appendix A for your reference. Thus, this book is ideally suited for a classroom “introduction to Perl” course. We know this directly, because the material for this book was lifted almost word-for-word from our flagship “Learning Perl” course, delivered to thousands of students around the world. However, we’ve designed the book for self-study as well.

Although Perl lives as the “toolbox for Unix,” you don’t have to be a Unix guru, or even a Unix user, to use this book. Unless otherwise noted, everything we’re saying applies equally well to Windows ActivePerl from ActiveState, as well as to the later releases of MacPerl for the Macintosh and pretty much every other modern implementation of Perl.

Although you don’t need to know a single bit about Perl to begin reading this book, we do recommend that you already have familiarity with basic programming concepts such as variables, loops, subroutines, and arrays, and the all-important “editing a source code file with your favorite text editor.” We won’t spend any time trying to explain those concepts. Although we’re pleased that we’ve had many reports of people successfully picking up Learning Perl and grasping Perl as their first programming language, of course we can’t promise the same results for everyone.

Typographical Conventions

The following font conventions are used in this book:

Constant width

is used for method names, function names, variables, and attributes. It is also used for code examples.

Constant width bold

is used to indicate user input.

Constant width italic

is used to indicate a replaceable item in code (e.g., filename, where you are supposed to substitute an actual filename).

Italic

is used for filenames, URLs, hostnames, commands in text, important words on first mention, and emphasis.

Footnotes

are used to attach parenthetical notes that you should not read on your first (or perhaps second or third) reading of this book. Sometimes lies are spoken to simplify the presentation, and the footnotes restore the lie to truth. Often the material in the footnote will be advanced material not even discussed anywhere else in the book.

How to Contact Us

We have tested and verified all the information in this book to the best of our abilities, but you may find that features have changed or that we have let errors slip through the production of the book. Please let us know of any errors that you find, as well as suggestions for future editions, by writing to:

O’Reilly & Associates, Inc.
101 Morris St.
Sebastopol, CA 95472
1-800-998-9938 (in the U.S. or Canada)
1-707-829-0515 (international/local)
1-707-829-0104 (fax)

You can also send messages electronically. To be put on our mailing list or to request a catalog, send email to:

To ask technical questions or to comment on the book, send email to:

We have a web site for the book, where we’ll list examples, errata, and any plans for future editions. It also offers a downloadable set of text files (and a couple of Perl programs) which are useful, but not required, when doing some of the exercises. You can access this page at:

http://www.oreilly.com/catalog/lperl3/

For more information about this book and others, see the O’Reilly web site:

http://www.oreilly.com

Code Examples

You are invited to copy the code in the book and adapt it for your own needs. Rather than copying by hand, however, we encourage you to download the code from http://www.oreilly.com/catalog/lperl3/.

History of This Book

For the curious, here’s how Randal tells the story of how this book came about:

After I had finished the first Programming Perl book with Larry Wall (in 1991), I was approached by Taos Mountain Software in Silicon Valley to produce a training course. This included having me deliver the first dozen or so courses and train their staff to continue offering the course. I wrote the course for them[1] and delivered it for them as promised.

On the third or fourth delivery of that course (in late 1991), someone came up to me and said, “you know, I really like Programming Perl, but the way the material is presented in this course is so much easier to follow—you oughta write a book like this course.” It sounded like an opportunity to me, so I started thinking about it.

I wrote to Tim O’Reilly with a proposal based on an outline that was similar to the course I was presenting for Taos—although I had rearranged and modified a few of the chapters based on observations in the classroom. I think that was my fastest proposal acceptance in history—I got a message from Tim within fifteen minutes saying “we’ve been waiting for you to pitch a second book—Programming Perl is selling like gangbusters.” That started the effort over the next eighteen months to finish the first edition of Learning Perl.

During that time, I was starting to see an opportunity to teach Perl classes outside Silicon Valley[2], so I created a class based on the text I was writing for Learning Perl. I gave a dozen classes for various clients (including my primary contractor, Intel Oregon), and used the feedback to fine-tune the book draft even further.

The first edition hit the streets on the first day of November, 1993[3] and became a smashing success, frequently even outpacing Programming Perl book sales.

The back-cover jacket of the first book said “written by a leading Perl trainer.” Well, that became a self-fulfilling prophesy. Within a few months, I was starting to get email from all over the United States from people asking to have me teach at their site. In the following seven years, my company became the leading worldwide on-site Perl training company, and I had personally racked up (literally) a million frequent-flier miles. It didn’t hurt that the Web started taking off about then, and the webmasters and webmistresses picked Perl as the language of choice for content management, interaction through CGI, and maintenance.

For the past two years, I’ve been working closely with Tom Phoenix in his role as lead trainer and content manager for Stonehenge, giving him charter to experiment with the “Llama” course by moving things around and breaking things up. When we had come up with what we thought was the best major revision of the course, I contacted O’Reilly and said “it’s time for a new book!” And now you’re reading it.

Some of the differences you may notice from prior editions:

  • The text is completely new. Rather than simply copy-and-paste from previous editions, we have derived the text from our Stonehenge “Learning Perl” courseware and the instructor notes we’ve created and road-tested. (Some of the exercises are similar to the originals simply because we were using the prior editions as our textbook until recently. But even those have mutated during the rewrites.)

  • We’ve broken the hard-to-swallow-all-at-once regular expressions section into three easily digestible sections.

  • We’ve created exercises with both Unix and Windows in mind.

  • We got rid of the artificial “control structures” chapter, moving the while and if statement earlier, and the foreach and for loops later. This gives us more useful examples and exercises for the scalars chapter, for example.

  • We moved subroutines much earlier to permit subsequent exercises to use subroutines for the questions and answers.

  • We now teach element syntax before the aggregate syntax for both arrays and hashes. This has worked a bit of a miracle in the classrooms, since it nearly always keeps beginners from the all-too-common mistake of writing a slice where they mean an element. At the risk of hubris, we’ll admit that we expect other Perl instructors and books to follow our lead here.

  • The exercises are more real-world and better paced.

  • We’ve included information on use strict, warnings, and modules, although mostly as pointers for further information.

  • We’ve made the book much less addressed to the Unix system administrator, and much more to the general programmer. The phrase “like C” has been nearly completely eliminated.

  • The jokes are better. (We’re constantly improvising jokes in the classroom, and some of these end up as part of the standard Stonehenge script. The best of those ended up here. You should see what didn’t make the cut!)

  • We deeply regret that this edition lacks the wonderfully witty Foreword, written by Larry Wall, who was busy defining Perl 6 as we went to press. Larry is always supportive of our efforts, and we know that he’s still part of the book in spirit, if not in word, to wish you the best as you start your holiday in the lustrous land of Perl.

Acknowledgments

From Randal. I want to thank the Stonehenge trainers past and present (Joseph Hall, Tom Phoenix, Chip Salzenberg, brian d foy, and Tad McClellan) for their willingness to go out and teach in front of classrooms week after week and to come back with their notes about what’s working (and what’s not) so we could fine-tune the material for this book. I especially want to single out my co-author and business associate, Tom Phoenix, for having spent many, many hours working to improve Stonehenge’s Llama course and to provide the wonderful core text for most of this book.

I also want to thank everyone at O’Reilly, especially our very encouraging and patient editor, Linda Mui, and Tim O’Reilly himself for taking a chance on me in the first place with the Camel and Llama books.

I am also absolutely indebted to the thousands of people who have purchased the past editions of the Llama so that I could use the money to stay “off the streets and out of jail,” and to those students in my classrooms who have trained me to be a better trainer, and to the stunning array of Fortune 1000 clients who have purchased our classes in the past and will continue to do so into the future.

As always, a special thanks to Lyle and Jack, for teaching me nearly everything I know about writing. I won’t ever forget you guys.

From Tom. I’ve got to echo Randal’s thanks to our editor, Linda Mui, for her patience in pointing out which jokes and footnotes were most excessive, while pointing out that she is in no way to blame for the ones that remain. Both she and Randal have guided me through the process of writing, and I am grateful.

And another echo with regard to Randal and the other Stonehenge trainers, who hardly ever complained when I unexpectedly updated the course materials to try out a new teaching technique. You folks have contributed many different viewpoints on teaching methods that I would never have seen.

For many years, I worked at the Oregon Museum of Science and Industry (OMSI), and I’d like to thank the folks there for letting me hone my teaching skills as I learned to build a joke or two into every activity, explosion, or dissection.

To the many folks on Usenet who have given me your appreciation and encouragement for my contributions there, thanks. As always, I hope this helps.

Of course, deep thanks are due especially to my co-author, Randal, for giving me the freedom to try various ways of presenting the material both in the classroom and here in the book, as well as for the push to make this material into a book in the first place. And without fail, I must say that I am indeed inspired by your on-going work to ensure that no one else becomes ensnared by the legal troubles that have stolen so much of your time and energy; you’re a fine example.

From Both of Us. Thanks to our reviewers (Elaine Ashton, Kevin Bingham, Jarkko Hietaniemi, Joe Johnston, and Ben Tilly) for providing comments on the draft of this book.

Thanks also to our many students who have let us know what parts of the course material have needed improvement over the years. It’s because of you that we’re both so proud of it today.

Thanks to the many Perl Mongers who have made us feel at home as we’ve visited your cities. Let’s do it again sometime.

And finally, our sincerest thanks to our friend Larry Wall, for having the wisdom to share his really cool and powerful toys with the rest of the world so that we can all get our work done just a little bit faster, easier, and with more fun.



[1] In the contract, I retained the rights to the exercises, hoping someday to reuse them in some other way, like in the magazine columns I was writing at the time. The exercises are the only things that leapt from the Taos course to the book.

[2] My Taos contract had a no-compete clause, so I had to stay out of Silicon Valley with any similar courses, which I respected for many years.

[3] I remember that date very well, because it was also the day I was arrested at my home for computer-related-activities around my Intel contract, a series of felony charges for which I was later convicted. The appeals battle continues—see http://www.lightlink.com/fors/for details.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset