O'Reilly logo

Mastering Perl by brian d foy

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required


Mastering Perl is the third book in the series starting with Learning Perl, which taught you the basics of Perl syntax, progressing to Intermediate Perl, which taught you how to create reusable Perl software, and finally this book, which pulls everything together to show you how to bend Perl to your will. This isn’t a collection of clever tricks, but a way of thinking about Perl programming so you integrate the real-life problems of debugging, maintenance, configuration, and other tasks you’ll encounter as a working programmer. This book starts you on your path to becoming the person with the answers, and, failing that, the person who knows how to find the answers or discover the problem.

Structure of This Book

Chapter 1, Introduction: Becoming a Master

An introduction to the scope and intent of this book.

Chapter 2, Advanced Regular Expressions

More regular expression features, including global matches, lookarounds, readable regexes, and regex debugging.

Chapter 3, Secure Programming Techniques

Avoid some common programing problems with the techniques in this chapter, which covers taint checking and gotchas.

Chapter 4, Debugging Perl

A little bit about the Perl debugger, writing your own debugger, and using the debuggers others wrote.

Chapter 5, Profiling Perl

Before you set out to improve your Perl program, find out where you should concentrate your efforts.

Chapter 6, Benchmarking Perl

Figure out which implementations do better on time, memory, and other metrics, along with cautions about what your numbers actually mean.

Chapter 7, Cleaning Up Perl

Wrangle Perl code you didn’t write (or even code you did write) to make it more presentable and readable by using Perl::Tidy or Perl::Critic.

Chapter 8, Symbol Tables and Typeglobs

Learn how Perl keeps track of package variables and how you can use that mechanism for some powerful Perl tricks.

Chapter 9, Dynamic Subroutines

Define subroutines on the fly and turn the tables on normal procedural programming. Iterate through subroutine lists rather than data to make your code more effective and easy to maintain.

Chapter 10, Modifying and Jury-Rigging Modules

Fix code without editing the original source so you can always get back to where you started.

Chapter 11, Configuring Perl Programs

Let your users configure your programs without touching the code.

Chapter 12, Detecting and Reporting Errors

Learn how Perl reports errors, how you can detect errors Perl doesn’t report, and how to tell your users about them.

Chapter 13, Logging

Let your Perl program talk back to you by using Log4perl, an extremely flexible and powerful logging package.

Chapter 14, Data Persistence

Store data for later use in other programs, a later run of the same program, or to send as text over a network.

Chapter 15, Working with Pod

Translate plain ol’ documentation into any format that you like, and test it, too.

Chapter 16, Working with Bits

Use bit operations and bit vectors to efficiently store large data.

Chapter 17, The Magic of Tied Variables

Implement your own versions of Perl’s basic data types to perform fancy operations without getting in the user’s way.

Chapter 18, Modules As Programs

Write programs as modules to get all of the benefits of Perl’s module distribution, installation, and testing tools.

Appendix A

Explore these resources to continue your Perl education.

Appendix B

My popular step-by-step guide to solving any Perl problem. Follow these steps to improve your troubleshooting skills.

Conventions Used in This Book

The following typographic conventions are used in this book:

Constant width

Used for function names, module names, environment variables, code snippets, and other literal text


Used for emphasis, Perl documentation, filenames, and for new terms where they are defined

Using Code Examples

This book is here to help you get your job done. In general, you may use the code in this book in your programs and documentation. You do not need to contact O’Reilly for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission.

We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: “Mastering Perl by brian d foy. Copyright 2007 O’Reilly Media, Inc., 978-0-596-52724-2.”

If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at permissions@oreilly.com.

Safari® Enabled


When you see a Safari® Enabled icon on the cover of your favorite technology book, that means the book is available online through the O’Reilly Network Safari Bookshelf.

Safari offers a solution that’s better than e-books. It’s a virtual library that lets you easily search thousands of top tech books, cut and paste code samples, download chapters, and find quick answers when you need the most accurate, current information. Try it for free at http://safari.oreilly.com.

Comments and Questions

Please address comments and questions concerning this book to the publisher:

O’Reilly Media, 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)

The web page for this book, which lists errata, examples, or any additional information, can be found at:


Additionally, while the book was being written, the author maintained a web site for this book where you can find additional information, including links to related resources and possible updates to the book:


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:



Many people helped me during the year I took to write this book. The readers of the Mastering Perl mailing list gave constant feedback on the manuscript and sent patches, which I mostly applied as is, including those from Andy Armstrong, David H. Adler, Renée Bäcker, Anthony R. J. Ball, Daniel Bosold, Alessio Bragadini, Philippe Bruhat, Katharine Farah, Shlomi Fish, David Golden, Bob Goolsby, Ask Bjørn Hansen, Jarkko Hietaniemi, Joseph Hourcle, Adrian Howard, Offer Kaye, Stefan Lidman, Eric Maki, Josh McAdams, Florian Merges, Jason Messmer, Thomas Nagel, Xavier Noria, Les Peters, Bill Riker, Yitzchak Scott-Thoennes, Ian Sealy, Sagar R. Shah, Alberto Simões, Derek B. Smith, Kurt Starsinic, Adam Turoff, David Westbrook, and Evan Zacks. I’m quite reassured that their constant scrutiny kept me on the right path.

Tim Bunce provided gracious advice about the profiling chapter, which includes DBI::Profile, and Jeffrey Thalhammer updated me on the current developments with his Perl::Critic module.

Perrin Harkins, Rob Kinyon, and Randal Schwartz gave the manuscript a thorough beating at the end, and I’m glad I chose them as technical reviewers because their advice is always spot on.

Allison Randal provided valuable Perl advice and editorial guidance on the project, even though she probably dreaded my constant queries. Near the end of the year, Andy Oram took over as editor and helped me get the manuscript into shape so we could turn it into a book. The entire O’Reilly Media staff, from editorial, production, marketing, sales, and everyone else, was friendly and helpful, and it’s always a pleasure to work with them. It takes much more than an author to create a book, so thank a random O’Reilly employee next time you see one.

Randal Schwartz, my partner at Stonehenge Consulting, warned me that writing a book was a lot of work and still let me mostly take the year off to do it. I started in Perl by reading his Learning Perl and am now quite pleased to be adding another book to the series. As Randal has told me many times “You’ll get paid more at Starbucks and get health insurance, too.” Authors write to share their thoughts with the world, and we write to make other people better programmers.

Finally, I have to thank the Perl community, which has been incredibly kind and supportive over the 10 years that I’ve been part of it. So many great programmers and managers helped me become a better programmer, and I hope this book does the same for people just joining the crowd.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required