search for books and compare prices
Tables of Contents for Perl Cookbook
Chapter/Section Title
Page #
Page Count
Foreword
xxi
2
Preface
xxiii
 
1. Strings
1
41
1.0 Introduction
1
2
1.1 Accessing Substrings
3
3
1.2 Establishing a Default Value
6
2
1.3 Exchanging Values Without Using Temporary Variables
8
1
1.4 Converting Between ASCII Characters and Values
9
2
1.5 Processing a String One Character at a Time
11
2
1.6 Reversing a String by Word or Character
13
2
1.7 Expanding and Compressing Tabs
15
1
1.8 Expanding Variables in User Input
16
3
1.9 Controlling Case
19
2
1.10. Interpolating Functions and Expressions Within Strings
21
2
1.11. Indenting Here Documents
23
3
1.12. Reformatting Paragraphs
26
2
1.13. Escaping Characters
28
2
1.14. Trimming Blanks from the Ends of a String
30
1
1.15. Parsing Comma-Separated Data
31
1
1.16. Soundex Matching
32
2
1.17. Program: fixstyle
34
3
1.18. Program: Psgrep
37
5
2. Numbers
42
28
2.0. Introduction
42
2
2.1. Checking Whether a String Is a Valid Number
44
1
2.2. Comparing Floating-Point Numbers
45
1
2.3. Rounding Floating-Point Numbers
46
2
2.4. Converting Between Binary and Decimal
48
1
2.5. Operating on a Series of Integers
49
1
2.6. Working with Roman Numerals
50
1
2.7. Generating Random Numbers
51
1
2.8. Generating Different Random Numbers
52
1
2.9. Making Numbers Even More Random
53
1
2.10. Generating Biased Random Numbers
54
2
2.11. Doing Trigonmetry in Degrees, not Radians
56
2
2.12. Calculating More Trigonometric Functions
58
1
2.13. Taking Logarithms
59
1
2.14. Multiplying Matrices
60
2
2.15. Using Complex Numbers
62
1
2.16. Converting Between Octal and Hexadecimal
63
1
2.17. Putting Commas in Numbers
64
1
2.18. Printing Correct Plurals
65
2
2.19. Program: Calculating Prime Factors
67
3
3. Dates and Times
70
20
3.0. Introduction
70
3
3.1. Finding Today's Date
73
1
3.2. Converting DMYHMS to Epoch Seconds
74
1
3.3. Converting Epoch Seconds to DMYHMS
75
1
3.4. Adding to or Subtracting from a Date
76
2
3.5. Difference of Two Dates
78
1
3.6. Day in a Week/Month/Year or Week Number
79
2
3.7. Parsing Dates and Times from Strings
81
1
3.8. Printing a Date
82
2
3.9. High-Resolution Timers
84
2
3.10. Short Sleeps
86
1
3.11. Program: hopdelta
87
3
4. Arrays
90
38
4.0. Introduction
90
1
4.1. Specifying a List In Your Program
91
2
4.2. Printing a List With Commas
93
2
4.3. Changing Array Size
95
2
4.4. Doing Something with Every Element in a List
97
3
4.5. Iterating Over an Array by Reference
100
2
4.6. Extracting Unique Elements from a List
102
2
4.7. Finding Elements in One Array but Not Another
104
2
4.8. Computing Union, Intersection, or Difference of Unique Lists
106
2
4.9. Appending One Array to Another
108
1
4.10. Reversing an Array
109
1
4.11. Processing Multiple Elements of an Array
110
1
4.12. Finding the First List Element That Passes a Test
111
2
4.13. Finding All Elements in an Array Matching Certain Criteria
113
2
4.14. Sorting an Array Numerically
115
1
4.15. Sorting a List by Computable Field
116
4
4.16. Implementing a Circular List
120
1
4.17. Randomizing an Array
121
1
4.18. Program: words
122
2
4.19. Program: Permute
124
4
5. Hashes
128
30
5.0. Introduction
128
2
5.1. Adding an Element to a Hash
130
1
5.2. Testing for the Presence of a Key in a Hash
131
2
5.3. Deleting from a Hash
133
2
5.4. Traversing a Hash
135
2
5.5. Printing a Hash
137
2
5.6. Retrieving from a Hash in Insertion Order
139
1
5.7. Hashes with Multiple Values Per Key
140
2
5.8. Inverting a Hash
142
2
5.9. Sorting a Hash
144
1
5.10. Merging Hashes
145
2
5.11. Finding Common or Different Keys in Two Hashes
147
1
5.12. Hashing Reference
148
1
5.13. Presizing a Hash
149
1
5.14. Finding the Most Common Anything
150
1
5.15. Representing Relationships Between Data
151
2
5.16. Program: dutree
153
5
6. Pattern Matching
158
63
6.0. Introduction
158
6
6.1. Copying and Substituting Simultaneously
164
1
6.2. Matching Letters
165
2
6.3. Matching Words
167
1
6.4. Commenting Regular Expressions
168
2
6.5. Finding the Nth Occurrence of a Match
170
3
6.6. Matching Multiple Lines
173
3
6.7. Reading Records with a Pattern Separator
176
1
6.8. Extracting a Range of Lines
177
3
6.9. Matching Shell Globs as Regular Expressions
180
1
6.10. Speeding Up Interpolated Matches
181
5
6.11. Testing for a Valid Pattern
186
2
6.12. Honoring Locale Settings in Regular Expressions
188
1
6.13. Approximate Matching
189
1
6.14. Matching from Where the Last Pattern Left Off
190
2
6.15. Greedy and Non-Greedy Matches
192
2
6.16. Detecting Duplicate Words
194
3
6.17. Expressing AND, OR, and NOT in a Single Pattern
197
5
6.18. Matching Multiple-Byte Characters
202
4
6.19. Matching a Valid Mail Address
206
2
6.20. Matching Abbreviations
208
2
6.21. Program: urlify
210
1
6.22. Program: tcgrep
211
7
6.23. Regular Expression Grabbag
218
3
7. File Access
221
52
7.0. Introduction
221
52
7.1. Opening a File
225
4
7.2. Opening Files with Unusual Filenames
229
2
7.3. Expanding Tildes in Filenames
231
1
7.4. Making Perl Report Filenames in Errors
232
1
7.5. Creating Temporary Files
232
2
7.6. Storing Files Inside Your Program Text
234
2
7.7. Writing a Filter
236
4
7.8. Modifying a File in Place with Temporary File
240
1
7.9. Modifying a File in Place with -- i Switch
241
2
7.10. Modifying a File In Place Without a Temporary File
243
2
7.11. Locking a File
245
3
7.12. Flushing Output
248
2
7.13. Reading from Many Filehandles Without Blocking
250
2
7.14. Doing Non-Blocking I/O
252
2
7.15. Determining the Number of Bytes to Read
254
1
7.16. Storing Filehandless in Variables
255
3
7.17. Caching Open Output Filehandles
258
1
7.18. Printing to Many Filehandles Simultaneously
259
2
7.19. Opening and Closing File Descriptors by Number
261
1
7.20. Copying Filehandles
262
2
7.21. Program: netlock
264
3
7.22. Program: lockarea
267
6
8. File Contents
273
35
8.0. Introduction
273
4
8.1. Reading Lines with Continuation Characters
277
1
8.2. Counting Lines (or Paragraphs or Records) in a File
278
2
8.3. Processing Every Word in a File
280
1
8.4. Reading a File Backwards by Line or Paragraph
281
1
8.5. Trailing a Growing File
282
2
8.6. Picking a Random Line from a File
284
1
8.7. Randomizing All Lines
285
1
8.8. Reading a Particular Line in a File
286
4
8.9. Processing Variable-Length Text Fields
290
1
8.10. Removing the Last Line of a File
291
1
8.11. Processing Binary Files
291
3
8.12. Using Random-Access I/O
294
1
8.13. Updating a Random-Access File
294
2
8.14. Reading a String from a Binary File
296
1
8.15. Reading Fixed-Length Records
297
2
8.16. Reading Configuration Files
299
3
8.17. Testing a File for Trustworthiness
302
2
8.18. Program: tailwtmp
304
1
8.19. Program: tctee
304
2
8.20. Program: laston
306
2
9. Directories
308
26
9.0. Introduction
308
5
9.1. Getting and Setting Timestamps
313
2
9.2. Delecting a File 9.3. Copying or Moving a File
316
1
9.4. Recognizing Two Names for the Same File
317
1
9.5. Processing, All Files in a Directory
318
2
9.6. Globbing, or Getting a List of Filenames Matching a Pattern
320
2
9.7. Processing All Files in a Directory Recursively
322
3
9.8. Removing a Directory and Its Contents
325
1
9.9. Renaming Files
326
2
9.10. Splitting a Filename into Its Component Parts
328
1
9.11. Program: Symlink
329
1
9.12. Program: 1st
330
4
10. Subroutines
334
30
10.0. Introduction
334
1
10.1. Accessing Subroutine Arguments
335
2
10.2. Making Variables Private to a Function
337
2
10.3. Creating Persistent Private Variables
339
2
10.4. Determining Current Function Name
341
1
10.5. Passing Arrays and Hashes by Reference
342
2
10.6. Detecting Return Context
344
1
10.7. Passing by Named Parameter
345
1
10.8. Skipping Selected Return Values
346
1
10.9. Returning More Than One Array or Hash
347
1
10.10. Returning Failure
348
1
10.11. Prototyping Functions
349
2
10.12. Handling Exceptions
351
2
10.13. Saving Global Values
353
3
10.14. Redifining a Function
356
2
10.15. Trapping Undefined Function Calls With AUTOLOAD
358
2
10.16. Nesting Subroutines
360
1
10.17. Program: Sorting Your Mail
361
3
11. References and Records
364
31
11.0. Introduction
364
6
11.1. Taking References to Arrays
370
2
11.2. Making Hashes of Arrays
372
1
11.3. Taking References to Hashes
373
1
11.4. Taking References to Functions
374
3
11.5. Taking References to Scalars
377
1
11.6. Creating Arrays of Scalar References
378
2
11.7. Using Closures Instead of Objects
380
1
11.8. Creating References to Methods
381
1
11.9. Constructing Records
382
3
11.10. Reading and Writing Hash Records to Text Files
385
1
11.11. Printing Data Structures
386
2
11.12. Copying Data Structures
388
1
11.13. Storing Data Structures to Disk
389
2
11.14. Transparently Persistent Data Structures
391
1
11.15. Program: Binary Trees
392
3
12. Packages, Libraries, and Modules
395
46
12.0. Introduction
395
6
12.1. Defining a Module's Interface
401
2
12.2. Trapping Errors in require or use
403
1
12.3. Delaying use Until Run, Time
404
3
12.4. Making Variables Private to a Module
407
2
12.5. Determining the Caller's Pacakage
409
2
12.6. Automating Module Clean-Up
411
2
12.7. Keeping Your Own Module Directory
413
3
12.8. Preparing a Module for Distribution
416
2
12.9. Speeding Module Loading with SelfLoader
418
1
12.10. Speeding Up Module Loading With Autoloader
419
1
12.11. Overriding Built-In Functions
420
2
12.12. Reporting Errors and Warnings Like Built-Ins
422
1
12.13. Referring to Packages Indirectly
423
2
12.14. Using h2ph to Translate C #include Files
425
3
12.15. Using h2xs to Make a Module With C Code
428
3
12.16. Documenting Your Module With Pod
431
3
12.17. Building and Installing a CPAN Module
434
1
12.18. Example: Module Template
435
2
12.19. Program: Finding Versions and Descriptions of Installed Modules
437
4
13. Classes, Objects, and Ties
441
48
13.0. Introduction
441
7
13.1. Constructing an Object
448
2
13.2. Destroying an Object
450
1
13.3. Managing Instance Data
451
3
13.4. Managing Class Data
454
2
13.5. Using Classes as Structs
456
4
13.6. Cloning Objects
460
1
13.7. Calling Methods Indirectly
461
2
13.8. Determining Subclass Membership
463
1
13.9. Writing an Inheritable Class
464
2
13.10. Accessing Overriden Methods
466
2
13.11. Generating Attribute Methods Using AUTOLOAD
468
2
13.12. Solving the Data Inheritance Problem
470
2
13.13. Copying With Circular Data Structures
472
3
13.14. Overloading Operators
475
6
13.15. Creating Magic Variables with tie
481
8
14. Database Access
489
25
14.0. Introduction
489
2
14.1. Making and Using a DBM File
491
2
14.2. Emptying a DBM File
493
1
14.3. Converting Between DBM Files
494
2
14.4. Merging DBM Files
496
1
14.5. Locking DBM Files
497
1
14.6. Sorting Large DBM Files
498
2
14.7. Treating a Text File as a Database Array
500
4
14.8. Storing Complex Data in a DBM File
504
2
14.9. Persistent Data
506
2
14.10. Executing an SQL Command Using DBI and DBD
508
2
14.11. Program: ggh-Grep Netscape Global History
510
4
15. User Interfaces
514
37
15.0. Introduction
514
2
15.1. Parsing Program Arguments
516
2
15.2. Testing Whether a Program Is Running Interactively
518
2
15.3. Clearing the Screen
520
1
15.4. Determining Terminal or Window Size
521
1
15.5. Changing Text Color
522
2
15.6. Reading from the Keyboard
524
1
15.7. Ringing the Terminal Bell
525
1
15.8. Using POSIX termios
526
7
15.9. Checking for Waiting Input
528
1
15.10. Reading Passwords
529
1
15.11. Editing Input
530
2
15.12. Managing the Screen
532
2
15.13. Controlling Another Program with Expect
534
2
15.14. Creating Menus with Tk
536
3
15.15. Creating Dialog Boxes with Tk
539
3
15.16. Responding to Tk Resize Events
542
2
15.17. Removing the DOS Shell Window with Windows Perl/Tk
544
1
15.18. Program: Small termcap program
545
2
15.19. Program: tkshufflepod
547
4
16. Process Management and Communication
551
50
16.0. Introduction
551
3
16.1. Gathering Output from a Program
554
1
16.2. Running Another Program
555
3
16.3. Replacing the Current Program with a Different One
558
1
16.4. Reading or Writing to Another Program
559
2
16.5. Filtering Your Own Output
561
2
16.6. Preprocessing Input
563
1
16.7. Reading STDERR from a Program
564
3
16.8. Controlling Input and Output of Another Program
567
2
16.9. Controlling the Input, Output, and Error of Another Program
569
2
16.10. Communicating Between Related Processes
571
5
16.11. Making a Process Look Like a File With Named Pipes
576
4
16.12. Sharing Variables in Different Processes
580
2
16.13. Listing Available Signals
582
1
16.14. Sending a Signal
583
1
16.15. Installing a Signal Handler
584
2
16.16. Temporarily Overriding a Signal Handler
586
1
16.17. Writing a Signal Handler
587
2
16.18. Catching Ctrl-C
589
1
16.19. Avoiding Zombie Processes
590
3
16.20. Blocking Signals
593
1
16.21. Timing Out an Operation
594
1
16.22. Program: Sigrand
595
6
17. Sockets
601
42
17.0. Introduction
601
2
17.1. Writing a TCP Client
603
3
17.2. Writing a TCP Server
606
2
17.3. Communicating over TCP
608
4
17.4. Setting Up a UDP Client
612
2
17.5. Setting Up a UDP Server
614
2
17.6. Using UNIX Domain Sockets
616
2
17.7. Identifying the Other End of a Sockets
618
2
17.8. Finding Your Own Name and Address
620
1
17.9. Closing a Sockets After Forking
621
1
17.10. Writing Bidirectional Clients
622
2
17.11. Forking Servers
624
1
17.12. Pre-Forking Servers
625
3
17.13. Non-Forking Servers
628
4
17.14. Writing a Multi-Homed Server
632
2
17.15. Making a Daemon Server
634
1
17.16. Restarting a Server on Demand
635
1
17.17. Program: backsniff
636
1
17.18. Program: fwdport
637
6
18. Internet Services
643
24
18.0. Introduction
643
1
18.1. Simple DNS Lookups
644
3
18.2. Being an FTP Client
647
3
18.3. Sending Mail
650
4
18.4. Reading and Posting Usenet News Messages
654
2
18.5. Reading Mail with POP3
656
3
18.6. Simulating Telent from a Program
659
2
18.7. Pinging a Machine
661
1
18.8. Using Whois to Retrieve Information from the InterNIC
662
2
18.9. Program: expn and vrfy
664
3
19. CGI Programming
667
39
19.0. Introduction
667
4
19.1. Writing a CGI Script
671
3
19.2. Redirecting Error Message
674
1
19.3. Fixing a 500 Server Error
675
4
19.4. Writing a Safe CGI Program
679
3
19.5. Making CGI Scripts Efficient
682
1
19.6. Executing Commands Without Shell Escapes
683
3
19.7. Formatting Lists and Tables with HTML Shortcuts
686
2
19.8. Redirecting to a Different Location
688
2
19.9. Debugging the Raw HTTP Exchange
690
3
19.10. Managing Cookies
693
1
19.11. Creating Sticky Widgets
694
2
19.12. Writing a Multiscreen CGI Script
696
2
19.13. Saving a Form to a File or Mail Pipe
698
2
19.14. Program: Chemiserie
700
6
20. Web Automation
706
29
20.0. Introduction
706
2
20.1. Fetching a URL from a Perl Script
708
2
20.2. Automating Form Submission
710
1
20.3. Extracting URLs
711
3
20.4. Converting ASCII to HTML
714
1
20.5. Converting HTML to ASCII
715
1
20.6. Extracting or Removing HTML Tags
716
2
20.7. Finding Stale Links
718
1
20.8. Finding Fresh Links
719
2
20.9. Creating HTML Templates
721
3
20.10. Mirroring Web Pages
724
1
20.11. Creating a Robot
725
1
20.12. Parsing a Web Server Log File
726
2
20.13. Processing Server Logs
728
3
20.14. Program: htmlsub
731
1
20.15. Program: hrefsub
732
3
Index
735