You are previewing Perl Cookbook.
O'Reilly logo
Perl Cookbook

Book Description

The Perl Cookbook is a comprehensive collection of problems, solutions, and practical examples for anyone programming in Perl. Topics range from beginner questions to techniques that even the most experienced of Perl programmers will learn from. More than just a collection of tips and tricks, the Perl Cookbook is the long-awaited companion volume to Programming Perl, filled with previously unpublished Perl arcana. The Perl Cookbook contains thousands upon thousands of examples ranging from brief one-liners to complete applications. Covered topic areas spread across nearly four hundred separate "recipes," including:

  • Manipulation of strings, numbers, dates, arrays, and hashes

  • Reading, writing, and updating text and binary files

  • Pattern matching and text substitutions

  • Subroutines, libraries, and modules

  • References, data structures, objects, and classes

  • Signals and exceptions

  • Accessing text, hashes, and SQL databases

  • Screen addressing, menus, and graphical applications

  • Managing other processes

  • Writing secure scripts

  • Client-server programming

  • Internet applications programming with mail, news, ftp, and telnet

These recipes were rigorously reviewed by scores of the best minds inside and outside Perl, foremost of which was Larry Wall, the creator of Perl himself. The Perl Cookbook is written by Tom Christiansen, Perl evangelist and coauthor of the bestselling Programming Perl and Learning Perl; and Nathan Torkington, Perl trainer and co-maintainer of the Perl Frequently Asked Questions list.

Table of Contents

  1. Perl Cookbook
    1. List of Examples
    2. Foreword
    3. Preface
      1. What’s in This Book
      2. Platform Notes
      3. Other Books
      4. Conventions Used in This Book
        1. Programming Conventions
        2. Typesetting Conventions
        3. Documentation Conventions
      5. We’d Like to Hear from You
      6. Acknowledgments
        1. Tom
        2. Nat
    4. 1. Strings
      1. Introduction
      2. Accessing Substrings
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Establishing a Default Value
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Exchanging Values Without Using Temporary Variables
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Converting Between ASCII Characters and Values
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Processing a String One Character at a Time
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Reversing a String by Word or Character
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Expanding and Compressing Tabs
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Expanding Variables in User Input
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. Controlling Case
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. Interpolating Functions and Expressions Within Strings
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. Indenting Here Documents
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      13. Reformatting Paragraphs
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      14. Escaping Characters
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      15. Trimming Blanks from the Ends of a String
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      16. Parsing Comma-Separated Data
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      17. Soundex Matching
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      18. Program: fixstyle
      19. Program: psgrep
    5. 2. Numbers
      1. Introduction
      2. Checking Whether a String Is a Valid Number
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Comparing Floating-Point Numbers
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Rounding Floating-Point Numbers
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Converting Between Binary and Decimal
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Operating on a Series of Integers
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Working with Roman Numerals
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Generating Random Numbers
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Generating Different Random Numbers
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. Making Numbers Even More Random
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. Generating Biased Random Numbers
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. Doing Trigonometry in Degrees, not Radians
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      13. Calculating More Trigonometric Functions
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      14. Taking Logarithms
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      15. Multiplying Matrices
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      16. Using Complex Numbers
        1. Problem
        2. Solution
          1. Manually
          2. Math::Complex
        3. Discussion
        4. See Also
      17. Converting Between Octal and Hexadecimal
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      18. Putting Commas in Numbers
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      19. Printing Correct Plurals
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      20. Program: Calculating Prime Factors
    6. 3. Dates and Times
      1. Introduction
      2. Finding Today’s Date
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Converting DMYHMS to Epoch Seconds
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Converting Epoch Seconds to DMYHMS
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Adding to or Subtracting from a Date
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Difference of Two Dates
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Day in a Week/Month/Year or Week Number
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Parsing Dates and Times from Strings
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Printing a Date
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. High-Resolution Timers
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. Short Sleeps
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. Program: hopdelta
    7. 4. Arrays
      1. Introduction
      2. Specifying a List In Your Program
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Printing a List with Commas
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Changing Array Size
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Doing Something with Every Element in a List
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Iterating Over an Array by Reference
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Extracting Unique Elements from a List
        1. Problem
        2. Solution
          1. Straightforward
          2. Faster
          3. Similar but with user function
          4. Faster but different
          5. Faster and even more different
        3. Discussion
        4. See Also
      8. Finding Elements in One Array but Not Another
        1. Problem
        2. Solution
          1. Straightforward implementation
          2. More idiomatic version
        3. Discussion
        4. See Also
      9. Computing Union, Intersection, or Difference of Unique Lists
        1. Problem
        2. Solution
          1. Simple solution for union and intersection
          2. More idiomatic version
          3. Union, intersection, and symmetric difference
          4. Indirect solution
        3. Discussion
        4. See Also
      10. Appending One Array to Another
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. Reversing an Array
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. Processing Multiple Elements of an Array
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      13. Finding the First List Element That Passes a Test
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      14. Finding All Elements in an Array Matching Certain Criteria
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      15. Sorting an Array Numerically
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      16. Sorting a List by Computable Field
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      17. Implementing a Circular List
        1. Problem
        2. Solution
        3. Procedure
        4. Discussion
        5. See Also
      18. Randomizing an Array
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      19. Program: words
        1. Description
        2. See Also
      20. Program: permute
        1. Problem
        2. See Also
    8. 5. Hashes
      1. Introduction
        1. See Also
      2. Adding an Element to a Hash
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Testing for the Presence of a Key in a Hash
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Deleting from a Hash
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Traversing a Hash
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Printing a Hash
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Retrieving from a Hash in Insertion Order
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Hashes with Multiple Values Per Key
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Inverting a Hash
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. Sorting a Hash
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. Merging Hashes
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. Finding Common or Different Keys in Two Hashes
        1. Problem
        2. Solution
          1. Find common keys
          2. Find keys from one hash that aren’t in both
        3. Discussion
        4. See Also
      13. Hashing References
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      14. Presizing a Hash
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      15. Finding the Most Common Anything
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      16. Representing Relationships Between Data
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      17. Program: dutree
    9. 6. Pattern Matching
      1. Introduction
        1. The Tricky Bits
        2. Pattern-Matching Modifiers
        3. Special Variables
      2. Copying and Substituting Simultaneously
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Matching Letters
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Matching Words
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Commenting Regular Expressions
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Finding the Nth Occurrence of a Match
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Matching Multiple Lines
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Reading Records with a Pattern Separator
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Extracting a Range of Lines
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. Matching Shell Globs as Regular Expressions
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. Speeding Up Interpolated Matches
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. Testing for a Valid Pattern
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      13. Honoring Locale Settings in Regular Expressions
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      14. Approximate Matching
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      15. Matching from Where the Last Pattern Left Off
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      16. Greedy and Non-Greedy Matches
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      17. Detecting Duplicate Words
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      18. Expressing AND, OR, and NOT in a Single Pattern
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      19. Matching Multiple-Byte Characters
        1. Problem
        2. Solution
        3. Discussion
          1. Avoiding false matches
          2. Splitting multiple-byte strings
          3. Validating multiple-byte strings
          4. Converting between encodings
        4. See Also
      20. Matching a Valid Mail Address
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      21. Matching Abbreviations
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      22. Program: urlify
      23. Program: tcgrep
      24. Regular Expression Grabbag
    10. 7. File Access
      1. Introduction
        1. Getting a Handle on the File
        2. Standard FileHandles
        3. I/O Operations
      2. Opening a File
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Opening Files with Unusual Filenames
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Expanding Tildes in Filenames
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Making Perl Report Filenames in Errors
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Creating Temporary Files
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Storing Files Inside Your Program Text
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Writing a Filter
        1. Problem
        2. Solution
        3. Discussion
          1. Behavior
          2. Command-line options
        4. See Also
      9. Modifying a File in Place with Temporary File
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. Modifying a File in Place with -i Switch
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. Modifying a File in Place Without a Temporary File
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. Locking a File
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      13. Flushing Output
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      14. Reading from Many Filehandles Without Blocking
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      15. Doing Non-Blocking I/O
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      16. Determining the Number of Bytes to Read
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      17. Storing Filehandles in Variables
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      18. Caching Open Output Filehandles
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      19. Printing to Many Filehandles Simultaneously
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      20. Opening and Closing File Descriptors by Number
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      21. Copying Filehandles
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      22. Program: netlock
      23. Program: lockarea
    11. 8. File Contents
      1. Introduction
      2. Reading Lines with Continuation Characters
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Counting Lines (or Paragraphs or Records) in a File
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Processing Every Word in a File
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Reading a File Backwards by Line or Paragraph
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Trailing a Growing File
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Picking a Random Line from a File
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Randomizing All Lines
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Reading a Particular Line in a File
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. Processing Variable-Length Text Fields
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. Removing the Last Line of a File
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. Processing Binary Files
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      13. Using Random-Access I/O
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      14. Updating a Random-Access File
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      15. Reading a String from a Binary File
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      16. Reading Fixed-Length Records
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      17. Reading Configuration Files
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      18. Testing a File for Trustworthiness
        1. Problem
        2. Solution
        3. Discussion
      19. Program: tailwtmp
      20. Program: tctee
      21. Program: laston
    12. 9. Directories
      1. Introduction
        1. Executive Summary
      2. Getting and Setting Timestamps
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Deleting a File
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Copying or Moving a File
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Recognizing Two Names for the Same File
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Processing All Files in a Directory
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Globbing, or Getting a List of Filenames Matching a Pattern
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Processing All Files in a Directory Recursively
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Removing a Directory and Its Contents
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. Renaming Files
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. Splitting a Filename into Its Component Parts
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. Program: symirror
      13. Program: lst
    13. 10. Subroutines
      1. Introduction
      2. Accessing Subroutine Arguments
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Making Variables Private to a Function
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Creating Persistent Private Variables
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Determining Current Function Name
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Passing Arrays and Hashes by Reference
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Detecting Return Context
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Passing by Named Parameter
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Skipping Selected Return Values
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. Returning More Than One Array or Hash
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. Returning Failure
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. Prototyping Functions
        1. Problem
        2. Solution
        3. Discussion
          1. Omitting parentheses
          2. Mimicking built-ins
        4. See Also
      13. Handling Exceptions
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      14. Saving Global Values
        1. Problem
        2. Solution
        3. Discussion
          1. Using local() for temporary values for globals
          2. Using local() for local handles
          3. Using local( ) on parts of aggregates
        4. See Also
      15. Redefining a Function
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      16. Trapping Undefined Function Calls with AUTOLOAD
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      17. Nesting Subroutines
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      18. Program: Sorting Your Mail
        1. See Also
    14. 11. References and Records
      1. Introduction
        1. References
        2. Anonymous Data
        3. Records
        4. See Also
      2. Taking References to Arrays
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Making Hashes of Arrays
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Taking References to Hashes
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Taking References to Functions
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Taking References to Scalars
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Creating Arrays of Scalar References
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Using Closures Instead of Objects
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Creating References to Methods
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. Constructing Records
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. Reading and Writing Hash Records to Text Files
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. Printing Data Structures
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      13. Copying Data Structures
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      14. Storing Data Structures to Disk
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      15. Transparently Persistent Data Structures
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      16. Program: Binary Trees
    15. 12. Packages, Libraries, and Modules
      1. Introduction
        1. Modules
        2. Import/Export Regulations
        3. Other Kinds of Library Files
        4. Not Reinventing the Wheel
        5. See Also
      2. Defining a Module’s Interface
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Trapping Errors in require or use
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Delaying use Until Run Time
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Making Variables Private to a Module
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Determining the Caller’s Package
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Automating Module Clean-Up
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Keeping Your Own Module Directory
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Preparing a Module for Distribution
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. Speeding Module Loading with SelfLoader
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. Speeding Up Module Loading with Autoloader
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. Overriding Built-In Functions
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      13. Reporting Errors and Warnings Like Built-Ins
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      14. Referring to Packages Indirectly
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      15. Using h2ph to Translate C #include Files
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      16. Using h2xs to Make a Module with C Code
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      17. Documenting Your Module with Pod
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      18. Building and Installing a CPAN Module
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      19. Example: Module Template
      20. Program: Finding Versions and Descriptions of Installed Modules
    16. 13. Classes, Objects, and Ties
      1. Introduction
        1. Under the Hood
        2. Methods
        3. Inheritance
        4. A Warning on Indirect Object Notation
        5. Some Notes on Object Terminology
        6. Philosophical Aside
        7. See Also
      2. Constructing an Object
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Destroying an Object
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Managing Instance Data
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Managing Class Data
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Using Classes as Structs
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Cloning Objects
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Calling Methods Indirectly
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Determining Subclass Membership
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. Writing an Inheritable Class
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. Accessing Overridden Methods
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. Generating Attribute Methods Using AUTOLOAD
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      13. Solving the Data Inheritance Problem
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      14. Coping with Circular Data Structures
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      15. Overloading Operators
        1. Problem
        2. Solution
        3. Discussion
        4. Example: Overloaded StrNum Class
        5. Example: Overloaded FixNum Class
        6. See Also
      16. Creating Magic Variables with tie
        1. Problem
        2. Solution
        3. Discussion
        4. Tie Example: Outlaw $_
        5. Tie Example: Make a Hash That Always Appends
        6. Tie Example: Case-Insensitive Hash
        7. Tie Example: Hash That Allows Look-Ups by Key or Value
        8. Tie Example: Handle That Counts Access
        9. Tie Example: Multiple Sink Filehandles
        10. See Also
    17. 14. Database Access
      1. Introduction
      2. Making and Using a DBM File
        1. Problem
        2. Solution
          1. dbmopen
          2. tie
        3. Discussion
        4. See Also
      3. Emptying a DBM File
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Converting Between DBM Files
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Merging DBM Files
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Locking DBM Files
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Sorting Large DBM Files
        1. Problem
        2. Solution
        3. Description
        4. See Also
      8. Treating a Text File as a Database Array
        1. Problem
        2. Solution
        3. Description
        4. See Also
      9. Storing Complex Data in a DBM File
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. Persistent Data
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. Executing an SQL Command Using DBI and DBD
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. Program: ggh—Grep Netscape Global History
        1. See Also
    18. 15. User Interfaces
      1. Introduction
      2. Parsing Program Arguments
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Testing Whether a Program Is Running Interactively
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Clearing the Screen
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Determining Terminal or Window Size
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Changing Text Color
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Reading from the Keyboard
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Ringing the Terminal Bell
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Using POSIX termios
        1. Problem
        2. Solution
        3. Description
        4. See Also
      10. Checking for Waiting Input
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. Reading Passwords
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. Editing Input
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      13. Managing the Screen
        1. Problem
        2. Solution
        3. Description
        4. See Also
      14. Controlling Another Program with Expect
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      15. Creating Menus with Tk
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      16. Creating Dialog Boxes with Tk
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      17. Responding to Tk Resize Events
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      18. Removing the DOS Shell Window with Windows Perl/Tk
        1. Problem
        2. Solution
        3. Description
        4. See Also
      19. Program: Small termcap program
        1. Description
        2. See Also
      20. Program: tkshufflepod
    19. 16. Process Management and Communication
      1. Introduction
        1. Process Creation
        2. Signals
      2. Gathering Output from a Program
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Running Another Program
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Replacing the Current Program with a Different One
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Reading or Writing to Another Program
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Filtering Your Own Output
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Preprocessing Input
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Reading STDERR from a Program
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Controlling Input and Output of Another Program
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. Controlling the Input, Output, and Error of Another Program
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. Communicating Between Related Processes
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. Making a Process Look Like a File with Named Pipes
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      13. Sharing Variables in Different Processes
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      14. Listing Available Signals
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      15. Sending a Signal
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      16. Installing a Signal Handler
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      17. Temporarily Overriding a Signal Handler
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      18. Writing a Signal Handler
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      19. Catching Ctrl-C
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      20. Avoiding Zombie Processes
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      21. Blocking Signals
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      22. Timing Out an Operation
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      23. Program: sigrand
        1. Description
    20. 17. Sockets
      1. Introduction
      2. Writing a TCP Client
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Writing a TCP Server
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Communicating over TCP
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Setting Up a UDP Client
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Setting Up a UDP Server
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Using UNIX Domain Sockets
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Identifying the Other End of a Socket
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Finding Your Own Name and Address
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. Closing a Socket After Forking
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. Writing Bidirectional Clients
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. Forking Servers
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      13. Pre-Forking Servers
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      14. Non-Forking Servers
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      15. Writing a Multi-Homed Server
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      16. Making a Daemon Server
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      17. Restarting a Server on Demand
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      18. Program: backsniff
      19. Program: fwdport
        1. See Also
    21. 18. Internet Services
      1. Introduction
      2. Simple DNS Lookups
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Being an FTP Client
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Sending Mail
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Reading and Posting Usenet News Messages
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Reading Mail with POP3
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Simulating Telnet from a Program
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Pinging a Machine
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Using Whois to Retrieve Information from the InterNIC
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. Program: expn and vrfy
    22. 19. CGI Programming
      1. Introduction
        1. Architecture
        2. Behind the Scenes
        3. Security
        4. HTML and Forms
        5. Web-Related Resources
      2. Writing a CGI Script
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Redirecting Error Messages
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Fixing a 500 Server Error
        1. Problem
        2. Solution
        3. Discussion
          1. Make sure the web server can run the script.
          2. Make sure the script has permissions to do what it’s trying to do.
          3. Is the script valid Perl?
          4. Is the script upholding its end of the CGI protocol?
          5. Asking for help elsewhere.
        4. See Also
      5. Writing a Safe CGI Program
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Making CGI Scripts Efficient
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Executing Commands Without Shell Escapes
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Formatting Lists and Tables with HTML Shortcuts
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Redirecting to a Different Location
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. Debugging the Raw HTTP Exchange
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. Managing Cookies
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. Creating Sticky Widgets
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      13. Writing a Multiscreen CGI Script
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      14. Saving a Form to a File or Mail Pipe
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      15. Program: chemiserie
    23. 20. Web Automation
      1. Introduction
      2. Fetching a URL from a Perl Script
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Automating Form Submission
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Extracting URLs
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Converting ASCII to HTML
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Converting HTML to ASCII
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Extracting or Removing HTML Tags
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Finding Stale Links
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Finding Fresh Links
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. Creating HTML Templates
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. Mirroring Web Pages
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. Creating a Robot
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      13. Parsing a Web Server Log File
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      14. Processing Server Logs
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      15. Program: htmlsub
      16. Program: hrefsub
    24. Index
    25. Colophon