321x Filetype PDF File size 0.92 MB Source: www.tutorialspoint.com
Perl
About the Tutorial
Perl is a programming language developed by Larry Wall, especially designed for text
processing. It stands for Practical Extraction and Report Language. It runs on a
variety of platforms, such as Windows, Mac OS, and the various versions of UNIX.
This tutorial provides a complete understanding on Perl.
Audience
This reference has been prepared for beginners to help them understand the basic to
advanced concepts related to Perl Scripting languages.
Prerequisites
Before you start practicing with various types of examples given in this reference, we are
making an assumption that you have prior exposure to C programming and Unix Shell.
Copyright & Disclaimer
Copyright 2015 by Tutorials Point (I) Pvt. Ltd.
All the content and graphics published in this e-book are the property of Tutorials Point (I)
Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish
any contents or a part of contents of this e-book in any manner without written consent
of the publisher.
We strive to update the contents of our website and tutorials as timely and as precisely as
possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt.
Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our
website or its contents including this tutorial. If you discover any errors on our website or
in this tutorial, please notify us at contact@tutorialspoint.com
i
Perl
Table of Contents
About the Tutorial ............................................................................................................................................ i
Audience ........................................................................................................................................................... i
Prerequisites ..................................................................................................................................................... i
Copyright & Disclaimer ..................................................................................................................................... i
Table of Contents ............................................................................................................................................ ii
PART 1: PERL – BASICS ................................................................................................................ 1
1. Perl ─ Introduction .................................................................................................................................... 2
What is Perl? .................................................................................................................................................... 2
Perl Features .................................................................................................................................................... 2
Perl and the Web ............................................................................................................................................. 3
Perl is Interpreted ............................................................................................................................................ 3
2. Perl ─ Environment ................................................................................................................................... 4
Unix and Linux Installation .............................................................................................................................. 5
Running Perl .................................................................................................................................................... 6
3. Perl ─ Syntax Overview ............................................................................................................................. 9
Script Mode Programming .............................................................................................................................. 9
4. Perl ─ Data Types .................................................................................................................................... 15
Numeric Literals............................................................................................................................................. 15
String Literals ................................................................................................................................................. 16
5. Perl ─ Variables ....................................................................................................................................... 19
Creating Variables ......................................................................................................................................... 19
6. Perl ─ Scalars ........................................................................................................................................... 23
Scalar Operations .......................................................................................................................................... 25
7. Perl ─ Arrays ........................................................................................................................................... 28
8. Perl ─ Hashes .......................................................................................................................................... 39
9. Perl ─ If…Else........................................................................................................................................... 45
if statement ................................................................................................................................................... 46
if...else statement .......................................................................................................................................... 48
if...elsif...else statement ................................................................................................................................ 50
unless statement ........................................................................................................................................... 51
unless...else statement .................................................................................................................................. 53
unless...elsif..else statement ......................................................................................................................... 55
switch statement ........................................................................................................................................... 56
The ? : Operator ............................................................................................................................................ 59
10. Perl ─ Loops ............................................................................................................................................ 61
while loop ...................................................................................................................................................... 62
ii
Perl
until loop ....................................................................................................................................................... 64
for loop .......................................................................................................................................................... 66
foreach loop .................................................................................................................................................. 68
do...while loop ............................................................................................................................................... 70
nested loops .................................................................................................................................................. 71
Loop Control Statements ............................................................................................................................... 74
next statement .............................................................................................................................................. 74
last statement ................................................................................................................................................ 77
continue statement ....................................................................................................................................... 80
redo statement .............................................................................................................................................. 82
goto statement .............................................................................................................................................. 83
The Infinite Loop ............................................................................................................................................ 86
11. Perl ─ Operators ...................................................................................................................................... 87
What is an Operator? .................................................................................................................................... 87
Perl Arithmetic Operators ............................................................................................................................. 87
Perl Equality Operators ................................................................................................................................. 89
Perl Assignment Operators............................................................................................................................ 94
Perl Bitwise Operators ................................................................................................................................... 96
Perl Logical Operators ................................................................................................................................... 98
Quote-like Operators ................................................................................................................................... 100
Miscellaneous Operators ............................................................................................................................. 101
Perl Operators Precedence.......................................................................................................................... 103
12. Perl ─ Date and Time ............................................................................................................................. 106
GMT Time .................................................................................................................................................... 107
Format Date & Time .................................................................................................................................... 107
Epoch time ................................................................................................................................................... 108
POSIX Function strftime() ............................................................................................................................ 109
13. Perl ─ Subroutines ................................................................................................................................. 112
Define and Call a Subroutine ....................................................................................................................... 112
Passing Arguments to a Subroutine ............................................................................................................ 113
Passing Lists to Subroutines ........................................................................................................................ 114
Passing Hashes to Subroutines .................................................................................................................... 114
Returning Value from a Subroutine ............................................................................................................. 115
Private Variables in a Subroutine ................................................................................................................ 116
Temporary Values via local() ....................................................................................................................... 117
State Variables via state() ............................................................................................................................ 118
Subroutine Call Context ............................................................................................................................... 119
14. Perl ─ References .................................................................................................................................. 121
Create References ....................................................................................................................................... 121
Dereferencing .............................................................................................................................................. 122
Circular References ...................................................................................................................................... 123
References to Functions .............................................................................................................................. 124
iii
no reviews yet
Please Login to review.