SlideShare uma empresa Scribd logo
1 de 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

Mais conteúdo relacionado

Mais procurados

Sender Policy Framework​
Sender Policy Framework​Sender Policy Framework​
Sender Policy Framework​ScottMcKeown10
 
Device Tree for Dummies (ELC 2014)
Device Tree for Dummies (ELC 2014)Device Tree for Dummies (ELC 2014)
Device Tree for Dummies (ELC 2014)Thomas Petazzoni
 
Advanced Perl Techniques
Advanced Perl TechniquesAdvanced Perl Techniques
Advanced Perl TechniquesDave Cross
 
Assembly Language Lecture 4
Assembly Language Lecture 4Assembly Language Lecture 4
Assembly Language Lecture 4Motaz Saad
 
Shell Scripting in Linux
Shell Scripting in LinuxShell Scripting in Linux
Shell Scripting in LinuxAnu Chaudhry
 
User Administration in Linux
User Administration in LinuxUser Administration in Linux
User Administration in LinuxSAMUEL OJO
 
[PDF] Information Security Management Handbook, 6th Edition
[PDF] Information Security Management Handbook, 6th Edition[PDF] Information Security Management Handbook, 6th Edition
[PDF] Information Security Management Handbook, 6th Editiontubw3r343
 
Transport Layer Security
Transport Layer SecurityTransport Layer Security
Transport Layer SecurityChhatra Thapa
 
Double data rate (ddr)
Double data rate (ddr)Double data rate (ddr)
Double data rate (ddr)Anderson Huang
 
Típos de grámatica y más, exposición de compiladores e intérpretes
Típos de grámatica y más, exposición de compiladores e intérpretesTípos de grámatica y más, exposición de compiladores e intérpretes
Típos de grámatica y más, exposición de compiladores e intérpretesElmer André Boulangger Alberca
 
Linux fundamentals
Linux fundamentalsLinux fundamentals
Linux fundamentalsRaghu nath
 
Browsing Linux Kernel Source
Browsing Linux Kernel SourceBrowsing Linux Kernel Source
Browsing Linux Kernel SourceMotaz Saad
 

Mais procurados (20)

Introduction to shell
Introduction to shellIntroduction to shell
Introduction to shell
 
Sender Policy Framework​
Sender Policy Framework​Sender Policy Framework​
Sender Policy Framework​
 
Hashing
HashingHashing
Hashing
 
Device Tree for Dummies (ELC 2014)
Device Tree for Dummies (ELC 2014)Device Tree for Dummies (ELC 2014)
Device Tree for Dummies (ELC 2014)
 
Advanced Perl Techniques
Advanced Perl TechniquesAdvanced Perl Techniques
Advanced Perl Techniques
 
Assembly Language Lecture 4
Assembly Language Lecture 4Assembly Language Lecture 4
Assembly Language Lecture 4
 
Shell Scripting in Linux
Shell Scripting in LinuxShell Scripting in Linux
Shell Scripting in Linux
 
Linux basics
Linux basicsLinux basics
Linux basics
 
assembler-ppt.pdf
assembler-ppt.pdfassembler-ppt.pdf
assembler-ppt.pdf
 
User Administration in Linux
User Administration in LinuxUser Administration in Linux
User Administration in Linux
 
Sas cheat
Sas cheatSas cheat
Sas cheat
 
[PDF] Information Security Management Handbook, 6th Edition
[PDF] Information Security Management Handbook, 6th Edition[PDF] Information Security Management Handbook, 6th Edition
[PDF] Information Security Management Handbook, 6th Edition
 
Transport Layer Security
Transport Layer SecurityTransport Layer Security
Transport Layer Security
 
Double data rate (ddr)
Double data rate (ddr)Double data rate (ddr)
Double data rate (ddr)
 
Típos de grámatica y más, exposición de compiladores e intérpretes
Típos de grámatica y más, exposición de compiladores e intérpretesTípos de grámatica y más, exposición de compiladores e intérpretes
Típos de grámatica y más, exposición de compiladores e intérpretes
 
Arrays in SAS
Arrays in SASArrays in SAS
Arrays in SAS
 
DAS RAID NAS SAN
DAS RAID NAS SANDAS RAID NAS SAN
DAS RAID NAS SAN
 
Linux fundamentals
Linux fundamentalsLinux fundamentals
Linux fundamentals
 
Wishbone
WishboneWishbone
Wishbone
 
Browsing Linux Kernel Source
Browsing Linux Kernel SourceBrowsing Linux Kernel Source
Browsing Linux Kernel Source
 

Semelhante a Perl Reference.ppt

Semelhante a Perl Reference.ppt (20)

perl lauange
perl lauangeperl lauange
perl lauange
 
Pearl
PearlPearl
Pearl
 
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
 

Último

Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...RKavithamani
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 

Último (20)

Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 

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