Perl 5 Tutorial
240 pages
English

Perl 5 Tutorial

-

Le téléchargement nécessite un accès à la bibliothèque YouScribe
Tout savoir sur nos offres
240 pages
English
Le téléchargement nécessite un accès à la bibliothèque YouScribe
Tout savoir sur nos offres

Description

Perl 5 Tutorial
First Edition
(Release Candidate 2)
Chan Bernard Ki Hong Perl is copyright by Larry Wall.
Linux is a trademark of Linus Torvalds.
Unix is a 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: September 14, 2003
Total Number of Pages: 240
APrepared from LT X source les by the author.E
Important: Please note that this is a preview edition of the document and is released for
collection of feedback purposes only. Therefore, drastic modi cations may be made to this
document at any time until it is completed and nalized.
DISCLAIMER
This document is released as is without guarantee for accuracy or suitability of any kind, so
use it at your own risk. However, if you notice any errors, or have any suggestions on how I
can further improve this tutorial, please feel free to forward me your comments through the
email feedback form or feedback forum. Your comments are very much appreciated. 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.4 An Overview of the Software Development Process . . . . . . . . . . . . . . . . 4
2 Getting Started 7
2.1 What can Perl do? . . . . . . . . . . . . . . ...

Sujets

Informations

Publié par
Nombre de lectures 122
Langue English
Poids de l'ouvrage 1 Mo

Extrait

Perl 5 Tutorial First Edition (Release Candidate 2) Chan Bernard Ki Hong Perl is copyright by Larry Wall. Linux is a trademark of Linus Torvalds. Unix is a 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: September 14, 2003 Total Number of Pages: 240 APrepared from LT X source les by the author.E Important: Please note that this is a preview edition of the document and is released for collection of feedback purposes only. Therefore, drastic modi cations may be made to this document at any time until it is completed and nalized. DISCLAIMER This document is released as is without guarantee for accuracy or suitability of any kind, so use it at your own risk. However, if you notice any errors, or have any suggestions on how I can further improve this tutorial, please feel free to forward me your comments through the email feedback form or feedback forum. Your comments are very much appreciated. 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 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? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 2.7.2 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 2.8 Introduction to Data Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 3.3.1 Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 i ii CONTENTS 3.3.2 Accessing elements in the Hash . . . . . . . . . . . . . . . . . . . . . . . . 40 3.3.3 Removing Elements from a Hash . . . . . . . . . . . . . . . . . . . . . . . 41 3.3.4 Searching for an Element in a Hash . . . . . . . . . . . . . . . . . . . . . 42 3.4 Contexts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 4.2.2 String Manipulation Operators . . . . . . . . . . . . . . . . . . . . . . . . 50 4.2.3 Comparison Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 4.2.4 Equality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 4.2.5 Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 4.2.6 Bitwise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 4.2.7 Assignment Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 4.2.8 Other Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 4.3 Operator Precedence and Associativity . . . . . . . . . . . . . . . . . . . . . . . 60 4.4 Constructing Your Ownsort() Routine . . . . . . . . . . . . . . . . . . . . . . . 65 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 5.3.3 Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 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 Variables and Symbol Tables . . . . . . . . . . . . . . . . . . . . 82 5.4.4 Package Constructors withBEGIN fg . . . . . . . . . . . . . . . . . . . . . 82 5.5 Lexical Binding and Dynamic Binding . . . . . . . . . . . . . . . . . . . . . . . . 83 5.6 Conditionals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 5.7 Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 5.7.1 for loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 5.7.2 while loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 5.7.3 foreach loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 5.7.4 Loop Control Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 6 References 93 6.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 6.2 Creating a Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 6.3 Using References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 6.4 Pass By Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 6.5 How Everything Fits Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 6.6 Typeglobs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 CONTENTS iii 7 Object-Oriented Programming 103 7.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 7.2 Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 7.2.1 Programming Paradigms . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 7.2.2 Basic Ideas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 7.2.3 Fundamental Elements of Object-Oriented Programming . . . . . . . . . 105 7.3 OOP Primer: Statistics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 7.3.1 Creating and Using A Perl Class . . . . . . . . . . . . . . . . . . . . . . . 109 7.3.2 How A Class Is Instantiated . . . . . . . . . . . . . . . . . . . . . . . . . . 110 7.4 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 7.5 Another Example: Traf c Light Simulation . . . . . . . . . . . . . . . . . . . . . 116 8 Files and Filehandles 121 8.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 8.2 Filehandles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 8.2.1 open a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 8.2.2 Output Redirection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 8.3 File Input and Output Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 8.3.1 readline() Reads A Line from Filehandle . . . . . . . . . . . . . . . . 124 8.3.2 binmode() Binary
  • Univers Univers
  • Ebooks Ebooks
  • Livres audio Livres audio
  • Presse Presse
  • Podcasts Podcasts
  • BD BD
  • Documents Documents