Perl Best Practices (Paperback)

Damian Conway

  • 出版商: O'Reilly
  • 出版日期: 2005-08-16
  • 售價: $1,400
  • 貴賓價: 9.5$1,330
  • 語言: 英文
  • 頁數: 544
  • 裝訂: Paperback
  • ISBN: 0596001738
  • ISBN-13: 9780596001735
  • 相關分類: Perl 程式語言
  • 立即出貨 (庫存 < 4)




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.


Table of Contents:


1. Best Practices

     Three Goals

     This Book


2. Code Layout



     Subroutines and Variables


     Keys and Indices




     Line Lengths






     Vertical Alignment

     Breaking Long Lines

     Non-Terminal Expressions

     Breaking by Precedence




     Automated Layout

3. Naming Conventions



     Reference Variables

     Arrays and Hashes




     Ambiguous Abbreviations

     Ambiguous Names

     Utility Subroutines

4. Values and Expressions

     String Delimiters

     Empty Strings

     Single-Character Strings

     Escaped Characters


     Leading Zeros

     Long Numbers

     Multiline Strings

     Here Documents

     Heredoc Indentation

     Heredoc Terminators

     Heredoc Quoters


     Fat Commas

     Thin Commas

     Low-Precedence Operators


     List Membership

5. Variables

     Lexical Variables

     Package Variables



     Punctuation Variables

     Localizing Punctuation Variables

     Match Variables


     Array Indices


     Slice Layout

     Slice Factoring

6. Control Structures

     If Blocks

     Postfix Selectors

     Other Postfix Modifiers

     Negative Control Statements

     C-Style Loops

     Unnecessary Subscripting

     Necessary Subscripting

     Iterator Variables

     Non-Lexical Loop Iterators

     List Generation

     List Selections

     List Transformation

     Complex Mappings

     List Processing Side Effects

     Multipart Selections

     Value Switches

     Tabular Ternaries

     do-while Loops

     Linear Coding

     Distributed Control


     Loop Labels

7. Documentation

     Types of Documentation


     Extended Boilerplates




     Technical Documentation


     Algorithmic Documentation

     Elucidating Documentation

     Defensive Documentation

     Indicative Documentation

     Discursive Documentation


8. Built-in Functions


     Reversing Lists

     Reversing Scalars

     Fixed-Width Data

     Separated Data

     Variable-Width Data

     String Evaluations

     Automating Sorts


     Hash Values



     Mapping and Grepping


9. Subroutines

     Call Syntax


     Argument Lists

     Named Arguments

     Missing Arguments

     Default Argument Values

     Scalar Return Values

     Contextual Return Values

     Multi-Contextual Return Values


     Implicit Returns

     Returning Failure

10. I/O


     Indirect Filehandles

     Localizing Filehandles

     Opening Cleanly

     Error Checking


     Input Loops

     Line-Based Input

     Simple Slurping

     Power Slurping

     Standard Input

     Printing to Filehandles

     Simple Prompting


     Power Prompting

     Progress Indicators

     Automatic Progress Indicators


11. References


     Braced References

     Symbolic References

     Cyclic References

12. Regular Expressions

     Extended Formatting

     Line Boundaries

     String Boundaries

     End of String

     Matching Anything

     Lazy Flags

     Brace Delimiters

     Other Delimiters


     Named Characters



     Unconstrained Repetitions

     Capturing Parentheses

     Captured Values

     Capture Variables

     Piecewise Matching

     Tabular Regexes

     Constructing Regexes

     Canned Regexes


     Factoring Alternations


     String Comparisons

13. Error Handling


     Builtin Failures

     Contextual Failure

     Systemic Failure

     Recoverable Failure

     Reporting Failure

     Error Messages

     Documenting Errors

     OO Exceptions

     Volatile Error Messages

     Exception Hierarchies

     Processing Exceptions

     Exception Classes

     Unpacking Exceptions

14. Command-Line Processing

     Command-Line Structure

     Command-Line Conventions


     In-situ Arguments

     Command-Line Processing

     Interface Consistency

     Interapplication Consistency

15. Objects

     Using OO



     Restricted Hashes







     Lvalue Accessors

     Indirect Objects

     Class Interfaces

     Operator Overloading


16. Class Hierarchies



     Blessing Objects

     Constructor Arguments

     Base Class Initialization

     Construction and Destruction

     Automating Class Hierarchies

     Attribute Demolition

     Attribute Building


     Cumulative Methods


17. Modules



     Version Numbers

     Version Requirements


     Declarative Exporting

     Interface Variables

     Creating Modules

     The Standard Library


18. Testing and Debugging

     Test Cases

     Modular Testing

     Test Suites


     What to Test

     Debugging and Testing




     Overriding Strictures

     The Debugger

     Manual Debugging

     Semi-Automatic Debugging

19. Miscellanea

     Revision Control

     Other Languages

     Configuration Files




     Encapsulated Cleverness





     Caching for Optimization



A. Essential Perl Best Practices

B. Perl Best Practices

C. Editor Configurations

D. Recommended Modules and Utilities

E. Bibliography




但是,如果你對自己的專業嚴肅認真,直覺是不夠的。《Perl最佳實踐》的作者Damian Conway解釋,規則、慣例、標準和實踐不僅幫助程式設計師相互溝通和協調,還為思考問題提供了可靠的框架,並提供了一種表達解決方案的共同語言。這在Perl中尤其重要,因為該語言設計了許多完成相同任務的方法,因此支持許多不兼容的方言。




1. 最佳實踐
2. 代碼佈局
3. 命名慣例
4. 數據和控制結構的選擇
5. 程序分解
6. 介面設計和實現
7. 模塊化
8. 面向對象
9. 錯誤處理
10. 測試和調試