Cover image for Perl Best Practices

Book description

Many programmers code by instinct, relying on convenient habits ora "style" they picked up early on. They aren't conscious of all thechoices they make, like how they format their source, the namesthey use for variables, or the kinds of loops they use. They'refocused entirely on problems they're solving, solutions they'recreating, and algorithms they're implementing. So they write codein the way that seems natural, that happens intuitively, and thatfeels good.

But if you're serious about your profession, intuition isn'tenough. Perl Best Practices author Damian Conway explainsthat rules, conventions, standards, and practices not only helpprogrammers communicate and coordinate with one another, they alsoprovide a reliable framework for thinking about problems, and acommon language for expressing solutions. This is especiallycritical in Perl, because the language is designed to offer manyways to accomplish the same task, and consequently it supports manyincompatible dialects.

With a good dose of Aussie humor, Dr. Conway (familiar to manyin the Perl community) offers 256 guidelines on the art of codingto help you write better Perl code--in fact, the best Perl code youpossibly can. The guidelines cover code layout, naming conventions,choice of data and control structures, program decomposition,interface design and implementation, modularity, objectorientation, error handling, testing, and debugging.

They're designed to work together to produce code that is clear,robust, efficient, maintainable, and concise, but Dr. Conwaydoesn't pretend that this is the one true universal and unequivocalset of best practices. Instead, Perl Best Practices offerscoherent and widely applicable suggestions based on real-worldexperience of how code is actually written, rather than onsomeone's ivory-tower theories on how software ought to becreated.

Most of all, Perl Best Practices offers guidelines thatactually work, and that many developers around the world arealready using. Much like Perl itself, these guidelines are abouthelping you to get your job done, without getting in the way.

Praise for Perl Best Practices from Perl communitymembers:

"As a manager of a large Perl project, I'd ensure that everymember of my team has a copy of Perl Best Practices on theirdesk, and use it as the basis for an in-house style guide." --Randal Schwartz

"There are no more excuses for writing bad Perl programs. Alllevels of Perl programmer will be more productive after readingthis book." -- Peter Scott

"Perl Best Practices will be the next big important bookin the evolution of Perl. The ideas and practices Damian lays downwill help bring Perl out from under the embarrassing heading of"scripting languages". Many of us have known Perl is a realprogramming language, worthy of all the tasks normally delegated toJava and C++. With Perl Best Practices, Damian shows specificallyhow and why, so everyone else can see, too." -- Andy Lester

"Damian's done what many thought impossible: show how to buildlarge, maintainable Perl applications, while still letting Perl bethe powerful, expressive language that programmers have loved foryears." -- Bill Odom

"Finally, a means to bring lasting order to the process andproduct of real Perl development teams." -- Andrew Sundstrom

"Perl Best Practices provides a valuable education in how towrite robust, maintainable Perl, and is a definitive citationsource when coaching other programmers." -- Bennett Todd "I've beenteaching Perl for years, and find the same question keeps beingasked: Where can I find a reference for writing reusable,maintainable Perl code? Finally I have a decent answer." -- PaulFenwick "At last a well researched, well thought-out, comprehensiveguide to Perl style. Instead of each of us developing our own, wecan learn good practices from one of Perl's most prolific andexperienced authors. I recommend this book to anyone who prefersgetting on with the job rather than going back and fixing errorscaused by syntax and poor style issues." -- Jacinta Richardson "Ifyou care about programming in any language read this book. Even ifyou don't intend to follow all of the practices, thinking throughyour style will improve it." -- Steven Lembark "The Perlcommunity's best author is back with another outstanding book.There has never been a comprehensive reference on high quality Perlcoding and style until Perl Best Practices. This book fillsa large gap in every Perl bookshelf." -- Uri Guttman

Table of Contents

  1. Perl Best Practices
  2. Dedication
  3. SPECIAL OFFER: Upgrade this ebook with O’Reilly
  4. A Note Regarding Supplemental Files
  5. Preface
    1. Contents of This Book
    2. Conventions Used in This Book
    3. Code Examples
    4. Feedback
    5. Acknowledgments
  6. 1. Best Practices
    1. Three Goals
      1. Robustness
      2. Efficiency
      3. Maintainability
    2. This Book
    3. Rehabiting
  7. 2. Code Layout
    1. Bracketing
    2. Keywords
    3. Subroutines and Variables
    4. Builtins
    5. Keys and Indices
    6. Operators
    7. Semicolons
    8. Commas
    9. Line Lengths
    10. Indentation
    11. Tabs
    12. Blocks
    13. Chunking
    14. Elses
    15. Vertical Alignment
    16. Breaking Long Lines
    17. Non-Terminal Expressions
    18. Breaking by Precedence
    19. Assignments
    20. Ternaries
    21. Lists
    22. Automated Layout
  8. 3. Naming Conventions
    1. Identifiers
    2. Booleans
    3. Reference Variables
    4. Arrays and Hashes
    5. Underscores
    6. Capitalization
    7. Abbreviations
    8. Ambiguous Abbreviations
    9. Ambiguous Names
    10. Utility Subroutines
  9. 4. Values and Expressions
    1. String Delimiters
    2. Empty Strings
    3. Single-Character Strings
    4. Escaped Characters
    5. Constants
    6. Leading Zeros
    7. Long Numbers
    8. Multiline Strings
    9. Here Documents
    10. Heredoc Indentation
    11. Heredoc Terminators
    12. Heredoc Quoters
    13. Barewords
    14. Fat Commas
    15. Thin Commas
    16. Low-Precedence Operators
    17. Lists
    18. List Membership
  10. 5. Variables
    1. Lexical Variables
    2. Package Variables
    3. Localization
    4. Initialization
    5. Punctuation Variables
    6. Localizing Punctuation Variables
    7. Match Variables
    8. Dollar-Underscore
    9. Array Indices
    10. Slicing
    11. Slice Layout
    12. Slice Factoring
  11. 6. Control Structures
    1. If Blocks
    2. Postfix Selectors
    3. Other Postfix Modifiers
    4. Negative Control Statements
    5. C-Style Loops
    6. Unnecessary Subscripting
    7. Necessary Subscripting
    8. Iterator Variables
    9. Non-Lexical Loop Iterators
    10. List Generation
    11. List Selections
    12. List Transformation
    13. Complex Mappings
    14. List Processing Side Effects
    15. Multipart Selections
    16. Value Switches
    17. Tabular Ternaries
    18. do-while Loops
    19. Linear Coding
    20. Distributed Control
    21. Redoing
    22. Loop Labels
  12. 7. Documentation
    1. Types of Documentation
    2. Boilerplates
    3. Extended Boilerplates
    4. Location
    5. Contiguity
    6. Position
    7. Technical Documentation
    8. Comments
    9. Algorithmic Documentation
    10. Elucidating Documentation
    11. Defensive Documentation
    12. Indicative Documentation
    13. Discursive Documentation
    14. Proofreading
  13. 8. Built-in Functions
    1. Sorting
    2. Reversing Lists
    3. Reversing Scalars
    4. Fixed-Width Data
    5. Separated Data
    6. Variable-Width Data
    7. String Evaluations
    8. Automating Sorts
    9. Substrings
    10. Hash Values
    11. Globbing
    12. Sleeping
    13. Mapping and Grepping
    14. Utilities
  14. 9. Subroutines
    1. Call Syntax
    2. Homonyms
    3. Argument Lists
    4. Named Arguments
    5. Missing Arguments
    6. Default Argument Values
    7. Scalar Return Values
    8. Contextual Return Values
    9. Multi-Contextual Return Values
    10. Prototypes
    11. Implicit Returns
    12. Returning Failure
  15. 10. I/O
    1. Filehandles
    2. Indirect Filehandles
    3. Localizing Filehandles
    4. Opening Cleanly
    5. Error Checking
    6. Cleanup
    7. Input Loops
    8. Line-Based Input
    9. Simple Slurping
    10. Power Slurping
    11. Standard Input
    12. Printing to Filehandles
    13. Simple Prompting
    14. Interactivity
    15. Power Prompting
    16. Progress Indicators
    17. Automatic Progress Indicators
    18. Autoflushing
  16. 11. References
    1. Dereferencing
    2. Braced References
    3. Symbolic References
    4. Cyclic References
  17. 12. Regular Expressions
    1. Extended Formatting
    2. Line Boundaries
    3. String Boundaries
    4. End of String
    5. Matching Anything
    6. Lazy Flags
    7. Brace Delimiters
    8. Other Delimiters
    9. Metacharacters
    10. Named Characters
    11. Properties
    12. Whitespace
    13. Unconstrained Repetitions
    14. Capturing Parentheses
    15. Captured Values
    16. Capture Variables
    17. Piecewise Matching
    18. Tabular Regexes
    19. Constructing Regexes
    20. Canned Regexes
    21. Alternations
    22. Factoring Alternations
    23. Backtracking
    24. String Comparisons
  18. 13. Error Handling
    1. Exceptions
    2. Builtin Failures
    3. Contextual Failure
    4. Systemic Failure
    5. Recoverable Failure
    6. Reporting Failure
    7. Error Messages
    8. Documenting Errors
    9. OO Exceptions
    10. Volatile Error Messages
    11. Exception Hierarchies
    12. Processing Exceptions
    13. Exception Classes
    14. Unpacking Exceptions
  19. 14. Command-Line Processing
    1. Command-Line Structure
    2. Command-Line Conventions
    3. Meta-options
    4. In-situ Arguments
    5. Command-Line Processing
    6. Interface Consistency
    7. Interapplication Consistency
  20. 15. Objects
    1. Using OO
    2. Criteria
    3. Pseudohashes
    4. Restricted Hashes
    5. Encapsulation
    6. Constructors
    7. Cloning
    8. Destructors
    9. Methods
    10. Accessors
    11. Lvalue Accessors
    12. Indirect Objects
    13. Class Interfaces
    14. Operator Overloading
    15. Coercions
  21. 16. Class Hierarchies
    1. Inheritance
    2. Objects
    3. Blessing Objects
    4. Constructor Arguments
    5. Base Class Initialization
    6. Construction and Destruction
    7. Automating Class Hierarchies
    8. Attribute Demolition
    9. Attribute Building
    10. Coercions
    11. Cumulative Methods
    12. Autoloading
  22. 17. Modules
    1. Interfaces
    2. Refactoring
    3. Version Numbers
    4. Version Requirements
    5. Exporting
    6. Declarative Exporting
    7. Interface Variables
    8. Creating Modules
    9. The Standard Library
    10. CPAN
  23. 18. Testing and Debugging
    1. Test Cases
    2. Modular Testing
    3. Test Suites
    4. Failure
    5. What to Test
    6. Debugging and Testing
    7. Strictures
    8. Warnings
    9. Correctness
    10. Overriding Strictures
    11. The Debugger
    12. Manual Debugging
    13. Semi-Automatic Debugging
  24. 19. Miscellanea
    1. Revision Control
    2. Other Languages
    3. Configuration Files
    4. Formats
    5. Ties
    6. Cleverness
    7. Encapsulated Cleverness
    8. Benchmarking
    9. Memory
    10. Caching
    11. Memoization
    12. Caching for Optimization
    13. Profiling
    14. Enbugging
  25. A. Essential Perl Best Practices
  26. B. Perl Best Practices
    1. Chapter 2, Code Layout
    2. Chapter 3, Naming Conventions
    3. Chapter 4, Values and Expressions
    4. Chapter 5, Variables
    5. Chapter 6, Control Structures
    6. Chapter 7, Documentation
    7. Chapter 8, Built-in Functions
    8. Chapter 9, Subroutines
    9. Chapter 10, I/O
    10. Chapter 11, References
    11. Chapter 12, Regular Expressions
    12. Chapter 13, Error Handling
    13. Chapter 14, Command-Line Processing
    14. Chapter 15, Objects
    15. Chapter 16, Class Hierarchies
    16. Chapter 17, Modules
    17. Chapter 18, Testing and Debugging
    18. Chapter 19, Miscellanea
  27. C. Editor Configurations
    1. vim
    2. vile
    3. Emacs
    4. BBEdit
    5. TextWrangler
  28. D. Recommended Modules and Utilities
    1. Recommended Core Modules
    2. Recommended CPAN Modules
    3. Utility Subroutines
  29. E. Bibliography
    1. Perl Coding and Development Practices
      1. Testing and Debugging
      2. Algorithms and Efficiency
      3. Coding Style and Common Mistakes
    2. General Coding and Development Practices
      1. Coding Standards
      2. Development Practices
      3. Text Editors
  30. Index
  31. About the Author
  32. Colophon
  33. SPECIAL OFFER: Upgrade this ebook with O’Reilly
  34. Copyright