Mastering Perl, 2nd Edition

Book description

Take the next step toward Perl mastery with advanced concepts that make coding easier, maintenance simpler, and execution faster. Mastering Perl isn't a collection of clever tricks, but a way of thinking about Perl programming for solving debugging, configuration, and many other real-world problems you’ll encounter as a working programmer.

The third in O’Reilly’s series of landmark Perl tutorials (after Learning Perl and Intermediate Perl), this fully upated edition pulls everything together and helps you bend Perl to your will.

  • Explore advanced regular expressions features
  • Avoid common problems when writing secure programs
  • Profile and benchmark Perl programs to see where they need work
  • Wrangle Perl code to make it more presentable and readable
  • Understand how Perl keeps track of package variables
  • Define subroutines on the fly
  • Jury-rig modules to fix code without editing the original source
  • Use bit operations and bit vectors to store large data efficiently
  • Learn how to detect errors that Perl doesn’t report
  • Dive into logging, data persistence, and the magic of tied variables

Publisher resources

View/Submit Errata

Table of contents

  1. Preface
    1. Becoming a Master
    2. What It Means to Be a Master
    3. Who Should Read This Book
    4. How to Read This Book
    5. What Should You Know Already?
    6. What I Cover
    7. What I Don’t Cover
    8. Structure of This Book
    9. Conventions Used in This Book
    10. Using Code Examples
    11. Safari® Books Online
    12. How to Contact Us
    13. Acknowledgments
  2. 1. Advanced Regular Expressions
    1. Readable Regexes, /x and (?#…)
    2. Global Matching
      1. Global Match Anchors
    3. Recursive Regular Expressions
      1. Repeating a Subpattern
    4. Lookarounds
      1. Lookahead Assertions, (?=PATTERN) and (?!PATTERN)
        1. Positive lookahead assertions
        2. Negative lookahead assertions
      2. Lookbehind Assertions, (?<!PATTERN) and (?<=PATTERN)
    5. Debugging Regular Expressions
      1. The -D Switch
    6. Summary
    7. Further Reading
  3. 2. Secure Programming Techniques
    1. Bad Data Can Ruin Your Day
    2. Taint Checking
      1. Warnings Instead of Fatal Errors
      2. Automatic Taint Mode
      3. mod_perl
      4. Tainted Data
      5. Side Effects of Taint Checking
    3. Untainting Data
      1. IO::Handle::untaint
      2. Hash Keys
      3. Taint::Util
      4. Choosing Untainted Data with Tainted Data
      5. Symbolic References
    4. Defensive Database Programming with DBI
    5. List Forms of system and exec
      1. Three-Argument open
      2. sysopen
      3. Limit Special Privileges
    6. Safe Compartments
      1. Safe Limitations
    7. A Little Fun
    8. Summary
    9. Further Reading
  4. 3. Perl Debuggers
    1. Before You Waste Too Much Time
    2. The Best Debugger in the World
      1. Safely Changing Modules
      2. Wrapping Subroutines
    3. The Perl Debugger
    4. Alternative Debuggers
      1. Using a Different Debugger with -d
      2. Devel::ptkdb
      3. Devel::ebug
      4. Devel::hdb
    5. IDE Debuggers
      1. EPIC
      2. Komodo
    6. Summary
    7. Further Reading
  5. 4. Profiling Perl
    1. Finding the Culprit
    2. The General Approach
    3. Profiling DBI
      1. Other DBI::Profile Reports
      2. Making It Even Easier
      3. Switching Databases
    4. Devel::NYTProf
    5. Writing My Own Profiler
      1. Devel::LineCounter
    6. Profiling Test Suites
      1. Devel::Cover
    7. Summary
    8. Further Reading
  6. 5. Benchmarking Perl
    1. Benchmarking Theory
    2. Benchmarking Time
    3. Comparing Code
    4. Don’t Turn Off Your Thinking Cap
    5. Isolating the Environment
    6. Handling Outliers
    7. Memory Use
    8. The perlbench Tool
    9. Summary
    10. Further Reading
  7. 6. Cleaning Up Perl
    1. Good Style
    2. perltidy
    3. Deobfuscation
      1. De-encoding Hidden Source
      2. Unparsing Code with B::Deparse
    4. Perl::Critic
      1. Creating My Own Perl::Critic Policy
    5. Summary
    6. Further Reading
  8. 7. Symbol Tables and Typeglobs
    1. Package and Lexical Variables
      1. Getting the Package Version
    2. The Symbol Table
      1. Typeglobs
      2. Aliasing
      3. Filehandle Arguments in Older Code
      4. Naming Anonymous Subroutines
    3. The Easy Way
    4. Summary
    5. Further Reading
  9. 8. Dynamic Subroutines
    1. Subroutines as Data
    2. Creating and Replacing Named Subroutines
    3. Symbolic References
    4. Iterating Through Subroutine Lists
    5. Processing Pipelines
    6. Self-Referencing Anonymous Subroutines
    7. Method Lists
    8. Subroutines as Arguments
    9. Autoloaded Methods
      1. Hashes as Objects
      2. AutoSplit
    10. Summary
    11. Further Reading
  10. 9. Modifying and Jury-Rigging Modules
    1. Choosing the Right Solution
      1. Sending Patches to the Author
      2. Local Patches
      3. Taking Over a Module
      4. Forking
      5. Starting Over on My Own
    2. Replacing Module Parts
    3. Subclassing
      1. An ExtUtils::MakeMaker Example
      2. Other Examples
    4. Wrapping Subroutines
    5. Summary
    6. Further Reading
  11. 10. Configuring Perl Programs
    1. Things Not to Do
      1. Code in a Separate File
    2. Better Ways
      1. Environment Variables
      2. Special Environment Variables
      3. Turning on Extra Output
    3. Command-Line Switches
      1. The -s Switch
      2. Getopt Modules
        1. Getopt::Std
        2. Getopt::Long
    4. Configuration Files
      1. ConfigReader::Simple
      2. Config::IniFiles
      3. Config::Scoped
      4. Other Configuration Formats
    5. Scripts with a Different Name
    6. Interactive and Noninteractive Programs
    7. perl’s Config
      1. Different Operating Systems
    8. Summary
    9. Further Reading
  12. 11. Detecting and Reporting Errors
    1. Perl Error Basics
      1. Operating System Errors
      2. Child Process Errors
      3. Errors Specific to the Operating System
    2. Reporting Module Errors
      1. Separation of Concerns
    3. Exceptions
      1. eval
      2. Multiple Levels of die
      3. die with a Reference
      4. Propagating Objects with die
      5. Clobbering $@
      6. autodie
    4. Reporting the Culprit
    5. Catching Exceptions
      1. Try::Tiny
      2. TryCatch
    6. Polymorphic Return Values
    7. Summary
    8. Further Reading
  13. 12. Logging
    1. Recording Errors and Other Information
    2. Log4perl
      1. Subroutine Arguments
      2. Configuring Log4perl
        1. Adding my own information
      3. Persistent Configuration
      4. Logging Categories
        1. Categories are hierarchical
      5. Other Log::Log4perl Features
    3. Summary
    4. Further Reading
  14. 13. Data Persistence
    1. Perl-Specific Formats
      1. pack
      2. Fixed-Length Records
      3. Unpacking Binary Formats
      4. Data::Dumper
      5. Similar Modules
    2. Storable
      1. Freezing Data
      2. Storable’s Security Problem
    3. Sereal
    4. DBM Files
      1. dbmopen
      2. DBM::Deep
    5. Perl-Agnostic Formats
      1. JSON
      2. YAML
        1. YAML module variants
      3. MessagePack
    6. Summary
    7. Further Reading
  15. 14. Working with Pod
    1. The Pod Format
      1. Directives
      2. Encoding
      3. Body Elements
    2. Translating Pod
      1. Pod Translators
      2. Pod::Perldoc::ToToc
      3. Pod::Simple
      4. Subclassing Pod::Simple
      5. Pod in Your Web Server
    3. Testing Pod
      1. Checking Pod
      2. Pod Coverage
      3. Hiding and Ignoring Functions
    4. Summary
    5. Further Reading
  16. 15. Working with Bits
    1. Binary Numbers
      1. Writing in Binary
    2. Bit Operators
      1. Unary NOT (~)
      2. Bitwise AND (&)
      3. Binary OR (|)
      4. Exclusive OR (^)
      5. Left << and Right >> Shift Operators
    3. Bit Vectors
    4. The vec Function
      1. Bit String Storage
      2. Storing DNA
      3. Checking Primes
    5. Keeping Track of Things
    6. Summary
    7. Further Reading
  17. 16. The Magic of Tied Variables
    1. They Look Like Normal Variables
    2. At the User Level
    3. Behind the Curtain
    4. Scalars
      1. Tie::Cycle
      2. Bounded Integers
      3. Self-Destructing Values
    5. Arrays
      1. Reinventing Arrays
      2. Something a Bit More Realistic
    6. Hashes
    7. Filehandles
    8. Summary
    9. Further Reading
  18. 17. Modules as Programs
    1. The main Thing
    2. Backing Up
    3. Who’s Calling?
    4. Testing the Program
      1. Modules as Tests
    5. Creating a Program Distribution
      1. Adding to the Script
    6. Distributing the Programs
    7. Summary
    8. Further Reading
  19. A. Further Reading
    1. Perl Books
    2. Non-Perl Books
  20. B. brian’s Guide to Solving Any Perl Problem
    1. My Philosophy of Problem-Solving
    2. My Method
    3. Some Stupid Things I’ve Done and Still Do
  21. Index of Perl Modules in This Book
  22. Index
  23. Colophon
  24. Copyright

Product information

  • Title: Mastering Perl, 2nd Edition
  • Author(s): brian d foy
  • Release date: January 2014
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781449364960