Buying Options
Perl Cookbook, Second Edition
Print $49.95
Add to Cart
Print+Ebook $54.95
Add to Cart
Ebook $39.99
Add to Cart
Safari Books Online
Add to Cart
What is this?
Print £38.50
Add to Cart
What is this?

Product Editions

  1. Perl Cookbook, Second Edition - August 2003
  2. Perl Cookbook - August 1998
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.
Full Description
Table of Contents
  1. Chapter 1 Strings

    1. Introduction

    2. Accessing Substrings

    3. Establishing a Default Value

    4. Exchanging Values Without Using Temporary Variables

    5. Converting Between Characters and Values

    6. Using Named Unicode Characters

    7. Processing a String One Character at a Time

    8. Reversing a String by Word or Character

    9. Treating Unicode Combined Characters as Single Characters

    10. Canonicalizing Strings with Unicode Combined Characters

    11. Treating a Unicode String as Octets

    12. Expanding and Compressing Tabs

    13. Expanding Variables in User Input

    14. Controlling Case

    15. Properly Capitalizing a Title or Headline

    16. Interpolating Functions and Expressions Within Strings

    17. Indenting Here Documents

    18. Reformatting Paragraphs

    19. Escaping Characters

    20. Trimming Blanks from the Ends of a String

    21. Parsing Comma-Separated Data

    22. Constant Variables

    23. Soundex Matching

    24. Program: fixstyle

    25. Program: psgrep

  2. Chapter 2 Numbers

    1. Introduction

    2. Checking Whether a String Is a Valid Number

    3. Rounding Floating-Point Numbers

    4. Comparing Floating-Point Numbers

    5. Operating on a Series of Integers

    6. Working with Roman Numerals

    7. Generating Random Numbers

    8. Generating Repeatable Random Number Sequences

    9. Making Numbers Even More Random

    10. Generating Biased Random Numbers

    11. Doing Trigonometry in Degrees, Not Radians

    12. Calculating More Trigonometric Functions

    13. Taking Logarithms

    14. Multiplying Matrices

    15. Using Complex Numbers

    16. Converting Binary, Octal, and Hexadecimal Numbers

    17. Putting Commas in Numbers

    18. Printing Correct Plurals

    19. Program: Calculating Prime Factors

  3. Chapter 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. Chapter 4 Arrays

    1. Introduction

    2. Specifying a List in Your Program

    3. Printing a List with Commas

    4. Changing Array Size

    5. Implementing a Sparse Array

    6. Iterating Over an Array

    7. Iterating Over an Array by Reference

    8. Extracting Unique Elements from a List

    9. Finding Elements in One Array but Not Another

    10. Computing Union, Intersection, or Difference of Unique Lists

    11. Appending One Array to Another

    12. Reversing an Array

    13. Processing Multiple Elements of an Array

    14. Finding the First List Element That Passes a Test

    15. Finding All Elements in an Array Matching Certain Criteria

    16. Sorting an Array Numerically

    17. Sorting a List by Computable Field

    18. Implementing a Circular List

    19. Randomizing an Array

    20. Program: words

    21. Program: permute

  5. Chapter 5 Hashes

    1. Introduction

    2. Adding an Element to a Hash

    3. Testing for the Presence of a Key in a Hash

    4. Creating a Hash with Immutable Keys or Values

    5. Deleting from a Hash

    6. Traversing a Hash

    7. Printing a Hash

    8. Retrieving from a Hash in Insertion Order

    9. Hashes with Multiple Values per Key

    10. Inverting a Hash

    11. Sorting a Hash

    12. Merging Hashes

    13. Finding Common or Different Keys in Two Hashes

    14. Hashing References

    15. Presizing a Hash

    16. Finding the Most Common Anything

    17. Representing Relationships Between Data

    18. Program: dutree

  6. Chapter 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 Within Multiple Lines

    8. Reading Records with a Separator

    9. Extracting a Range of Lines

    10. Matching Shell Globs as Regular Expressions

    11. Speeding Up Interpolated Matches

    12. Testing for a Valid Pattern

    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 Doubled Words

    18. Matching Nested Patterns

    19. Expressing AND, OR, and NOT in a Single Pattern

    20. Matching a Valid Mail Address

    21. Matching Abbreviations

    22. Program: urlify

    23. Program: tcgrep

    24. Regular Expression Grab Bag

  7. Chapter 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 Error Messages

    6. Storing Filehandles into Variables

    7. Writing a Subroutine That Takes Filehandles as Built-ins Do

    8. Caching Open Output Filehandles

    9. Printing to Many Filehandles Simultaneously

    10. Opening and Closing File Descriptors by Number

    11. Copying Filehandles

    12. Creating Temporary Files

    13. Storing a File Inside Your Program Text

    14. Storing Multiple Files in the DATA Area

    15. Writing a Unix-Style Filter Program

    16. Modifying a File in Place with a Temporary File

    17. Modifying a File in Place with the -i Switch

    18. Modifying a File in Place Without a Temporary File

    19. Locking a File

    20. Flushing Output

    21. Doing Non-Blocking I/O

    22. Determining the Number of Unread Bytes

    23. Reading from Many Filehandles Without Blocking

    24. Reading an Entire Line Without Blocking

    25. Program: netlock

    26. Program: lockarea

  8. Chapter 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 Backward 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. Treating a File as an Array

    20. Setting the Default I/O Layers

    21. Reading or Writing Unicode from a Filehandle

    22. Converting Microsoft Text Files into Unicode

    23. Comparing the Contents of Two Files

    24. Pretending a String Is a File

    25. Program: tailwtmp

    26. Program: tctee

    27. Program: laston

    28. Program: Flat File Indexes

  9. Chapter 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. Working with Symbolic File Permissions Instead of Octal Values

    13. Program: symirror

    14. Program: lst

  10. Chapter 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 Values

    15. Redefining a Function

    16. Trapping Undefined Function Calls with AUTOLOAD

    17. Nesting Subroutines

    18. Writing a Switch Statement

    19. Program: Sorting Your Mail

  11. Chapter 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. Coping with Circular Data Structures Using Weak References

    17. Program: Outlines

    18. Program: Binary Trees

  12. Chapter 12 Packages, Libraries, and Modules

    1. Introduction

    2. Defining a Module's Interface

    3. Trapping Errors in require or use

    4. Delaying use Until Runtime

    5. Making Variables Private to a Module

    6. Making Functions Private to a Module

    7. Determining the Caller's Package

    8. Automating Module Cleanup

    9. Keeping Your Own Module Directory

    10. Preparing a Module for Distribution

    11. Speeding Module Loading with SelfLoader

    12. Speeding Up Module Loading with Autoloader

    13. Overriding Built-in Functions

    14. Overriding a Built-in Function in All Packages

    15. Reporting Errors and Warnings Like Built-ins

    16. Customizing Warnings

    17. Referring to Packages Indirectly

    18. Using h2ph to Translate C #include Files

    19. Using h2xs to Make a Module with C Code

    20. Writing Extensions in C with Inline::C

    21. Documenting Your Module with Pod

    22. Building and Installing a CPAN Module

    23. Example: Module Template

    24. Program: Finding Versions and Descriptions of Installed Modules

  13. Chapter 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 Constructors

    8. Copy Constructors

    9. Invoking Methods Indirectly

    10. Determining Subclass Membership

    11. Writing an Inheritable Class

    12. Accessing Overridden Methods

    13. Generating Attribute Methods Using AUTOLOAD

    14. Coping with Circular Data Structures Using Objects

    15. Overloading Operators

    16. Creating Magic Variables with tie

  14. Chapter 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. Sorting Large DBM Files

    7. Storing Complex Data in a DBM File

    8. Persistent Data

    9. Saving Query Results to Excel or CSV

    10. Executing an SQL Command Using DBI

    11. Escaping Quotes

    12. Dealing with Database Errors

    13. Repeating Queries Efficiently

    14. Building Queries Programmatically

    15. Finding the Number of Rows Returned by a Query

    16. Using Transactions

    17. Viewing Data One Page at a Time

    18. Querying a CSV File with SQL

    19. Using SQL Without a Database Server

    20. Program: ggh—Grep Netscape Global History

  15. Chapter 15 Interactivity

    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 Single Characters 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 Windows Perl/Tk

    19. Graphing Data

    20. Thumbnailing Images

    21. Adding Text to an Image

    22. Program: Small termcap Program

    23. Program: tkshufflepod

    24. Program: graphbox

  16. Chapter 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. Turning Signals into Fatal Errors

    24. Program: sigrand

  17. Chapter 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. Multitasking Server with Threads

    16. Writing a Multitasking Server with POE

    17. Writing a Multihomed Server

    18. Making a Daemon Server

    19. Restarting a Server on Demand

    20. Managing Multiple Streams of Input

    21. Program: backsniff

    22. Program: fwdport

  18. Chapter 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. Accessing an LDAP Server

    10. Sending Attachments in Mail

    11. Extracting Attachments from Mail

    12. Writing an XML-RPC Server

    13. Writing an XML-RPC Client

    14. Writing a SOAP Server

    15. Writing a SOAP Client

    16. Program: rfrm

    17. Program: expn and vrfy

  19. Chapter 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. Executing Commands Without Shell Escapes

    7. Formatting Lists and Tables with HTML Shortcuts

    8. Redirecting to a Different Location

    9. Debugging the Raw HTTP Exchange

    10. Managing Cookies

    11. Creating Sticky Widgets

    12. Writing a Multiscreen CGI Script

    13. Saving a Form to a File or Mail Pipe

    14. Program: chemiserie

  20. Chapter 20 Web Automation

    1. Introduction

    2. Fetching a URL from a Perl Script

    3. Automating 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. Using Templates to Generate HTML

    11. Mirroring Web Pages

    12. Creating a Robot

    13. Parsing a Web Server Log File

    14. Processing Server Logs

    15. Using Cookies

    16. Fetching Password-Protected Pages

    17. Fetching https:// Web Pages

    18. Resuming an HTTP GET

    19. Parsing HTML

    20. Extracting Table Data

    21. Program: htmlsub

    22. Program: hrefsub

  21. Chapter 21 mod_perl

    1. Introduction

    2. Authenticating

    3. Setting Cookies

    4. Accessing Cookie Values

    5. Redirecting the Browser

    6. Interrogating Headers

    7. Accessing Form Parameters

    8. Receiving Uploaded Files

    9. Speeding Up Database Access

    10. Customizing Apache's Logging

    11. Transparently Storing Information in URLs

    12. Communicating Between mod_perl and PHP

    13. Migrating from CGI to mod_perl

    14. Sharing Information Between Handlers

    15. Reloading Changed Modules

    16. Benchmarking a mod_perl Application

    17. Templating with HTML::Mason

    18. Templating with Template Toolkit

  22. Chapter 22 XML

    1. Introduction

    2. Parsing XML into Data Structures

    3. Parsing XML into a DOM Tree

    4. Parsing XML into SAX Events

    5. Making Simple Changes to Elements or Text

    6. Validating XML

    7. Finding Elements and Text Within an XML Document

    8. Processing XML Stylesheet Transformations

    9. Processing Files Larger Than Available Memory

    10. Reading and Writing RSS Files

    11. Writing XML

  1. Colophon

View Full Table of Contents
Product Details
Title:
Perl Cookbook, Second Edition
By:
Tom Christiansen, Nathan Torkington
Publisher:
O'Reilly Media
Formats:
  • Print
  • Ebook
  • Safari Books Online
Print Release:
August 2003
Ebook Release:
February 2009
Pages:
976
Print ISBN:
978-0-596-00313-5
| ISBN 10:
0-596-00313-7
Ebook ISBN:
978-0-596-15888-0
| ISBN 10:
0-596-15888-2
Customer Reviews
About the Authors
  1. Tom Christiansen

    Tom Christiansen is a freelance consultant specializing in Perl training and writing. After working for several years for TSR Hobbies (of Dungeons and Dragons fame), he set off for college where he spent a year in Spain and five in America, dabbling in music, linguistics, programming, and some half-dozen different spoken languages. Tom finally escaped UW-Madison with B.A.s in Spanish and computer science and an M.S. in computer science. He then spent five years at Convex as a jack-of-all-trades working on everything from system administration to utility and kernel development, with customer support and training thrown in for good measure. Tom also served two terms on the USENIX Association Board of directors. With over fifteen years' experience in UNIX system administration and programming, Tom presents seminars internationally. Living in the foothills above Boulder, Colorado, surrounded by mule deer, skunks, and the occasional mountain lion and black bear, Tom takes summers off for hiking, hacking, birding, music making, and gaming.

    View Tom Christiansen's full profile page.

  2. Nathan Torkington

    Nathan Torkington is a banjo player, father, and husband. His crimes in the computing community include: coauthor of the Perl Cookbook, editor for O'Reilly and Associates, content coordinator for the Open Source Convention and Perl Conference, and project manager for perl6.

    View Nathan Torkington's full profile page.

Colophon

Our look is the result of reader comments, our own experimentation, and feedback from distribution channels. Distinctive covers complement our distinctive approach to technical topics, breathing personality and life into potentially dry subjects. The animal featured on the cover of Perl Cookbook, Second Edition is a bighorn sheep. Bighorn sheep (Ovis canadensis) are wild sheep noted, not surprisingly, for their large, curved horns. Male bighorns grow to approximately 5 feet long and 40 inches tall to the shoulder and weigh up to 350 pounds. Their horns measure up to 18 inches in circumference and 4 feet long and can weigh up to 30 pounds. Despite their bulk, bighorns are adept at negotiating mountainous terrains. With their sharp, cloven hooves they can walkon ledges as thin as two inches. They have excellent eyesight that enables them to locate footholds and to accurately judge distances between ledges. They can jump as far as 20 feet from ledge to ledge.

Competition for ewes is intense and often leads to fierce battles that can continue for a full day. During the battle two rams race at each other at speeds of up to 20 miles an hour, clashing their horns together. The skull of the bighorn sheep is doublelayered to provide protection from these blows. Horn size is a significant factor in determining rank, and rams will only fight other rams with an equivalently sized horn. Mature males usually stay apart from the females and young. In these "bachelor flocks" the lower-ranking males often play the part of ewes and behave in a submissive manner toward the dominant males. The dominant male, in turn, behaves like a courting ram and mounts the lower-ranking male. This behavior is believed to enable the rams to live together without rankdisputes that might otherwise drive the lower-ranking males out of the flock.

Bighorns can be found in the Rocky Mountains from Canada to Colorado and in the desert from California to west Texas and Mexico. They are threatened with extinction as a result of disease, habitat reduction, and hunting. Genevieve d'Entremont was the production editor and copyeditor for Perl Cookbook, Second Edition. Phil Dangler, Emily Quill, Matt Hutchinson, and Colleen Gorman provided quality control. Mary Agner and Jamie Peppard provided production assistance. Nancy Crumpton wrote the index.

Edie Freedman designed the cover of this book, using a 19th-century engraving from the Dover Pictorial Archive. Emma Colby produced the cover layout with Quark-XPress 4.1 using Adobe's ITC Garamond font.

David Futato designed the interior layout. This bookwas converted by Julie Hawks and Andrew Savikas from Pod to FrameMaker 5.5.6 with a format conversion tool created by ErikRay, Jason McIntosh, Neil Walls, and Mike Sierra that uses Perl and XML technologies. The text font is Linotype Birka; the heading font is Adobe Myriad Condensed; and the code font is LucasFont's TheSans Mono Condensed. The illustrations that appear in the bookwere produced by Robert Romano and Jessamyn Read using Macromedia FreeHand 9 and Adobe Photoshop 6. The warning icon was drawn by Christopher Bing. This colophon was written by Clairemarie Fisher O'Leary.

  • Book cover of Perl Cookbook