Book description
Find a Perl programmer, and you'll find a copy of Perl Cookbook nearby. Perl Cookbook is a comprehensive collection of problems, solutions, and practical examples for anyone programming in Perl. The book contains hundreds of rigorously reviewed Perl "recipes" and thousands of examples ranging from brief one-liners to complete applications.The second edition of Perl Cookbook has been fully updated for Perl 5.8, with extensive changes for Unicode support, I/O layers, mod_perl, and new technologies that have emerged since the previous edition of the book. Recipes have been updated to include the latest modules. New recipes have been added to every chapter of the book, and some chapters have almost doubled in size.Covered topic areas include:
- Manipulating strings, numbers, dates, arrays, and hashes
- Pattern matching and text substitutions
- References, data structures, objects, and classes
- Signals and exceptions
- Screen addressing, menus, and graphical applications
- Managing other processes
- Writing secure scripts
- Client-server programming
- Internet applications programming with mail, news, ftp, and telnet
- CGI and mod_perl programming
- Web programming
Publisher resources
Table of contents
- A Note Regarding Supplemental Files
- Foreword
- Preface
-
1. Strings
- Introduction
- 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
- Introduction
- 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
- Introduction
- 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
- Introduction
- 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
- Introduction
- 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
- Introduction
- 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
- Introduction
- 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
- Introduction
- 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
- Introduction
- 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
- Introduction
- 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
- Introduction
- 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
- Introduction
- 12.1. Defining a Module’s Interface
- 12.2. Trapping Errors in require or use
- 12.3. Delaying use Until Runtime
- 12.4. Making Variables Private to a Module
- 12.5. Making Functions Private to a Module
- 12.6. Determining the Caller’s Package
- 12.7. Automating Module Cleanup
- 12.8. Keeping Your Own Module Directory
- 12.9. Preparing a Module for Distribution
- 12.10. Speeding Module Loading with SelfLoader
- 12.11. Speeding Up Module Loading with Autoloader
- 12.12. Overriding Built-in Functions
- 12.13. Overriding a Built-in Function in All Packages
- 12.14. Reporting Errors and Warnings Like Built-ins
- 12.15. Customizing Warnings
- 12.16. Referring to Packages Indirectly
- 12.17. Using h2ph to Translate C #include Files
- 12.18. Using h2xs to Make a Module with C Code
- 12.19. Writing Extensions in C with Inline::C
- 12.20. Documenting Your Module with Pod
- 12.21. Building and Installing a CPAN Module
- 12.22. Example: Module Template
- 12.23. Program: Finding Versions and Descriptions of Installed Modules
-
13. Classes, Objects, and Ties
- Introduction
- 13.1. Constructing an Object
- 13.2. Destroying an Object
- 13.3. Managing Instance Data
- 13.4. Managing Class Data
- 13.5. Using Classes as Structs
- 13.6. Cloning Constructors
- 13.7. Copy Constructors
- 13.8. Invoking Methods Indirectly
- 13.9. Determining Subclass Membership
- 13.10. Writing an Inheritable Class
- 13.11. Accessing Overridden Methods
- 13.12. Generating Attribute Methods Using AUTOLOAD
- 13.13. Coping with Circular Data Structures Using Objects
- 13.14. Overloading Operators
- 13.15. Creating Magic Variables with tie
-
14. Database Access
- Introduction
- 14.1. Making and Using a DBM File
- 14.2. Emptying a DBM File
- 14.3. Converting Between DBM Files
- 14.4. Merging DBM Files
- 14.5. Sorting Large DBM Files
- 14.6. Storing Complex Data in a DBM File
- 14.7. Persistent Data
- 14.8. Saving Query Results to Excel or CSV
- 14.9. Executing an SQL Command Using DBI
- 14.10. Escaping Quotes
- 14.11. Dealing with Database Errors
- 14.12. Repeating Queries Efficiently
- 14.13. Building Queries Programmatically
- 14.14. Finding the Number of Rows Returned by a Query
- 14.15. Using Transactions
- 14.16. Viewing Data One Page at a Time
- 14.17. Querying a CSV File with SQL
- 14.18. Using SQL Without a Database Server
- 14.19. Program: ggh—Grep Netscape Global History
-
15. Interactivity
- Introduction
- 15.1. Parsing Program Arguments
- 15.2. Testing Whether a Program Is Running Interactively
- 15.3. Clearing the Screen
- 15.4. Determining Terminal or Window Size
- 15.5. Changing Text Color
- 15.6. Reading Single Characters from the Keyboard
- 15.7. Ringing the Terminal Bell
- 15.8. Using POSIX termios
- 15.9. Checking for Waiting Input
- 15.10. Reading Passwords
- 15.11. Editing Input
- 15.12. Managing the Screen
- 15.13. Controlling Another Program with Expect
- 15.14. Creating Menus with Tk
- 15.15. Creating Dialog Boxes with Tk
- 15.16. Responding to Tk Resize Events
- 15.17. Removing the DOS Shell Window with Windows Perl/Tk
- 15.18. Graphing Data
- 15.19. Thumbnailing Images
- 15.20. Adding Text to an Image
- 15.21. Program: Small termcap Program
- 15.22. Program: tkshufflepod
- 15.23. Program: graphbox
-
16. Process Management and Communication
- Introduction
- 16.1. Gathering Output from a Program
- 16.2. Running Another Program
- 16.3. Replacing the Current Program with a Different One
- 16.4. Reading or Writing to Another Program
- 16.5. Filtering Your Own Output
- 16.6. Preprocessing Input
- 16.7. Reading STDERR from a Program
- 16.8. Controlling Input and Output of Another Program
- 16.9. Controlling the Input, Output, and Error of Another Program
- 16.10. Communicating Between Related Processes
- 16.11. Making a Process Look Like a File with Named Pipes
- 16.12. Sharing Variables in Different Processes
- 16.13. Listing Available Signals
- 16.14. Sending a Signal
- 16.15. Installing a Signal Handler
- 16.16. Temporarily Overriding a Signal Handler
- 16.17. Writing a Signal Handler
- 16.18. Catching Ctrl-C
- 16.19. Avoiding Zombie Processes
- 16.20. Blocking Signals
- 16.21. Timing Out an Operation
- 16.22. Turning Signals into Fatal Errors
- 16.23. Program: sigrand
-
17. Sockets
- Introduction
- 17.1. Writing a TCP Client
- 17.2. Writing a TCP Server
- 17.3. Communicating over TCP
- 17.4. Setting Up a UDP Client
- 17.5. Setting Up a UDP Server
- 17.6. Using Unix Domain Sockets
- 17.7. Identifying the Other End of a Socket
- 17.8. Finding Your Own Name and Address
- 17.9. Closing a Socket After Forking
- 17.10. Writing Bidirectional Clients
- 17.11. Forking Servers
- 17.12. Pre-Forking Servers
- 17.13. Non-Forking Servers
- 17.14. Multitasking Server with Threads
- 17.15. Writing a Multitasking Server with POE
- 17.16. Writing a Multihomed Server
- 17.17. Making a Daemon Server
- 17.18. Restarting a Server on Demand
- 17.19. Managing Multiple Streams of Input
- 17.20. Program: backsniff
- 17.21. Program: fwdport
-
18. Internet Services
- Introduction
- 18.1. Simple DNS Lookups
- 18.2. Being an FTP Client
- 18.3. Sending Mail
- 18.4. Reading and Posting Usenet News Messages
- 18.5. Reading Mail with POP3
- 18.6. Simulating Telnet from a Program
- 18.7. Pinging a Machine
- 18.8. Accessing an LDAP Server
- 18.9. Sending Attachments in Mail
- 18.10. Extracting Attachments from Mail
- 18.11. Writing an XML-RPC Server
- 18.12. Writing an XML-RPC Client
- 18.13. Writing a SOAP Server
- 18.14. Writing a SOAP Client
- 18.15. Program: rfrm
- 18.16. Program: expn and vrfy
-
19. CGI Programming
- Introduction
- 19.1. Writing a CGI Script
- 19.2. Redirecting Error Messages
- 19.3. Fixing a 500 Server Error
- 19.4. Writing a Safe CGI Program
- 19.5. Executing Commands Without Shell Escapes
- 19.6. Formatting Lists and Tables with HTML Shortcuts
- 19.7. Redirecting to a Different Location
- 19.8. Debugging the Raw HTTP Exchange
- 19.9. Managing Cookies
- 19.10. Creating Sticky Widgets
- 19.11. Writing a Multiscreen CGI Script
- 19.12. Saving a Form to a File or Mail Pipe
- 19.13. Program: chemiserie
-
20. Web Automation
- Introduction
- 20.1. Fetching a URL from a Perl Script
- 20.2. Automating Form Submission
- 20.3. Extracting URLs
- 20.4. Converting ASCII to HTML
- 20.5. Converting HTML to ASCII
- 20.6. Extracting or Removing HTML Tags
- 20.7. Finding Stale Links
- 20.8. Finding Fresh Links
- 20.9. Using Templates to Generate HTML
- 20.10. Mirroring Web Pages
- 20.11. Creating a Robot
- 20.12. Parsing a Web Server Log File
- 20.13. Processing Server Logs
- 20.14. Using Cookies
- 20.15. Fetching Password-Protected Pages
- 20.16. Fetching https:// Web Pages
- 20.17. Resuming an HTTP GET
- 20.18. Parsing HTML
- 20.19. Extracting Table Data
- 20.20. Program: htmlsub
- 20.21. Program: hrefsub
-
21. mod_perl
- Introduction
- 21.1. Authenticating
- 21.2. Setting Cookies
- 21.3. Accessing Cookie Values
- 21.4. Redirecting the Browser
- 21.5. Interrogating Headers
- 21.6. Accessing Form Parameters
- 21.7. Receiving Uploaded Files
- 21.8. Speeding Up Database Access
- 21.9. Customizing Apache’s Logging
- 21.10. Transparently Storing Information in URLs
- 21.11. Communicating Between mod_perl and PHP
- 21.12. Migrating from CGI to mod_perl
- 21.13. Sharing Information Between Handlers
- 21.14. Reloading Changed Modules
- 21.15. Benchmarking a mod_perl Application
- 21.16. Templating with HTML::Mason
- 21.17. Templating with Template Toolkit
-
22. XML
- Introduction
- 22.1. Parsing XML into Data Structures
- 22.2. Parsing XML into a DOM Tree
- 22.3. Parsing XML into SAX Events
- 22.4. Making Simple Changes to Elements or Text
- 22.5. Validating XML
- 22.6. Finding Elements and Text Within an XML Document
- 22.7. Processing XML Stylesheet Transformations
- 22.8. Processing Files Larger Than Available Memory
- 22.9. Reading and Writing RSS Files
- 22.10. Writing XML
- About the Authors
- Colophon
- Copyright
Product information
- Title: Perl Cookbook, 2nd Edition
- Author(s):
- Release date: August 2003
- Publisher(s): O'Reilly Media, Inc.
- ISBN: 9780596003135
You might also like
book
Perl Cookbook
The Perl Cookbook is a comprehensive collection of problems, solutions, and practical examples for anyone programming …
book
Mastering Perl, 2nd Edition
Take the next step toward Perl mastery with advanced concepts that make coding easier, maintenance simpler, …
book
Learning Perl, 7th Edition
If you’re just getting started with Perl, this is the book you want—whether you’re a programmer, …
book
Programming Perl, 4th Edition
Adopted as the undisputed Perl bible soon after the first edition appeared in 1991, Programming Perl …