The Perl Cookbook is a comprehensive collection of problems, solutions, and practical examples for anyone programming in Perl. Topics range from beginner questions to techniques that even the most experienced of Perl programmers will 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 upon thousands of examples ranging from brief one-liners to complete applications. Covered topic areas spread across nearly four hundred separate ''recipes,'' including: 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 These recipes were rigorously reviewed by scores of the best minds inside and outside Perl, foremost of which was Larry Wall, the creator of Perl himself. The Perl Cookbook is written by Tom Christiansen, Perl evangelist and coauthor of the bestselling Programming Perl and Learning Perl; and Nathan Torkington, Perl trainer and co-maintainer of the Perl Frequently Asked Questions list. --This text refers to an out of print or unavailable edition of this title. Perl Cookbook -1 Table of Contents 2 Foreword 4 Preface 6 [Preface] Platform Notes 10 [Preface] Other Books 12 [Preface] Conventions Used in This Book 14 [Preface] We'd Like to Hear from You 16 [Preface] Acknowledgments 17 [Chapter 1] Strings 20 Recipe 1.1. Accessing Substrings 23 Recipe 1.2. Establishing a Default Value 26 Recipe 1.3. Exchanging Values Without Using Temporary Variables 29 Recipe 1.4. Converting Between ASCII Characters and Values 31 Recipe 1.5. Processing a String One Character at a Time 33 Recipe 1.6. Reversing a String by Word or Character 36 Recipe 1.7. Expanding and Compressing Tabs 38 Recipe 1.8. Expanding Variables in User Input 40 Recipe 1.9. Controlling Case 43 Recipe 1.10. Interpolating Functions and Expressions Within Strings 46 Recipe 1.11. Indenting Here Documents 48 Recipe 1.12. Reformatting Paragraphs 53 Recipe 1.13. Escaping Characters 56 Recipe 1.14. Trimming Blanks from the Ends of a String 58 Recipe 1.15. Parsing Comma-Separated Data 60 Recipe 1.16. Soundex Matching 62 Recipe 1.17. Program: fixstyle 64 Recipe 1.18. Program: psgrep 68 [Chapter 2] Numbers 72 Recipe 2.1. Checking Whether a String Is a Valid Number 75 Recipe 2.2. Comparing Floating-Point Numbers 77 Recipe 2.3. Rounding Floating-Point Numbers 79 Recipe 2.4. Converting Between Binary and Decimal 81 Recipe 2.5. Operating on a Series of Integers 83 Recipe 2.6. Working with Roman Numerals 85 Recipe 2.7. Generating Random Numbers 87 Recipe 2.8. Generating Different Random Numbers 89 Recipe 2.9. Making Numbers Even More Random 91 Recipe 2.10. Generating Biased Random Numbers 93 Recipe 2.11. Doing Trigonometry in Degrees, not Radians 96 Recipe 2.12. Calculating More Trigonometric Functions 98 Recipe 2.13. Taking Logarithms 100 Recipe 2.14. Multiplying Matrices 102 Recipe 2.15. Using Complex Numbers 105 Recipe 2.16. Converting Between Octal and Hexadecimal 107 Recipe 2.17. Putting Commas in Numbers 109 Recipe 2.18. Printing Correct Plurals 111 Recipe 2.19. Program: Calculating Prime Factors 114 [Chapter 3] Dates and Times 117 Recipe 3.1. Finding Today's Date 121 Recipe 3.2. Converting DMYHMS to Epoch Seconds 123 Recipe 3.3. Converting Epoch Seconds to DMYHMS 125 Recipe 3.4. Adding to or Subtracting from a Date 127 Recipe 3.5. Difference of Two Dates 129 Recipe 3.6. Day in a Week/Month/Year or Week Number 131 Recipe 3.7. Parsing Dates and Times from Strings 133 Recipe 3.8. Printing a Date 135 Recipe 3.9. High-Resolution Timers 137 Recipe 3.10. Short Sleeps 140 Recipe 3.11. Program: hopdelta 142 [Chapter 4] Arrays 146 Recipe 4.1. Specifying a List In Your Program 149 Recipe 4.2. Printing a List with Commas 151 Recipe 4.3. Changing Array Size 154 Recipe 4.4. Doing Something with Every Element in a List 157 Recipe 4.5. Iterating Over an Array by Reference 161 Recipe 4.6. Extracting Unique Elements from a List 163 Recipe 4.7. Finding Elements in One Array but Not Another 166 Recipe 4.8. Computing Union, Intersection, or Difference of Unique Lists 169 Recipe 4.9. Appending One Array to Another 172 Recipe 4.10. Reversing an Array 174 Recipe 4.11. Processing Multiple Elements of an Array 176 Recipe 4.12. Finding the First List Element That Passes a Test 178 Recipe 4.13. Finding All Elements in an Array Matching Certain Criteria 181 Recipe 4.14. Sorting an Array Numerically 183 Recipe 4.15. Sorting a List by Computable Field 185 Recipe 4.16. Implementing a Circular List 190 Recipe 4.17. Randomizing an Array 192 Recipe 4.18. Program: words 194 Recipe 4.19. Program: permute 197 [Chapter 5] Hashes 201 Recipe 5.1. Adding an Element to a Hash 204 Recipe 5.2. Testing for the Presence of a Key in a Hash 206 Recipe 5.3. Deleting from a Hash 209 Recipe 5.4. Traversing a Hash 212 Recipe 5.5. Printing a Hash 215 Recipe 5.6. Retrieving from a Hash in Insertion Order 217 Recipe 5.7. Hashes with Multiple Values Per Key 219 Recipe 5.8. Inverting a Hash 221 Recipe 5.9. Sorting a Hash 224 Recipe 5.10. Merging Hashes 226 Recipe 5.11. Finding Common or Different Keys in Two Hashes 229 Recipe 5.12. Hashing References 231 Recipe 5.13. Presizing a Hash 233 Recipe 5.14. Finding the Most Common Anything 235 Recipe 5.15. Representing Relationships Between Data 236 Recipe 5.16. Program: dutree 239 [Chapter 6] Pattern Matching 244 Recipe 6.1. Copying and Substituting Simultaneously 250 Recipe 6.2. Matching Letters 252 Recipe 6.3. Matching Words 254 Recipe 6.4. Commenting Regular Expressions 256 Recipe 6.5. Finding the Nth Occurrence of a Match 259 Recipe 6.6. Matching Multiple Lines 263 Recipe 6.7. Reading Records with a Pattern Separator 266 Recipe 6.8. Extracting a Range of Lines 268 Recipe 6.9. Matching Shell Globs as Regular Expressions 271 Recipe 6.10. Speeding Up Interpolated Matches 273 Recipe 6.11. Testing for a Valid Pattern 278 Recipe 6.12. Honoring Locale Settings in Regular Expressions 281 Recipe 6.13. Approximate Matching 283 Recipe 6.14. Matching from Where the Last Pattern Left Off 285 Recipe 6.15. Greedy and Non-Greedy Matches 287 Recipe 6.16. Detecting Duplicate Words 290 Recipe 6.17. Expressing AND, OR, and NOT in a Single Pattern 294 Recipe 6.18. Matching Multiple-Byte Characters 299 Recipe 6.19. Matching a Valid Mail Address 304 Recipe 6.20. Matching Abbreviations 306 Recipe 6.21. Program: urlify 309 Recipe 6.22. Program: tcgrep 311 Recipe 6.23. Regular Expression Grabbag 318 [Chapter 7] File Access 321 Recipe 7.1. Opening a File 326 Recipe 7.2. Opening Files with Unusual Filenames 330 Recipe 7.3. Expanding Tildes in Filenames 332 Recipe 7.4. Making Perl Report Filenames in Errors 334 Recipe 7.5. Creating Temporary Files 335 Recipe 7.6. Storing Files Inside Your Program Text 337 Recipe 7.7. Writing a Filter 339 Recipe 7.8. Modifying a File in Place with Temporary File 344 Recipe 7.9. Modifying a File in Place with -i Switch 346 Recipe 7.10. Modifying a File in Place Without a Temporary File 349 Recipe 7.11. Locking a File 351 Recipe 7.12. Flushing Output 354 Recipe 7.13. Reading from Many Filehandles Without Blocking 357 Recipe 7.14. Doing Non-Blocking I/O 360 Recipe 7.15. Determining the Number of Bytes to Read 362 Recipe 7.16. Storing Filehandles in Variables 364 Recipe 7.17. Caching Open Output Filehandles 368 Recipe 7.18. Printing to Many Filehandles Simultaneously 370 Recipe 7.19. Opening and Closing File Descriptors by Number 372 Recipe 7.20. Copying Filehandles 374 Recipe 7.21. Program: netlock 376 Recipe 7.22. Program: lockarea 380 [Chapter 8] File Contents 386 Recipe 8.1. Reading Lines with Continuation Characters 391 Recipe 8.2. Counting Lines (or Paragraphs or Records) in a File 393 Recipe 8.3. Processing Every Word in a File 395 Recipe 8.4. Reading a File Backwards by Line or Paragraph 397 Recipe 8.5. Trailing a Growing File 399 Recipe 8.6. Picking a Random Line from a File 401 Recipe 8.7. Randomizing All Lines 403 Recipe 8.8. Reading a Particular Line in a File 405 Recipe 8.9. Processing Variable-Length Text Fields 409 Recipe 8.10. Removing the Last Line of a File 411 Recipe 8.11. Processing Binary Files 412 Recipe 8.12. Using Random-Access I/O 415 Recipe 8.13. Updating a Random-Access File 416 Recipe 8.14. Reading a String from a Binary File 418 Recipe 8.15. Reading Fixed-Length Records 420 Recipe 8.16. Reading Configuration Files 422 Recipe 8.17. Testing a File for Trustworthiness 425 Recipe 8.18. Program: tailwtmp 428 Recipe 8.19. Program: tctee 430 Recipe 8.20. Program: laston 433 [Chapter 9] Directories 435 Recipe 9.1. Getting and Setting Timestamps 441 Recipe 9.2. Deleting a File 443 Recipe 9.3. Copying or Moving a File 445 Recipe 9.4. Recognizing Two Names for the Same File 447 Recipe 9.5. Processing All Files in a Directory 449 Recipe 9.6. Globbing, or Getting a List of Filenames Matching a Pattern 452 Recipe 9.7. Processing All Files in a Directory Recursively 455 Recipe 9.8. Removing a Directory and Its Contents 458 Recipe 9.9. Renaming Files 460 Recipe 9.10. Splitting a Filename into Its Component Parts 463 Recipe 9.11. Program: symirror 465 Recipe 9.12. Program: lst 467 [Chapter 10] Subroutines 471 Recipe 10.1. Accessing Subroutine Arguments 473 Recipe 10.2. Making Variables Private to a Function 476 Recipe 10.3. Creating Persistent Private Variables 478 Recipe 10.4. Determining Current Function Name 480 Recipe 10.5. Passing Arrays and Hashes by Reference 482 Recipe 10.6. Detecting Return Context 484 Recipe 10.7. Passing by Named Parameter 486 Recipe 10.8. Skipping Selected Return Values 488 Recipe 10.9. Returning More Than One Array or Hash 490 Recipe 10.10. Returning Failure 492 Recipe 10.11. Prototyping Functions 494 Recipe 10.12. Handling Exceptions 497 Recipe 10.13. Saving Global Values 499 Recipe 10.14. Redefining a Function 503 Recipe 10.15. Trapping Undefined Function Calls with AUTOLOAD 506 Recipe 10.16. Nesting Subroutines 508 Recipe 10.17. Program: Sorting Your Mail 510 [Chapter 11] References and Records 513 Recipe 11.1. Taking References to Arrays 519 Recipe 11.2. Making Hashes of Arrays 522 Recipe 11.3. Taking References to Hashes 524 Recipe 11.4. Taking References to Functions 526 Recipe 11.5. Taking References to Scalars 530 Recipe 11.6. Creating Arrays of Scalar References 532 Recipe 11.7. Using Closures Instead of Objects 534 Recipe 11.8. Creating References to Methods 536 Recipe 11.9. Constructing Records 538 Recipe 11.10. Reading and Writing Hash Records to Text Files 541 Recipe 11.11. Printing Data Structures 543 Recipe 11.12. Copying Data Structures 545 Recipe 11.13. Storing Data Structures to Disk 547 Recipe 11.14. Transparently Persistent Data Structures 549 Recipe 11.15. Program: Binary Trees 551 [Chapter 12] Packages, Libraries, and Modules 555 Recipe 12.1. Defining a Module's Interface 561 Recipe 12.2. Trapping Errors in require or use 564 Recipe 12.3. Delaying use Until Run Time 566 Recipe 12.4. Making Variables Private to a Module 569 Recipe 12.5. Determining the Caller's Package 572 Recipe 12.6. Automating Module Clean-Up 575 Recipe 12.7. Keeping Your Own Module Directory 578 Recipe 12.8. Preparing a Module for Distribution 581 Recipe 12.9. Speeding Module Loading with SelfLoader 584 Recipe 12.10. Speeding Up Module Loading with Autoloader 586 Recipe 12.11. Overriding Built-In Functions 588 Recipe 12.12. Reporting Errors and Warnings Like Built-Ins 590 Recipe 12.13. Referring to Packages Indirectly 592 Recipe 12.14. Using h2ph to Translate C #include Files 595 Recipe 12.15. Using h2xs to Make a Module with C Code 599 Recipe 12.16. Documenting Your Module with Pod 603 Recipe 12.17. Building and Installing a CPAN Module 606 Recipe 12.18. Example: Module Template 609 Recipe 12.19. Program: Finding Versions and Descriptions of Installed Modules 611 [Chapter 13] Classes, Objects, and Ties 615 Recipe 13.1. Constructing an Object 622 Recipe 13.2. Destroying an Object 625 Recipe 13.3. Managing Instance Data 627 Recipe 13.4. Managing Class Data 631 Recipe 13.5. Using Classes as Structs 634 Recipe 13.6. Cloning Objects 638 Recipe 13.7. Calling Methods Indirectly 640 Recipe 13.8. Determining Subclass Membership 642 Recipe 13.9. Writing an Inheritable Class 644 Recipe 13.10. Accessing Overridden Methods 647 Recipe 13.11. Generating Attribute Methods Using AUTOLOAD 649 Recipe 13.12. Solving the Data Inheritance Problem 652 Recipe 13.13. Coping with Circular Data Structures 655 Recipe 13.14. Overloading Operators 659 Recipe 13.15. Creating Magic Variables with tie 666 [Chapter 14] Database Access 675 Recipe 14.1. Making and Using a DBM File 679 Recipe 14.2. Emptying a DBM File 682 Recipe 14.3. Converting Between DBM Files 684 Recipe 14.4. Merging DBM Files 686 Recipe 14.5. Locking DBM Files 688 Recipe 14.6. Sorting Large DBM Files 691 Recipe 14.7. Treating a Text File as a Database Array 694 Recipe 14.8. Storing Complex Data in a DBM File 699 Recipe 14.9. Persistent Data 701 Recipe 14.10. Executing an SQL Command Using DBI and DBD 704 Recipe 14.11. Program: ggh€- Grep Netscape Global History 707 [Chapter 15] User Interfaces 711 Recipe 15.1. Parsing Program Arguments 714 Recipe 15.2. Testing Whether a Program Is Running Interactively 717 Recipe 15.3. Clearing the Screen 719 Recipe 15.4. Determining Terminal or Window Size 721 Recipe 15.5. Changing Text Color 723 Recipe 15.6. Reading from the Keyboard 726 Recipe 15.7. Ringing the Terminal Bell 728 Recipe 15.8. Using POSIX termios 730 Recipe 15.9. Checking for Waiting Input 733 Recipe 15.10. Reading Passwords 735 Recipe 15.11. Editing Input 737 Recipe 15.12. Managing the Screen 739 Recipe 15.13. Controlling Another Program with Expect 742 Recipe 15.14. Creating Menus with Tk 745 Recipe 15.15. Creating Dialog Boxes with Tk 749 Recipe 15.16. Responding to Tk Resize Events 753 Recipe 15.17. Removing the DOS Shell Window with Windows Perl/Tk 755 Recipe 15.18. Program: Small termcap program 757 Recipe 15.19. Program: tkshufflepod 760 [Chapter 16] Process Management and Communication 764 Recipe 16.1. Gathering Output from a Program 768 Recipe 16.2. Running Another Program 770 Recipe 16.3. Replacing the Current Program with a Different One 773 Recipe 16.4. Reading or Writing to Another Program 775 Recipe 16.5. Filtering Your Own Output 778 Recipe 16.6. Preprocessing Input 781 Recipe 16.7. Reading STDERR from a Program 783 Recipe 16.8. Controlling Input and Output of Another Program 786 Recipe 16.9. Controlling the Input, Output, and Error of Another Program 789 Recipe 16.10. Communicating Between Related Processes 792 Recipe 16.11. Making a Process Look Like a File with Named Pipes 798 Recipe 16.12. Sharing Variables in Different Processes 802 Recipe 16.13. Listing Available Signals 805 Recipe 16.14. Sending a Signal 807 Recipe 16.15. Installing a Signal Handler 809 Recipe 16.16. Temporarily Overriding a Signal Handler 811 Recipe 16.17. Writing a Signal Handler 813 Recipe 16.18. Catching Ctrl-C 816 Recipe 16.19. Avoiding Zombie Processes 818 Recipe 16.20. Blocking Signals 821 Recipe 16.21. Timing Out an Operation 823 Recipe 16.22. Program: sigrand 825 [Chapter 17] Sockets 831 Recipe 17.1. Writing a TCP Client 834 Recipe 17.2. Writing a TCP Server 837 Recipe 17.3. Communicating over TCP 840 Recipe 17.4. Setting Up a UDP Client 844 Recipe 17.5. Setting Up a UDP Server 847 Recipe 17.6. Using UNIX Domain Sockets 850 Recipe 17.7. Identifying the Other End of a Socket 852 Recipe 17.8. Finding Your Own Name and Address 854 Recipe 17.9. Closing a Socket After Forking 856 Recipe 17.10. Writing Bidirectional Clients 858 Recipe 17.11. Forking Servers 861 Recipe 17.12. Pre-Forking Servers 863 Recipe 17.13. Non-Forking Servers 866 Recipe 17.14. Writing a Multi-Homed Server 871 Recipe 17.15. Making a Daemon Server 873 Recipe 17.16. Restarting a Server on Demand 875 Recipe 17.17. Program: backsniff 877 Recipe 17.18. Program: fwdport 879 [Chapter 18] Internet Services 884 Recipe 18.1. Simple DNS Lookups 886 Recipe 18.2. Being an FTP Client 890 Recipe 18.3. Sending Mail 893 Recipe 18.4. Reading and Posting Usenet News Messages 897 Recipe 18.5. Reading Mail with POP3 900 Recipe 18.6. Simulating Telnet from a Program 903 Recipe 18.7. Pinging a Machine 906 Recipe 18.8. Using Whois to Retrieve Information from the InterNIC 908 Recipe 18.9. Program: expn and vrfy 910 [Chapter 19] CGI Programming 913 Recipe 19.1. Writing a CGI Script 918 Recipe 19.2. Redirecting Error Messages 921 Recipe 19.3. Fixing a 500 Server Error 923 Recipe 19.4. Writing a Safe CGI Program 927 Recipe 19.5. Making CGI Scripts Efficient 931 Recipe 19.6. Executing Commands Without Shell Escapes 933 Recipe 19.7. Formatting Lists and Tables with HTML Shortcuts 936 Recipe 19.8. Redirecting to a Different Location 939 Recipe 19.9. Debugging the Raw HTTP Exchange 942 Recipe 19.10. Managing Cookies 945 Recipe 19.11. Creating Sticky Widgets 948 Recipe 19.12. Writing a Multiscreen CGI Script 950 Recipe 19.13. Saving a Form to a File or Mail Pipe 953 Recipe 19.14. Program: chemiserie 955 [Chapter 20] Web Automation 961 Recipe 20.1. Fetching a URL from a Perl Script 964 Recipe 20.2. Automating Form Submission 967 Recipe 20.3. Extracting URLs 969 Recipe 20.4. Converting ASCII to HTML 972 Recipe 20.5. Converting HTML to ASCII 974 Recipe 20.6. Extracting or Removing HTML Tags 976 Recipe 20.7. Finding Stale Links 979 Recipe 20.8. Finding Fresh Links 981 Recipe 20.9. Creating HTML Templates 983 Recipe 20.10. Mirroring Web Pages 987 Recipe 20.11. Creating a Robot 989 Recipe 20.12. Parsing a Web Server Log File 991 Recipe 20.13. Processing Server Logs 993 Recipe 20.14. Program: htmlsub 997 Recipe 20.15. Program: hrefsub 999 Index 2 Index: Symbols and Numbers 1001 Index: A 1004 Index: B 1007 Index: C 1010 Index: D 1017 Index: E 1024 Index: F 1027 Index: G 1033 Index: H 1035 Index: I 1038 Index: J 1043 Index: K 1044 Index: L 1045 Index: M 1049 Index: N 1053 Index: O 1056 Index: P 1059 Index: Q 1066 Index: R 1067 Index: S 1072 Index: T 1082 Index: U 1086 Index: V 1089 Index: W 1091 Index: X 1094 Index: Y 1095 Index: Y 1096 "The Perl Cookbook is a comprehensive collection of problems, solutions, and practical examples for anyone programming in Perl. Topics range from beginner questions to techniques that even the most experienced Perl programmers 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."--BOOK JACKET