Perl 5 Tutorial, First Edition
Total Page:16
File Type:pdf, Size:1020Kb
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 trademark of AT&T Bell Laboratories. Perl 5 Tutorial First Edition Author: Chan Bernard Ki Hong ([email protected]) Web site: http://www.cbkihong.com Date of Printing: December 24, 2003 Total Number of Pages: 241 Prepared from LATEX source files by the author. © 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 “Hello 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 Elements . 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 Own sort() Routine . 64 5 Conditionals, Loops & Subroutines 67 5.1 Breaking Up Your Code . 67 5.1.1 Sourcing External Files with require() . 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 Package Variables and Symbol Tables . 81 5.4.4 Package Constructors with BEGIN 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 . ..