Perl Cookbook, 2nd Edition is a comprehensive collection of problems, solutions, and practical examples for anyone programming in Perl. The book contains hundreds of rigorously reviewed Perl "recipes" for manipulating strings, numbers, dates, arrays, and hashes; pattern matching and text substitutions; references, data structures, objects, and classes; signals and exceptions; and much more. In the second edition of this classic book, the authors have revisited the topic areas to add and revise many recipes, and they have also written new chapters to cover emerging topics such as XML processing and embedding programs with the Inline module.
Foreword
Preface
1. Strings
1.
1 Accessing Substrings
1.
2 Establishing a Default Value
1.
3 Exchanging Values Without Using Temporary Variables
1.
4 Converting Between Characters and Values
1.
5 Using Named Unicode Characters
1.
6 Processing a String One Character at a Time
1.
7 Reversing a String by Word or Character
1.
8 Treating Unicode Combined Characters as Single Characters
1.
9 Canonicalizing Strings with Unicode Combined Characters
1.
10 Treating a Unicode String as Octets
1.
11 Expanding and Compressing Tabs
1.
12 Expanding Variables in User Input
1.
13 Controlling Case
1.
14 Properly Capitalizing a Title or Headline
1.
15 Interpolating Functions and Expressions Within Strings
1.
16 Indenting Here Documents
1.
17 Reformatting Paragraphs
1.
18 Escaping Characters
1.
19 Trimming Blanks from the Ends of a String
1.
20 Parsing Comma-Separated Data
1.
21 Constant Variables
1.
22 Soundex Matching
1.
23 Program: fixstyle
1.
24 Program: psgrep
2. Numbers
2.
1 Checking Whether a String Is a Valid Number
2.
2 Rounding Floating-Point Numbers
2.
3 Comparing Floating-Point Numbers
2.
4 Operating on a Series of Integers
2.
5 Working with Roman Numerals
2.
6 Generating Random Numbers
2.
7 Generating Repeatable Random Number Sequences
2.
8 Making Numbers Even More Random
2.
9 Generating Biased Random Numbers
2.
10 Doing Trigonometry in Degrees, Not Radians
2.
11 Calculating More Trigonometric Functions
2.
12 Taking Logarithms
2.
13 Multiplying Matrices
2.
14 Using Complex Numbers
2.
15 Converting Binary, Octal, and Hexadecimal Numbers
2.
16 Putting Commas in Numbers
2.
17 Printing Correct Plurals
2.
18 Program: Calculating Prime Factors
3. Dates and Times
3.
1 Finding Today''s Date
3.
2 Converting DMYHMS to Epoch Seconds
3.
3 Converting Epoch Seconds to DMYHMS
3.
4 Adding to or Subtracting from a Date
3.
5 Difference of Two Dates
3.
6 Day in a Week/Month/Year or Week Number
3.
7 Parsing Dates and Times from Strings
3.
8 Printing a Date
3.
9 High-Resolution Timers
3.
10 Short Sleeps
3.
11 Program: hopdelta
4. Arrays
4.
1 Specifying a List in Your Program
4.
2 Printing a List with Commas
4.
3 Changing Array Size
4.
4 Implementing a Sparse Array
4.
5 Iterating Over an Array
4.
6 Iterating Over an Array by Reference
4.
7 Extracting Unique Elements from a List
4.
8 Finding Elements in One Array but Not Another
4.
9 Computing Union, Intersection, or Difference of Unique Lists
4.
10 Appending One Array to Another
4.
11 Reversing an Array
4.
12 Processing Multiple Elements of an Array
4.
13 Finding the First List Element That Passes a Test
4.
14 Finding All Elements in an Array Matching Certain Criteria
4.
15 Sorting an Array Numerically
4.
16 Sorting a List by Computable Field
4.
17 Implementing a Circular List
4.
18 Randomizing an Array
4.
19 Program: words
4.
20 Program: permute
5. Hashes
5.
1 Adding an Element to a Hash
5.
2 Testing for the Presence of a Key in a Hash
5.
3 Creating a Hash with Immutable Keys or Values
5.
4 Deleting from a Hash
5.
5 Traversing a Hash
5.
6 Printing a Hash
5.
7 Retrieving from a Hash in Insertion Order
5.
8 Hashes with Multiple Values per Key
5.
9 Inverting a Hash
5.
10 Sorting a Hash
5.
11 Merging Hashes
5.
12 Finding Common or Different Keys in Two Hashes
5.
13 Hashing References
5.
14 Presizing a Hash
5.
15 Finding the Most Common Anything
5.
16 Representing Relationships Between Data
5.
17 Program: dutree
6. Pattern Matching
6.
1 Copying and Substituting Simultaneously
6.
2 Matching Letters
6.
3 Matching Words
6.
4 Commenting Regular Expressions
6.
5 Finding the Nth Occurrence of a Match
6.
6 Matching Within Multiple Lines
6.
7 Reading Records with a Separator
6.
8 Extracting a Range of Lines
6.
9 Matching Shell Globs as Regular Expressions
6.
10 Speeding Up Interpolated Matches
6.
11 Testing for a Valid Pattern
6.
12 Honoring Locale Settings in Regular Expressions
6.
13 Approximate Matching
6.
14 Matching from Where the Last Pattern Left Off
6.
15 Greedy and Non-Greedy Matches
6.
16 Detecting Doubled Words
6.
17 Matching Nested Patterns
6.
18 Expressing AND, OR, and NOT in a Single Pattern
6.
19 Matching a Valid Mail Address
6.
20 Matching Abbreviations
6.
21 Program: urlify
6.
22 Program: tcgrep
6.
23 Regular Expression Grab Bag
7. File Access
7.
1 Opening a File
7.
2 Opening Files with Unusual Filenames
7.
3 Expanding Tildes in Filenames
7.
4 Making Perl Report Filenames in Error Messages
7.
5 Storing Filehandles into Variables
7.
6 Writing a Subroutine That Takes Filehandles as Built-ins Do
7.
7 Caching Open Output Filehandles
7.
8 Printing to Many Filehandles Simultaneously
7.
9 Opening and Closing File Descriptors by Number
7.
10 Copying Filehandles
7.
11 Creating Temporary Files
7.
12 Storing a File Inside Your Program Text
7.
13 Storing Multiple Files in the DATA Area
7.
14 Writing a Unix-Style Filter Program
7.
15 Modifying a File in Place with a Temporary File
7.
16 Modifying a File in Place with the -i Switch
7.
17 Modifying a File in Place Without a Temporary File
7.
18 Locking a File
7.
19 Flushing Output
7.
20 Doing Non-Blocking I/O
7.
21 Determining the Number of Unread Bytes
7.
22 Reading from Many Filehandles Without Blocking
7.
23 Reading an Entire Line Without Blocking
7.
24 Program: netlock
7.
25 Program: lockarea
8. File Contents
8.
1 Reading Lines with Continuation Characters
8.
2 Counting Lines (or Paragraphs or Records) in a File
8.
3 Processing Every Word in a File
8.
4 Reading a File Backward by Line or Paragraph
8.
5 Trailing a Growing File
8.
6 Picking a Random Line from a File
8.
7 Randomizing All Lines
8.
8 Reading a Particular Line in a File
8.
9 Processing Variable-Length Text Fields
8.
10 Removing the Last Line of a File
8.
11 Processing Binary Files
8.
12 Using Random-Access I/O
8.
13 Updating a Random-Access File
8.
14 Reading a String from a Binary File
8.
15 Reading Fixed-Length Records
8.
16 Reading Configuration Files
8.
17 Testing a File for Trustworthiness
8.
18 Treating a File as an Array
8.
19 Setting the Default I/O Layers
8.
20 Reading or Writing Unicode from a Filehandle
8.
21 Converting Microsoft Text Files into Unicode
8.
22 Comparing the Contents of Two Files
8.
23 Pretending a String Is a File
8.
24 Program: tailwtmp
8.
25 Program: tctee
8.
26 Program: laston
8.
27 Program: Flat File Indexes
9. Directories
9.
1 Getting and Setting Timestamps
9.
2 Deleting a File
9.
3 Copying or Moving a File
9.
4 Recognizing Two Names for the Same File
9.
5 Processing All Files in a Directory
9.
6 Globbing, or Getting a List of Filenames Matching a Pattern
9.
7 Processing All Files in a Directory Recursively
9.
8 Removing a Directory and Its Contents
9.
9 Renaming Files
9.
10 Splitting a Filename into Its Component Parts
9.
11 Working with Symbolic File Permissions Instead of Octal Values
9.
12 Program: symirror
9.
13 Program: lst
10. Subroutines
10.
1 Accessing Subroutine Arguments
10.
2 Making Variables Private to a Function
10.
3 Creating Persistent Private Variables
10.
4 Determining Current Function Name
10.
5 Passing Arrays and Hashes by Reference
10.
6 Detecting Return Context
10.
7 Passing by Named Parameter
10.
8 Skipping Selected Return Values
10.
9 Returning More Than One Array or Hash
10.
10 Returning Failure
10.
11 Prototyping Functions
10.
12 Handling Exceptions
10.
13 Saving Global Values
10.
14 Redefining a Function
10.
15 Trapping Undefined Function Calls with AUTOLOAD
10.
16 Nesting Subroutines
10.
17 Writing a Switch Statement
10.
18 Program: Sorting Your Mail
11. References and Records
11.
1 Taking References to Arrays
11.
2 Making Hashes of Arrays
11.
3 Taking References to Hashes
11.
4 Taking References to Functions
11.
5 Taking References to Scalars
11.
6 Creating Arrays of Scalar References
11.
7 Using Closures Instead of Objects
11.
8 Creating References to Methods
11.
9 Constructing Records
11.
10 Reading and Writing Hash Records to Text Files
11.
11 Printing Data Structures
11.
12 Copying Data Structures
11.
13 Storing Data Structures to Disk
11.
14 Transparently Persistent Data Structures
11.
15 Coping with Circular Data Structures Using Weak References
11.
16 Program: Outlines
11.
17 Program: Binary Trees
12. Packages, Libraries, and Modules
12.
1 Defining a Module''s Inte