Mastering Perl
Author: Brian D Foy
This is the third in O'Reilly's series of landmark Perl tutorials, which started with Learning Perl, the bestselling introduction that taught you the basics of Perl syntax, and Intermediate Perl, which taught you how to create re-usable Perl software. Mastering Perl pulls everything together to show you how to bend Perl to your will. It convey's Perl's special models and programming idioms. This book isn't a collection of clever tricks, but a way of thinking about Perl programming so you can integrate the real-life problems of debugging, maintenance, configuration, and other tasks you encounter as a working programmer. The book explains how to: Use advanced regular expressions, including global matches, lookarounds, readable regexes, and regex debugging Avoid common programing problems with secure programming techniques Profile and benchmark Perl to find out where to focus your improvements Wrangle Perl code to make it more presentable and readable See how Perl keeps track of package variables and how you can use that for some powerful tricks Define subroutines on the fly and turn the tables on normal procedural programming. Modify and jury rig modules to fix code without editing the original source Let your users configure your programs without touching the code Learn how you can detect errors Perl doesn't report, and how to tell users about them Let your Perl program talk back to you by using Log4perl Store data for later use in another program, a later run of the same program, or to send them over a network Write programs as modules to get the benefit of Perl's distribution and testing tools Appendices include "brian's Guide to Solving Any Perl Problem" to improveyourtroubleshooting skills, as well as suggested reading to continue your Perl education. Mastering Perl 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.
Table of Contents:
Foreword xi
Preface xiii
Introduction: Becoming a Master 1
What It Means to Be a Master 2
Who Should Read This Book 3
How to Read This Book 3
What Should You Know Already? 4
What I Cover 4
What I Don't Cover 5
Advanced Regular Expressions 7
References to Regular Expressions 7
Noncapturing Grouping, (?:PATTERN) 13
Readable Regexes, /x and (?#...) 14
Global Matching 15
Lookarounds 19
Deciphering Regular Expressions 25
Final Thoughts 28
Summary 29
Further Reading 29
Secure Programming Techniques 31
Bad Data Can Ruin Your Day 31
Taint Checking 32
Untainting Data 38
List Forms of system and exec 42
Summary 44
Further Reading 44
Debugging Perl 47
Before You Waste Too Much Time 47
The Best Debugger in the World 48
perl5db.pl 59
AlternativeDebuggers 60
Other Debuggers 64
Summary 66
Further Reading 66
Profiling Perl 69
Finding the Culprit 69
The General Approach 73
Profiling DBI 74
Devel::DProf 83
Writing My Own Profiler 85
Profiling Test Suites 86
Summary 88
Further Reading 88
Benchmarking Perl 91
Benchmarking Theory 91
Benchmarking Time 93
Comparing Code 96
Don't Turn Off Your Thinking Cap 97
Memory Use 102
The perlbench Tool 107
Summary 109
Further Reading 110
Cleaning Up Perl 111
Good Style 111
perltidy 112
De-Obfuscation 114
Perl::Critic 118
Summary 123
Further Reading 123
Symbol Tables and Typeglobs 125
Package and Lexical Variables 125
The Symbol Table 128
Summary 136
Further Reading 136
Dynamic Subroutines 137
Subroutines As Data 137
Creating and Replacing Named Subroutines 141
Symbolic References 143
Iterating Through Subroutine Lists 145
Processing Pipelines 147
Method Lists 147
Subroutines As Arguments 148
Autoloaded Methods 152
Hashes As Objects 154
AutoSplit 154
Summary 155
Further Reading 155
Modifying and Jury-Rigging Modules 157
Choosing the Right Solution 157
Replacing Module Parts 160
Subclassing 162
Wrapping Subroutines 167
Summary 169
Further Reading 170
Configuring Perl Programs 171
Things Not to Do 171
Better Ways 174
Command-Line Switches 177
Configuration Files 183
Scripts with a Different Name 187
Interactive and Noninteractive Programs 188
perl's Config 189
Summary 191
Further Reading 191
Detecting and Reporting Errors 193
Perl Error Basics 193
Reporting Module Errors 199
Exceptions 202
Summary 209
Further Reading 209
Logging 211
Recording Errors and Other Information 211
Log4perl 212
Summary 218
Further Reading 218
Data Persistence 219
Flat Files 219
Storable 228
DBM Files 232
Summary 234
Further Reading 234
Working with Pod 237
The Pod Format 237
Translating Pod 238
Testing Pod 245
Summary 248
Further Reading 249
Working with Bits 251
Binary Numbers 251
Bit Operators 253
Bit Vectors 260
The vec Function 261
Keeping Track of Things 266
Summary 268
Further Reading 268
The Magic of Tied Variables 269
They Look Like Normal Variables 269
At the User Level 270
Behind the Curtain 271
Scalars 272
Arrays 277
Hashes 286
Filehandles 288
Summary 290
Further Reading 291
Modules As Programs 293
The main Thing 293
Backing Up 294
Who's Calling? 294
Testing the Program 295
Distributing the Programs 302
Summary 303
Further Reading 303
Further Reading 305
Brian's Guide to Solving Any Perl Problem 309
Index 315
Read also Business to Business Marketing or Charismatic Leadership in Organizations
Rapid Development
Author: Steve McConnell
If you wrestle with development mileposts and deadlines, this book can show you the tested strategies and tactics you need to keep your projects on time and on budget. In the tradition of McConnell's bestselling Code Complete (Microsoft Press, 1996), Rapid Development brings together the best research and hard-won experience to help you get control of your development projects.
Experience Level: All
Ray Duncan
Biting the Silver Bullet
Pop Quiz: What do Charles Petzold's Programming Windows and Andrew Schulman's Undocumented DOS have in common? Your knee-jerk reaction would probably be, "Not very much!" Philosophically, the two books lie at extreme opposite ends of the programming spectrum. Yet, on a more abstract level, the books are members of the same exclusive club: they established a new genre or ecological niche in computer trade book publishing, and then -- by virtue of their authority, comprehensiveness, and readability -- went on to dominate that niche for many years.
Steve McConnell's Rapid Development is instantly recognizable as another member of that rare breed of highly original and definitive books. It addresses a dire need in mainstream commercial or "shrinkwrap" software development that was previously unmet and only dimly perceived. It integrates a vast amount of practical information within a logical, easily grasped structure. It is soundly grounded in the author's mastery of his subject and common sense, and it is backed up by hundreds of references. And, last but hardly least, it is beautifully written in an economical, direct style that makes every page count.
For those of you who are (justifiably) skeptical about the extravagant claims made for "Rapid Application Development" (RAD) products, fear not --this book is not about CASE or Visual Basic. RAD development tools are certainly described in the book, but only as one arrow in a quiver of many. Rather, Rapid Development is a wide-ranging book on the professional and fact-based management of software development projects, with "rapid(er) development" as the hook.
The chapters of Rapid Development are organized under the umbrella of three main themes. The first section is principally concerned with "efficient development" rather than "rapid development" -- focusing in on fundamental technical and management principles, assessment and management of risk, and avoidance of classic mistakes. Proper attention to these areas makes schedules and costs at least predictable. Most of the topics are illustrated with entertaining (and sometimes painfully familiar) case histories.
The second section is a detailed exploration of a diverse strategies, techniques, and tools for speeding up the development process. Each topic, ranging from lifecycle planning to improving the motivation of developers, gets the careful, thoughtful treatment that is McConnell's hallmark. The chapters on estimation, scheduling, and feature-set control are especially valuable. I've read about code size estimation and software project scheduling many times elsewhere, but only after reading Rapid Development did I truly believe.
The last section of the book is a collection of mini-essays on 27 "best practices"in a common format. Each begins with a table that summarizes the technique's efficacy in various domains, major risks, interactions, and tradeoffs. The table is followed by a (sometimes extensive) discussion of usage, risk management, side effects, and "keys to success" for the practice at hand. Citations for further reading on each "best practice" are often provided as well.
In Rapid Development, we are privileged to see a virtuoso author/programmer and a superb editing and publishing team working together at the top of their form. Very few books I have encountered in the last few years have given me as much pleasure to read as this one. The only teensy quibble I have with the book is a design issue -- elimination of the excessive white space and the slightly patronizing "hard facts," "classic mistake," and "cross-reference" icons and tags in the left margins would have shortened the book by a hundred pages or more and saved quite a few trees.--Dr. Dobb's Electronic Review of Computer Books