You are previewing Programming Perl, 4th Edition.
O'Reilly logo
Programming Perl, 4th Edition

Book Description

Adopted as the undisputed Perl bible soon after the first edition appeared in 1991, Programming Perl is still the go-to guide for this highly practical language. In this much-anticipated update to "the Camel," three renowned Perl authors cover the language up to its current version, Perl 5.14, with a preview of features in the upcoming 5.16.

Table of Contents

  1. Programming Perl
  2. Preface
    1. The Pursuit of Happiness
    2. What’s New in This Edition
    3. The Standard Distribution
    4. Online Documentation
      1. Navigating the Standard Manpages
      2. Non-Perl Manpages
    5. Offline Documentation
    6. Additional Resources
      1. Perl on the Web
      2. Bug Reports
    7. Conventions Used in This Book
    8. Acknowledgments
    9. Safari® Books Online
    10. We’d Like to Hear from You
  3. I. Overview
    1. 1. An Overview of Perl
      1. Getting Started
      2. Natural and Artificial Languages
        1. Variable Syntax
          1. Singularities
          2. Pluralities
            1. Arrays
            2. Hashes
          3. Complexities
          4. Simplicities
        2. Verbs
      3. An Average Example
        1. How to Do It
      4. Filehandles
      5. Operators
        1. Some Binary Arithmetic Operators
        2. String Operators
        3. Assignment Operators
        4. Unary Arithmetic Operators
        5. Logical Operators
        6. Some Numeric and String Comparison Operators
        7. Some File Test Operators
      6. Control Structures
        1. What Is Truth?
          1. The if and unless statements
        2. The given and when Statements
        3. Looping Constructs
          1. Conditional loops
          2. The three-part loop
          3. The foreach loop
          4. Breaking out: next and last
      7. Regular Expressions
        1. Quantifiers
        2. Minimal Matching
        3. Nailing Things Down
        4. Backreferences
      8. List Processing
      9. What You Don’t Know Won’t Hurt You (Much)
  4. II. The Gory Details
    1. 2. Bits and Pieces
      1. Atoms
      2. Molecules
      3. Built-in Data Types
      4. Variables
      5. Names
        1. Name Lookups
      6. Scalar Values
        1. Numeric Literals
        2. String Literals
        3. Pick Your Own Quotes
        4. Or Leave Out the Quotes Entirely
        5. Interpolating Array Values
        6. “Here” Documents
        7. Version Literals
        8. Other Literal Tokens
      7. Context
        1. Scalar and List Context
        2. Boolean Context
        3. Void Context
        4. Interpolative Context
      8. List Values and Arrays
        1. List Assignment
        2. Array Length
      9. Hashes
      10. Typeglobs and Filehandles
      11. Input Operators
        1. Command Input (Backtick) Operator
        2. Line Input (Angle) Operator
        3. Filename Globbing Operator
    2. 3. Unary and Binary Operators
      1. Terms and List Operators (Leftward)
      2. The Arrow Operator
      3. Autoincrement and Autodecrement
      4. Exponentiation
      5. Ideographic Unary Operators
      6. Binding Operators
      7. Multiplicative Operators
      8. Additive Operators
      9. Shift Operators
      10. Named Unary and File Test Operators
      11. Relational Operators
      12. Equality Operators
      13. Smartmatch Operator
        1. Smartmatching of Objects
      14. Bitwise Operators
      15. C-Style Logical (Short-Circuit) Operators
      16. Range Operators
      17. Conditional Operator
      18. Assignment Operators
      19. Comma Operators
      20. List Operators (Rightward)
      21. Logical and, or, not, and xor
      22. C Operators Missing from Perl
    3. 4. Statements and Declarations
      1. Simple Statements
      2. Compound Statements
      3. if and unless Statements
      4. The given Statement
        1. The when Statement and Modifier
      5. Loop Statements
        1. while and until Statements
        2. Three-Part Loops
        3. foreach Loops
        4. Loop Control
        5. Bare Blocks as Loops
        6. Loopy Topicalizers
      6. The goto Operator
      7. Paleolithic Perl Case Structures
      8. The Ellipsis Statement
      9. Global Declarations
      10. Scoped Declarations
        1. Scoped Variable Declarations
        2. Lexically Scoped Variables: my
        3. Persistent Lexically Scoped Variables: state
        4. Lexically Scoped Global Declarations: our
        5. Dynamically Scoped Variables: local
      11. Pragmas
        1. Controlling Warnings
        2. Controlling the Use of Globals
    4. 5. Pattern Matching
      1. The Regular Expression Bestiary
      2. Pattern-Matching Operators
        1. Pattern Modifiers
        2. The m// Operator (Matching)
        3. The s/// Operator (Substitution)
          1. Modifying strings en passant
          2. Modifying arrays en masse
          3. When a global substitution just isn’t global enough
        4. The tr/// Operator (Transliteration)
      3. Metacharacters and Metasymbols
        1. Metasymbol Tables
        2. Specific Characters
        3. Wildcard Metasymbols
      4. Character Classes
        1. Bracketed Character Classes
        2. Classic Perl Character Class Shortcuts
        3. Character Properties
        4. POSIX-Style Character Classes
      5. Quantifiers
      6. Positions
        1. Beginnings: The \A and ^ Assertions
        2. Endings: The \z, \Z, and $ Assertions
        3. Boundaries: The \b and \B Assertions
        4. Progressive Matching
        5. Where You Left Off: The \G Assertion
      7. Grouping and Capturing
        1. Capturing
          1. Named capture groups
        2. Grouping Without Capturing
        3. Scoped Pattern Modifiers
      8. Alternation
      9. Staying in Control
        1. Letting Perl Do the Work
        2. Variable Interpolation
          1. When backslashes happen
          2. The qr/PATTERN/modifiers quote regex operator
        3. The Regex Compiler
        4. The Little Engine That /Could(n’t)?/
      10. Fancy Patterns
        1. Lookaround Assertions
        2. Possessive Groups
        3. Programmatic Patterns
          1. Generated patterns
          2. Substitution evaluations
          3. Match-time code evaluation
          4. Match-time pattern interpolation
          5. Conditional interpolation
        4. Recursive Patterns
        5. Grammatical Patterns
          1. Grammars
        6. Defining Your Own Assertions
        7. Alternate Engines
    5. 6. Unicode
      1. Show, Don’t Tell
      2. Getting at Unicode Data
        1. The Encode Module
      3. A Case of Mistaken Identity
      4. Graphemes and Normalization
      5. Comparing and Sorting Unicode Text
        1. Using the uca with Perl’s sort
        2. Locale Sorting
      6. More Goodies
        1. Custom Regex Boundaries
        2. Building Character
      7. References
    6. 7. Subroutines
      1. Syntax
      2. Semantics
        1. Tricks with Parameter Lists
        2. Error Indications
        3. Scoping Issues
      3. Passing References
      4. Prototypes
        1. Inlining Constant Functions
        2. Care with Prototypes
        3. Prototypes of Built-in Functions
      5. Subroutine Attributes
        1. The method Attribute
        2. The lvalue Attribute
    7. 8. References
      1. What Is a Reference?
      2. Creating References
        1. The Backslash Operator
        2. Anonymous Data
          1. The anonymous array composer
          2. The anonymous hash composer
          3. The anonymous subroutine composer
        3. Object Constructors
        4. Handle References
        5. Symbol Table References
        6. Implicit Creation of References
      3. Using Hard References
        1. Using a Variable As a Variable Name
        2. Using a block As a Variable Name
        3. Using the Arrow Operator
        4. Using Object Methods
        5. Pseudohashes
        6. Other Tricks You Can Do with Hard References
        7. Closures
          1. Closures as function templates
          2. Nested subroutines
      4. Symbolic References
      5. Braces, Brackets, and Quoting
        1. References Don’t Work As Hash Keys
        2. Garbage Collection, Circular References, and Weak References
    8. 9. Data Structures
      1. Arrays of Arrays
        1. Creating and Accessing a Two-Dimensional Array
        2. Growing Your Own
        3. Access and Printing
        4. Slices
        5. Common Mistakes
      2. Hashes of Arrays
        1. Composition of a Hash of Arrays
        2. Generation of a Hash of Arrays
        3. Access and Printing of a Hash of Arrays
      3. Arrays of Hashes
        1. Composition of an Array of Hashes
        2. Generation of an Array of Hashes
        3. Access and Printing of an Array of Hashes
      4. Hashes of Hashes
        1. Composition of a Hash of Hashes
        2. Generation of a Hash of Hashes
        3. Access and Printing of a Hash of Hashes
      5. Hashes of Functions
      6. More Elaborate Records
        1. Composition, Access, and Printing of More Elaborate Records
        2. Composition, Access, and Printing of Even More Elaborate Records
        3. Generation of a Hash of Complex Records
      7. Saving Data Structures
    9. 10. Packages
      1. Symbol Tables
      2. Qualified Names
      3. The Default Package
      4. Changing the Package
      5. Autoloading
    10. 11. Modules
      1. Loading Modules
      2. Unloading Modules
      3. Creating Modules
        1. Naming Modules
        2. A Sample Module
        3. Module Privacy and the Exporter
          1. Exporting without using Exporter’s import method
          2. Version checking
          3. Managing unknown symbols
          4. Tag-handling utility functions
      4. Overriding Built-in Functions
    11. 12. Objects
      1. Brief Refresher on Object-Oriented Lingo
      2. Perl’s Object System
      3. Method Invocation
        1. Method Invocation Using the Arrow Operator
        2. Method Invocation Using Indirect Objects
        3. Syntactic Snafus with Indirect Objects
        4. Package-Quoted Classes
      4. Object Construction
        1. Inheritable Constructors
        2. Initializers
      5. Class Inheritance
        1. Inheritance Through @ISA
        2. Alternate Method Searching
        3. Accessing Overridden Methods
        4. UNIVERSAL: The Ultimate Ancestor Class
        5. Method Autoloading
        6. Private Methods
      6. Instance Destructors
        1. Garbage Collection with destroy Methods
      7. Managing Instance Data
        1. Generating Accessors with Autoloading
        2. Generating Accessors with Closures
        3. Using Closures for Private Objects
        4. New Tricks
      8. Managing Class Data
      9. The Moose in the Room
      10. Summary
    12. 13. Overloading
      1. The overload Pragma
      2. Overload Handlers
      3. Overloadable Operators
      4. The Copy Constructor (=)
      5. When an Overload Handler Is Missing (nomethod and fallback)
      6. Overloading Constants
      7. Public Overload Functions
      8. Inheritance and Overloading
      9. Runtime Overloading
      10. Overloading Diagnostics
    13. 14. Tied Variables
      1. Tying Scalars
        1. Scalar-Tying Methods
        2. Magical Counter Variables
        3. Cycling Through Values
        4. Magically Banishing $_
      2. Tying Arrays
        1. Array-Tying Methods
        2. Notational Convenience
      3. Tying Hashes
        1. Hash-Tying Methods
      4. Tying Filehandles
        1. Filehandle-Tying Methods
        2. Creative Filehandles
      5. A Subtle Untying Trap
      6. Tie Modules on CPAN
  5. III. Perl as Technology
    1. 15. Interprocess Communication
      1. Signals
        1. Signalling Process Groups
        2. Reaping Zombies
        3. Timing Out Slow Operations
        4. Blocking Signals
        5. Signal Safety
      2. Files
        1. File Locking
        2. Passing Filehandles
      3. Pipes
        1. Anonymous Pipes
        2. Talking to Yourself
        3. Bidirectional Communication
        4. Named Pipes
      4. System V IPC
      5. Sockets
        1. Networking Clients
        2. Networking Servers
        3. Message Passing
    2. 16. Compiling
      1. The Life Cycle of a Perl Program
      2. Compiling Your Code
      3. Executing Your Code
      4. Compiler Backends
      5. Code Generators
        1. The Bytecode Generator
        2. The C Code Generators
      6. Code Development Tools
      7. Avant-Garde Compiler, Retro Interpreter
    3. 17. The Command-Line Interface
      1. Command Processing
        1. #! and Quoting on Non-Unix Systems
        2. Location of Perl
        3. Switches
      2. Environment Variables
    4. 18. The Perl Debugger
      1. Using the Debugger
      2. Debugger Commands
        1. Stepping and Running
        2. Breakpoints
        3. Tracing
        4. Display
        5. Locating Code
        6. Actions and Command Execution
        7. Miscellaneous Commands
      3. Debugger Customization
        1. Editor Support for Debugging
        2. Customizing with Init Files
        3. Debugger Options
      4. Unattended Execution
      5. Debugger Support
        1. Writing Your Own Debugger
      6. Profiling Perl
        1. Devel::DProf
        2. Devel::NYTProf
    5. 19. CPAN
      1. History
      2. A Tour of the Repository
        1. Creating a MiniCPAN
      3. The CPAN Ecosystem
        1. PAUSE
        2. Searching CPAN
        3. Testing
        4. Bug Tracking
          1. rt.cpan.org
          2. Other bug tracking
          3. perlbug
          4. rt.perl.org
      4. Installing CPAN Modules
        1. By Hand
        2. CPAN Clients
          1. cpan
          2. cpanp
          3. cpanminus
      5. Creating CPAN Distributions
        1. Starting Your Distribution
          1. h2xs
          2. Distribution::Cooker
          3. Module::Starter
          4. Dist::Zilla
        2. Testing Your Modules
          1. Internal testing
          2. External testing
  6. IV. Perl as Culture
    1. 20. Security
      1. Handling Insecure Data
        1. Detecting and Laundering Tainted Data
        2. Cleaning Up Your Environment
        3. Accessing Commands and Files Under Reduced Privileges
        4. Defeating Taint Checking
      2. Handling Timing Glitches
        1. Unix Kernel Security Bugs
        2. Handling Race Conditions
        3. Temporary Files
      3. Handling Insecure Code
        1. Changing Root
        2. Safe Compartments
          1. Restricting namespace access
          2. Restricting operator access
          3. Safe examples
        3. Code Masquerading As Data
    2. 21. Common Practices
      1. Common Goofs for Novices
        1. Universal Blunders
        2. Frequently Ignored Advice
        3. C Traps
        4. Shell Traps
        5. Python Traps
        6. Ruby Traps
        7. Java Traps
      2. Efficiency
        1. Time Efficiency
        2. Space Efficiency
        3. Programmer Efficiency
        4. Maintainer Efficiency
        5. Porter Efficiency
        6. User Efficiency
      3. Programming with Style
      4. Fluent Perl
      5. Program Generation
        1. Generating Other Languages in Perl
        2. Generating Perl in Other Languages
        3. Source Filters
    3. 22. Portable Perl
      1. Newlines
      2. Endianness and Number Width
      3. Files and Filesystems
      4. System Interaction
      5. Interprocess Communication (IPC)
      6. External Subroutines (XS)
      7. Standard Modules
      8. Dates and Times
      9. Internationalization
      10. Style
    4. 23. Plain Old Documentation
      1. Pod in a Nutshell
        1. Verbatim Paragraphs
        2. Command Paragraphs
        3. Flowed Text
      2. Pod Translators and Modules
      3. Writing Your Own Pod Tools
      4. Pod Pitfalls
      5. Documenting Your Perl Programs
    5. 24. Perl Culture
      1. History Made Practical
      2. Perl Poetry
      3. Virtues of the Perl Programmer
      4. Events
      5. Getting Help
  7. V. Reference Material
    1. 25. Special Names
      1. Special Names Grouped by Type
        1. Regular Expression Special Variables
        2. Per-Filehandle Variables
        3. Per-Package Special Variables
        4. Program-Wide Special Variables
        5. Per-Package Special Filehandles
        6. Per-Package Special Functions
      2. Special Variables in Alphabetical Order
    2. 26. Formats
      1. String Formats
      2. Binary Formats
        1. pack
          1. Repetition
          2. Other modifiers
          3. More examples
        2. unpack
      3. Picture Formats
        1. Format Variables
        2. Footers
        3. Accessing Formatting Internals
    3. 27. Functions
      1. Perl Functions by Category
      2. Perl Functions in Alphabetical Order (A–G)
        1. abs
        2. accept
        3. alarm
        4. atan2
        5. bind
        6. binmode
        7. bless
        8. break
        9. caller
        10. chdir
        11. chmod
        12. chomp
        13. chop
        14. chown
        15. chr
        16. chroot
        17. close
        18. closedir
        19. connect
        20. continue
        21. cos
        22. crypt
        23. dbmclose
        24. dbmopen
        25. defined
        26. delete
        27. die
        28. do (block)
        29. do (file)
        30. do (subroutine)
        31. dump
        32. each
        33. eof
        34. eval
        35. exec
        36. exists
        37. exit
        38. exp
        39. _ _FILE_ _
        40. fc
        41. fcntl
        42. fileno
        43. flock
        44. fork
        45. format
        46. formline
        47. getc
        48. getgrent
        49. getgrgid
        50. getgrnam
        51. gethostbyaddr
        52. gethostbyname
        53. gethostent
        54. getlogin
        55. getnetbyaddr
        56. getnetbyname
        57. getnetent
        58. getpeername
        59. getpgrp
        60. getppid
        61. getpriority
        62. getprotobyname
        63. getprotobynumber
        64. getprotoent
        65. getpwent
        66. getpwnam
        67. getpwuid
        68. getservbyname
        69. getservbyport
        70. getservent
        71. getsockname
        72. getsockopt
        73. glob
        74. gmtime
        75. goto
        76. grep
      3. Perl Functions in Alphabetical Order (H–R)
        1. hex
        2. import
        3. index
        4. int
        5. ioctl
        6. join
        7. keys
        8. kill
        9. last
        10. lc
        11. lcfirst
        12. length
        13. _ _LINE_ _
        14. link
        15. listen
        16. local
        17. localtime
        18. lock
        19. log
        20. lstat
        21. m//
        22. map
        23. mkdir
        24. msgctl
        25. msgget
        26. msgrcv
        27. msgsnd
        28. my
        29. new
        30. next
        31. no
        32. oct
        33. open
        34. opendir
        35. ord
        36. our
        37. pack
        38. package
        39. _ _PACKAGE_ _
        40. pipe
        41. pop
        42. pos
        43. print
        44. printf
        45. prototype
        46. push
        47. q/STRING/
        48. quotemeta
        49. rand
        50. read
        51. readdir
        52. readline
        53. readlink
        54. readpipe
        55. recv
        56. redo
        57. ref
        58. rename
        59. require
        60. reset
        61. return
        62. reverse
        63. rewinddir
        64. rindex
        65. rmdir
      4. Perl Functions in Alphabetical Order (S–Y)
        1. s///
        2. say
        3. scalar
        4. seek
        5. seekdir
        6. select (output filehandle)
        7. select (ready file descriptors)
        8. semctl
        9. semget
        10. semop
        11. send
        12. setpgrp
        13. setpriority
        14. setsockopt
        15. shift
        16. shmctl
        17. shmget
        18. shmread
        19. shmwrite
        20. shutdown
        21. sin
        22. sleep
        23. socket
        24. socketpair
        25. sort
        26. splice
        27. split
        28. sprintf
        29. sqrt
        30. srand
        31. stat
        32. state
        33. study
        34. sub
        35. substr
        36. symlink
        37. syscall
        38. sysopen
        39. sysread
        40. sysseek
        41. system
        42. syswrite
        43. tell
        44. telldir
        45. tie
        46. tied
        47. time
        48. times
        49. tr///
        50. truncate
        51. uc
        52. ucfirst
        53. umask
        54. undef
        55. unlink
        56. unpack
        57. unshift
        58. untie
        59. use
        60. utime
        61. values
        62. vec
        63. wait
        64. waitpid
        65. wantarray
        66. warn
        67. write
        68. y//
    4. 28. The Standard Perl Library
      1. Library Science
      2. A Tour of the Perl Library
        1. Roll Call
        2. The Future of the Standard Perl Library
        3. Wandering the Stacks
    5. 29. Pragmatic Modules
      1. attributes
      2. autodie
      3. autouse
      4. base
      5. bigint
      6. bignum
      7. bigrat
      8. blib
      9. bytes
      10. charnames
        1. Custom Character Names
        2. Runtime Lookups
      11. constant
        1. Restrictions on constant
      12. deprecate
      13. diagnostics
      14. encoding
      15. feature
      16. fields
      17. filetest
      18. if
      19. inc::latest
      20. integer
      21. less
      22. lib
      23. locale
      24. mro
      25. open
      26. ops
      27. overload
      28. overloading
      29. parent
      30. re
      31. sigtrap
        1. Signal Handlers
        2. Predefined Signal Lists
        3. Other Arguments to sigtrap
        4. Examples of sigtrap
      32. sort
      33. strict
        1. strict "refs"
        2. strict "vars"
        3. strict "subs"
      34. subs
      35. threads
      36. utf8
      37. vars
      38. version
      39. vmsish
        1. exit
        2. hushed
        3. status
        4. time
      40. warnings
      41. User-Defined Pragmas
  8. Glossary
  9. Index of Perl Modules in This Book
  10. Index
  11. About the Authors
  12. Colophon
  13. Copyright