Perl Cookbook

Solutions and Examples for Perl Programmers

Tom Christiansen, Nathan Torkington

Publisher: O'Reilly, 1999, 757 pages

ISBN: 1-56592-243-3

Keywords: Perl

Last modified: April 14, 2022, 5:25 p.m.

The Perl Cookbook is a comprehensive collection of problems, solutions, and practical examplesfor anyone programming in Perl. Topics range from beginner questions to techniques that even the most experienced Perl programmer can 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 of examples ranging from brief one-liners to complete applications. Covered topic areas include:

  • 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
  • CGI programming and web automation

These recipes were rigorously reviewed by score of the best minds inside and outside Perl, including Larry Wall, the creator of Perl.

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

Reviews

Perl Cookbook

Reviewed by Roland Buresund

OK ***** (5 out of 10)

Last modified: April 14, 2022, 5:26 p.m.

If you're into Perl, this is maybe OK. Otherwise, it doesn't add much to your knowledge.

Comments

There are currently no comments

New Comment

required

required (not published)

optional

required

captcha

required