Praise for Beginning Perl, Second Edition
Total Page:16
File Type:pdf, Size:1020Kb
Praise for Beginning Perl, Second Edition: "This book is a well-conceived introduction to Perl in tutorial form, covering the basics ofPerl and also covering the basics ofsome more advanced areas ofPerl." -George Woolley, Oakland Perl Mongers (http:/ I oakland.pm.org) "Beginning Perl, Second Edition, is a great choice to move from no knowledge or little knowledge of Perl to becoming an intermediate Perl user. It is highly recommended to anyone wanting to acquire a well-rounded foundation in Perl." -Harold McFarland, Midwest Book Review (www.midwestbookreview.com) "If I were to write an introductory Perl text, I'd want it to look like the first 10 chapters of this book. If you are a complete novice, though, I think this book is as gentle an introduction to Perl as you'll find anywhere." -Ed Schaefer, Unix Review (www.unixreview.com) "The Perl [this book] teaches is strong industry-standard Perl that is ... considered best practice." -Jack Herrington, Editor, Code Generation Network (www.codegeneration.net) "This book is a well-conceived introduction to Perl in tutorial form ... A Very Good Book. :) :) :) :) of5." -Mark Spritzler, Javaranch Bartender (www.javaranch.com) "To summarize, ifyou've wanted to learn Perl but haven't yet taken the plunge, you could do a lot worse than to start with this book. It's a casual tour of the basics with a few teasers for further study opportunities." -James Edward Gray II, Slashdot contributor (www.slashdot.org) Beginning Perl, Second Edition JAMES LEE with SIMON COZENS and PETER WAINWRIGHT APress Media, LLC Beginning Peri, Second Edition Copyright C 2004 by James Lee, Simon Cozens, Peter Wainwright Originally published by Apress in 2004 Ali rights reserved. No part of this work rnay be reproduced or transrnitted in any form or by any rneans, electronic or rnechanical, including photocopying, recording, or by any information storage or retrieval systern, without the prior written perrnission of the copyright owner and the publisher. ISBN 978-1-59059-391-2 ISBN 978-1-4302-0665-1 (eBook) DOI 10.1007/978-1-4302-0665-1 Tradernarked narnes rnay appear in this book. Rather than use a tradernark syrnbol with every occurrence of a tradernarked narne, we use the narnes only in an editorial fashion and to the benefit of the tradernark owner, with no intention of infringernent of the tradernark. Lead Editor: Chris Mills Technical Reviewer: Sarn Tregar Editorial Board: Steve Anglin, Dan Applernan, Ewan Buckingharn, Gary Cornell, Tony Davis, Iason Gilrnore, Jonathon Hassell, Chris Mills, Dorninic Shakeshaft, Jirn Surnser Project Manager: Kylie Johnston Copy Edit Manager: Nicole LeClerc Copy Editor: Arni Knox Production Manager: Kari Brooks Production Editor: Katie Stence Cornpositor and Artist: Kinetic Publishing Services, LLC Proofreader: Elizabeth Berry Indexer: John Collin Cover Designer: Kurt Krarnes Manufacturing Manager: Torn Debolski In the United States: phone 1-800-SPRINGER, e-mail orders@springer -ny. corn, or visit http: 1/IWIW. springer -ny. corn. Outside the United States: fax +49 6221 345229, e-mail orders@springer. de, or visit http: 1lwww. springer. de. For information on translations, please contactApress directly at 2560 Ninth Street, Suite 219, Berkeley, CA 94 71 O. Phone 510-549-5930, fax 510-549-5939, e-mall info@apress. corn, or visit http: 1lwww. apress. corn. The information in this book is distributed onan "as is" hasis, without warranty. Although every precau tion has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or darnage caused or alleged to be caused directly or indirectly by the inforrnation contained in this work. The source code for this book is available to readers at http: 1/www. apress. corn in the Source Code section. To Kelli, Ryan, Christian, and Madeline Contents at a Glance About the Author .................................................................. xv Acknowledgments ............................................................... xvii Introduction ...................................................................... xix CHAPTER 1 First Steps in Perl .............................................. 1 CHAPTER 2 Scalars ........................................................ 13 CHAPTER 3 Control Flow Constructs ....................................... 51 CHAPTER 4 Lists and Arrays ............................................... 77 CHAPTER 5 Hashes ....................................................... 111 CHAPTER 6 Subroutines/Functions ........................................ 121 CHAPTER 7 Regular Expressions .......................................... 149 CHAPTER 8 Files and Data ................................................ 177 CHAPTER 9 String Processing ............................................. 205 CHAPTER 10 Interfacing to the Operating System ........................... 213 CHAPTER 11 References .................................................... 229 CHAPTER 12 Object-Oriented Perl .......................................... 253 CHAPTER 13 Modules ...................................................... 287 CHAPTER 14 Introduction to CGI. ........................................... 315 CHAPTER 15 Perl and OBI .................................................. 347 APPENDIX Exercise Solutions ............................................ 385 INDEX ......................................................................... 407 v Contents About the Author .................................................................. xv Acknowledgments ............................................................... xvii Introduction ...................................................................... xix HAPTER 1 First Steps in Perl ............................................ 1 Programming Languages .......................................... 1 Our First Perl Program ............................................ 2 Program Structure ........................................... 5 Escape Sequences ........................................... 8 Whitespace ................................................. 8 Number Systems ................................................. 8 The Perl Debugger ............................................... 1o Summary ...................................................... 10 Exercises ....................................................... 11 HAPTER 2 Scalars: ....................................................... 13 Types of Data ................................................... 13 Numbers .................................................. 14 Strings .................................................... 17 Here-Documents ........................................... 20 Converting Between Numbers and Strings ..................... 20 Operators ...................................................... 21 Numeric Operators ......................................... 22 String Operators ............................................ 31 Operators to Be Seen Later .................................. 35 Operator Precedence ........................................ 36 Variables ....................................................... 37 Scoping ................................................... 41 Variable Names ............................................ 44 Variable Interpolation ............................................ 44 Currency Converter ......................................... 46 vii viii Two Miscellaneous Functions ..................................... 48 The exitO Function .......................................... 48 The dieO Function .......................................... 49 Summary ...................................................... 50 Exercises ....................................................... 50 3 Control Flow Constructs;, .................................... 51 The if Statement ................................................ 52 Operators Revisited ......................................... 53 Multiple Choice: if ... else ................................... 58 The unless Statement ....................................... 61 Expression Modifiers ........................................ 62 Using Short-Circuited Evaluation .............................. 63 Looping Constructs .............................................. 63 The while Loop .................................................. 63 while (<STDIN>) ........................................... 65 Infinite Loops .............................................. 66 Looping Until ............................................... 67 The for Loop ............................................... 68 The foreach Loop ........................................... 68 do .. while and do .. until .................................... 69 Loop Control Constructs .......................................... 71 Breaking Out. .............................................. 71 Going On to the Next ........................................ 72 Reexecuting the Loop ....................................... 73 Loop Labels ................................................ 74 goto ...................................................... 76 Summary ...................................................... 76 Exercises ....................................................... 76 4 Lists and Arrays ............................................. 77 Lists ........................................................... 77 Simple Lists ............................................... 77 Less Simple Lists ..........................................