Perl Best Practices [Electronic resources]

Damian Conway

نسخه متنی -صفحه : 317/ 288
نمايش فراداده

Appendix A. Essential Perl Best Practices

Ten Essential Development Practices

  1. Design the module's interface first.

    [Chapter 17:

    Interfaces ]

  2. Write the test cases before the code.

    [Chapter 18:

    Test Cases ]

  3. Create standard POD templates for modules and applications.

    [Chapter 7:

    Boilerplates ]

  4. Use a revision control system.

    [Chapter 19:

    Revision Control ]

  5. Create consistent command-line and configuration interfaces.

    [Chapter 14:

    Command-Line Structure , Chapter 19:

    Configuration Files ]

  6. Agree upon a coherent layout style and automate it with perltidy.

    [Chapter 2:

    Automated Layout ]

  7. Code in commented paragraphs.

    [Chapter 2:

    Chunking ]

  8. Throw exceptions instead of returning special values or setting flags.

    [Chapter 13:

    Exceptions ]

  9. Add new test cases before you start debugging.

    [Chapter 18:

    Debugging and Testing ]

  10. Don't optimize codebenchmark it.

    [Chapter 19:

    Benchmarking ]

Ten Essential Coding Practices

  1. Always use strict and use warnings.

    [Chapter 18:

    Strictures ,

    Warnings ]

  2. Use grammatical templates when forming identifiers.

    [Chapter 3:

    Identifiers, Booleans, Reference Variables, Arrays and Hashes ]

  3. Use lexical variables, not package variables.

    [Chapter 5:

    Lexical Variables ]

  4. Label every loop that is exited explicitly, and every next, last, or redo.

    [Chapter 6:

    Loop Labels ]

  5. Don't use bareword filehandles; use indirect filehandles.

    [Chapter 10:

    Filehandles, Indirect Filehandles ]

  6. In a subroutine, always unpack @_ first, using a hash of named arguments if there are more than three parameters.

    [Chapter 9:

    Argument Lists, Named Arguments ]

  7. Always return via an explicit return.

    [Chapter 9:

    Implicit Returns ]

  8. Always use the /x, /m, and /s flags, and the \A and \z anchors.

    [Chapter 12:

    Extended Formatting, Line Boundaries, Matching Anything, String Boundaries ]

  9. Use capturing parentheses in regexes only when deliberately capturing, then give the captured substrings proper names.

    [Chapter 12:

    Capturing Parentheses, Capture Variables ]

  10. Never make variables part of a module's interface.

    [Chapter 17:

    Interface Variables ]

Ten Essential Module Practices

  1. Write tests using the Test::Simple or Test::More modules.

    [Chapter 18:

    Modular Testing ]

  2. use English for the less familiar punctuation variables.

    [Chapter 5:

    Punctuation Variables ]

  3. Use named constants created with the Readonly module.

    [Chapter 4:

    Constants ]

  4. Use the "non-builtin builtins" from Scalar::Util, List::Util, and List::MoreUtils.

    [Chapter 8:

    Utilities ]

  5. Use IO::Prompt when prompting for interactive input.

    [Chapter 10:

    Simple Prompting, Power Prompting ]

  6. Use the Carp and Exception::Class modules to create OO exceptions that report from the caller's location.

    [Chapter 13:

    Reporting Failure, Exception Classes ]

  7. Use the Fatal module to make builtins throw exceptions on failure.

    [Chapter 13:

    Builtin Failures, Contextual Failure ]

  8. Create aliases using the Data::Alias or Lexical::Alias module.

    [Chapter 6:

    Necessary Subscripting ]

  9. Use Regexp::Common instead of writing your own regexes.

    [Chapter 12:

    Canned Regexes ]

  10. Use the Class::Std module to create properly encapsulated classes.

    [Chapter 16:

    Automating Class Hierarchies, Attribute Demolition, Attribute Building ]