SlideShare a Scribd company logo
1 of 176
UNIT I – Perl Basics
Lecture 1- Introduction to Perl
Programming
Introduction
Perl is a family of high-level, general-purpose, interpreted, dynamic
programming languages.
The languages in this family include Perl 5 and Perl 6.
Though Perl is not officially an acronym, there are various backronyms in use,
the most well-known being "Practical Extraction and Reporting Language".
Perl was originally developed by Larry Wall in 1987 as a general-purpose Unix
scripting language to make report processing easier. Since then, it has
undergone many changes and revisions.
Lecture 1 – Topic Name
2
3
Introduction
4
The Perl languages borrow features from other programming languages
including C, shell script (sh), AWK, and sed. They provide powerful text
processing facilities without the arbitrary data-length limits of many
contemporary Unix command line tools, facilitating easy manipulation of text
files.
Perl 5 gained widespread popularity in the late 1990s as a CGI scripting
language, in part due to its unsurpassed regular expression and string parsing
abilities
Introduction
5
A scripting or script language is a programming language that supports
scripts, programs written for a special run-time environment that automate the
execution of tasks that could alternatively be executed one-by-one by a human
operator. Scripting languages are often interpreted (rather than compiled).
Primitives are usually the elementary tasks or API calls, and the language
allows them to be combined into more complex programs. Environments that
can be automated through scripting include software applications, web pages
within a web browser, the shells of operating systems (OS), embedded
systems, as well as numerous games. A scripting language can be viewed as a
domain-specific language for a particular environment; in the case of scripting
an application, this is also known as an extension language.
Scripting languages
6
In an interpreted environment, the instructions are executed immediately after
parsing. Both tasks are performed by the interpreter. Interpreted languages
include the MS-Dos Batch language (the OS itself is the interpreter), shell
scripts in Unix/Linux systems, Java, Perl and BASICA (a very old BASIC
language). Advantages of interpreted languages include relative ease of
programming (since once you type your instructions into a text file, the
interpreter can run it) and no linker is required. Disadvantages include poor
speed performance and that you do not generate an executable (and therefore
distributable) program. The interpreter must be present on a system to run the
program.
Interpreted vs. Compiled Languages
7
Compilers parse the instructions into machine code and store them in a
separate file for later execution. Many modern compilers can compile (parse)
and execute in memory, giving the 'appearance' of an interpreted language.
However, the key difference is that parsing and execution occurs in two distinct
steps. Examples include newer forms of BASIC (such as Visual Basic), C/C++,
Delphi and many others. In a compiled environment, you may speak of several
separate files: source code (the text instructions you actually enter), object
code (the parsed source code) and the executable (the linked object code).
There is definitely an increase in complexity in using compilers, but the key
advantages are speed performance and that you can distribute stand-alone
executables.
Interpreted vs. Compiled Languages
8
Interpreted vs. Compiled Languages
9
Interpreted vs. Compiled Languages
10
Interpreted vs. Compiled Languages
11
Interpreted vs. Compiled Languages
12
ī‚§Perl takes the best features from other languages, such as C, awk, sed, sh,
and BASIC, among others.
ī‚§Perl is an interpreted language, which means that your code can be run as is,
without a compilation stage that creates a non portable executable program
ī‚§Perls database integration interface DBI supports third-party databases
including Oracle, Sybase, Postgres, MySQL and others.
ī‚§Perl works with HTML, XML, and other mark-up languages.
ī‚§Perl works cross platform.
ī‚§Perl supports both procedural and object-oriented programming.
ī‚§Perl interfaces with external C/C++ libraries through XS or SWIG.
ī‚§Perl is extensible. There are over 20,000 third party modules available from
the Comprehensive Perl Archive Network (CPAN).
ī‚§The Perl interpreter can be embedded into other systems.
Perl Features
13
ī‚§Perl used to be the most popular web programming language due to its text
manipulation capabilities and rapid development cycle.
ī‚§Perl is widely known as " the duct-tape of the Internet".
ī‚§Perl can handle encrypted Web data, including e-commerce transactions.
ī‚§Perl can be embedded into web servers to speed up processing by as much
as 2000%.
ī‚§Perl's mod_perl allows the Apache web server to embed a Perl interpreter.
ī‚§Perl's DBI package makes web-database integration easy.
Perl and the Web
14
Why Perl?
15
Perl Execution
16
Getting Perl Installation
17
Windows Installation
18
Windows Installation
19
Lecture 2 (Separator Page for every section)
Topic Name
Simple Perl program
21
Whitespaces in Perl
22
23
Single and Double Quotes in Perl
24
25
Escaping Characters
26
Unit No: 1 Unit name: Perl Basics
Lecture 3:
Datatypes and Variables
Perl is a loosely typed language and there is no need to specify a type for your data while using in
your program. The Perl interpreter will choose the type based on the context of the data itself.
Data Types in Perl
Lecture 3:
Datatypes and Variables
28
Numeric Literals
Lecture 3:
Datatypes and Variables
29
String Literals
Lecture 3:
Datatypes and Variables
30
Escape Characters
Lecture 3:
Datatypes and Variables
31
ī‚§ Variables are the reserved memory locations to store values. This means
that when you create a variable you reserve some space in memory.
ī‚§ Based on the data type of a variable, the interpreter allocates memory and
decides what can be stored in the reserved memory. Therefore, by assigning
different data types to variables, you can store integers, decimals, or strings
in these variables.
ī‚§ Perl maintains every variable type in a separate namespace. So you can,
without fear of conflict, use the same name for a scalar variable, an array, or
a hash. This means that $foo and @foo are two different variables.
Perl - Variables
Lecture 3:
Datatypes and Variables
32
Creating Variables
Lecture 3:
Datatypes and Variables
33
A scalar is a single unit of data. That data might be an integer number, floating point, a
character, a string, a paragraph, or an entire web page. Simply saying it could be
anything, but only a single thing.
Scalar Variables
Lecture 3:
Datatypes and Variables
34
An array is a variable that stores an ordered list of scalar values. Array variables are preceded by an
"at" (@) sign. To refer to a single element of an array, you will use the dollar sign ($) with the variable
name followed by the index of the element in square brackets.
Array Variables
Lecture 3:
Datatypes and Variables
35
A hash is a set of key/value pairs. Hash variables are preceded by a percent
(%) sign. To refer to a single element of a hash, you will use the hash variable
name followed by the "key" associated with the value in curly brackets.
Hash Variables
Lecture 3:
Datatypes and Variables
36
The strict pragma checks for unsafe programming constructs. Strict forces a
programmer to declare all variables as package or lexically scoped variables.
Strict also forces specific syntax with sub, forcing the programmer to call each
subroutine explicitly. The programmer also needs to use quotes around all
strings, and to call each subroutine explicitly, which forces a distrust of bare
words.
The warnings pragma sends warnings when the Perl compiler detects a
possible typographical error and looks for potential problems. There are a
number of possible, but warnings mainly look for the most common syntax
mistakes and common scripting bugs.
Perl: Strict and Warnings
Lecture 3:
Datatypes and Variables
37
ī‚§ Variable names can start with a letter, a number, or an underscore, although
they normally begin with a letter and can then be composed of any
combination of letters, numbers, and the underscore character.
ī‚§ Variables can start with a number, but they must be entirely composed of
that number; for example, $123 is valid, but $1var is not.
ī‚§ Variable names that start with anything other than a letter, digit, or
underscore are generally reserved for special use by Perl
ī‚§ Variable names are case sensitive; $foo, $FOO, and $fOo are all separate
variables as far as Perl is concerned.
ī‚§ As an unwritten (and therefore unenforced) rule, names all in uppercase are
constants.
ī‚§ All scalar values start with $, including those accessed from an array of
hash, for example $array[0] or $hash{key}.
ī‚§ Namespaces are separate for each variable type—the variables $var, @var,
and %var are all different variables in their own right. Limited to 255
characters.
Basic Naming Rules in Perl
Lecture 3:
Datatypes and Variables
38
Thank You
Unit No: 1 Unit name: Perl Basics
Lecture 4:
Perl Operators
ī‚§ Input refers to getting information into your program
ī‚§ Output refers to getting information out of your program
I/O is how computer programs talk to the rest of the “world”
Perl provides access to the standard files: STDIN,STDOUT, STDERR
STDIN is accessed through the angle brackets (<>) operator. When placed in a
scalar context, the operator returns the next line; when place in an array
context, it returns the entire file, one line per item in the array.
Perl Basic Input Output
Lecture 4:
Perl Operators
41
One of these is for input to your program, and two are for output from your program
Input
– By default, Perl has a connection set up for taking information entered from the
keyboard.
This connection is referred to as
STDIN
Output
– 1) By default, Perl has a connection set up for writing data out to your terminal
(screen).
This connection is referred to as
STDOUT
2) By default, Perl has a connection set up for writing diagnostic messages,
(warnings, etc.) to your terminal
. This connection is referred to as
STDERR
You can change the default locations for STDIN,STDOUT and STDERR
Every Perl script starts with three connections to
the outside world
Lecture 4:
Perl Operators
42
print “Please enter your name: “;
$name = <STDIN>;
print “Hello $name. Glad to meet you.”;
Reading data from STDIN
Lecture 4:
Perl Operators
43
Chomp and n
Lecture 4:
Perl Operators
44
STDOUT – getting stuff to the screen
Lecture 4:
Perl Operators
45
Simple answer can be given using the expression 4 + 5 is equal to 9. Here 4
and 5 are called operands and + is called operator. Perl language supports
many operator types, but following is a list of important and most frequently
used operators −
ī‚§ Arithmetic Operators
ī‚§ Equality Operators
ī‚§ Logical Operators
ī‚§ Assignment Operators
ī‚§ Bitwise Operators
ī‚§ Logical Operators
ī‚§ Quote-like Operators
ī‚§ Miscellaneous Operators
Perl - Operators
Lecture 4:
Perl Operators
46
Assume variable $a holds 10 and variable $b holds 20 then −
Perl Arithmetic Operators
Lecture 4:
Perl Operators
47
These are also called relational
operators. Assume variable $a
holds 10 and variable $b holds
20 thenâ€Ļâ€Ļ
Perl Equality Operators
Lecture 4:
Perl Operators
48
Assume variable $a holds
"abc" and variable $b
holds "xyz" then, lets
check following string
equality operators:
Lecture 4:
Perl Operators
49
Perl Assignment Operators
Lecture 4:
Perl Operators
50
There are following logical operators supported by Perl language. Assume
variable $a holds true and variable $b holds false then −
Perl Logical Operators
Lecture 4:
Perl Operators
51
Thank You
Unit No: 1 Unit name: Perl Basics
Lecture 5:
Loops and Controls
Perl conditional statements helps in
the decision making, which require
that the programmer specifies one or
more conditions to be evaluated or
tested by the program, along with a
statement or statements to be
executed if the condition is
determined to be true, and optionally,
other statements to be executed if
the condition is determined to be
false.
Perl Conditional Statements
Lecture 5:
Loops and Controls
54
Perl IF Statement
Lecture 5:
Loops and Controls
55
Perl IF...ELSE statement
Lecture 5:
Loops and Controls
56
An if statement can be followed by an optional elsif...else statement, which is
very useful to test the various conditions using single if...elsif statement.
When using if , elsif , else statements there are few points to keep in mind.
ī‚§ An if can have zero or one else's and it must come after any elsif's.
ī‚§ An if can have zero to many elsif's and they must come before the else.
ī‚§ Once an elsif succeeds, none of the remaining elsif's or else's will be tested.
Perl IF...ELSIF statement
Lecture 5:
Loops and Controls
57
Lecture 5:
Loops and Controls
58
Lecture 5:
Loops and Controls
59
Switch
Lecture 5:
Loops and Controls
60
Lecture 5:
Loops and Controls
61
Lecture 5:
Loops and Controls
62
Perl - Loops
Lecture 5:
Loops and Controls
63
Perl while Loop
Lecture 5:
Loops and Controls
64
Lecture 5:
Loops and Controls
65
Until loop
Lecture 5:
Loops and Controls
66
Lecture 5:
Loops and Controls
67
Perl for Loop
Lecture 5:
Loops and Controls
68
Lecture 5:
Loops and Controls
69
Perl foreach Loop
Lecture 5:
Loops and Controls
70
Lecture 5:
Loops and Controls
71
Lecture 5:
Loops and Controls
72
Lecture 5:
Loops and Controls
73
Perl nested Loop
Lecture 5:
Loops and Controls
74
Lecture 5:
Loops and Controls
75
Loop control statements change the execution from its normal sequence. When
execution leaves a scope, all automatic objects that were created in that scope
are destroyed.
Loop Control Statements
Lecture 5:
Loops and Controls
76
The Perl next statement starts the next iteration of the loop. You can provide a
LABEL with next statement where LABEL is the label for a loop. A next
statement can be used inside a nested loop where it will be applicable to the
nearest loop if a LABEL is not specified.
next
Lecture 5:
Loops and Controls
77
Using LABEL in next
Lecture 5:
Loops and Controls
78
A continue BLOCK, is always executed just before the conditional is about to
be evaluated again. A continue statement can be used with while and foreach
loops. A continue statement can also be used alone along with a BLOCK of
code in which case it will be assumed as a flow control statement rather than a
function.
continue
Lecture 5:
Loops and Controls
79
Lecture 5:
Loops and Controls
80
Thank You
Unit No: 1 Unit name: Perl Basics
Lecture 6:
Printing Functions
The printf operator takes a format string followed by a list of things to print
Printing Formats: printf - string formatting:
Lecture 6:
Printing Functions
83
Perl printf - formatting integers
Lecture 6:
Printing Functions
84
Lecture 6:
Printing Functions
85
Lecture 6:
Printing Functions
86
Formatting floating-point numbers
Lecture 6:
Printing Functions
87
This function uses FORMAT to return a formatted string based on the values in
LIST. Essentially identical to printf, but the formatted string is returned instead
of being printed.
Perl sprintf Function
Lecture 6:
Printing Functions
88
Thank You
Unit No: 1 Unit name: Perl Basics
Lecture 7:
Perl Arrays
Arrays are a special type of variable that store list style data types. Each object
of the list is termed an element and elements can either be a string, a number,
or any type of scalar data including another variable.
Array Variables
Lecture 7:
Perl Arrays
91
Arrays are stored in array variables. An array variable starts with @ sign
ī‚§ Names can be upto 255 characters long and can contain numbers, letters
and underscores.
ī‚§ Names are case sensitive.
ī‚§ Array variables names can start with a number and then can only contain
numbers.
ī‚§ Scalar and array variable names do not conflict with each other. The variable
$x is different from @x.
Rules for naming array variables
Lecture 7:
Perl Arrays
92
Defining an array
Lecture 7:
Perl Arrays
93
Each element of the array can be indexed using a scalar version of the same
array. When an array is defined, PERL automatically numbers each element in
the array beginning with zero. This phenomenon is termed array indexing.
PERL - Array Indexing
Lecture 7:
Perl Arrays
94
Elements can also be indexed backwards using negative integers instead of
positive numbers.
Lecture 7:
Perl Arrays
95
Quotations can be a hassle, especially if the array you wish to build has more
than 5 elements. Use this neat little subroutine (quote word operator) to remove
the need for quotes around each element when you define an array.
PERL - The qw Subroutine
Lecture 7:
Perl Arrays
96
PERL offers a shortcut for sequential numbers and letters. Rather than typing
out each element when counting to 100 for example, we can do something like
this:
PERL - Sequential Number Arrays
Lecture 7:
Perl Arrays
97
FOR
Accessing array elements through the loop
Lecture 7:
Perl Arrays
98
FOREACH
Accessing array elements through the loop
Lecture 7:
Perl Arrays
99
Input variable values in an array from the user
Lecture 7:
Perl Arrays
100
ī‚§ scalar - find the length of the array
ī‚§ $# - gives the index no of last element of the array
ī‚§ defined - checks whether value of that index is defined or not
ī‚§ undef - undefines the value of particular index
ī‚§ delete - deletes an element from the array
ī‚§ pop - returns the last element of array and reduces its length by 1
ī‚§ push - adds incoming element in the end of the array
ī‚§ shift - returns the first element of array and reduces its length by 1
ī‚§ unshift - adds incoming element in the start of the array
ī‚§ splice - inserts or replaces an element at an arbitrary position in an array
ī‚§ split - transform a string into an array, splits string expr at occurrences of
pattern
ī‚§ join - transform an array into a string. Joins the list elements in single string
separated by pattern
ī‚§ reverse – reverses elements in the array
ī‚§ sort - sorts the elements of the array
Array Functions
Lecture 7:
Perl Arrays
101
syntax
Lecture 7:
Perl Arrays
102
Sorting an Array Numerically
Lecture 7:
Perl Arrays
103
Lecture 7:
Perl Arrays
104
Basically, the expression $a <=> $b (or $a cmp $b for strings) returns one of the
values 1, 0, -1 if $a is, respectively, larger, equal or lower than $b.
Lecture 7:
Perl Arrays
105
Create a new array with elements of another array
Lecture 7:
Perl Arrays
106
2D array
Lecture 7:
Perl Arrays
107
Taking input from the user
Lecture 7:
Perl Arrays
108
Thank You
Unit No: 1 Unit name: Perl Basics
Lecture 8:
String Functions
ī‚§ length()
ī‚§ reverse()
ī‚§ index()
ī‚§ rindex()
ī‚§ substr()
ī‚§ lc()
ī‚§ uc()
String functions
Lecture 8:
String Functions
111
Lecture 8:
String Functions
112
This function returns the position of the first occurrence of SUBSTR in STR,
starting at the beginning (starting at zero), or from POSITION if specified.
Perl index Function
Lecture 8:
String Functions
113
Lecture 8:
String Functions
114
Lecture 8:
String Functions
115
This function operates similar to index, except it returns the position of the last
occurrence of SUBSTR in STR. If POSITION is specified, returns the last
occurrence at or before that position.
Perl rindex Function
Lecture 8:
String Functions
116
Lecture 8:
String Functions
117
Lecture 8:
String Functions
118
This function returns a substring of EXPR, starting at OFFSET within the string. If
OFFSET is negative, starts that many characters from the end of the string. If LEN
is specified, returns that number of bytes, or all bytes up until end-of-string if not
specified. If LEN is negative, leaves that many characters off the end of the string.
If REPLACEMENT is specified, replaces the substring with the REPLACEMENT
string.
If you specify a substring that passes beyond the end of the string, it returns only
the valid element of the original string.
Perl substr Function
Lecture 8:
String Functions
119
Lecture 8:
String Functions
120
Lecture 8:
String Functions
121
Lecture 8:
String Functions
122
Thank You
Unit No: 1 Unit name: Perl Basics
Lecture 9:
File Handling
The basics of handling files are simple: you associate a filehandle with an
external entity (usually a file) and then use a variety of operators and functions
within Perl to read and update the data stored within the data stream
associated with the filehandle.
A filehandle is a named internal Perl structure that associates a physical file
with a name. All filehandles are capable of read/write access, so you can read
from and update any file or device associated with a filehandle. However, when
you associate a filehandle, you can specify the mode in which the filehandle is
opened.
Perl - File I/O
Lecture 9:
File Handling
125
Lecture 9:
File Handling
126
Lecture 9:
File Handling
127
Lecture 9:
File Handling
128
Lecture 9:
File Handling
129
Lecture 9:
File Handling
130
Lecture 9:
File Handling
131
Lecture 9:
File Handling
132
Lecture 9:
File Handling
133
Lecture 9:
File Handling
134
Thank You
Unit No: 2 Unit name: Essential Perl
Lecture 10:
Perl Subroutines
The String Operators (. and x)
Lecture 10:
Perl Subroutines
137
Perl has two different string operators-the concatenation (.) operator and the
repetition (x) operator. These operators make it easy to manipulate strings in
certain ways. Let's start with the concatenation operator. Strings can be
concatenated by the . operator.
For example:
$first_name = "David";
$last_name = "Marshall";
$full_name = $first_name . " " . $last_name;
we need the " " to insert a space between the strings.
Lecture 10:
Perl Subroutines
138
Strings can be repeated with tt x operator
For example:
$first_name = "David";
$david_cubed = $first_name x 3;
which gives "DavidDavidDavid".
Conversion between numbers and Strings
Lecture 10:
Perl Subroutines
139
Lecture 10:
Perl Subroutines
140
The chop() operator
Perl - Special Variables
Lecture 10:
Perl Subroutines
141
Lecture 10:
Perl Subroutines
142
Lecture 10:
Perl Subroutines
143
Lecture 10:
Perl Subroutines
144
A function is a block of code that has a name and it has a property that it is
reusable i.e. it can be executed from as many different points in a Program as
required.
Function groups a number of program statements into a unit and gives it a
name. This unit can be invoked from other parts of a program. A computer
program cannot handle all the tasks by it self. Instead its requests other
program like entities – called functions to get its tasks done. A function is a self
contained block of statements that perform a coherent task of same kind
The name of the function is unique in a Program and is Global. It means that a
function can be accessed from any location with in a Program. We pass
information to the function called arguments specified when the function is
called. And the function either returns some value to the point it was called from
or returns nothing.
We can divide a long program into small blocks which can perform a certain
task. A function is a self contained block of statements that perform a coherent
task of same kind.
Function
Lecture 10:
Perl Subroutines
145
Perl - Subroutines
Lecture 10:
Perl Subroutines
146
Define and Call a Subroutine
Lecture 10:
Perl Subroutines
147
Lecture 10:
Perl Subroutines
148
print "Enter the height, width and depthn";
chomp($x=<STDIN>);
chomp($y=<STDIN>);
chomp($z=<STDIN>);
volume($x,$y,$z);
print "the volume is: $voln";
sub volume {
my ($height, $width, $depth) = @_;
$vol = $height * $width * $depth;
print "the volume is: $voln";
}
Passing of variables
Lecture 10:
Perl Subroutines
149
Lecture 10:
Perl Subroutines
150
You can pass various arguments to a subroutine like you do in any other
programming language and they can be acessed inside the function using the
special array @_. Thus the first argument to the function is in $_[0], the second
is in $_[1], and so on.
Lecture 10:
Perl Subroutines
151
By default, all variables in Perl are global variables, which means they can be
accessed from anywhere in the program. But you can create private variables
called lexical variables at any time with the my operator.
The my operator confines a variable to a particular region of code in which it
can be used and accessed. Outside that region, this variable cannot be used or
accessed. This region is called its scope. A lexical scope is usually a block of
code with a set of braces around it, such as those defining the body of the
subroutine or those marking the code blocks of if, while, for, foreach, and eval
statements.
Private Variables in a Subroutine
Lecture 10:
Perl Subroutines
152
Types of subroutines
Lecture 10:
Perl Subroutines
153
Function
without
parameter
without
return value
Function
without
parameter
with return
value
Function
with
parameter
with return
value
Function
with
parameter
without
return value
Types
Function without parameter without return value
154
Function with parameter without return value
155
Function with parameter with return value
156
Function without parameter with return value
157
@one = ('a','b','c');
@two = (1,2,3);
print "@one n";
print "@two n";
mod1(@one);
mod2(@two);
print "@one n";
print "@two n";
#####################
sub mod1
{
my(@one) = @_;
push(@one, 'X');
print "@one n";
}
sub mod2
{
my(@two) = @_;
push(@two,'Z');
print "@two n";
}
Passing of arguments – Call by/ pass by value
Lecture 10:
Perl Subroutines
158
Passing of arrays together
159
@one = ('a','b','c');
@two = (1,2,3);
print "@one n";
print "@two n";
print "############ n";
modify(@one,@two);
print "@one n";
print "@two n";
#####################
sub modify
{
my($one,$two) = @_;
print "@$one n";
print "@$two n";
print "############n";
push(@$one, 'X');
shift(@$two);
print "@$one n";
print "@$two n";
print "############n";
print "$one n";
}
Passing of arguments – Call by/ pass by reference
Lecture 10:
Perl Subroutines
160
Thank You
Unit No: 2 Unit name: Essential Perl
Lecture 11:
Perl Regular Expressions
A regular expression is a string of characters that defines the pattern or
patterns you are viewing. Basically, a regular expression is a pattern describing
a certain amount of text. Their name comes from the mathematical theory on
which they are based. But we will not dig into that. You will usually find the
name abbreviated to "regex" or "regexp".
b[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,}b is a more complex pattern. It
describes a series of letters, digits, dots, underscores, percentage signs and
hyphens, followed by an at sign, followed by another series of letters, digits and
hyphens, finally followed by a single dot and two or more letters. In other
words: this pattern describes an email address. With the above regular
expression pattern, you can search through a text file to find email addresses,
or verify if a given string looks like an email address.
Regular Expressions in Perl
Lecture 11:
Perl Regular Expressions
163
There are three regular expression operators within Perl.
Lecture 11:
Perl Regular Expressions
164
The match operator, m//, is used to match a string or statement to a regular
expression.
The Match Operator
Lecture 11:
Perl Regular Expressions
165
Lecture 11:
Perl Regular Expressions
166
Note that the entire match expression that is the expression on the left of =~ or
!~ and the match operator, returns true (in a scalar context) if the expression
matches. 1 if matches the regex, or 0 if the match fails.
Lecture 11:
Perl Regular Expressions
167
Regular expression variables include $, which contains whatever the last
grouping match matched; $&, which contains the entire matched string; $`,
which contains everything before the matched string; and $', which contains
everything after the matched string.
Regular Expression Variables
Lecture 11:
Perl Regular Expressions
168
The Substitution Operator
Lecture 11:
Perl Regular Expressions
169
This is the transliteration operator; it replaces all occurrences of the characters
in SEARCHLIST with the characters in REPLACEMENTLIST.
The Translation Operator (Transliterate)
Lecture 11:
Perl Regular Expressions
170
Lecture 11:
Perl Regular Expressions
171
Lecture 11:
Perl Regular Expressions
172
Complex Regular Expressions
Lecture 11:
Perl Regular Expressions
173
Lecture 11:
Perl Regular Expressions
174
Lecture 11:
Perl Regular Expressions
175
Thank You

More Related Content

What's hot

Lesson 2 Understanding Linux File System
Lesson 2 Understanding Linux File SystemLesson 2 Understanding Linux File System
Lesson 2 Understanding Linux File SystemSadia Bashir
 
Process and Threads in Linux - PPT
Process and Threads in Linux - PPTProcess and Threads in Linux - PPT
Process and Threads in Linux - PPTQUONTRASOLUTIONS
 
Complete Guide for Linux shell programming
Complete Guide for Linux shell programmingComplete Guide for Linux shell programming
Complete Guide for Linux shell programmingsudhir singh yadav
 
Introduction to Shell script
Introduction to Shell scriptIntroduction to Shell script
Introduction to Shell scriptBhavesh Padharia
 
Linux standard file system
Linux standard file systemLinux standard file system
Linux standard file systemTaaanu01
 
Introduction to Makefile
Introduction to MakefileIntroduction to Makefile
Introduction to MakefileTusharadri Sarkar
 
Boot process
Boot processBoot process
Boot processSalman Memon
 
Process management
Process managementProcess management
Process managementBirju Tank
 
Chapter01Introducing Hardware
Chapter01Introducing HardwareChapter01Introducing Hardware
Chapter01Introducing HardwarePatty Ramsey
 
Event driven programming amazeballs
Event driven programming amazeballsEvent driven programming amazeballs
Event driven programming amazeballsMsWillcox
 
Unit 1 introduction to Operating System
Unit 1 introduction to Operating SystemUnit 1 introduction to Operating System
Unit 1 introduction to Operating Systemzahid7578
 
Linux kernel modules
Linux kernel modulesLinux kernel modules
Linux kernel modulesEddy Reyes
 
Memory Management in OS
Memory Management in OSMemory Management in OS
Memory Management in OSvampugani
 
Introduction to shell scripting
Introduction to shell scriptingIntroduction to shell scripting
Introduction to shell scriptingCorrado Santoro
 
Part 02 Linux Kernel Module Programming
Part 02 Linux Kernel Module ProgrammingPart 02 Linux Kernel Module Programming
Part 02 Linux Kernel Module ProgrammingTushar B Kute
 

What's hot (20)

Lesson 2 Understanding Linux File System
Lesson 2 Understanding Linux File SystemLesson 2 Understanding Linux File System
Lesson 2 Understanding Linux File System
 
Process and Threads in Linux - PPT
Process and Threads in Linux - PPTProcess and Threads in Linux - PPT
Process and Threads in Linux - PPT
 
A practical guide to buildroot
A practical guide to buildrootA practical guide to buildroot
A practical guide to buildroot
 
Linux systems - Linux Commands and Shell Scripting
Linux systems - Linux Commands and Shell ScriptingLinux systems - Linux Commands and Shell Scripting
Linux systems - Linux Commands and Shell Scripting
 
Complete Guide for Linux shell programming
Complete Guide for Linux shell programmingComplete Guide for Linux shell programming
Complete Guide for Linux shell programming
 
Introduction to Shell script
Introduction to Shell scriptIntroduction to Shell script
Introduction to Shell script
 
Linux standard file system
Linux standard file systemLinux standard file system
Linux standard file system
 
Introduction to Makefile
Introduction to MakefileIntroduction to Makefile
Introduction to Makefile
 
Boot process
Boot processBoot process
Boot process
 
Linux kernel
Linux kernelLinux kernel
Linux kernel
 
Process management
Process managementProcess management
Process management
 
Chapter01Introducing Hardware
Chapter01Introducing HardwareChapter01Introducing Hardware
Chapter01Introducing Hardware
 
Event driven programming amazeballs
Event driven programming amazeballsEvent driven programming amazeballs
Event driven programming amazeballs
 
Unit 1 introduction to Operating System
Unit 1 introduction to Operating SystemUnit 1 introduction to Operating System
Unit 1 introduction to Operating System
 
Linux kernel modules
Linux kernel modulesLinux kernel modules
Linux kernel modules
 
Memory Management in OS
Memory Management in OSMemory Management in OS
Memory Management in OS
 
Introduction to shell scripting
Introduction to shell scriptingIntroduction to shell scripting
Introduction to shell scripting
 
Part 02 Linux Kernel Module Programming
Part 02 Linux Kernel Module ProgrammingPart 02 Linux Kernel Module Programming
Part 02 Linux Kernel Module Programming
 
U-Boot - An universal bootloader
U-Boot - An universal bootloader U-Boot - An universal bootloader
U-Boot - An universal bootloader
 
Know the UNIX Commands
Know the UNIX CommandsKnow the UNIX Commands
Know the UNIX Commands
 

Similar to Perl Reference.ppt

perl lauange
perl lauangeperl lauange
perl lauangeNaga Dinesh
 
WEB PROGRAMMING UNIT V BY BHAVSINGH MALOTH
WEB PROGRAMMING UNIT V BY BHAVSINGH MALOTHWEB PROGRAMMING UNIT V BY BHAVSINGH MALOTH
WEB PROGRAMMING UNIT V BY BHAVSINGH MALOTHBhavsingh Maloth
 
Unit 1-introduction to perl
Unit 1-introduction to perlUnit 1-introduction to perl
Unit 1-introduction to perlsana mateen
 
Summer Training Project On Python Programming
Summer Training Project On Python ProgrammingSummer Training Project On Python Programming
Summer Training Project On Python ProgrammingKAUSHAL KUMAR JHA
 
Perl_Part6
Perl_Part6Perl_Part6
Perl_Part6Frank Booth
 
Shell-Scripting-1.pdf
Shell-Scripting-1.pdfShell-Scripting-1.pdf
Shell-Scripting-1.pdfaznabi
 
220 runtime environments
220 runtime environments220 runtime environments
220 runtime environmentsJ'tong Atong
 
Introduction to perl_ a scripting language
Introduction to perl_ a scripting languageIntroduction to perl_ a scripting language
Introduction to perl_ a scripting languageVamshi Santhapuri
 
OOP Comparative Study
OOP Comparative StudyOOP Comparative Study
OOP Comparative StudyDarren Tan
 
Perl_Part1
Perl_Part1Perl_Part1
Perl_Part1Frank Booth
 
presentation_intro_to_python
presentation_intro_to_pythonpresentation_intro_to_python
presentation_intro_to_pythongunanandJha2
 

Similar to Perl Reference.ppt (20)

Pearl
PearlPearl
Pearl
 
perl lauange
perl lauangeperl lauange
perl lauange
 
Perl
PerlPerl
Perl
 
WEB PROGRAMMING UNIT V BY BHAVSINGH MALOTH
WEB PROGRAMMING UNIT V BY BHAVSINGH MALOTHWEB PROGRAMMING UNIT V BY BHAVSINGH MALOTH
WEB PROGRAMMING UNIT V BY BHAVSINGH MALOTH
 
Unit 1-introduction to perl
Unit 1-introduction to perlUnit 1-introduction to perl
Unit 1-introduction to perl
 
Mit gnu scheme reference manual
Mit gnu scheme reference manualMit gnu scheme reference manual
Mit gnu scheme reference manual
 
Summer Training Project On Python Programming
Summer Training Project On Python ProgrammingSummer Training Project On Python Programming
Summer Training Project On Python Programming
 
Perl_Part6
Perl_Part6Perl_Part6
Perl_Part6
 
Shell-Scripting-1.pdf
Shell-Scripting-1.pdfShell-Scripting-1.pdf
Shell-Scripting-1.pdf
 
220 runtime environments
220 runtime environments220 runtime environments
220 runtime environments
 
Stay fresh
Stay freshStay fresh
Stay fresh
 
Introduction to perl_ a scripting language
Introduction to perl_ a scripting languageIntroduction to perl_ a scripting language
Introduction to perl_ a scripting language
 
OOP Comparative Study
OOP Comparative StudyOOP Comparative Study
OOP Comparative Study
 
Perl_Part1
Perl_Part1Perl_Part1
Perl_Part1
 
Perl Basics with Examples
Perl Basics with ExamplesPerl Basics with Examples
Perl Basics with Examples
 
python and perl
python and perlpython and perl
python and perl
 
Perl-Tutorial
Perl-TutorialPerl-Tutorial
Perl-Tutorial
 
Perl-Tutorial
Perl-TutorialPerl-Tutorial
Perl-Tutorial
 
Perl
PerlPerl
Perl
 
presentation_intro_to_python
presentation_intro_to_pythonpresentation_intro_to_python
presentation_intro_to_python
 

Recently uploaded

call girls in Kamla Market (DELHI) 🔝 >āŧ’9953330565🔝 genuine Escort Service 🔝✔ī¸âœ”ī¸
call girls in Kamla Market (DELHI) 🔝 >āŧ’9953330565🔝 genuine Escort Service 🔝✔ī¸âœ”ī¸call girls in Kamla Market (DELHI) 🔝 >āŧ’9953330565🔝 genuine Escort Service 🔝✔ī¸âœ”ī¸
call girls in Kamla Market (DELHI) 🔝 >āŧ’9953330565🔝 genuine Escort Service 🔝✔ī¸âœ”ī¸9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)Dr. Mazin Mohamed alkathiri
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 

Recently uploaded (20)

call girls in Kamla Market (DELHI) 🔝 >āŧ’9953330565🔝 genuine Escort Service 🔝✔ī¸âœ”ī¸
call girls in Kamla Market (DELHI) 🔝 >āŧ’9953330565🔝 genuine Escort Service 🔝✔ī¸âœ”ī¸call girls in Kamla Market (DELHI) 🔝 >āŧ’9953330565🔝 genuine Escort Service 🔝✔ī¸âœ”ī¸
call girls in Kamla Market (DELHI) 🔝 >āŧ’9953330565🔝 genuine Escort Service 🔝✔ī¸âœ”ī¸
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 

Perl Reference.ppt

  • 1. UNIT I – Perl Basics Lecture 1- Introduction to Perl Programming
  • 2. Introduction Perl is a family of high-level, general-purpose, interpreted, dynamic programming languages. The languages in this family include Perl 5 and Perl 6. Though Perl is not officially an acronym, there are various backronyms in use, the most well-known being "Practical Extraction and Reporting Language". Perl was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions. Lecture 1 – Topic Name 2
  • 3. 3
  • 5. The Perl languages borrow features from other programming languages including C, shell script (sh), AWK, and sed. They provide powerful text processing facilities without the arbitrary data-length limits of many contemporary Unix command line tools, facilitating easy manipulation of text files. Perl 5 gained widespread popularity in the late 1990s as a CGI scripting language, in part due to its unsurpassed regular expression and string parsing abilities Introduction 5
  • 6. A scripting or script language is a programming language that supports scripts, programs written for a special run-time environment that automate the execution of tasks that could alternatively be executed one-by-one by a human operator. Scripting languages are often interpreted (rather than compiled). Primitives are usually the elementary tasks or API calls, and the language allows them to be combined into more complex programs. Environments that can be automated through scripting include software applications, web pages within a web browser, the shells of operating systems (OS), embedded systems, as well as numerous games. A scripting language can be viewed as a domain-specific language for a particular environment; in the case of scripting an application, this is also known as an extension language. Scripting languages 6
  • 7. In an interpreted environment, the instructions are executed immediately after parsing. Both tasks are performed by the interpreter. Interpreted languages include the MS-Dos Batch language (the OS itself is the interpreter), shell scripts in Unix/Linux systems, Java, Perl and BASICA (a very old BASIC language). Advantages of interpreted languages include relative ease of programming (since once you type your instructions into a text file, the interpreter can run it) and no linker is required. Disadvantages include poor speed performance and that you do not generate an executable (and therefore distributable) program. The interpreter must be present on a system to run the program. Interpreted vs. Compiled Languages 7
  • 8. Compilers parse the instructions into machine code and store them in a separate file for later execution. Many modern compilers can compile (parse) and execute in memory, giving the 'appearance' of an interpreted language. However, the key difference is that parsing and execution occurs in two distinct steps. Examples include newer forms of BASIC (such as Visual Basic), C/C++, Delphi and many others. In a compiled environment, you may speak of several separate files: source code (the text instructions you actually enter), object code (the parsed source code) and the executable (the linked object code). There is definitely an increase in complexity in using compilers, but the key advantages are speed performance and that you can distribute stand-alone executables. Interpreted vs. Compiled Languages 8
  • 10. Interpreted vs. Compiled Languages 10
  • 11. Interpreted vs. Compiled Languages 11
  • 12. Interpreted vs. Compiled Languages 12
  • 13. ī‚§Perl takes the best features from other languages, such as C, awk, sed, sh, and BASIC, among others. ī‚§Perl is an interpreted language, which means that your code can be run as is, without a compilation stage that creates a non portable executable program ī‚§Perls database integration interface DBI supports third-party databases including Oracle, Sybase, Postgres, MySQL and others. ī‚§Perl works with HTML, XML, and other mark-up languages. ī‚§Perl works cross platform. ī‚§Perl supports both procedural and object-oriented programming. ī‚§Perl interfaces with external C/C++ libraries through XS or SWIG. ī‚§Perl is extensible. There are over 20,000 third party modules available from the Comprehensive Perl Archive Network (CPAN). ī‚§The Perl interpreter can be embedded into other systems. Perl Features 13
  • 14. ī‚§Perl used to be the most popular web programming language due to its text manipulation capabilities and rapid development cycle. ī‚§Perl is widely known as " the duct-tape of the Internet". ī‚§Perl can handle encrypted Web data, including e-commerce transactions. ī‚§Perl can be embedded into web servers to speed up processing by as much as 2000%. ī‚§Perl's mod_perl allows the Apache web server to embed a Perl interpreter. ī‚§Perl's DBI package makes web-database integration easy. Perl and the Web 14
  • 20. Lecture 2 (Separator Page for every section) Topic Name
  • 23. 23
  • 24. Single and Double Quotes in Perl 24
  • 25. 25
  • 27. Unit No: 1 Unit name: Perl Basics Lecture 3: Datatypes and Variables
  • 28. Perl is a loosely typed language and there is no need to specify a type for your data while using in your program. The Perl interpreter will choose the type based on the context of the data itself. Data Types in Perl Lecture 3: Datatypes and Variables 28
  • 32. ī‚§ Variables are the reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. ī‚§ Based on the data type of a variable, the interpreter allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals, or strings in these variables. ī‚§ Perl maintains every variable type in a separate namespace. So you can, without fear of conflict, use the same name for a scalar variable, an array, or a hash. This means that $foo and @foo are two different variables. Perl - Variables Lecture 3: Datatypes and Variables 32
  • 34. A scalar is a single unit of data. That data might be an integer number, floating point, a character, a string, a paragraph, or an entire web page. Simply saying it could be anything, but only a single thing. Scalar Variables Lecture 3: Datatypes and Variables 34
  • 35. An array is a variable that stores an ordered list of scalar values. Array variables are preceded by an "at" (@) sign. To refer to a single element of an array, you will use the dollar sign ($) with the variable name followed by the index of the element in square brackets. Array Variables Lecture 3: Datatypes and Variables 35
  • 36. A hash is a set of key/value pairs. Hash variables are preceded by a percent (%) sign. To refer to a single element of a hash, you will use the hash variable name followed by the "key" associated with the value in curly brackets. Hash Variables Lecture 3: Datatypes and Variables 36
  • 37. The strict pragma checks for unsafe programming constructs. Strict forces a programmer to declare all variables as package or lexically scoped variables. Strict also forces specific syntax with sub, forcing the programmer to call each subroutine explicitly. The programmer also needs to use quotes around all strings, and to call each subroutine explicitly, which forces a distrust of bare words. The warnings pragma sends warnings when the Perl compiler detects a possible typographical error and looks for potential problems. There are a number of possible, but warnings mainly look for the most common syntax mistakes and common scripting bugs. Perl: Strict and Warnings Lecture 3: Datatypes and Variables 37
  • 38. ī‚§ Variable names can start with a letter, a number, or an underscore, although they normally begin with a letter and can then be composed of any combination of letters, numbers, and the underscore character. ī‚§ Variables can start with a number, but they must be entirely composed of that number; for example, $123 is valid, but $1var is not. ī‚§ Variable names that start with anything other than a letter, digit, or underscore are generally reserved for special use by Perl ī‚§ Variable names are case sensitive; $foo, $FOO, and $fOo are all separate variables as far as Perl is concerned. ī‚§ As an unwritten (and therefore unenforced) rule, names all in uppercase are constants. ī‚§ All scalar values start with $, including those accessed from an array of hash, for example $array[0] or $hash{key}. ī‚§ Namespaces are separate for each variable type—the variables $var, @var, and %var are all different variables in their own right. Limited to 255 characters. Basic Naming Rules in Perl Lecture 3: Datatypes and Variables 38
  • 40. Unit No: 1 Unit name: Perl Basics Lecture 4: Perl Operators
  • 41. ī‚§ Input refers to getting information into your program ī‚§ Output refers to getting information out of your program I/O is how computer programs talk to the rest of the “world” Perl provides access to the standard files: STDIN,STDOUT, STDERR STDIN is accessed through the angle brackets (<>) operator. When placed in a scalar context, the operator returns the next line; when place in an array context, it returns the entire file, one line per item in the array. Perl Basic Input Output Lecture 4: Perl Operators 41
  • 42. One of these is for input to your program, and two are for output from your program Input – By default, Perl has a connection set up for taking information entered from the keyboard. This connection is referred to as STDIN Output – 1) By default, Perl has a connection set up for writing data out to your terminal (screen). This connection is referred to as STDOUT 2) By default, Perl has a connection set up for writing diagnostic messages, (warnings, etc.) to your terminal . This connection is referred to as STDERR You can change the default locations for STDIN,STDOUT and STDERR Every Perl script starts with three connections to the outside world Lecture 4: Perl Operators 42
  • 43. print “Please enter your name: “; $name = <STDIN>; print “Hello $name. Glad to meet you.”; Reading data from STDIN Lecture 4: Perl Operators 43
  • 44. Chomp and n Lecture 4: Perl Operators 44
  • 45. STDOUT – getting stuff to the screen Lecture 4: Perl Operators 45
  • 46. Simple answer can be given using the expression 4 + 5 is equal to 9. Here 4 and 5 are called operands and + is called operator. Perl language supports many operator types, but following is a list of important and most frequently used operators − ī‚§ Arithmetic Operators ī‚§ Equality Operators ī‚§ Logical Operators ī‚§ Assignment Operators ī‚§ Bitwise Operators ī‚§ Logical Operators ī‚§ Quote-like Operators ī‚§ Miscellaneous Operators Perl - Operators Lecture 4: Perl Operators 46
  • 47. Assume variable $a holds 10 and variable $b holds 20 then − Perl Arithmetic Operators Lecture 4: Perl Operators 47
  • 48. These are also called relational operators. Assume variable $a holds 10 and variable $b holds 20 thenâ€Ļâ€Ļ Perl Equality Operators Lecture 4: Perl Operators 48
  • 49. Assume variable $a holds "abc" and variable $b holds "xyz" then, lets check following string equality operators: Lecture 4: Perl Operators 49
  • 50. Perl Assignment Operators Lecture 4: Perl Operators 50
  • 51. There are following logical operators supported by Perl language. Assume variable $a holds true and variable $b holds false then − Perl Logical Operators Lecture 4: Perl Operators 51
  • 53. Unit No: 1 Unit name: Perl Basics Lecture 5: Loops and Controls
  • 54. Perl conditional statements helps in the decision making, which require that the programmer specifies one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false. Perl Conditional Statements Lecture 5: Loops and Controls 54
  • 55. Perl IF Statement Lecture 5: Loops and Controls 55
  • 56. Perl IF...ELSE statement Lecture 5: Loops and Controls 56
  • 57. An if statement can be followed by an optional elsif...else statement, which is very useful to test the various conditions using single if...elsif statement. When using if , elsif , else statements there are few points to keep in mind. ī‚§ An if can have zero or one else's and it must come after any elsif's. ī‚§ An if can have zero to many elsif's and they must come before the else. ī‚§ Once an elsif succeeds, none of the remaining elsif's or else's will be tested. Perl IF...ELSIF statement Lecture 5: Loops and Controls 57
  • 58. Lecture 5: Loops and Controls 58
  • 59. Lecture 5: Loops and Controls 59
  • 61. Lecture 5: Loops and Controls 61
  • 62. Lecture 5: Loops and Controls 62
  • 63. Perl - Loops Lecture 5: Loops and Controls 63
  • 64. Perl while Loop Lecture 5: Loops and Controls 64
  • 65. Lecture 5: Loops and Controls 65
  • 66. Until loop Lecture 5: Loops and Controls 66
  • 67. Lecture 5: Loops and Controls 67
  • 68. Perl for Loop Lecture 5: Loops and Controls 68
  • 69. Lecture 5: Loops and Controls 69
  • 70. Perl foreach Loop Lecture 5: Loops and Controls 70
  • 71. Lecture 5: Loops and Controls 71
  • 72. Lecture 5: Loops and Controls 72
  • 73. Lecture 5: Loops and Controls 73
  • 74. Perl nested Loop Lecture 5: Loops and Controls 74
  • 75. Lecture 5: Loops and Controls 75
  • 76. Loop control statements change the execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed. Loop Control Statements Lecture 5: Loops and Controls 76
  • 77. The Perl next statement starts the next iteration of the loop. You can provide a LABEL with next statement where LABEL is the label for a loop. A next statement can be used inside a nested loop where it will be applicable to the nearest loop if a LABEL is not specified. next Lecture 5: Loops and Controls 77
  • 78. Using LABEL in next Lecture 5: Loops and Controls 78
  • 79. A continue BLOCK, is always executed just before the conditional is about to be evaluated again. A continue statement can be used with while and foreach loops. A continue statement can also be used alone along with a BLOCK of code in which case it will be assumed as a flow control statement rather than a function. continue Lecture 5: Loops and Controls 79
  • 80. Lecture 5: Loops and Controls 80
  • 82. Unit No: 1 Unit name: Perl Basics Lecture 6: Printing Functions
  • 83. The printf operator takes a format string followed by a list of things to print Printing Formats: printf - string formatting: Lecture 6: Printing Functions 83
  • 84. Perl printf - formatting integers Lecture 6: Printing Functions 84
  • 87. Formatting floating-point numbers Lecture 6: Printing Functions 87
  • 88. This function uses FORMAT to return a formatted string based on the values in LIST. Essentially identical to printf, but the formatted string is returned instead of being printed. Perl sprintf Function Lecture 6: Printing Functions 88
  • 90. Unit No: 1 Unit name: Perl Basics Lecture 7: Perl Arrays
  • 91. Arrays are a special type of variable that store list style data types. Each object of the list is termed an element and elements can either be a string, a number, or any type of scalar data including another variable. Array Variables Lecture 7: Perl Arrays 91
  • 92. Arrays are stored in array variables. An array variable starts with @ sign ī‚§ Names can be upto 255 characters long and can contain numbers, letters and underscores. ī‚§ Names are case sensitive. ī‚§ Array variables names can start with a number and then can only contain numbers. ī‚§ Scalar and array variable names do not conflict with each other. The variable $x is different from @x. Rules for naming array variables Lecture 7: Perl Arrays 92
  • 93. Defining an array Lecture 7: Perl Arrays 93
  • 94. Each element of the array can be indexed using a scalar version of the same array. When an array is defined, PERL automatically numbers each element in the array beginning with zero. This phenomenon is termed array indexing. PERL - Array Indexing Lecture 7: Perl Arrays 94
  • 95. Elements can also be indexed backwards using negative integers instead of positive numbers. Lecture 7: Perl Arrays 95
  • 96. Quotations can be a hassle, especially if the array you wish to build has more than 5 elements. Use this neat little subroutine (quote word operator) to remove the need for quotes around each element when you define an array. PERL - The qw Subroutine Lecture 7: Perl Arrays 96
  • 97. PERL offers a shortcut for sequential numbers and letters. Rather than typing out each element when counting to 100 for example, we can do something like this: PERL - Sequential Number Arrays Lecture 7: Perl Arrays 97
  • 98. FOR Accessing array elements through the loop Lecture 7: Perl Arrays 98
  • 99. FOREACH Accessing array elements through the loop Lecture 7: Perl Arrays 99
  • 100. Input variable values in an array from the user Lecture 7: Perl Arrays 100
  • 101. ī‚§ scalar - find the length of the array ī‚§ $# - gives the index no of last element of the array ī‚§ defined - checks whether value of that index is defined or not ī‚§ undef - undefines the value of particular index ī‚§ delete - deletes an element from the array ī‚§ pop - returns the last element of array and reduces its length by 1 ī‚§ push - adds incoming element in the end of the array ī‚§ shift - returns the first element of array and reduces its length by 1 ī‚§ unshift - adds incoming element in the start of the array ī‚§ splice - inserts or replaces an element at an arbitrary position in an array ī‚§ split - transform a string into an array, splits string expr at occurrences of pattern ī‚§ join - transform an array into a string. Joins the list elements in single string separated by pattern ī‚§ reverse – reverses elements in the array ī‚§ sort - sorts the elements of the array Array Functions Lecture 7: Perl Arrays 101
  • 103. Sorting an Array Numerically Lecture 7: Perl Arrays 103
  • 105. Basically, the expression $a <=> $b (or $a cmp $b for strings) returns one of the values 1, 0, -1 if $a is, respectively, larger, equal or lower than $b. Lecture 7: Perl Arrays 105
  • 106. Create a new array with elements of another array Lecture 7: Perl Arrays 106
  • 108. Taking input from the user Lecture 7: Perl Arrays 108
  • 110. Unit No: 1 Unit name: Perl Basics Lecture 8: String Functions
  • 111. ī‚§ length() ī‚§ reverse() ī‚§ index() ī‚§ rindex() ī‚§ substr() ī‚§ lc() ī‚§ uc() String functions Lecture 8: String Functions 111
  • 113. This function returns the position of the first occurrence of SUBSTR in STR, starting at the beginning (starting at zero), or from POSITION if specified. Perl index Function Lecture 8: String Functions 113
  • 116. This function operates similar to index, except it returns the position of the last occurrence of SUBSTR in STR. If POSITION is specified, returns the last occurrence at or before that position. Perl rindex Function Lecture 8: String Functions 116
  • 119. This function returns a substring of EXPR, starting at OFFSET within the string. If OFFSET is negative, starts that many characters from the end of the string. If LEN is specified, returns that number of bytes, or all bytes up until end-of-string if not specified. If LEN is negative, leaves that many characters off the end of the string. If REPLACEMENT is specified, replaces the substring with the REPLACEMENT string. If you specify a substring that passes beyond the end of the string, it returns only the valid element of the original string. Perl substr Function Lecture 8: String Functions 119
  • 124. Unit No: 1 Unit name: Perl Basics Lecture 9: File Handling
  • 125. The basics of handling files are simple: you associate a filehandle with an external entity (usually a file) and then use a variety of operators and functions within Perl to read and update the data stored within the data stream associated with the filehandle. A filehandle is a named internal Perl structure that associates a physical file with a name. All filehandles are capable of read/write access, so you can read from and update any file or device associated with a filehandle. However, when you associate a filehandle, you can specify the mode in which the filehandle is opened. Perl - File I/O Lecture 9: File Handling 125
  • 136. Unit No: 2 Unit name: Essential Perl Lecture 10: Perl Subroutines
  • 137. The String Operators (. and x) Lecture 10: Perl Subroutines 137 Perl has two different string operators-the concatenation (.) operator and the repetition (x) operator. These operators make it easy to manipulate strings in certain ways. Let's start with the concatenation operator. Strings can be concatenated by the . operator. For example: $first_name = "David"; $last_name = "Marshall"; $full_name = $first_name . " " . $last_name; we need the " " to insert a space between the strings.
  • 138. Lecture 10: Perl Subroutines 138 Strings can be repeated with tt x operator For example: $first_name = "David"; $david_cubed = $first_name x 3; which gives "DavidDavidDavid".
  • 139. Conversion between numbers and Strings Lecture 10: Perl Subroutines 139
  • 141. Perl - Special Variables Lecture 10: Perl Subroutines 141
  • 145. A function is a block of code that has a name and it has a property that it is reusable i.e. it can be executed from as many different points in a Program as required. Function groups a number of program statements into a unit and gives it a name. This unit can be invoked from other parts of a program. A computer program cannot handle all the tasks by it self. Instead its requests other program like entities – called functions to get its tasks done. A function is a self contained block of statements that perform a coherent task of same kind The name of the function is unique in a Program and is Global. It means that a function can be accessed from any location with in a Program. We pass information to the function called arguments specified when the function is called. And the function either returns some value to the point it was called from or returns nothing. We can divide a long program into small blocks which can perform a certain task. A function is a self contained block of statements that perform a coherent task of same kind. Function Lecture 10: Perl Subroutines 145
  • 146. Perl - Subroutines Lecture 10: Perl Subroutines 146
  • 147. Define and Call a Subroutine Lecture 10: Perl Subroutines 147
  • 149. print "Enter the height, width and depthn"; chomp($x=<STDIN>); chomp($y=<STDIN>); chomp($z=<STDIN>); volume($x,$y,$z); print "the volume is: $voln"; sub volume { my ($height, $width, $depth) = @_; $vol = $height * $width * $depth; print "the volume is: $voln"; } Passing of variables Lecture 10: Perl Subroutines 149
  • 151. You can pass various arguments to a subroutine like you do in any other programming language and they can be acessed inside the function using the special array @_. Thus the first argument to the function is in $_[0], the second is in $_[1], and so on. Lecture 10: Perl Subroutines 151
  • 152. By default, all variables in Perl are global variables, which means they can be accessed from anywhere in the program. But you can create private variables called lexical variables at any time with the my operator. The my operator confines a variable to a particular region of code in which it can be used and accessed. Outside that region, this variable cannot be used or accessed. This region is called its scope. A lexical scope is usually a block of code with a set of braces around it, such as those defining the body of the subroutine or those marking the code blocks of if, while, for, foreach, and eval statements. Private Variables in a Subroutine Lecture 10: Perl Subroutines 152
  • 153. Types of subroutines Lecture 10: Perl Subroutines 153 Function without parameter without return value Function without parameter with return value Function with parameter with return value Function with parameter without return value Types
  • 154. Function without parameter without return value 154
  • 155. Function with parameter without return value 155
  • 156. Function with parameter with return value 156
  • 157. Function without parameter with return value 157
  • 158. @one = ('a','b','c'); @two = (1,2,3); print "@one n"; print "@two n"; mod1(@one); mod2(@two); print "@one n"; print "@two n"; ##################### sub mod1 { my(@one) = @_; push(@one, 'X'); print "@one n"; } sub mod2 { my(@two) = @_; push(@two,'Z'); print "@two n"; } Passing of arguments – Call by/ pass by value Lecture 10: Perl Subroutines 158
  • 159. Passing of arrays together 159
  • 160. @one = ('a','b','c'); @two = (1,2,3); print "@one n"; print "@two n"; print "############ n"; modify(@one,@two); print "@one n"; print "@two n"; ##################### sub modify { my($one,$two) = @_; print "@$one n"; print "@$two n"; print "############n"; push(@$one, 'X'); shift(@$two); print "@$one n"; print "@$two n"; print "############n"; print "$one n"; } Passing of arguments – Call by/ pass by reference Lecture 10: Perl Subroutines 160
  • 162. Unit No: 2 Unit name: Essential Perl Lecture 11: Perl Regular Expressions
  • 163. A regular expression is a string of characters that defines the pattern or patterns you are viewing. Basically, a regular expression is a pattern describing a certain amount of text. Their name comes from the mathematical theory on which they are based. But we will not dig into that. You will usually find the name abbreviated to "regex" or "regexp". b[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,}b is a more complex pattern. It describes a series of letters, digits, dots, underscores, percentage signs and hyphens, followed by an at sign, followed by another series of letters, digits and hyphens, finally followed by a single dot and two or more letters. In other words: this pattern describes an email address. With the above regular expression pattern, you can search through a text file to find email addresses, or verify if a given string looks like an email address. Regular Expressions in Perl Lecture 11: Perl Regular Expressions 163
  • 164. There are three regular expression operators within Perl. Lecture 11: Perl Regular Expressions 164
  • 165. The match operator, m//, is used to match a string or statement to a regular expression. The Match Operator Lecture 11: Perl Regular Expressions 165
  • 166. Lecture 11: Perl Regular Expressions 166
  • 167. Note that the entire match expression that is the expression on the left of =~ or !~ and the match operator, returns true (in a scalar context) if the expression matches. 1 if matches the regex, or 0 if the match fails. Lecture 11: Perl Regular Expressions 167
  • 168. Regular expression variables include $, which contains whatever the last grouping match matched; $&, which contains the entire matched string; $`, which contains everything before the matched string; and $', which contains everything after the matched string. Regular Expression Variables Lecture 11: Perl Regular Expressions 168
  • 169. The Substitution Operator Lecture 11: Perl Regular Expressions 169
  • 170. This is the transliteration operator; it replaces all occurrences of the characters in SEARCHLIST with the characters in REPLACEMENTLIST. The Translation Operator (Transliterate) Lecture 11: Perl Regular Expressions 170
  • 171. Lecture 11: Perl Regular Expressions 171
  • 172. Lecture 11: Perl Regular Expressions 172
  • 173. Complex Regular Expressions Lecture 11: Perl Regular Expressions 173
  • 174. Lecture 11: Perl Regular Expressions 174
  • 175. Lecture 11: Perl Regular Expressions 175