Perl 5 Tutorial, First Edition

Perl 5 Tutorial, First Edition

-

English
241 Pages
Read
Download
Downloading requires you to have access to the YouScribe library
Learn all about the services we offer

Description

Perl 5 Tutorial
First Edition
Chan Bernard Ki Hong Perl is copyright by Larry Wall.
Linux is a trademark of Linus Torvalds.
Unix is a trk of AT&T Bell Laboratories.
Perl 5 Tutorial
First Edition
Author: Chan Bernard Ki Hong (webmaster@cbkihong.com)
Web site: http://www.cbkihong.com
Date of Printing: December 24, 2003
Total Number of Pages: 241
APrepared from LT X source les by the author.E
' 2001 2003 by Chan Bernard Ki Hong.
While the author of this document has taken the best effort in enhancing the technical accuracy as well
as readability of this publication, please note that this document is released as is without guarantee for
accuracy or suitability of any kind. The full text of the terms and conditions of usage and distribution
can be found at the end of this document.
In order to further enhance the quality of this publication, the author would like to hear from you, the
fellow readers. Comments or suggestions on this publication are very much appreciated. Please feel free
to forward me your comments through the email feedback form or the feedback forum on the author’s
Web site. Contents
1 Introduction to Programming 1
1.1 What is Perl? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 A Trivial Introduction to Computer Programming . . . . . . . . . . . . . . . . . . . . 1
1.3 Scripts vs. Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.4 An Overview of the Software Development Process . . . ...

Subjects

Informations

Published by
Reads 54
Language English
Document size 1 MB
Report a problem
Perl 5 Tutorial First Edition Chan Bernard Ki Hong Perl is copyright by Larry Wall. Linux is a trademark of Linus Torvalds. Unix is a trk of AT&T Bell Laboratories. Perl 5 Tutorial First Edition Author: Chan Bernard Ki Hong (webmaster@cbkihong.com) Web site: http://www.cbkihong.com Date of Printing: December 24, 2003 Total Number of Pages: 241 APrepared from LT X source les by the author.E ' 2001 2003 by Chan Bernard Ki Hong. While the author of this document has taken the best effort in enhancing the technical accuracy as well as readability of this publication, please note that this document is released as is without guarantee for accuracy or suitability of any kind. The full text of the terms and conditions of usage and distribution can be found at the end of this document. In order to further enhance the quality of this publication, the author would like to hear from you, the fellow readers. Comments or suggestions on this publication are very much appreciated. Please feel free to forward me your comments through the email feedback form or the feedback forum on the author’s Web site. Contents 1 Introduction to Programming 1 1.1 What is Perl? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.2 A Trivial Introduction to Computer Programming . . . . . . . . . . . . . . . . . . . . 1 1.3 Scripts vs. Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1.4 An Overview of the Software Development Process . . . . . . . . . . . . . . . . . . . . 4 2 Getting Started 7 2.1 What can Perl do? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 2.2 Comparison with Other Programming Languages . . . . . . . . . . . . . . . . . . . . 8 2.2.1 C/C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 2.2.2 PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 2.2.3 Java/JSP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 2.2.4 ASP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 2.3 What do I need to learn Perl? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 2.4 Make Good Use of Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 2.5 The Traditional H ello World Program . . . . . . . . . . . . . . . . . . . . . . . . . . 12 2.6 How A Perl Program Is Executed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 2.7 Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 2.7.1 Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 2.7.2 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 2.8 Introduction to Data Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 3 Manipulation of Data Structures 23 3.1 Scalar Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 3.1.1 Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 3.1.2 Nomenclature . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 3.1.3 Variable Substitution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 3.1.4 substr() Extraction of Substrings . . . . . . . . . . . . . . . . . . . . . . 26 3.1.5 length() Length of String . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 3.2 Lists and Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 3.2.1 Creating an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 3.2.2 Adding Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 3.2.3 Getting the number of Elements in an Array . . . . . . . . . . . . . . . . . . . 29 3.2.4 Accessing Elements in an Array . . . . . . . . . . . . . . . . . . . . . . . . . . 30 3.2.5 Removing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 3.2.6 splice(): the Versatile Function . . . . . . . . . . . . . . . . . . . . . . . . . 32 3.2.7 Miscellaneous List-Related Functions . . . . . . . . . . . . . . . . . . . . . . . 33 3.2.8 Check for Existence of Elements in an Array (Avoid!) . . . . . . . . . . . . . . 35 3.3 Hashes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 3.3.1 Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 i ii CONTENTS 3.3.2 Accessing elements in the Hash . . . . . . . . . . . . . . . . . . . . . . . . . . 40 3.3.3 Removing Elements from a Hash . . . . . . . . . . . . . . . . . . . . . . . . . 40 3.3.4 Searching for an Element in a Hash . . . . . . . . . . . . . . . . . . . . . . . . 41 3.4 Contexts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 3.5 Miscellaneous Issues with Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 4 Operators 47 4.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 4.2 Description of some Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 4.2.1 Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 4.2.2 String Manipulation Operators . . . . . . . . . . . . . . . . . . . . . . . . . . 50 4.2.3 Comparison Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 4.2.4 Equality Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 4.2.5 Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 4.2.6 Bitwise Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 4.2.7 Assignment Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 4.2.8 Other Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 4.3 Operator Precedence and Associativity . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 4.4 Constructing Your Ownsort() Routine . . . . . . . . . . . . . . . . . . . . . . . . . 64 5 Conditionals, Loops & Subroutines 67 5.1 Breaking Up Your Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 5.1.1 Sourcing External Files withrequire() . . . . . . . . . . . . . . . . . . . . . 67 5.2 Scope and Code Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 5.2.1 Introduction to Associations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 5.2.2 Code Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 5.3 Subroutines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 5.3.1 Creating and Using A Subroutine . . . . . . . . . . . . . . . . . . . . . . . . . 71 5.3.2 Prototypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 5.3.3 Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 5.3.4 Creating Context-sensitive Subroutines . . . . . . . . . . . . . . . . . . . . . . 78 5.4 Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 5.4.1 Declaring a Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 5.4.2 Package Variable Referencing . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 5.4.3 P Variables and Symbol Tables . . . . . . . . . . . . . . . . . . . . . . . 81 5.4.4 Package Constructors withBEGIN fg . . . . . . . . . . . . . . . . . . . . . . . 82 5.5 Lexical Binding and Dynamic Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 5.6 Conditionals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 5.7 Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 5.7.1 for loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 5.7.2 while loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 5.7.3 foreach loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 5.7.4 Loop Control Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 5.8 Leftovers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 6 References 95 6.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 6.2 Creating a Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 6.3 Using References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 6.4 Pass By Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 6.5 How Everything Fits Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 CONTENTS iii 6.6 Typeglobs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 7 Object-Oriented Programming 105 7.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 7.2 Object-Oriented Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 7.2.1 Programming Paradigms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 7.2.2 Basic Ideas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 7.2.3 Fundamental Elements of Object-Oriented Programming . . . . . . . . . . . . 107 7.3 OOP Primer: Statistics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 7.3.1 Creating and Using A Perl Class . . . . . . . . . . . . . . . . . . . . . . . . . . 110 7.3.2 How A Class Is Instantiated . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 7.4 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 8 Files and Filehandles 119 8.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 8.2 Filehandles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 8.2.1 open a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 8.2.2 Output Redirection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 8.3 File Input and Output Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 8.3.1 readline() Read A Line from Filehandle . . . . . . . . . . . . . . . . . . . 122 8.3.2 binmode() Binary Mode Declaration . . . . . . . . . . . . . . . . . . . . . 122 8.3.3 read() Read A Speci ed Number of Characters from Filehandle . . . . . . . 123 8.3.4 print()/printf() Output To A FileHandle . . . . . . . . . . . . . . . . . 124 8.3.5 seek() Set File Pointer Position . . . . . . . . . . . . . . . . . . . . . . . . 126 8.3.6 tell() Return File Pointer Position . . . . . . . . . . . . . . . . . . . . . . 127 8.3.7 close() Close Anopened File . . . . . . . . . . . . . . . . . . . . . . . . . 127 8.4 Directory Traversal Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 8.4.1 opendir() Open A Directory . . . . . . . . . . . . . . . . . . . . . . . . . 128 8.4.2 readdir() Read Directory Content . . . . . . . . . . . . . . . . . . . . . . 128 8.4.3 closedir() Close A Directory . . . . . . . . . . . . . . . . . . . . . . . . . 128 8.4.4 Example: File Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 8.5 File Test Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 8.6 File Locking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 9 Regular Expressions 137 9.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 9.2 Building a Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 9.2.1 Getting your Foot Wet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 9.2.2 Introduction tom// and the Binding Operator . . . . . . . . . . . . . . . . . . 139 9.2.3 Metacharacters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 9.2.4 Quanti ers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 9.2.5 Character Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 9.2.6 Backtracking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 9.3 Regular Expression Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 9.3.1 m// Pattern Matching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 9.3.2 s/// Search and Replace . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 9.3.3 tr/// Global Character Transliteration . . . . . . . . . . . . . . . . . . . . 144 9.4 Constructing Complex Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . 145 iv CONTENTS 10 Runtime Evaluation & Error Trapping 149 10.1 Warnings and Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 10.2 Error-Related Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 10.3 eval . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 10.4 Backticks andsystem() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 10.5 Why Runtime Evaluation Should Be Restricted . . . . . . . . . . . . . . . . . . . . . . 151 10.6 Next Generation Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . 152 10.6.1 Basic Ideas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152 10.6.2 Throwing Different Kinds of Errors . . . . . . . . . . . . . . . . . . . . . . . . 155 10.6.3 Other Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 10.7 Other Methods To Catch Programming Errors . . . . . . . . . . . . . . . . . . . . . . 159 10.7.1 The-w Switch Enable Warnings . . . . . . . . . . . . . . . . . . . . . . . . 159 10.7.2 Banning Unsafe Constructs Withstrict . . . . . . . . . . . . . . . . . . . . . 160 10.7.3 The-T Switch Enable Taint Checking . . . . . . . . . . . . . . . . . . . . . 163 11 CGI Programming 169 11.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 11.2 Static Content and Dynamic Content . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 11.2.1 The Hypertext Markup Language . . . . . . . . . . . . . . . . . . . . . . . . . 169 11.2.2 The World Wide Web . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170 11.3 What is CGI? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 11.4 Your First CGI Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 11.5 GET vs. POST . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 11.6 File Upload . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180 11.7 Important HTTP Header Fields and Environment Variables . . . . . . . . . . . . . . . 182 11.7.1 CGI-Related Environment Variables . . . . . . . . . . . . . . . . . . . . . . . . 182 11.7.2 HTTP Header Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 11.8 Server Side Includes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 11.9 Security Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 11.9.1 Why Should I Care? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 11.9.2 Some Forms of Attack Explained . . . . . . . . . . . . . . . . . . . . . . . . . 187 11.9.3 Safe CGI Scripting Guidelines . . . . . . . . . . . . . . . . . . . . . . . . . . . 190 A How A Hash Works 193 A.1 Program Listing of Example Implementation . . . . . . . . . . . . . . . . . . . . . . . 193 A.2 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198 A.3 Principles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 A.4 Notes on Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 B Administration 201 B.1 CPAN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 B.1.1 Accessing the Module Database on the Web . . . . . . . . . . . . . . . . . . . . 201 B.1.2 Package Managers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 B.1.3 Installing Modules using CPAN.pm . . . . . . . . . . . . . . . . . . . . . . . . 202 B.1.4 I M The Traditional Way . . . . . . . . . . . . . . . . . . . . 203 C Setting Up A Web Server 205 C.1 Apache . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205 C.1.1 Microsoft Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205 C.1.2 Unix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210 CONTENTS v D A Unix Primer 213 D.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 D.1.1 Why Should I Care About Unix? . . . . . . . . . . . . . . . . . . . . . . . . . . 213 D.1.2 What Is Unix? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 D.1.3 The Overall Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214 D.2 Filesystems and Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 D.2.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 D.2.2 Symbolic Links and Hard Links . . . . . . . . . . . . . . . . . . . . . . . . . . 216 D.2.3 Permission and Ownership . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220 D.2.4 Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222 D.2.5 The Special Permission Bits . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 E In The Next Edition 225 Index 226 vi CONTENTS Preface If you are looking for a free Perl tutorial that is packed with everything you need to know to get started on Perl programming, look no further. Presenting before you is probably the most comprehensive Perl tutorial on the Web, the product of two years of diligence seeking reference from related books and Web sites. Perl is a programming language that is offered at no cost. So wouldn’t it be nice if you can also learn it at no cost? Packed with some background knowledge of programming in C++ and Visual Basic, when I started learning Perl several years ago, I couldn’t even nd one good online tutorial that covered at least the basics of the Perl language and was free. Most Perl tutorials I could nd merely covered the very basic topics such as scalar/list assignment, operators and some o w control structures etc. On the other hand, although I have accumulated certain levels of experience in a number of programming languages, the of cial Perl manual pages are quite technical with whole pages of jargons that I was not very familiar with. As a result, the book Lear ning Perl written by Larry Wall, the inventor of the Perl language, nat- urally became the only Perl textbook available. The O’Reilly Perl Series present the most authoritative and well-written resources on the subject written by the core developers of Perl. While you are strongly recommended to grab one copy of each if you have the money, they are not so cheap, though, and that’s the motive behind my writing of this tutorial so that more people with no programming background can start learning this stupendous and powerful language in a more cost-effective way. Although this tutorial covers a rather wide range of topics, similar to what you can nd in some other Perl guidebooks, you are strongly encouraged to read those books too, since their paedagogies of teach- ing may suit you more. Here are several features of this tutorial: ? As this is not a printed book, I will constantly add new materials to this tutorial as needed, thus enriching the content of this tutorial. Moreover, in order to help me improve the quality of this tutorial, it is crucial for you to forward me your comments and suggestions so that I can make further improvements to it. ? Earlier drafts of this tutorial were published in HTML format on my Web site. In response to re- quests made from several visitors, this tutorial has been made available in PDF format for down- load. I hope this will help those who are charged on time basis for connecting to the Internet. This Atutorial is typeset in LT X, a renowned document typesetting system that has been widely usedE in the academic community on Unix-compatible systems (although it is available on nearly any operating systems you can think of). The HTML version has been discontinued, until a solution can be found which allows both versions to be generated from the same source base. ? You will nd a list of Web links and references to book chapters after each chapter where applica- ble which contains additional materials that ambitious learners will nd helpful to further your understanding of the subject. vii viii Preface ? Throughout the text there would be many examples. In this tutorial, you will nd two types of examples examples and illustrations. Illustrations are intended to demonstrate a partic- ular concept just mentioned, and are shorter in general. You will nd them embedded inline throughout the tutorial. On the other hand, examples are more functional and resemble practical scripts, and are usually simpli ed versions of such. They usually demonstrate how different parts of a script can work together to realize the desired functionalities or consolidate some important concepts learned in a particular chapter. ? If applicable, there will be some exercises in the form of concept consolidation questions as well as programming exercises at the end of each chapter to give readers chances to test how much they understand the materials learned from this tutorial and apply their knowledge through practice. This is the First Edition of the Perl 5 Tutorial. It primarily focuses on fundamental Perl programming knowledge that any Perl programmer should be familiar with. I start with some basic ideas behind com- puter programming in general, and then move on to basic Perl programming with elementary topics such as operators and simple data structures. The chapter on scoping and subroutines is the gateway to subsequent, but more advanced topics such as references and object-oriented programming. The remaining chapters are rather assorted in topic, covering the use of lehandles, le I/O and regular ex- pressions in detail. There is also a dedicated chapter on error handling which discusses facilities that you can use to locate logical errors and enhance program security. The nal chapter on CGI programming builds on knowledge covered in all earlier chapters. Readers will learn how to write a Perl program that can be used for dynamic scripting on the World Wide Web. However short, the main text already embraces the most important fundamental subjects in the Perl programming language. In the appen- dices, instructions are given on acquiring and installing Perl modules, setting up a basic but functional CGI-enabled Web server for script testing, and there is a voluminous coverage of Unix fundamentals. As much of Perl is based on Unix concepts, I believe a brief understanding of this operating system is bene cial to Perl programmers. An appendix is also prepared to give my readers an idea of the internal structure of general hashes. While authoring of this tutorial cannot proceed inde nit ely, topics that were planned but cannot be included in this edition subject to time constraints are deferred to the Second Edition. A list of these topics appear at the end of this document for your reference. In the second release candidate of this tutorial I made an audacious attempt of adding into it two topics that are rarely discussed in most Perl literature. The rst is theError CPAN module for exception han- dling. The second attempt, which is an even more audacious one, is an introduction of the nit e-state automaton (FSA) for construction of complex regular expressions for pattern matching. While FSA is a fundamental topic in Computer Science (CS) studies, this is seldom mentioned outside the CS circle. Although there is a high degree of correspondence between regular expressions and FSA, this may not be at all obvious to a reader without relevant background, despite I have avoided rigorous treatment of the topic and tried to explain it in a manner that would be more easily communicable to fellow readers. I would like to emphasize this topic is not essential in Perl programming, and I only intend to use it as a tool to formulate better patterns. Feel free to skip it if that is not comfortable to you and I require your feedback of whether these topics can help you as I originally intended. It is important for me to reiterate that this document is not intended to be a substitute for the of cial Perl manual pages (aka man pages) and other of cial Perl literature. In fact, it is the set of manual pages that covers the Perl language in suf ciently ne detail, and it will be the most important set of document after you have accumulated certain level of knowledge and programming experience. The Perl man pages are written in the most concise and correct technical parlance, and as a result they are not very suitable for new programmers to understand. The primary objective of this tutorial is to bridge the gap so as to supplement readers with suf cient knowledge to understand the man pages. Therefore, this tu- torial presents a different perspective compared with some other Perl guidebooks available at your local bookstores from the mainstream computer book publishers. With a Computer Science background, I