SAMS Teach Yourself Perl in 24 Hours THIRD EDITION By Clinton Pierce Publisher: Sams Publishing Pub Date: June 15, 2005 ISBN: 0-672-32793-7 Pages: 480 Table of Contents | Index Learn Perl programming quickly and easily with 24 one-hour lessons in Sams Teach Yourself Perl in 24 Hours, 3/e These step-by-step lessons will teach you the basics of Perl and how to apply it in web development and system administration Plus, the third edition has been updated to include 5 chapters on new technologies, information on the latest version of Perl and a look ahead to Perl 6 You will focus on real-world development, including how to: Effectively use Perl for large development projects using Perl Modules Use Perl for data processing Utilize Perl as a "glue" language with other programming languages Use Perl as a web development language The concise, modular format of the Sams Teach Yourself Perl in 24 Hours, 3/e is the ideal way to learn for today's working professional SAMS Teach Yourself Perl in 24 Hours THIRD EDITION By Clinton Pierce Publisher: Sams Publishing Pub Date: June 15, 2005 ISBN: 0-672-32793-7 Pages: 480 Table of Contents | Index Copyright About the Author Acknowledgments We Want to Hear from You! Reader Services Introduction How to Use This Book Conventions Used in This Book Part I Perl Fundamentals Hour 1 Getting Started with Perl Installing Perl Documentation Your First Program Summary Q&A Workshop Hour 2 Perl's Building Blocks: Numbers and Strings Literals Scalar Variables Expressions and Operators More Operators Exercise: Interest Calculator Summary Q&A Workshop Hour 3 Controlling the Program's Flow Blocks The if Statement Looping Other Flow Control Tools Exercise: Finding Primes Summary Q&A Workshop Hour 4 Stacking Building Blocks: Lists and Arrays Putting Things into Lists and Arrays Getting Elements Out of an Array Manipulating Arrays Exercise: Playing a Little Game Summary Q&A Workshop Hour 5 Working with Files Opening Files Reading Writing Free Files, Testing Files, and Binary Data Summary Q&A Workshop Hour 6 Pattern Matching Simple Patterns The Metacharacters Substitution Exercise: Cleaning Up Input Data Pattern Matching Odds and Ends Summary Q&A Workshop Hour 7 Hashes Filling Your Hash Getting Data Out of a Hash Lists and Hashes Hash Odds and Ends Useful Things to Do with a Hash Exercise: Creating a Simple Customer Database with Perl Summary Q&A Workshop Hour 8 Functions Creating and Calling Subroutines Scope Exercise: Statistics Function Footnotes Summary Q&A Workshop Part II Advanced Features Hour 9 More Functions and Operators Searching Scalars Transliteration, Not Substitution A Better Way to print Exercise: A Formatted Report New Ways with Arrays Summary Q&A Workshop Hour 10 Files and Directories Getting a Directory Listing Exercise: The Unix grep Directories Unix Stuff Everything You Ever Wanted to Know About THAT File Exercise: Renaming Files En Masse Summary Q&A Workshop Hour 11 System Interaction The system() Function Capturing Output Pipes First Lesson in Portability Summary Q&A Workshop Hour 12 Using Perl's Command-Line Tools What Is the Debugger? Exercise: Finding the Bug Other Command-Line Stuff Summary Q&A Workshop Hour 13 References and Structures Reference Basics Recipes for Structures Exercise: Another Game, Maze Summary Q&A Workshop Hour 14 Using Modules A Gentle Introduction A Quick Tour Full List of Standard Modules Summary Q&A Workshop Hour 15 Finding Permanence DBM Files Exercise: A Free-Form Memo Pad Text Files as Databases Random File Access Locking Summary Q&A Workshop Hour 16 The Perl Community What's Perl All About, Anyway? The Comprehensive Perl Archive Network (CPAN) Your Next Steps Other Resources Summary Q&A Workshop Part III Applying Perl Hour 17 Writing Modules Building a Module Scoping Revisited Another statement: our Example: A Module to Handle Common File Information Requests Summary Q&A Workshop Hour 18 Object Primer Classes, Properties, and Methods Example: Implementation of Car in Perl Example: File Information Class Summary Q&A Workshop Hour 19 Data Processing How to Look at Data Dealing with Table Data XML Data Summary Q&A Workshop Hour 20 Perl as a Glue Language Weather Station Presenting Data as PDF Reading and Writing Excel Spreadsheets Summary Q&A Workshop Hour 21 Introduction to CGI Browsing the Web Don't Skip This Section Your First CGI Program What to Do When Your CGI Program Doesn't Work Summary Q&A Workshop Hour 22 Basic Forms How Forms Work Passing Information to Your CGI Program Web Security 101 A Guestbook Summary Q&A Workshop Hour 23 Complex Forms The Stateless Web Hidden Fields A Multipage Survey Summary Q&A Workshop Hour 24 Manipulating HTTP and Cookies The HTTP Conversation More Details on Calling CGI Programs Cookies Long Term Cookies Problems with Cookies Summary Q&A Workshop Part IV Appendixes Appendix A Installing Modules Picking the Right Module Installing the Modules Under… What to Do When You're Not Allowed to Install Modules Index Copyright Copyright © 2005 by Sams Publishing All rights reserved No part of this book shall be reproduced, stored in a retrieval system, or transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise, without written permission from the publisher No patent liability is assumed with respect to the use of the information contained herein Although every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions Nor is any liability assumed for damages resulting from the use of the information contained herein Library of Congress Catalog Card Number: 2004195543 Printed in the United States of America First Printing: June 2005 08 07 06 05 4 3 2 1 Trademarks All terms mentioned in this book that are known to be trademarks or service marks have been appropriately capitalized Sams Publishing cannot attest to the accuracy of this information Use of a term in this book should not be regarded as affecting the validity of any trademark or service mark Warning and Disclaimer Every effort has been made to make this book as complete and as accurate as possible, but no warranty or fitness is implied The information provided is on an "as is" basis The author and the publisher shall have neither liability nor responsibility to any person or entity with respect to any loss or damages arising from the information contained in this book Bulk Sales Sams Publishing offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales For more information, please contact U.S Corporate and Government Sales 1-800-382-3419 corpsales@pearsontechgroup.com For sales outside of the U.S., please contact International Sales international@pearsoned.com Credits Senior Editor Scott Meyers Managing Editor Charlotte Clapp Project Editor Andy Beaster Copy Editor Kate Givens Indexer Chris Barrick Proofreader Heather Arle Technical Editor Andy Lester Publishing Coordinator Vanessa Evans Book Designer Gary Adair Page Layout Kelly Maish Dedication To Heidi and Calvin, for not killing me this summer and supporting me during this adventure Without your support, I'd have lost even more of my marbles Frequently Asked Questions reading troubleshooting resources question procedure 2nd 3rd 4th 5th user-defined functions [See subroutines] Using Multiple FileInfo Objects at Once (Listing 18.8) 2nd 3rd 4th Using the Bulk Order Function (Listing 19.12) utilities grep getting directory listings 2nd 3rd 4th Index [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] [Y] [Z] value hashes values hashes DBM files variables array assignments converting between scalars and elements extracting elements finding the end 2nd iterating over reordering size 2nd slices splicing arrays passing to subroutines two-dimensional data structures building references debugging modifying as program runs output filehandles closing 2nd indirect names reading writing to files global hashes adding elements applications 2nd DBM files each function initializing inverting key-value pairs list context passing to subroutines removing keys retrieving elements testing for keys interpolation lexical 2nd local names package 2nd private blocks subroutines references arrays creating dereferencing hashes modifying original value 2nd regular expressions matching against other than $ 2nd renaming English module scalar converting between arrays and default values regular expressions scalars assignment string searches scope defined dynamic lexical scoping 2nd 3rd 4th Small Program Using Package and Lexical Variables (Listing 17.4) 2nd Small Scoping Example (Listing 17.3) 2nd sorting special $_ type identifiers Vendor Parts List (Listing 19.3) 2nd 3rd version numbers Perl module errors vin subroutine Car class example VMS pathnames Index [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] [Y] [Z] Wall, Larry Perl history warn function opening files weather reports NOAA (National Oceanic and Atmospheric Administration) queries/retrievals 2nd weather station application features requirements 2nd information elements assembling 2nd listings Fetching the Weather (20.3) 2nd Finding the Nearest Airport (20.2) 2nd 3rd Retrieving the ZIP Information (20.1) 2nd Setting Up the Document (20.4) 2nd 3rd 4th 5th 6th 7th NOAA (National Oceanic and Atmospheric Administration) airport queries/retrievals 2nd 3rd 4th METAR codes weather report queries/retrievals 2nd reports printing in PDF format 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th U.S Census Bureau zip code queries/retrievals 2nd 3rd zip code resources Web browsers web browsers cookies disabling erasing expiration dates 2nd 3rd 4th 5th flashing long-term 2nd 3rd 4th 5th privacy issues 2nd 3rd restricting return destinations 2nd 3rd retrieving via CGI programs 2nd sending via CGI programs storage file support levels 2nd transient nature of viewing options (IE6) HTTP server connection process 2nd 3rd HTTP cookies CGI program requests function of 2nd HTTP redirection 2nd 3rd redirecting while transmitting cookies 2nd Web browsers retrieving dynamic pages CGI programs 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 26th 27th 28th retrieving static pages 2nd 3rd 4th 5th Web clients web pages background colors setting via cookies 2nd 3rd 4th Web pages CGI (Common Gateway Interface) 2nd 3rd 4th 5th 6th debugging programs 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th installing programs on Web servers 2nd 3rd 4th running programs 2nd Web server needs 2nd 3rd 4th 5th writing programs 2nd dynamic retrieving 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 26th 27th 28th web pages fetching manually (HTTP) 2nd 3rd Web pages HTML forms 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 26th 27th 28th web pages links tracking user activity via cookies 2nd Web pages static retrieving 2nd 3rd 4th 5th Web servers debugging CGI programs 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th installing CGI programs 2nd 3rd 4th FTP-only access on NT servers 2nd FTP-only access on Unix servers 2nd 3rd local access on NT servers local access on Unix servers 2nd web servers manual connections Telnet client program 2nd 3rd Web servers running CGI programs 2nd writing CGI programs 2nd 3rd 4th 5th 6th 7th Apache Web sites Apache Web server CPAN searching CPAN (Comprehensive Perl Archive Network) modules GNU history of Perl Internet history online documentation Open Source essays Perl Online Documentation The Perl Journal 2nd Usenet archive on Google Yahoo! websites ActiveState module installation resources cookies anonymous surfing privacy issues 2nd 3rd restricting return destinations 2nd 3rd CPAN 2nd Hobbe's Internet Timeline HTTP redirection CGI programs implementing 2nd 3rd 4th Redirection Based on Browser (Listing 24.1) 2nd 3rd NOAA (National Oceanic and Atmospheric Administration) airport queries 2nd 3rd 4th METAR codes weather report queries 2nd PerlMonks help resources 2nd 3rd U.S Census Bureau zip code queries 2nd 3rd weights Earth to Moon conversion 2nd 3rd 4th 5th while loop 2nd Sample while Loop (Listing 3.2) 2nd whitespace character class WidgetCo Bike Parts Company data representation 2nd binary data 2nd hierarchical data 2nd table data 2nd unstructured data 2nd table data email header 2nd email order taker 2nd 3rd 4th 5th 6th email order verifier 2nd 3rd 4th 5th 6th order information 2nd payment block 2nd XML data Bulk Order Sample Document (Listing 19.10) 2nd Reading Orders in Bulk Using XML (Listing 19.11) 2nd 3rd Using the Bulk Order Function (Listing 19.12) wildcards 2nd 3rd 4th Win32++* module (CPAN) Windows end of line pathnames Universal Naming Convention (UNC) Windows 95/98 command prompt locking Perl installing preinstalled Perl documentation 2nd Windows 95/98/NT installing modules under 2nd 3rd Windows NT command prompt file locking Perl installing Perl documentation 2nd Perl preinstalled Web servers installing FTP-only CGI program access 2nd installing local CGI program access Windows NT Resource Kit word character class word processor for programming World Wide Web [See WWW] World Wide Web Consortium (W3C) XML format development specifications writeability file test writing CGI programs security programs functions WWW Web browsers retrieving dynamic pages 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 26th 27th 28th retrieving static pages 2nd 3rd 4th 5th WWW (World Wide Web WWW++* module (CPAN) Index [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] [Y] [Z] x (repetition operator) list context XML (extensible Markup Language) Data Munging with Perl documents elements uses Even More Convoluted XML Parse in Regular Expressions (Listing 19.8) 2nd 3rd files reading via regular expressions 2nd 3rd 4th 5th 6th 7th 8th 9th reading via XML++Simple 2nd 3rd Reading the XML File, Almost (Listing 19.5) 2nd Sample Using XML++Simple to Parse Your Album Document (Listing 19.9) 2nd 3rd Still Flawed XML Reading (Listing 19.6) 2nd The Album XML Document, re-arranged (Listing 19.7) 2nd WidgetCo Bike Parts Company example Bulk Order Sample Document (Listing 19.10) 2nd Reading Orders in Bulk Using XML (Listing 19.11) 2nd 3rd Using the Bulk Order Function (Listing 19.12) World Wide Web Consortium (W3C) format development specifications XML++Simple document errors troubleshooting returning hashes troubleshooting Sample Using XML++Simple to Parse Your Album Document (Listing 19.9) 2nd 3rd XML files reading 2nd 3rd XML++Simple module weather station application zip code retrieval 2nd 3rd Index [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] [Y] [Z] y/// [See tr///] Yahoo! Web site Your Moon Weight (Listing 6.2) 2nd 3rd Index [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] [Y] [Z] zip codes U.S Census Bureau database queries/retrievals 2nd 3rd weather station application resources .. .SAMS Teach Yourself Perl in 24 Hours THIRD EDITION By Clinton Pierce Publisher: Sams Publishing Pub Date: June 15, 2005 ISBN: 0-672-32793-7 Pages: 480 Table of Contents | Index ... HOUR 6 Pattern Matching HOUR 7 Hashes HOUR 8 Functions Hour 1 Getting Started with Perl What You'll Learn in This Hour Installing Perl Accessing Perl' s internal documentation Writing your first Perl script Perl is a general-purpose programming language... Generally for Mac OS X, follow the Unix instructions in this book There are extensions particular to Mac OS X that you can download from CPAN Installing Perl on Windows To install Perl under Windows, keep in mind that, as with many things in life,