TEAMFLY Team-Fly ® Perl: The Complete Reference Second Edition This page intentionally left blank. Perl: The Complete Reference Second Edition Martin C. Brown Osborne/McGraw-Hill New York Chicago San Francisco Lisbon London Madrid Mexico City Milan New Delhi San Juan Seoul Singapore Sydney Toronto Copyright © 2001 by The McGraw-Hill Companies. All rights reserved. Manufactured in the United States of America. Except as per- mitted under the United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the publisher. 0-07-219425-1 The material in this eBook also appears in the print version of this title: 0-07-212950-6. All trademarks are trademarks of their respective owners. Rather than put a trademark symbol after every occurrence of a trade- marked name, we use names in an editorial fashion only, and to the benefit of the trademark owner, with no intention of infringe- ment of the trademark. Where such designations appear in this book, they have been printed with initial caps. McGraw-Hill eBooks are available at special quantity discounts to use as premiums and sales promotions, or for use in corporate training programs. For more information, please contact George Hoare, Special Sales, at george_hoare@mcgraw-hill.com or (212) 904-4069. TERMS OF USE This is a copyrighted work and The McGraw-Hill Companies, Inc. (“McGraw-Hill”) and its licensors reserve all rights in and to the work. Use of this work is subject to these terms. Except as permitted under the Copyright Act of 1976 and the right to store and retrieve one copy of the work, you may not decompile, disassemble, reverse engineer, reproduce, modify, create derivative works based upon, transmit, distribute, disseminate, sell, publish or sublicense the work or any part of it without McGraw-Hill’s prior con- sent. You may use the work for your own noncommercial and personal use; any other use of the work is strictly prohibited. Your right to use the work may be terminated if you fail to comply with these terms. THE WORK IS PROVIDED “AS IS”. McGRAW-HILL AND ITS LICENSORS MAKE NO GUARANTEES OR WARRANTIES AS TO THE ACCURACY, ADEQUACY OR COMPLETENESS OF OR RESULTS TO BE OBTAINED FROM USING THE WORK, INCLUDING ANY INFORMATION THAT CAN BE ACCESSED THROUGH THE WORK VIA HYPERLINK OR OTHERWISE, AND EXPRESSLY DISCLAIM ANY WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. McGraw-Hill and its licensors do not warrant or guarantee that the functions contained in the work will meet your requirements or that its operation will be uninterrupted or error free. Neither McGraw-Hill nor its licensors shall be liable to you or anyone else for any inaccuracy, error or omission, regardless of cause, in the work or for any damages resulting therefrom. McGraw-Hill has no responsibility for the con- tent of any information accessed through the work. Under no circumstances shall McGraw-Hill and/or its licensors be liable for any indirect, incidental, special, punitive, consequential or similar damages that result from the use of or inability to use the work, even if any of them has been advised of the possibility of such damages. This limitation of liability shall apply to any claim or cause what- soever whether such claim or cause arises in contract, tort or otherwise. DOI: 10.1036/0072194251 To Darcy and Leon, two little kittens who do try to help daddy with the typing, but usually just end up typing “jjskdjjvoookko000000000000000000000” About the Author Martin C. Brown is the author of six Perl books, including the first edition of Perl: The Complete Reference, Perl Programmer’s Reference, ActivePerl Developer’s Guide, and Debugging Perl. In addition to Perl, he has worked in Python, Java, Visual Basic, and other languages. A programmer for 15 years, he is the former IT director of a large advertising agency dealing with blue chip clients such as Hewlett Packard, Oracle, and Cable & Wireless. Copyright 2001 The McGraw-Hill Companies, Inc. Click Here for Terms of Use. Contents at a Glance Part I Fundamentals 1 Perl Backgrounder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 2 Perl Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 3 Perl Parsing Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 4 Variables and Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 5 Statements and Control Structures . . . . . . . . . . . . . . . . . . 109 6 Subroutines, Packages, and Modules . . . . . . . . . . . . . . . . 123 7 Working with Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 8 Data Manipulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 9 Errors and Trapping Them . . . . . . . . . . . . . . . . . . . . . . . . . 269 Part II Programming with Perl 10 Complex Data Structures . . . . . . . . . . . . . . . . . . . . . . . . . . 287 11 System Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339 12 Networking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361 vii Copyright 2001 The McGraw-Hill Companies, Inc. Click Here for Terms of Use. 13 Database Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391 14 Interprocess Communication . . . . . . . . . . . . . . . . . . . . . . . 447 15 Other Execution Enhancements . . . . . . . . . . . . . . . . . . . . . 479 Part III Developing Applications 16 User Interface Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513 17 Advanced User Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . 529 18 Developing for the World Wide Web (WWW) . . . . . . . . 575 19 Controlling Execution with Pragmas . . . . . . . . . . . . . . . . 611 20 Extending and Embedding Perl . . . . . . . . . . . . . . . . . . . . . 641 Part IV Fine-Tuning Applications 21 Debugging and Tuning . . . . . . . . . . . . . . . . . . . . . . . . . . . . 715 22 Perl Compiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 773 23 Perl Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 803 24 Cross-Platform Migration Traps . . . . . . . . . . . . . . . . . . . . 825 25 Distributing Modules and Applications . . . . . . . . . . . . . . 837 Part V Appendixes A Function Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 869 B Standard Perl Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 993 C Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1141 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1153 viii Perl: The Complete Reference Contents Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxiii Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxv Part I Fundamentals 1 Perl Backgrounder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Versions and Naming Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Perl, perl or PeRl? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Life Before Perl 5.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Perl History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Main Perl Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Perl Is Free . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Perl Is Simple to Learn, Concise, and Easy to Read . . . . . . 7 Perl Is Fast . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Perl Is Extensible . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Perl Has Flexible Data Types . . . . . . . . . . . . . . . . . . . . . . . . . 8 Perl Is Object Oriented . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Perl Is Collaborative . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Compiler or Interpreter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 ix Copyright 2001 The McGraw-Hill Companies, Inc. Click Here for Terms of Use. [...]... utf8 TE xxx C 11 11 111 4 11 14 11 14 11 15 11 16 11 16 11 18 11 19 11 19 11 21 112 2 11 23 11 24 11 27 11 27 11 28 11 29 11 30 11 31 113 1 11 32 11 32 11 33 11 33 11 34 11 34 11 35 11 36 11 37 11 37 11 38 11 39 11 39 Resources 11 41 Supplied Documentation Unix Documentation ... File::Spec::VMS 10 17 10 18 10 20 10 20 10 21 10 21 1024 10 25 10 25 10 26 10 26 10 27 10 27 10 28 10 29 10 29 10 29 10 30 10 30 10 30 10 30 10 30 10 30 10 30 10 31 10 31 10 31 10 31 10 31 1032 10 33 10 35 10 36 10 37 10 37 10 38 10 39 10 40 10 41 1042 10 43 10 43 10 45 Contents File::Spec::Win32 FindBin ... Symbol Sets 10 46 10 46 10 47 10 48 10 49 10 50 10 50 10 51 10 51 1053 10 53 10 54 10 54 10 55 10 55 10 58 10 59 10 59 10 61 1063 10 65 10 66 10 67 10 68 10 68 10 70 10 72 10 73 10 75 10 76 10 77 10 78 10 79 10 79 10 79 10 80 10 82 10 87 10 87 10 87 10 89 10 89 11 02 xxix Perl: The Complete Reference AM FL Y Safe SDBM_File... Cwd 994 995 996 996 998 999 999 999 999 10 00 10 00 10 00 10 00 10 01 10 01 10 01 10 01 10 01 1002 10 03 10 04 10 05 10 06 10 06 10 07 10 08 10 08 10 09 10 10 10 10 10 11 1 012 10 12 10 15 10 16 10 16 10 16 xxvii xxviii Perl: The Complete Reference Data::Dumper DB_File ... Index 11 46 11 46 11 46 11 46 11 46 11 47 11 48 11 49 11 49 11 50 11 53 xxxi This page intentionally left blank Acknowledgments irst of all, I’d like to thank my wife Two years ago, based on the offer for the first edition of Perl: The Complete Reference, she stood by me when I wanted to start writing full time Without her continued support, I wouldn’t have written the. .. goto 897 897 897 898 899 899 900 900 9 01 9 01 902 903 903 904 904 904 905 906 906 907 907 908 908 909 909 910 910 911 911 911 912 912 913 914 914 915 915 916 916 917 918 918 919 xxiii xxiv Perl: The Complete Reference grep hex ... The defined Function and the Undefined Value Default Values Other Tokens Special Variables 4 11 0 11 1 11 4 11 4 11 5 11 5 11 6 11 7 11 8 11 9 12 0 12 2 Subroutines, Packages, and Modules 12 3 Functions ... 7 14 6 14 8 14 8 14 9 15 0 15 3 15 3 15 4 15 5 15 6 15 7 15 8 15 8 16 3 16 4 17 2 18 1 18 3 18 6 18 7 19 2 19 6 19 8 200 203 203 205 Data Manipulation 209 Working with Numbers abs the Absolute Value int—Converting Floating Points to Integers exp—Raising e to the Power sqrt the Square.. .Perl: The Complete Reference 9 9 10 10 11 12 12 13 13 13 13 14 14 14 14 15 15 15 AM FL Y Similar Programming Languages Unix Shells Tcl ... Converting Perl to Other Languages Calling Other Languages from Perl 643 644 644 646 647 650 652 653 654 662 668 669 669 670 670 6 71 672 698 699 700 704 705 706 706 711 712 xix Perl: The Complete Reference Part IV Fine-Tuning Applications 715 717 717 7 21 724 726 729 729 730 730 730 7 31 7 31 748 750 750 752 754 754 754 755 757 758 765 766 AM FL Y Debugging and . Team-Fly ® Perl: The Complete Reference Second Edition This page intentionally left blank. Perl: The Complete Reference Second Edition Martin C. Brown Osborne/McGraw-Hill New. typing “jjskdjjvoookko000000000000000000000” About the Author Martin C. Brown is the author of six Perl books, including the first edition of Perl: The Complete Reference, Perl Programmer’s Reference, ActivePerl Developer’s Guide,. . . . . . . . . . . . . . . . . 11 41 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 53 viii Perl: The Complete Reference Contents Acknowledgments