Perl Best Practices
Many programmers code by instinct, relying on convenient habits or a
"style" they picked up early on. They aren't conscious of all the
choices they make, like how they format their source, the names they use
for variables, or the kinds of loops they use. They're focused entirely
on problems they're solving, solutions they're creating, and algorithms
they're implementing. So they write code in the way that seems natural,
that happens intuitively, and that feels good.
But if you're serious about your profession, intuition isn't enough.
Perl Best Practices author Damian Conway explains that rules,
conventions, standards, and practices not only help programmers
communicate and coordinate with one another, they also provide a
reliable framework for thinking about problems, and a common language
for expressing solutions. This is especially critical in Perl, because
the language is designed to offer many ways to accomplish the same task,
and consequently it supports many incompatible dialects.
With a good dose of Aussie humor, Dr. Conway (familiar to many in the
Perl community) offers 256 guidelines on the art of coding to help you
write better Perl code--in fact, the best Perl code you possibly can.
The guidelines cover code layout, naming conventions, choice of data and
control structures, program decomposition, interface design and
implementation, modularity, object orientation, error handling, testing,
and debugging.
They're designed to work together to produce code that is clear, robust,
efficient, maintainable, and concise, but Dr. Conway doesn't pretend
that this is the one true universal and unequivocal set of best
practices. Instead, Perl Best Practices offers coherent and widely
applicable suggestions based on real-world experience of how code is
actually written, rather than on someone's ivory-tower theories on how
software ought to be created.
Most of all, Perl Best Practices offers guidelines that actually work,
and that many developers around the world are already using. Much like
Perl itself, these guidelines are about helping you to get your job
done, without getting in the way.
Praise for Perl Best Practices from Perl community members:
"As a manager of a large Perl project, I'd ensure that every member of my team has a copy of Perl Best Practices on their desk, and use it as the basis for an in-house style guide." -- Randal Schwartz
"There are no more excuses for writing bad Perl programs. All levels of Perl programmer will be more productive after reading this book." -- Peter Scott
"Perl Best Practices will be the next big important book in the evolution of Perl. The ideas and practices Damian lays down will help bring Perl out from under the embarrassing heading of "scripting languages". Many of us have known Perl is a real programming language, worthy of all the tasks normally delegated to Java and C++. With Perl Best Practices, Damian shows specifically how and why, so everyone else can see, too." -- Andy Lester
"Damian's done what many thought impossible: show how to build large, maintainable Perl applications, while still letting Perl be the powerful, expressive language that programmers have loved for years." -- Bill Odom
"Finally, a means to bring lasting order to the process and product of real Perl development teams." -- Andrew Sundstrom
"Perl Best Practices provides a valuable education in how to write robust, maintainable Perl, and is a definitive citation source when coaching other programmers." -- Bennett Todd
"I've been teaching Perl for years, and find the same question keeps being asked: Where can I find a reference for writing reusable, maintainable Perl code? Finally I have a decent answer." -- Paul Fenwick
"At last a well researched, well thought-out, comprehensive guide to Perl style. Instead of each of us developing our own, we can learn good practices from one of Perl's most prolific and experienced authors. I recommend this book to anyone who prefers getting on with the job rather than going back and fixing errors caused by syntax and poor style issues." -- Jacinta Richardson
"If you care about programming in any language read this book. Even if you don't intend to follow all of the practices, thinking through your style will improve it." -- Steven Lembark
"The Perl community's best author is back with another outstanding book. There has never been a comprehensive reference on high quality Perl coding and style until Perl Best Practices. This book fills a large gap in every Perl bookshelf." -- Uri Guttman
|
 |
Preface |
|
|
 |
Contents of This Book |
|
|
 |
Conventions Used in This Book |
|
 |
Chapter 1. Best Practices |
|
|
 |
Section 1.1. Three Goals |
|
|
 |
Section 1.2. This Book |
|
|
 |
Section 1.3. Rehabiting |
|
 |
Chapter 2. Code Layout |
|
|
 |
Section 2.1. Bracketing |
|
|
 |
Section 2.2. Keywords |
|
|
 |
Section 2.3. Subroutines and Variables |
|
|
 |
Section 2.4. Builtins |
|
|
 |
Section 2.5. Keys and Indices |
|
|
 |
Section 2.6. Operators |
|
|
 |
Section 2.7. Semicolons |
|
|
 |
Section 2.8. Commas |
|
|
 |
Section 2.9. Line Lengths |
|
|
 |
Section 2.10. Indentation |
|
|
 |
Section 2.11. Tabs |
|
|
 |
Section 2.12. Blocks |
|
|
 |
Section 2.13. Chunking |
|
|
 |
Section 2.14. Elses |
|
|
 |
Section 2.15. Vertical Alignment |
|
|
 |
Section 2.16. Breaking Long Lines |
|
|
 |
Section 2.17. Non-Terminal Expressions |
|
|
 |
Section 2.18. Breaking by Precedence |
|
|
 |
Section 2.19. Assignments |
|
|
 |
Section 2.20. Ternaries |
|
|
 |
Section 2.21. Lists |
|
|
 |
Section 2.22. Automated Layout |
|
 |
Chapter 3. Naming Conventions |
|
|
 |
Section 3.1. Identifiers |
|
|
 |
Section 3.2. Booleans |
|
|
 |
Section 3.3. Reference Variables |
|
|
 |
Section 3.4. Arrays and Hashes |
|
|
 |
Section 3.5. Underscores |
|
|
 |
Section 3.6. Capitalization |
|
|
 |
Section 3.7. Abbreviations |
|
|
 |
Section 3.8. Ambiguous Abbreviations |
|
|
 |
Section 3.9. Ambiguous Names |
|
|
 |
Section 3.10. Utility Subroutines |
|
 |
Chapter 4. Values and Expressions |
|
|
 |
Section 4.1. String Delimiters |
|
|
 |
Section 4.2. Empty Strings |
|
|
 |
Section 4.3. Single-Character Strings |
|
|
 |
Section 4.4. Escaped Characters |
|
|
 |
Section 4.5. Constants |
|
|
 |
Section 4.6. Leading Zeros |
|
|
 |
Section 4.7. Long Numbers |
|
|
 |
Section 4.8. Multiline Strings |
|
|
 |
Section 4.9. Here Documents |
|
|
 |
Section 4.10. Heredoc Indentation |
|
|
 |
Section 4.11. Heredoc Terminators |
|
|
 |
Section 4.12. Heredoc Quoters |
|
|
 |
Section 4.13. Barewords |
|
|
 |
Section 4.14. Fat Commas |
|
|
 |
Section 4.15. Thin Commas |
|
|
 |
Section 4.16. Low-Precedence Operators |
|
|
 |
Section 4.17. Lists |
|
|
 |
Section 4.18. List Membership |
|
 |
Chapter 5. Variables |
|
|
 |
Section 5.1. Lexical Variables |
|
|
 |
Section 5.2. Package Variables |
|
|
 |
Section 5.3. Localization |
|
|
 |
Section 5.4. Initialization |
|
|
 |
Section 5.5. Punctuation Variables |
|
|
 |
Section 5.6. Localizing Punctuation Variables |
|
|
 |
Section 5.7. Match Variables |
|
|
 |
Section 5.8. Dollar-Underscore |
|
|
 |
Section 5.9. Array Indices |
|
|
 |
Section 5.10. Slicing |
|
|
 |
Section 5.11. Slice Layout |
|
|
 |
Section 5.12. Slice Factoring |
|
 |
Chapter 6. Control Structures |
|
|
 |
Section 6.1. If Blocks |
|
|
 |
Section 6.2. Postfix Selectors |
|
|
 |
Section 6.3. Other Postfix Modifiers |
|
|
 |
Section 6.4. Negative Control Statements |
|
|
 |
Section 6.5. C-Style Loops |
|
|
 |
Section 6.6. Unnecessary Subscripting |
|
|
 |
Section 6.7. Necessary Subscripting |
|
|
 |
Section 6.8. Iterator Variables |
|
|
 |
Section 6.9. Non-Lexical Loop Iterators |
|
|
 |
Section 6.10. List Generation |
|
|
 |
Section 6.11. List Selections |
|
|
 |
Section 6.12. List Transformation |
|
|
 |
Section 6.13. Complex Mappings |
|
|
 |
Section 6.14. List Processing Side Effects |
|
|
 |
Section 6.15. Multipart Selections |
|
|
 |
Section 6.16. Value Switches |
|
|
 |
Section 6.17. Tabular Ternaries |
|
|
 |
Section 6.18. do-while Loops |
|
|
 |
Section 6.19. Linear Coding |
|
|
 |
Section 6.20. Distributed Control |
|
|
 |
Section 6.21. Redoing |
|
|
 |
Section 6.22. Loop Labels |
|
 |
Chapter 7. Documentation |
|
|
 |
Section 7.1. Types of Documentation |
|
|
 |
Section 7.2. Boilerplates |
|
|
 |
Section 7.3. Extended Boilerplates |
|
|
 |
Section 7.4. Location |
|
|
 |
Section 7.5. Contiguity |
|
|
 |
Section 7.6. Position |
|
|
 |
Section 7.7. Technical Documentation |
|
|
 |
Section 7.8. Comments |
|
|
 |
Section 7.9. Algorithmic Documentation |
|
|
 |
Section 7.10. Elucidating Documentation |
|
|
 |
Section 7.11. Defensive Documentation |
|
|
 |
Section 7.12. Indicative Documentation |
|
|
 |
Section 7.13. Discursive Documentation |
|
|
 |
Section 7.14. Proofreading |
|
 |
Chapter 8. Built-in Functions |
|
|
 |
Section 8.1. Sorting |
|
|
 |
Section 8.2. Reversing Lists |
|
|
 |
Section 8.3. Reversing Scalars |
|
|
 |
Section 8.4. Fixed-Width Data |
|
|
 |
Section 8.5. Separated Data |
|
|
 |
Section 8.6. Variable-Width Data |
|
|
 |
Section 8.7. String Evaluations |
|
|
 |
Section 8.8. Automating Sorts |
|
|
 |
Section 8.9. Substrings |
|
|
 |
Section 8.10. Hash Values |
|
|
 |
Section 8.11. Globbing |
|
|
 |
Section 8.12. Sleeping |
|
|
 |
Section 8.13. Mapping and Grepping |
|
|
 |
Section 8.14. Utilities |
|
 |
Chapter 9. Subroutines |
|
|
 |
Section 9.1. Call Syntax |
|
|
 |
Section 9.2. Homonyms |
|
|
 |
Section 9.3. Argument Lists |
|
|
 |
Section 9.4. Named Arguments |
|
|
 |
Section 9.5. Missing Arguments |
|
|
 |
Section 9.6. Default Argument Values |
|
|
 |
Section 9.7. Scalar Return Values |
|
|
 |
Section 9.8. Contextual Return Values |
|
|
 |
Section 9.9. Multi-Contextual Return Values |
|
|
 |
Section 9.10. Prototypes |
|
|
 |
Section 9.11. Implicit Returns |
|
|
 |
Section 9.12. Returning Failure |
|
 |
Chapter 10. I/O |
|
|
 |
Section 10.1. Filehandles |
|
|
 |
Section 10.2. Indirect Filehandles |
|
|
 |
Section 10.3. Localizing Filehandles |
|
|
 |
Section 10.4. Opening Cleanly |
|
|
 |
Section 10.5. Error Checking |
|
|
 |
Section 10.6. Cleanup |
|
|
 |
Section 10.7. Input Loops |
|
|
 |
Section 10.8. Line-Based Input |
|
|
 |
Section 10.9. Simple Slurping |
|
|
 |
Section 10.10. Power Slurping |
|
|
 |
Section 10.11. Standard Input |
|
|
 |
Section 10.12. Printing to Filehandles |
|
|
 |
Section 10.13. Simple Prompting |
|
|
 |
Section 10.14. Interactivity |
|
|
 |
Section 10.15. Power Prompting |
|
|
 |
Section 10.16. Progress Indicators |
|
|
 |
Section 10.17. Automatic Progress Indicators |
|
|
 |
Section 10.18. Autoflushing |
|
 |
Chapter 11. References |
|
|
 |
Section 11.1. Dereferencing |
|
|
 |
Section 11.2. Braced References |
|
|
 |
Section 11.3. Symbolic References |
|
|
 |
Section 11.4. Cyclic References |
|
 |
Chapter 12. Regular Expressions |
|
|
 |
Section 12.1. Extended Formatting |
|
|
 |
Section 12.2. Line Boundaries |
|
|
 |
Section 12.3. String Boundaries |
|
|
 |
Section 12.4. End of String |
|
|
 |
Section 12.5. Matching Anything |
|
|
 |
Section 12.6. Lazy Flags |
|
|
 |
Section 12.7. Brace Delimiters |
|
|
 |
Section 12.8. Other Delimiters |
|
|
 |
Section 12.9. Metacharacters |
|
|
 |
Section 12.10. Named Characters |
|
|
 |
Section 12.11. Properties |
|
|
 |
Section 12.12. Whitespace |
|
|
 |
Section 12.13. Unconstrained Repetitions |
|
|
 |
Section 12.14. Capturing Parentheses |
|
|
 |
Section 12.15. Captured Values |
|
|
 |
Section 12.16. Capture Variables |
|
|
 |
Section 12.17. Piecewise Matching |
|
|
 |
Section 12.18. Tabular Regexes |
|
|
 |
Section 12.19. Constructing Regexes |
|
|
 |
Section 12.20. Canned Regexes |
|
|
 |
Section 12.21. Alternations |
|
|
 |
Section 12.22. Factoring Alternations |
|
|
 |
Section 12.23. Backtracking |
|
|
 |
Section 12.24. String Comparisons |
|
 |
Chapter 13. Error Handling |
|
|
 |
Section 13.1. Exceptions |
|
|
 |
Section 13.2. Builtin Failures |
|
|
 |
Section 13.3. Contextual Failure |
|
|
 |
Section 13.4. Systemic Failure |
|
|
 |
Section 13.5. Recoverable Failure |
|
|
 |
Section 13.6. Reporting Failure |
|
|
 |
Section 13.7. Error Messages |
|
|
 |
Section 13.8. Documenting Errors |
|
|
 |
Section 13.9. OO Exceptions |
|
|
 |
Section 13.10. Volatile Error Messages |
|
|
 |
Section 13.11. Exception Hierarchies |
|
|
 |
Section 13.12. Processing Exceptions |
|
|
 |
Section 13.13. Exception Classes |
|
|
 |
Section 13.14. Unpacking Exceptions |
|
 |
Chapter 14. Command-Line Processing |
|
|
 |
Section 14.1. Command-Line Structure |
|
|
 |
Section 14.2. Command-Line Conventions |
|
|
 |
Section 14.3. Meta-options |
|
|
 |
Section 14.4. In-situ Arguments |
|
|
 |
Section 14.5. Command-Line Processing |
|
|
 |
Section 14.6. Interface Consistency |
|
|
 |
Section 14.7. Interapplication Consistency |
|
 |
Chapter 15. Objects |
|
|
 |
Section 15.1. Using OO |
|
|
 |
Section 15.2. Criteria |
|
|
 |
Section 15.3. Pseudohashes |
|
|
 |
Section 15.4. Restricted Hashes |
|
|
 |
Section 15.5. Encapsulation |
|
|
 |
Section 15.6. Constructors |
|
|
 |
Section 15.7. Cloning |
|
|
 |
Section 15.8. Destructors |
|
|
 |
Section 15.9. Methods |
|
|
 |
Section 15.10. Accessors |
|
|
 |
Section 15.11. Lvalue Accessors |
|
|
 |
Section 15.12. Indirect Objects |
|
|
 |
Section 15.13. Class Interfaces |
|
|
 |
Section 15.14. Operator Overloading |
|
|
 |
Section 15.15. Coercions |
|
 |
Chapter 16. Class Hierarchies |
|
|
 |
Section 16.1. Inheritance |
|
|
 |
Section 16.2. Objects |
|
|
 |
Section 16.3. Blessing Objects |
|
|
 |
Section 16.4. Constructor Arguments |
|
|
 |
Section 16.5. Base Class Initialization |
|
|
 |
Section 16.6. Construction and Destruction |
|
|
 |
Section 16.7. Automating Class Hierarchies |
|
|
 |
Section 16.8. Attribute Demolition |
|
|
 |
Section 16.9. Attribute Building |
|
|
 |
Section 16.10. Coercions |
|
|
 |
Section 16.11. Cumulative Methods |
|
|
 |
Section 16.12. Autoloading |
|
 |
Chapter 17. Modules |
|
|
 |
Section 17.1. Interfaces |
|
|
 |
Section 17.2. Refactoring |
|
|
 |
Section 17.3. Version Numbers |
|
|
 |
Section 17.4. Version Requirements |
|
|
 |
Section 17.5. Exporting |
|
|
 |
Section 17.6. Declarative Exporting |
|
|
 |
Section 17.7. Interface Variables |
|
|
 |
Section 17.8. Creating Modules |
|
|
 |
Section 17.9. The Standard Library |
|
|
 |
Section 17.10. CPAN |
|
 |
Chapter 18. Testing and Debugging |
|
|
 |
Section 18.1. Test Cases |
|
|
 |
Section 18.2. Modular Testing |
|
|
 |
Section 18.3. Test Suites |
|
|
 |
Section 18.4. Failure |
|
|
 |
Section 18.5. What to Test |
|
|
 |
Section 18.6. Debugging and Testing |
|
|
 |
Section 18.7. Strictures |
|
|
 |
Section 18.8. Warnings |
|
|
 |
Section 18.9. Correctness |
|
|
 |
Section 18.10. Overriding Strictures |
|
|
 |
Section 18.11. The Debugger |
|
|
 |
Section 18.12. Manual Debugging |
|
|
 |
Section 18.13. Semi-Automatic Debugging |
|
 |
Chapter 19. Miscellanea |
|
|
 |
Section 19.1. Revision Control |
|
|
 |
Section 19.2. Other Languages |
|
|
 |
Section 19.3. Configuration Files |
|
|
 |
Section 19.4. Formats |
|
|
 |
Section 19.5. Ties |
|
|
 |
Section 19.6. Cleverness |
|
|
 |
Section 19.7. Encapsulated Cleverness |
|
|
 |
Section 19.8. Benchmarking |
|
|
 |
Section 19.9. Memory |
|
|
 |
Section 19.10. Caching |
|
|
 |
Section 19.11. Memoization |
|
|
 |
Section 19.12. Caching for Optimization |
|
|
 |
Section 19.13. Profiling |
|
|
 |
Section 19.14. Enbugging |
|
 |
Appendix A. Essential Perl Best Practices |
|
 |
Appendix B. Perl Best Practices |
|
|
 |
Section B.1. Chapter 2, Code Layout |
|
|
 |
Section B.2. Chapter 3, Naming Conventions |
|
|
 |
Section B.3. Chapter 4, Values and Expressions |
|
|
 |
Section B.4. Chapter 5, Variables |
|
|
 |
Section B.5. Chapter 6, Control Structures |
|
|
 |
Section B.6. Chapter 7, Documentation |
|
|
 |
Section B.7. Chapter 8, Built-in Functions |
|
|
 |
Section B.8. Chapter 9, Subroutines |
|
|
 |
Section B.9. Chapter 10, I/O |
|
|
 |
Section B.10. Chapter 11, References |
|
|
 |
Section B.11. Chapter 12, Regular Expressions |
|
|
 |
Section B.12. Chapter 13, Error Handling |
|
|
 |
Section B.13. Chapter 14, Command-Line Processing |
|
|
 |
Section B.14. Chapter 15, Objects |
|
|
 |
Section B.15. Chapter 16, Class Hierarchies |
|
|
 |
Section B.16. Chapter 17, Modules |
|
|
 |
Section B.17. Chapter 18, Testing and Debugging |
|
|
 |
Section B.18. Chapter 19, Miscellanea |
|
 |
Appendix C. Editor Configurations |
|
|
 |
Section C.1. vim |
|
|
 |
Section C.2. vile |
|
|
 |
Section C.3. Emacs |
|
|
 |
Section C.4. BBEdit |
|
|
 |
Section C.5. TextWrangler |
|
 |
Appendix D. Recommended Modules and Utilities |
|
|
 |
Section D.1. Recommended Core Modules |
|
|
 |
Section D.2. Recommended CPAN Modules |
|
|
 |
Section D.3. Utility Subroutines |
|
 |
Appendix Bibliography. Bibliography |
|
 |
Colophon |
|
|
 |
About the Author |
|
|
 |
Colophon |
|
 |
Index |