Perl Cookbook
Tips and Tricks for Perl Programmers
By
Tom Christiansen,
Nat Torkington
August 1998
Pages: 794
| Table of Contents
| Index
| Sample Chapter
| Colophon
Table of Contents
-
Chapter 1 Strings
-
Introduction
-
Accessing Substrings
-
Establishing a Default Value
-
Exchanging Values Without Using Temporary Variables
-
Converting Between ASCII Characters and Values
-
Processing a String One Character at a Time
-
Reversing a String by Word or Character
-
Expanding and Compressing Tabs
-
Expanding Variables in User Input
-
Controlling Case
-
Interpolating Functions and Expressions Within Strings
-
Indenting Here Documents
-
Reformatting Paragraphs
-
Escaping Characters
-
Trimming Blanks from the Ends of a String
-
Parsing Comma-Separated Data
-
Soundex Matching
-
Program: fixstyle
-
Program: psgrep
-
Chapter 2 Numbers
-
Introduction
-
Checking Whether a String Is a Valid Number
-
Comparing Floating-Point Numbers
-
Rounding Floating-Point Numbers
-
Converting Between Binary and Decimal
-
Operating on a Series of Integers
-
Working with Roman Numerals
-
Generating Random Numbers
-
Generating Different Random Numbers
-
Making Numbers Even More Random
-
Generating Biased Random Numbers
-
Doing Trigonometry in Degrees, not Radians
-
Calculating More Trigonometric Functions
-
Taking Logarithms
-
Multiplying Matrices
-
Using Complex Numbers
-
Converting Between Octal and Hexadecimal
-
Putting Commas in Numbers
-
Printing Correct Plurals
-
Program: Calculating Prime Factors
-
Chapter 3 Dates and Times
-
Introduction
-
Finding Today's Date
-
Converting DMYHMS to Epoch Seconds
-
Converting Epoch Seconds to DMYHMS
-
Adding to or Subtracting from a Date
-
Difference of Two Dates
-
Day in a Week/Month/Year or Week Number
-
Parsing Dates and Times from Strings
-
Printing a Date
-
High-Resolution Timers
-
Short Sleeps
-
Program: hopdelta
-
Chapter 4 Arrays
-
Introduction
-
Specifying a List In Your Program
-
Printing a List with Commas
-
Changing Array Size
-
Doing Something with Every Element in a List
-
Iterating Over an Array by Reference
-
Extracting Unique Elements from a List
-
Finding Elements in One Array but Not Another
-
Computing Union, Intersection, or Difference of Unique Lists
-
Appending One Array to Another
-
Reversing an Array
-
Processing Multiple Elements of an Array
-
Finding the First List Element That Passes a Test
-
Finding All Elements in an Array Matching Certain Criteria
-
Sorting an Array Numerically
-
Sorting a List by Computable Field
-
Implementing a Circular List
-
Randomizing an Array
-
Program: words
-
Program: permute
-
Chapter 5 Hashes
-
Introduction
-
Adding an Element to a Hash
-
Testing for the Presence of a Key in a Hash
-
Deleting from a Hash
-
Traversing a Hash
-
Printing a Hash
-
Retrieving from a Hash in Insertion Order
-
Hashes with Multiple Values Per Key
-
Inverting a Hash
-
Sorting a Hash
-
Merging Hashes
-
Finding Common or Different Keys in Two Hashes
-
Hashing References
-
Presizing a Hash
-
Finding the Most Common Anything
-
Representing Relationships Between Data
-
Program: dutree
-
Chapter 6 Pattern Matching
-
Introduction
-
Copying and Substituting Simultaneously
-
Matching Letters
-
Matching Words
-
Commenting Regular Expressions
-
Finding the Nth Occurrence of a Match
-
Matching Multiple Lines
-
Reading Records with a Pattern Separator
-
Extracting a Range of Lines
-
Matching Shell Globs as Regular Expressions
-
Speeding Up Interpolated Matches
-
Testing for a Valid Pattern
-
Honoring Locale Settings in Regular Expressions
-
Approximate Matching
-
Matching from Where the Last Pattern Left Off
-
Greedy and Non-Greedy Matches
-
Detecting Duplicate Words
-
Expressing AND, OR, and NOT in a Single Pattern
-
Matching Multiple-Byte Characters
-
Matching a Valid Mail Address
-
Matching Abbreviations
-
Program: urlify
-
Program: tcgrep
-
Regular Expression Grabbag
-
Chapter 7 File Access
-
Introduction
-
Opening a File
-
Opening Files with Unusual Filenames
-
Expanding Tildes in Filenames
-
Making Perl Report Filenames in Errors
-
Creating Temporary Files
-
Storing Files Inside Your Program Text
-
Writing a Filter
-
Modifying a File in Place with Temporary File
-
Modifying a File in Place with -i Switch
-
Modifying a File in Place Without a Temporary File
-
Locking a File
-
Flushing Output
-
Reading from Many Filehandles Without Blocking
-
Doing Non-Blocking I/O
-
Determining the Number of Bytes to Read
-
Storing Filehandles in Variables
-
Caching Open Output Filehandles
-
Printing to Many Filehandles Simultaneously
-
Opening and Closing File Descriptors by Number
-
Copying Filehandles
-
Program: netlock
-
Program: lockarea
-
Chapter 8 File Contents
-
Introduction
-
Reading Lines with Continuation Characters
-
Counting Lines (or Paragraphs or Records) in a File
-
Processing Every Word in a File
-
Reading a File Backwards by Line or Paragraph
-
Trailing a Growing File
-
Picking a Random Line from a File
-
Randomizing All Lines
-
Reading a Particular Line in a File
-
Processing Variable-Length Text Fields
-
Removing the Last Line of a File
-
Processing Binary Files
-
Using Random-Access I/O
-
Updating a Random-Access File
-
Reading a String from a Binary File
-
Reading Fixed-Length Records
-
Reading Configuration Files
-
Testing a File for Trustworthiness
-
Program: tailwtmp
-
Program: tctee
-
Program: laston
-
Chapter 9 Directories
-
Introduction
-
Getting and Setting Timestamps
-
Deleting a File
-
Copying or Moving a File
-
Recognizing Two Names for the Same File
-
Processing All Files in a Directory
-
Globbing, or Getting a List of Filenames Matching a Pattern
-
Processing All Files in a Directory Recursively
-
Removing a Directory and Its Contents
-
Renaming Files
-
Splitting a Filename into Its Component Parts
-
Program: symirror
-
Program: lst
-
Chapter 10 Subroutines
-
Introduction
-
Accessing Subroutine Arguments
-
Making Variables Private to a Function
-
Creating Persistent Private Variables
-
Determining Current Function Name
-
Passing Arrays and Hashes by Reference
-
Detecting Return Context
-
Passing by Named Parameter
-
Skipping Selected Return Values
-
Returning More Than One Array or Hash
-
Returning Failure
-
Prototyping Functions
-
Handling Exceptions
-
Saving Global Values
-
Redefining a Function
-
Trapping Undefined Function Calls with AUTOLOAD
-
Nesting Subroutines
-
Program: Sorting Your Mail
-
Chapter 11 References and Records
-
Introduction
-
Taking References to Arrays
-
Making Hashes of Arrays
-
Taking References to Hashes
-
Taking References to Functions
-
Taking References to Scalars
-
Creating Arrays of Scalar References
-
Using Closures Instead of Objects
-
Creating References to Methods
-
Constructing Records
-
Reading and Writing Hash Records to Text Files
-
Printing Data Structures
-
Copying Data Structures
-
Storing Data Structures to Disk
-
Transparently Persistent Data Structures
-
Program: Binary Trees
-
Chapter 12 Packages, Libraries, and Modules
-
Introduction
-
Defining a Module's Interface
-
Trapping Errors in require or use
-
Delaying use Until Run Time
-
Making Variables Private to a Module
-
Determining the Caller's Package
-
Automating Module Clean-Up
-
Keeping Your Own Module Directory
-
Preparing a Module for Distribution
-
Speeding Module Loading with SelfLoader
-
Speeding Up Module Loading with Autoloader
-
Overriding Built-In Functions
-
Reporting Errors and Warnings Like Built-Ins
-
Referring to Packages Indirectly
-
Using h2ph to Translate C #include Files
-
Using h2xs to Make a Module with C Code
-
Documenting Your Module with Pod
-
Building and Installing a CPAN Module
-
Example: Module Template
-
Program: Finding Versions and Descriptions of Installed Modules
-
Chapter 13 Classes, Objects, and Ties
-
Introduction
-
Constructing an Object
-
Destroying an Object
-
Managing Instance Data
-
Managing Class Data
-
Using Classes as Structs
-
Cloning Objects
-
Calling Methods Indirectly
-
Determining Subclass Membership
-
Writing an Inheritable Class
-
Accessing Overridden Methods
-
Generating Attribute Methods Using AUTOLOAD
-
Solving the Data Inheritance Problem
-
Coping with Circular Data Structures
-
Overloading Operators
-
Creating Magic Variables with tie
-
Chapter 14 Database Access
-
Introduction
-
Making and Using a DBM File
-
Emptying a DBM File
-
Converting Between DBM Files
-
Merging DBM Files
-
Locking DBM Files
-
Sorting Large DBM Files
-
Treating a Text File as a Database Array
-
Storing Complex Data in a DBM File
-
Persistent Data
-
Executing an SQL Command Using DBI and DBD
-
Program: ggh—Grep Netscape Global History
-
Chapter 15 User Interfaces
-
Introduction
-
Parsing Program Arguments
-
Testing Whether a Program Is Running Interactively
-
Clearing the Screen
-
Determining Terminal or Window Size
-
Changing Text Color
-
Reading from the Keyboard
-
Ringing the Terminal Bell
-
Using POSIX termios
-
Checking for Waiting Input
-
Reading Passwords
-
Editing Input
-
Managing the Screen
-
Controlling Another Program with Expect
-
Creating Menus with Tk
-
Creating Dialog Boxes with Tk
-
Responding to Tk Resize Events
-
Removing the DOS Shell Window with Windows Perl/Tk
-
Program: Small termcap program
-
Program: tkshufflepod
-
Chapter 16 Process Management and Communication
-
Introduction
-
Gathering Output from a Program
-
Running Another Program
-
Replacing the Current Program with a
Different One
-
Reading or Writing to Another Program
-
Filtering Your Own Output
-
Preprocessing Input
-
Reading STDERR from a Program
-
Controlling Input and Output of Another Program
-
Controlling the Input, Output, and Error of Another Program
-
Communicating Between Related Processes
-
Making a Process Look Like a File with Named Pipes
-
Sharing Variables in Different Processes
-
Listing Available Signals
-
Sending a Signal
-
Installing a Signal Handler
-
Temporarily Overriding a Signal Handler
-
Writing a Signal Handler
-
Catching Ctrl-C
-
Avoiding Zombie Processes
-
Blocking Signals
-
Timing Out an Operation
-
Program: sigrand
-
Chapter 17 Sockets
-
Introduction
-
Writing a TCP Client
-
Writing a TCP Server
-
Communicating over TCP
-
Setting Up a UDP Client
-
Setting Up a UDP Server
-
Using UNIX Domain Sockets
-
Identifying the Other End of a Socket
-
Finding Your Own Name and Address
-
Closing a Socket After Forking
-
Writing Bidirectional Clients
-
Forking Servers
-
Pre-Forking Servers
-
Non-Forking Servers
-
Writing a Multi-Homed Server
-
Making a Daemon Server
-
Restarting a Server on Demand
-
Program: backsniff
-
Program: fwdport
-
Chapter 18 Internet Services
-
Introduction
-
Simple DNS Lookups
-
Being an FTP Client
-
Sending Mail
-
Reading and Posting Usenet News Messages
-
Reading Mail with POP3
-
Simulating Telnet from a Program
-
Pinging a Machine
-
Using Whois to Retrieve Information from the InterNIC
-
Program: expn and vrfy
-
Chapter 19 CGI Programming
-
Introduction
-
Writing a CGI Script
-
Redirecting Error Messages
-
Fixing a 500 Server Error
-
Writing a Safe CGI Program
-
Making CGI Scripts Efficient
-
Executing Commands Without Shell Escapes
-
Formatting Lists and Tables with HTML Shortcuts
-
Redirecting to a Different Location
-
Debugging the Raw HTTP Exchange
-
Managing Cookies
-
Creating Sticky Widgets
-
Writing a Multiscreen CGI Script
-
Saving a Form to a File or Mail Pipe
-
Program: chemiserie
-
Chapter 20 Web Automation
-
Introduction
-
Fetching a URL from a Perl Script
-
Automating Form Submission
-
Extracting URLs
-
Converting ASCII to HTML
-
Converting HTML to ASCII
-
Extracting or Removing HTML Tags
-
Finding Stale Links
-
Finding Fresh Links
-
Creating HTML Templates
-
Mirroring Web Pages
-
Creating a Robot
-
Parsing a Web Server Log File
-
Processing Server Logs
-
Program: htmlsub
-
Program: hrefsub
-
Colophon
Return to Perl Cookbook