0% found this document useful (0 votes)
25 views

Unit-I Part-II - Introduction To PERL

Uploaded by

anuradha.reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Unit-I Part-II - Introduction To PERL

Uploaded by

anuradha.reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 98

Introduction to PERL

UNIT-I PART-II
Introduction to Scripting and PERL

Introduction to PERL
 Names and Values
 Variables
 Scalar Expressions
 Control Structures
 Collection of Data- Working with lists/arrays and hashes
 Strings Pattern and Regular Expressions
 Subroutines
Introducing PERL

 PERL originated in the late 1980’s and was developed by Larry


Wall who was a lead for project developing secure wide-area
networks.
 PERL acronym is “Practical Extraction and Report
Language”
 PERL initially used as a data reduction language which would
navigate the file system, scan large amounts of text, and produce
easily formatted reports using powerful pattern matching and text
manipulation capabilities.
 Later it gained the facility to manipulate file ,themselves as well
as their contents ,and to create and control processes.
Introducing PERL

 PERL also became a networking language , providing system-


independent abstractions of files, processes and sockets etc.
 PERL borrowed the best features from C language ,the UNIX
utilities sed and awk.
 PERL philosophy is “ Making easy things easy while making
difficult things possible”.
PERL Features

Mission critical
 Used for mission critical projects in the public and private sectors.
Object-oriented, procedural and functional
 Supports object-oriented, procedural and functional programming.
Easily extendible
 There are over 25,000 open source modules available from the
Comprehensive Perl Archive Network (CPAN).
Text manipulation
 Perl includes powerful tools for processing text that make it ideal
for working with HTML, XML, and all other mark-up and natural
languages.
PERL Features

Unicode support
 Supports Unicode version 6 (from Perl 5.14).
Database integration
 Perl's database integration interface (DBI) supports third-party databases
including Oracle, Sybase, Postgres, MySQL and many others.
C/C++ library interface
 Perl interfaces with external C/C++ libraries through XS or SWIG(Simplified
Wrapper Interface Generator).
Embeddable
 The Perl interpreter can be embedded into other systems such as web servers
and database servers.
Open Source
 Perl is Open Source software, licensed under its Artistic License, or the GNU
General Public License (GPL).
PERL – Installation & Running

Available at the official website of Perl


 Strawberry Perl
 ActiveState Perl
PADRE - Perl Application Development and
Refactoring Environment. – An IDE for Perl
PADRE = Perl + an IDE (bundled)
Save the file with a .pl file extension. Eg: sample.pl
To run from the command prompt, Eg: perl sample.pl
PERL Syntax Overview

 Every simple statement must end with ;


 Case sensitive
 # - used for comments
 For multi-line comments, use # on each line
 White spaces are just ignored by the interpreter
 Both single quotes and double quotes can be used to represent
string literals.
 Single quotes print the strings AS IT IS without any change
 Double quotes supports substitution of variables
PERL Syntax Overview

 use strict;
It tell the interpreter to terminate the program if an error occurs
Eg: Defining a variable is MANDATORY using keyword ‘my’ if
you write ‘use strict;’ in a program.
Eg: my $marks=56;
 use warnings;
to tell the interpreter to display warning messages
 use 5.10.0;
to tell the interpreter to make use of version 5.10.0 features
Eg: say() function
 use q for single quotes and qq for double quotes
Names and Values in Perl

 Perl manipulates variables which have a name (or identifier) and a


value: a value is to (or stored in) assigned to a variable by an
assignment statement of the form
$name = value;
 Variable names resemble nouns in English (command names are verbs)
and, like English, Perl distinguishes between singular and plural nouns
(names).
 A singular name is associated with a variable that holds a single item
of data (a scalar value): a plural name is associated with a variable that
holds a collection of data items ( an array or hash).
 A notable characteristic of Perl is that variable names starts with a
special character that denotes the kind of thing that the name stands for
-scalar data ($), array(@), hash(%),subroutine (&) etc,
Names and Values in Perl

 The remainder of the name follows the more-or-Iess standard


convention: valid characters are letters, digits and underscores
and , the first character after the special character must be a letter
or an underscore.
 The syntax also allows a name that consists of a single non alpha
numeric character after the initial character, e.g. $$, $?; such
names are usually reserved for the Perl system.
Names and Values in Perl

Scalar data :
Strings and numbers
 In common with many scripting languages , Perl recognizes just two kinds
of scalar data: strings and numbers.
 There is no distinction between integers and real numbers as different types -
a number is a number. Internally, numbers are stored as integers if possible,
and otherwise as double length floating point numbers in the native format.
 strings are stored as sequences of bytes of unlimited length.
 Perl is a dynamically typed language the system keeps track of whether a
variable contains a numeric value or a string value, and the user doesn't have
to worry about the difference between strings and numbers since
conversions between the two kinds of data are done automatically as
required by the context in which they are used
Names and Values in Perl

 Thus if a string value occurs in an arithmetic context, e.g. as an


operand for an arithmetic operator, Perl will convert it to a
number by fair means or foul; if a numerical value occurs in a
string context, e.g. as an operand for a string operator, Perl will
convert it to a string.
Boolean values
 All programming languages need some way of representing truth
values (Boolean values), e.g. in control constructs for conditional
execution and repetition of blocks of code, and Perl is no
exception. Since scalar values are either numbers or strings, some
convention is needed for representing Boolean values, and Perl
adopts the simple rule that numeric zero, "0" and the empty
string ("") mean false, and anything else means true
Names and Values in Perl

Numeric constants
 Numeric constants (number literals) can be written in a variety of ways,
including scientific notation, octal and hexadecimal.
 Although Perl tries to emulate natural human communication, the common
practice of using commas or spaces to break up a large constant into meaningful
digit groups cannot be used, since the comma has a syntactic significance in
Perl.
 Instead, underscores can be included in a number literal to improve legibility.

Examples:
123,122.45,122.45e-5,122.45E-5
4929712198024,4929_712 198_024
0377 (octal)
Ox3 f f f (hex)
Names and Values in Perl

String Constants
 String constants (string literals) can be enclosed in single or double
quotes. The string is terminated by the first next occurrence of the quote
(single or double) which started it, so a single-quoted string can include
double quotes and vice versa.
 The q (quote) and qq (double you to use any character as a quoting
character. Thus
q/any string/ or q(any string) are the same as 'any string‘
and
qq/any string/ or qq(any string)are the same as "any string!!
 The character following the q or qq operator is the opening quote
character, and the next occurrence of that character is treated as closing
the quote character.
Variables and Assignment

Assignment:
 Perl uses ‘=' as the assignment operator. It is important to note that
an assignment statement returns a value, the value This statements
like
$b= 4 + ($a= 3) ; which the value 3 to $a and the value 7 to $b.
 A useful device often used in assignments is to interpolate the value
of a scalar variable into a double quoted after the assignments
$a ="Burger" ;
$b= "Beef $a ";
$c ="Turkey $a “;
 The value of $b is "Beef Burger" and the value of $c is "Turkey
Burger", in both cases with a space in the middle.
Variables and Assignment

<STDIN> - a special value :


 The ‘variable' <STDIN>: when it appears in a context where a scalar value is
required, it evaluates to a string containing the next line from standard input,
including the terminating newline. (The standard input is the keyboard )If there
is no input queued , Perl will wait until a line a line is typed and the return key
End-of-file. Causes <STDIN> to return undef, which evaluates to “ “ since
<STDIN> always occurs in a context where a string is expected.
 The empty string is treated as false in a Boolean context, hence the common
idiom
while <STDIN> { ….} to process all lines until end-of-file is reached. (A
cautious programmer would write
while (defined <STDIN>) {…}
 to make sure the loop is not entered if some unusual condition means that
STDIN is not available.)
Variables and Assignment

 If <STDIN> appears on the right-hand side of an assignment to a


scalar variable ,the string containing the input line is assigned to
the variable named on the left. If it appears in any other scalar
context the string is assigned to the anonymous variable: this can
be accessed the name $_: many operations use it as a default,
Scalar Expressions

 Scalar data items (whether literals or values of variables) are combined


into expressions operators.
 Perl has a lot of operators, which are ranked in 22 levels: these are
carefully chosen so that the 'obvious' meaning is what you get, but the
old advice still applies: if in doubt, use brackets to force the order of
evaluation.
Arithmetic Expressions
 The principle of 'no surprises' Perl provides the usual arithmetic
operators , including auto-increment and auto-decrement operators after
the manner of C: note that in
$c = 17; $d = ++$c; the sequence is increment then whereas in
$c = 17; $d= $c++; the sequence is assign then increment
$a+=3; equivalent $a=$a+3;
Scalar Expressions

String operators:
 Perl provides very basic operators on strings: most string
processing is done using built-in functions and regular
expressions.
 Perl uses period(.) as concatenation operator .
 The other string operator is x, which is used to replicate strings,
e.g. $a= "Hello" x 3; sets $a to "He110He110He110".
 The capability of combining an operator with assignment is
extended to string operations.
 e.g $foo.=“ “ it appends space to foo.
Scalar Expressions

Auto-increment: If a variable has only ever been used in


a string context, the auto increment operator (but not
auto-decrement) can be applied to it. If the value consists
of a sequence of letters, or a sequence of letters followed
by a sequence of digits, the auto-increment takes place in
string mode starting with the rightmost character, with
along the string.
For example, the sequence
$a = 'a0'; $b = 'Az9';
print ++$a, ‘ ‘, ++$b; "\n"; prints al BaO.
Scalar Expressions

 Unary minus. This has an unusual effect on non-numeric


values. Unary minus applied to a string which starts with a or
minus character returns the same string but starting with the
opposite sign . Unary minus applied to an identifier returns a
string consisting of a minus prefixed to the characters of the
identifier.
 Thus if we have a variable named $config with value
" foo", then -$config evaluates to the string " - f 00" .
Scalar Expressions

Comparison operators:
 The value of a comparison is returned as numeric 1 if true and an
empty string (“ “) if false, in accordance with the convention described
earlier.
 Two families of comparison operators are provided one for numbers (=
< > etc.) and one for strings (eq lt gt etc.): the operator used determines
the context, and Perl converts the as required to match the operator.
 This duality is necessary because a between a comparison between
strings made up entirely of numerical digits should apply the usual
rules for sorting strings using ASCII as a collating sequence, and this
may not give the same result as a numerical comparison.
E.g.(‘5’<’10’) returns true but (‘5’ lt ’10’) returns as false since 10
comes before 5 in the canonical sort order for ASCII strings
Scalar Expressions

 The comparison operator <=> for numbers, cmp for strings),


performs a three-way test, -1 for less-than, 0 for equal and + 1 for
greater-than. the comparison operators are non-associative.
Logical operators:
 The logical operators allow us to combine conditions using the
usual logical operations 'not' (!, 'and' and) and 'or' (||, or). Perl
implements the 'and' and 'or‘ || operators in 'shortcut' mode, i.e.
evaluation stops as soon as the final result is certain, using the
rules false && b = false, and true || b= true.
Scalar Expressions

Bitwise operators:
 The unary tilde (-) applied to a numerical argument performs bitwise
negation on its operand, the one's complement.
 If applied to a string operand it complements all the bits in the string -
an effective way of inverting a lot of bits.
 The bitwise operators - & (and), | (or) and ^ (exclusive or) have a rather
complicated definition.
 If either operand is a number or a variable that has previously been
used as a number, both operands are converted to integers if need be,
and the bitwise operation takes between the integers. If both operands
are strings, and if variables have never been used as numbers, Perl
performs the bitwise operation between corresponding bits in the two
strings padding the shorter string with zeros as required.
Scalar Expressions

Conditional expressions
 A conditional expression is one whose value is chosen from two
alternatives at run-time depending on the outcome of a test. The
syntax is borrowed from C:
test ? true_exp:false_exp
 The first expression is evaluated as a Boolean value: if it returns
true the whole expression is replaced by true_exp, otherwise it is
replaced by false_exp,
e.g. $a=($a>$b) ?0 :$a
Control Structures

Blocks
 The concept of a block is very important in Perl. A block is just a
sequence of one or more statements enclosed in braces(Curly brackets)
e.g.
{$positive= 1;
$negative= -1}
 The last statement in the block is terminated by the closing brace.
 The control structures in Perl use conditions to control the evaluation of
one or more blocks . In fact that the body of subroutine is also a block.
 Blocks can in fact appear almost anywhere that statement can appear:
such a block is sometimes called a bare block, and often in the context
of clever (or dirty)trick.
Control Structures

Conditions
 A condition is just a Perl expression which is evaluated in a Boolean
context: if it evaluates to zero or the empty string the condition is
treated as false otherwise it is treated as true in accord with the rules
already given.
 Conditions usually make use of the relational operators, and several
simple conditions can be combined into a complex condition using
the logical operators described in previous slides, e.g.
$total > 50
$total > 50 and $total <100
A condition can be negated using the ! operator, e.g.
!($total >50 and $total < 100)
Control Structures

Conditional execution
 If-then-else statements :
 If-statement e.g.
if ($total > 0){
print "$total\n"}
 If-else-statement e.g.
if ($total >0) {
print "$total\n“ }
else {
print "bad total!\n"}
Mandatory is block enclosed with brackets and condition enclosed in
braces
Control Structures

 If-else construct extends to multiple selection e.g.


if ($total 70){
$grade ="A”;
}elsif ($total > 50){
$grade= "B“;
}elsif ($total > 40){
$grade= "C“;
} else {
$grade =“F";
$total =0
}
Note: PERL uses a statement qualifier for ‘if ‘can be replaced with ‘unless’
with same expected effect
Control Structures

A common idiom is to use a conditional expression in


place of an if -then-else construct.
Thus
if ($a < 0)
{$b =0}
else {$b =1};
can be written
$b = ($a < 0) ? 0 : 1;
Control Structures

Statement Qualifiers
 Finally, PERL adds a bit more as we have seen in the examples
earlier ,a single statement (but not a block) can be followed by a
conditional modifier, as in the English 'I'll come if it's fine'. For example
print "OK\n" if $volts 1. 5;
print "Weak\n“ if $volts >= 1.2 and
$volts < 1. 5;
print “Replace\n" if $volts < 1. 2 ;
 This is readable and self-documenting: compare the following code
expressions, which has the same effect:
print (($volts >= 1.5) ? "OK\n“: (($volts >= 1.2) ? "Weak\n" :
"Replace\n“));
Control Structures

Repetition:
Perl provides a variety of repetition mechanisms to suit all
tastes, including both ‘testing’ loops and 'counting' loops.
‘Testing’ loops
while ($a != $b){
if ($a > $b) {
$a = $a - $b
}else {
$b=$b-$a
}
}
Control Structures

Likewise, statements (but not blocks) can use while and


until like if with unless as statement modifiers to improve
readability, e.g.
$a += 2 while $a < $b;
$a += 2 until $a > $b;
Note particularly, though, that this is purely syntactic sugar
- a notational convenience.
Although the condition is written after the statement, it is
evaluated before the statement is executed, just like any
other while/until loop: if the condition is initially false the
statement will never be executed –a zero-trip loop.
Control Structures

 The condition attached to a do loop looks superficially the same as a


statement modifier, but the semantics are that the condition is tested
after execution of the block, so the block is executed at least once.
do {
……..
} while $a!=$b;
 The while can be replaced by until with the obvious meaning, and the
modifier can be omitted entirely.
 A do statement without a modifier executes the statements of the block
and returns the value of the last expression evaluated in the block: this
construction is typically used if the earlier statements in the block are
executed solely for their side effects-- a dirty trick, but none the less
useful at times.
Control Structures

Counting loops
Counting loops use the same syntax as c:
for ($i = l; $i <= 10; $i++){
$i_square = $i*$i; $i_cube= $i**3;
print “$i\t$i_square\t$i_cube\n“;
}
There is also a foreach construct, which takes an explicit list of
values for the controlled variable.
foreach $i (1 .. 10) {
$i_square = $i*$i; $i_cube =$i**3;
print “$i\t$i_square\t$i_cube\n“;
}
Control Structures

foreach $i reverse (1 .. 10) {


$i_square = $i*$i; $i_cube =$i**3;
print “$i\t$i_square\t$i_cube\n“;
}
Loop Refinements
Many 'testing' loops in programs tum out to be what are
sometimes called 'n-and-a-half‘ loops, in which it is required to
terminate the loop part-way through an iteration. Strictly
speaking an 'n-and-a-half' loop is one from which 'we exit part-
way through: equally common is the requirement to abandon the
current iteration part-way through and start the next iteration.
Control Structures

Perl provides three loop control commands for this purpose:


last, next and redo. The last and next commands are analogous
to the 'break' and 'continue' statements in C: last breaks out of a
loop, and next forces the next iteration of a loop.
The redo command repeats the current iteration from the
beginning. It has been said that it is used 'mainly by programs
that want to lie to themselves about what was just input'.
If an undecorated last, next or redo appears in a nested loop
structure it operates on the innermost enclosing loop. More
elaborate loop control can be achieved by applying the loop
control commands to named loops
Control Structures

while <STDIN> {
last if /quit/;
……….
}
OUTER: while ( ... ){
INNER: while ( ... ){
if ( ... ) then {last OUTER;}
if ( ... ) then {next INNERi}
}}
Control Structures

 The last and redo commands can be used in a bare block, as well
as in a looping context. For example the following fragment will
read lines from standard input, throwing away blank lines until
the first non-blank line is reached:
{$line =<STDIN>;
redo until $line =~/\S/}
 The expression $line =~ / \S/ evaluates to true if the line contains
any characters that are not whitespace characters.
Built in Functions

Perl provides a large number of built-in functions, which can be


grouped in a number of categories including, but not exclusively:
 Numeric functions trigonometric functions, random numbers,
etc.
 Scalar conversion functions including decimal value to
character and vice versa, octal and hex to decimal, etc.
 Structure conversion functions convert a list of values into a
binary structure according to a template, and vice versa
String functions - mainly for inserting and removing sub-strings
Input/output and file manipulation functions.
Built in Functions

 Functions like print which take a list of arguments are called list
operators: functions that take a single argument are called named
unary operators. Both have rather unusual precedence rules that
nevertheless lead to natural and 'obvious' behaviour, as follows
 If the token following the function name (operator) on the same
line is an opening bracket, the operator and its arguments have
highest precedence if it looks like a function call, it behaves like
a function call. For example:
$n =rand($m*2) + 1;
print("Total is $total\n");
Built in Functions

 A named unary operator has lower precedence than arithmetic operations (but
higher precedence than logical operators), thus
$n = rand $m*2 + 1;
 has the same effect as
$n = rand($m*2 + 1);
 In the absence of the opening bracket, a list operator has very high precedence to
the and very low to the right. Thus in
print "Hello", "World!", "\n";
 The commas bind tighter than the print, giving the desired effect, but if a list
operator appears as a component of a list, e.g.
("foo", "bar", substr $line, l0, 5)
 The commas on the left of substr are evaluated after it, but the commas on the
right are evaluated before, giving the interpretation as
("foo", "bar", substr($line, 10, 5))
Collections of Data

Collections of data occur in Perl in two forms.


 Lists. A list is a collection of scalar data items which can be treated
as a whole, and has a temporary existence on the run-time stack.
 Arrays and hashes. These are collections of scalar data items
which have an assigned storage space in memory, and can
therefore be accessed using a variable name. The name of such a
variable is analogous to a plural noun.
 The difference between arrays and hashes is that the constituent
elements of an array are identified by a numerical index, which
starts at zero for the first element, while a hash is an associative
array (or table) in which each element is identified by an
associated string called the key.
Collections of Data

A list is a collection of variables, constants (numbers or strings) or


expressions, which is to be treated as a whole. It is written as a
comma-separated sequence of values, e.g.
"red", "green", "blue"
255, 128, 66
$a, $b, $c
$a + $b, $a - $b, $a*$b, $a/$b
A list often appears in a script enclosed in round brackets, e.g.
("red", "green", "blue")
It is important to appreciate that the brackets are not a required part of
the list syntax, as they are in, for example, Lisp, but are there for
grouping purposes, to satisfy precedence rules.
Collections of Data

Applying the principle that the language should always do


what is natural, 'obvious‘ shorthand is acceptable in lists,
e.g.(1. .8) ("A" .. "H", "0" .. "Z")
and to save tedious typing,
qw(the quick brown fox) is a shorthand for
("the", "quick", "brown", "fox")
qw, the 'quote words' operator, is an obvious extension
of the q and qq operators
Collections of Data

List Magics
 Lists are often used in connection with arrays and hashes,
 List containing only variables can appear as the target of an
assignment and/or as the value to be assigned.
 This makes it possible to write simultaneous assignments, e.g.
($a, $b, $c) =(1, 2, 3) ;
 and to perform swapping or permutation without using a temporary
variable, e.g.
($a, $b) =($b, Sa);
($b, $c, Sa) = ($a, $b, $c);
 Both of these are natural forms of expression that can be a great aid to
readability in Perl scripts.
Collections of Data

Arrays
 An array is an ordered collection of data whose components are
identified by an ordinal index: it is usually the value of an array
variable. The name of such a variable always starts with an @,
e.g. @days_of_week, denoting a separate namespace and
establishing a list context.
 The association between arrays and lists is a close one: an
array stores a collection, and a list is a collection, so it is
natural to assign a list to an array, e.g.
@rainfall=(1.2, 0.4, 0.3, 0.1, 0,0, 0);
Collections of Data

 This creates an array of seven elements, which can be accessed


individually as $rainfall [0], $rainfall [1], ... $rainfall [6]. Note
the use of $rainfall[] here: each element of the array (or list) is a
scalar.
 A list can occur as an element of another list. However, this does
not produce a LISP like list with sub-lists: the inner list is
inserted in a linear one-level structure, so that
@foo= (1,2, 3, "string");
@foobar = (4, 5, @foo, 6);
and gives foobar the value (4,5,1,2,3"string" , 6).
Collections of Data

Hashes
 In the world of scripting languages it is common to find
associative arrays (sometimes called content-addressable
arrays).
 An associative array is one in which each element has two
components, a key and a value, the element being 'indexed' by its
(just like a table).
 Such arrays are usually stored in a hash table to facilitate efficient
retrieval, and for this reason Perl uses the term hash for an
associative array.
 Hashes are a very natural way of storing data, and are widely
used in Perl -probably more than conventional arrays.
Collections of Data

 A particular· attraction of hashes is that they are elastic they


expand to accommodate new elements as they are introduced.
 Names of hashes in Perl start with a % character: such a name
establishes a list context.
 As with arrays, since each element in a hash is itself a scalar
reference to an element uses $ as the first character, establishing a
scalar context.
 The index (or key) is a string enclosed in braces(curly brackets):
if the key is a single 'word', i.e. does not contain space
characters, explicit quotes are not required.
Collections of Data

$somehash{aaa} =123;
$somehash{234} ="bbb" ;
$somehash{" $a "} =0;
%anotherhash =%somehash;
Working with Arrays and Lists

 Elements of an array are selected using a C-like square brackets.


syntax, e.g.
$bar = $foo[2];
$foo [2] = 7;
$foo =$foo[2];
 The leading $ in $ foo [ 2] establishes a scalar context, and the
square brackets make it clear that this instance of foo is an
element of the array foo, not the scalar variable foo,nor the entire
array foo.
Working with Arrays and Lists

 Perl a provides number of powerful facilities for accessing


collections of array elements.
 A group of contiguous elements is called a slice and is accessed
using a simple syntax:@foo[1 .. 3].
is the same as the list
($foo[l] ; $foo[2] ; $foo[3])
 A slice can be used as the destination of an assignment, e.g.
@foo[1..3]= ( “hop”,” skip “, " jump” ) ;
Working with Arrays and Lists

 The idea of a slice extends to a non-contiguous selection of array


elements ,thus @foo[1,3,5] and the list ($foo[l] ,$foo[3] ,$foo[5]) are one and
the same thing.
 Like a slice, a selection can appear on the left of an assignment: this leads to
a useful idiom for rearranging the elements in a list, e.g. to swap the first two
elements of an array we write
@foo [0 , 1] = @foo [1, 0] ;
 Array variables and lists (either literal lists or those generated as slices or
selections) can be used interchangeably in almost any sensible situation: in
particular you can subscript a List expression to obtain an element or a slice,
e.g.
$front = ("Bob" , "Carol" , "Ted" , "Alice") [0] ;
@rest =("Bob", "Carol“, "Ted", "Alice“) [1 .. 3];
@rest= qw/Bob Carol Ted Alice/[1 .. 3];
Working with Arrays and Lists

$# gives the last index of the last element of the array.


@value=(1,5,6,7);
print(“$#value\n”);
$smaller ($foo, $bar) =[$foo > $bar];
In the same spirit, elements of an array can be selected by
using another array as the selector e.g. if we write then
@foo= (7,”fred”,9);
@bar=(2,1,0);
@foo = @foo[@bar];
reverses the order of foo
Working with Arrays and Lists

Perl provides several built-in functions for list


manipulation. Three useful ones are
 shift LIST. Returns the first item of LIST, and moves the
remaining items down,reducing the size of LIST by 1.
unshift (ARRAY/LIST,itemvalue). The opposite of shift:
puts the items in LIST at the beginning of ARRAY, moving
the original contents up by the required amount.
push( ARRAY/ LIST,itemvalue). Similar to unshift, but adds
the values in LIST to the end of ARRAY.
pop ARRAY,LIST.similar to shift ,removes the last value
from the end of list.
Working with Arrays and Lists

Iterating over lists:


It is common to want to perform the same on all the
items in a list, or a selection of the items.
Perl provides a number of mechanisms to achieve this
foreach
The foreach loop performs a simple iteration over all the
elements of a list:
foreach $item (list) {

}
Working with Arrays and Lists

 The block is executed repeatedly with the variable $ item taking


each value from the list in turn.
 The variable can be omitted, in which case $_ will be used. Since
Perl will automatically convert an array into a list if required, the
natural Perl idiom for
 Manipulation of all items in an array is
foreach (@array) {
... # process $_
}
Working with Arrays and Lists

 For example, we can reverse the sign of every element in an array of


numbers @a with the simple code
foreach (@a){
$_ = -$_
}
Map
@s= qw/cat, dog, rabbit, hamster, rat/;
@pl = ( );
foreach (@s){
push @pl, $_. 's‘;
}

@pl= map $_. 's', @S;


Working with Arrays and Lists

General form of
map expression, list;
and
map BLOCK list;
The function evaluates the expression or block for each
element of list, temporarily setting $_ equal to the list
item and returns a list containing the values of each such
evaluation. (Remember that the value returned by a block
is the value of the last expression evaluated in the block.)
Working with Arrays and Lists

grep
 In UNlX, grep pattern file
 Print (i.e sends to STDOUT) all lines of the file file that contain an
instance of pattern .In its simplest form, the Perl grep function takes a
pattern and a list and returns a new list containing all the elements of
the original list that match the pattern.
 For example, given
@things =(car, bus, cardigan, jumper, carrot);
then
grep /car/ @things;
returns the list
(car, cardigan, carrot)
Working with Arrays and Lists

In fact the Perl grep is much more powerful than this. Its
general form is
Grep expression, list or grep BLOCK list.
Like map, the function evaluates the expression or block
for each element in the list,temporarily setting $_ to that
value: it returns a list of those elements for which the
evaluation returns true.
Working with Hashes

Creating Hashes:
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 preceded by a "$" sign and followed by the "key" associated with the
value in curly brackets.
A list of key-value pairs need to be assigned to hashes
%foo=(key1,value1,key2,value2,….);
Þ Can be used instead of ,
%foo=(key1=>value1,key2=>value2,…..);
example:%foo=(‘banana’=>’yellow’,’apple’=>’green’…..);
%data3 = ('John Paul', 45, 'Lisa', 30, 'Kumar', 40);
%data4 = ('John Paul' => 45, 'Lisa' => 30, 'Kumar' => 40);
%data5 = (-JohnPaul => 45, -Lisa => 30, -Kumar => 40);
Working with Hashes

Manipulating Hashes:
A hash can be unwound into list containing the key-value pairs by
assigning it to an array e.g. @list=%foo;
Perl provides a number of built-in functions to facilitate
manipulation of hashes.
@keylist=keys %data;
It returns a list of keys of the elements in the hash, and
@valuelist= values %data;
Returns a list of the values of elements in the hash.
Working with Hashes

 The foreach uses @keylist and @valuelist to iterate over the


elements of a hash.
foreach $key (@keylist){
do something with $data($key)
}
foreach (@keylist){
process $data($_);
}
while(($key,$value)= each %data){
…}
Working with Hashes

 delete $data{$key}-removes the element whose key matches $key from the hash
%data, and
 exists $data{$key}-returns true if the hash %data contains an element whose key
matches $key
exists($h{‘key’}) && do {statements } to avoid using if statement
%data10 = ('John Paul' => 45, 'Lisa' => 30, 'Kumar' => 40);
if( exists($data10{'Lisa'} ) ) {
print "Lisa is $data10{'Lisa'} years old\n";
} else {
print "I don't know age of Lisa\n";
}
@keys = keys %data10;
$size = @keys;
print "1 - Hash size: is $size\n";
Working with Hashes

Inverted Hash:
 A hash would be a natural structure for a phonebook application,
with the name as and the associated phone number as value.
 Hashes map keys to values efficiently, and exists can be used to
check that there is an entry with a particular key. Suppose,
however that we want to do the reverse, to find out if a hash
contains an entry with a particular value, and to map it onto the
associated key.
Working with Hashes

%phones=('asd',9876,'wer',4556778888,'yul',34556676868,'rth',
556767688);
%by_number=reverse %phones;
$target=exists($by_number{9876})?$by_number{9876}:"NOT
FOUND";
print("$target");
Strings, Patterns and Regular expressions

 Perl does more explore in string and text handling.


 In most conventional languages strings are a 'poor relation', with a
very limited set of string manipulation operations being provided by a
collection of library functions.
 Since the strings are stored as arrays of characters most of the
operations work in terms of numerical offsets within a string and the
length of the array limits the length of the string, making dynamic
string manipulation very difficult.
 Perl provides very powerful dynamic string manipulation based on the
use of regular expressions.
 Pattern matching with regular expressions and sub-string Substitutions
are an integral part of the language, making it particularly suitable for
text manipulation.
Strings, Patterns and Regular expressions

Introduction to regular expressions:


 Regular expressions provide an extremely powerful way of
defining patterns.
 Strictly ,a regular expression is a notation for describing the
strings produced by a regular grammar . It is thus a definition of a
class of strings.
 The first practical application of regular expressions in computer
systems was in the text editors ed and sed in the UNIX system.
Strings, Patterns and Regular expressions

The regular expression(RE) can be built up in stages:


1.The characters \ | ( ) { ^ $ * + ? . Are meta characters with special
meaning in a regular expression . To use a meta-character in a
regular expression with out a special meaning being attached , it
must be escaped with a backslash.
2.A part from the meta-characters ,any single character in a regular
expression matches it self, so regular expression /cat/ matches the
string cat. (syntax for RE is /String/,The slashes are not part of
string just they act as delimiting and identifying it. Generally a RE
enclosed between slashes is considered as a pattern).
Strings, Patterns and Regular expressions

3.The meta-characters ^ and $ act as anchors :^ matches the start


of a line and $ matches the end of line.
e.g :/^cat/ -matches the string cat only if appears at the start of a
line
/cat$/ -matches the string cat only at the end of line.
/^cat$/-matches lines which contain just the string cat,and
/^$/ -matches an empty line –a really empty lines, not a line
consisting only whitespace characters.
4.The meta character dot(.) matches any single character except
newline.
e.g: /c.t/ matches cat,cot,cut etc..
Strings, Patterns and Regular expressions

5.The character class ,a set of characters enclosed in square brackets, matches


any single character from those listed.
/[aeiou]/-matches any vowel
/[0123456789]/matches any digit./[0-9]/
A character class of the form /[^….]/matches any character except those
listed,so/[^0-9]/ matches any non-digit.
To match arithmetic operators the RE is /[+\-*/]/
6.Reptition of characters within a regular expression can be specified by the
quantifiers *(zero or more occurrences),+(one or more occurrences) and ?
(zero or one occurrence).
e.g /[0-9]+/ matches an unsigned decimal number, and /a.*b/ matches a sub-
string with ‘a’ and ending with ‘b’ ,with an indefinite number of other
characters in between.
Note : quantifiers are- ‘greedy’-they always match longest string possible.
Strings, Patterns and Regular expressions

Regular expressions in Perl:


Alternations: if RE1,RE2,and RE3 are regular expressions RE1|
RE2|RE3 is a regular expression that will match any one of the
components. e.g pattern /^Date:|^From:|^To|^Subject/ matches
some of header lines in a email message.
Grouping: Round brackets can be used to group items: the above
e.g can be written as (^(Date:|From:|To:|Subject)/
/pitt the (elder/younger)/ -patterns matches pitt the elder and pitt
the younger.
/(([0-9][ ])| ([a-z] [ ] ))+/-pattern matches with a sequence of one or
more items , each of which is either a sequence of digits followed
by a space or a sequence of lower-case letters followed by a space.
Strings, Patterns and Regular expressions

Repetition counts:
In addition to the quantifiers *,? and +,explicit repetition counts can
be added to a component of a RE,
e.g /(wet[ ]){2}wet/ -matches ‘wet wet wet’.
{n} must occur exactly n times
{n,} must occur at least n times
{n,m} must occur at least n times but no more than m times.
IP address pattern can be written as
/([0-9]{1,3}\.){3}[0-9]{1,3}/
Strings, Patterns and Regular expressions

Non greedy matching:


A pattern including .* matches the longest string similarly for
shortest match .*? is used as a quantifier.
Short hand :
Some character classes occur so often that a shorthand notation is
provided:
\d matches a digit \D any non digit character
\w matches a word character or letter i.e either
lowercase,uppercase or digit \W is quite opposite.
\s matches a white space characters(space,tab,newline) and \S
matches any non white space characters.
Strings, Patterns and Regular expressions

Anchors:
The use of ^ and $ to ‘anchor’ the match at the start or end of the target string .other
anchors can be specified as \b(word boundary) \B(not a word boundary).
Target string contains john and johnathon as space separated words,
/\b john/ will match with both target strings.
/\b john\b/ will only match with john
/\bjohn\B/ will match with johnathon.
Back References:
 Round brackets serve another purpose besides grouping: they define a series of partial
matches that are 'remembered' for use in subsequent processing or in the regular
expression itself. Thus, in a regular expression, \ 1, \ 2 etc. denote the substring that
actually matched the first, second etc. sub-pattern, the numbering being determined by
the sequence of opening brackets.
Note:?: is used for rounding brackets to define a grouping with out remembering
the sub-string matches.
Strings, Patterns and Regular expressions

Pattern Matching:
 Simple pattern matching operation for in the line of
 Code print if /shazzam!/
 We now recognize /Shazzam!/ as a pattern containing a regular
expression. Perl compares the pattern with the value of the
anonymous variable, $_, and returns true if the pattern given
matches a sub-string in that value, giving the desired effect.
 Short-hand full form of a match operation m/Shazzam!/:this
expression in which a scalar context , as here ,returns a boolean
value recording success or failure of the matching operator.
 If the m operator is present we can use any character as the pattern
delimiter ,e.g. print if m/Shazzam!/
Strings, Patterns and Regular expressions

 print if m/($word1|$word2)/ variable interpolation can, however ,be suppressed


by using single quotes as the pattern delimeter.
 An empty pattern ,e.g. //,denotes the most recently used regular expression.
Setting a target:
A match with a specific variable rather than the anonymous variable is denoted
by the =~ operator. for example,($a =~ /abc/) is an expression which has the
value true or false according as the pattern /abc/ match are does not match the
string held in $a.
Remembering what matched: If a pattern is matched successfully ,the variable
$& is set to the entire matched string and the portions of the target string that
matched sections of the regular expression in round brackets are assigned to the
variables $1, $2, etc., the numbering determined by the sequence of opening
brackets.
($proto,$site,$path)=($url = ~ m/(http|ftp)://(.*?/)(.*$)/);
Strings, Patterns and Regular expressions

Look ahead assertions:


 A lookahead assertion is denoted the syntax (? = .. ).. This
behaves like any other grouping in a regular expression , but if it
matches the target string, the matching character(s) are not
remembered. Thus an alternative way of parsing a URL without
including a trailing slash in the site would be to change the
pattern match operation to
m!(http|ftp)://(.*?(?=/))(.*$)!
Strings, Patterns and Regular expressions

The operation of the pattern match operator can be modified by adding trailing qualifiers,
thus:
 m/ /i - Ignore case when pattern matching
 m/ /g -find all occurrences. In a list context it returns a list of all the sub-strings
matched by all the bracketed sections of the regular expression. In a scalar context it
iterates through the target string returning true whenever it finds a match, and
false when runs out of matches.
 m/ /m -Treat a target string containing newline characters as multiple lines. In this
case ,the anchors ^ and $ are the start and end of line:\A ad \Z anchor to the start
and end of the string ,respectively.
 m/ /s -Treat a target string containing newline characters as a single string, i.e dot
matches any character including newline.
 m/ /x -Ignore whitespace characters in the regular expression unless they occur in a
character class ,or are escaped with a backslash.
 m//o -compile expression once only.
Strings, Patterns and Regular expressions

Substitution:
s/pattern/subst/- The substitution operator checks for a
match between the pattern and the value held $_,and if
match is found the matching sub-string in $_ is replaced by
the string subst.
while(<STDIN>){s/(^d{4}[ ])(.*$)/$2 $1/;
print;
}
$a=~ s/pattern/subst/
Strings, Patterns and Regular expressions

Substitution modifiers:
 The i, g, m, 0, s and x modifiers work with the substitution operator
in the same way as they do for the match operator.
 In addition, the substitution operator has an additional, very
powerful modifier, e.
 This modifier signifies that the substitution string is an expression
that is to be evaluated at run-time if the pattern match is successful,
to generate a new substitution string dynamically.
 For example, if the target string contains one or more sequences of
decimal digits, the following substitution operation will treat each
digit string as an and add 1 to it:
s/\d+/$&+l/eg;
Strings, Patterns and Regular expressions

Character translation: tr
The syntax of the tr operator is
tr/original/replacement/
$var = ~ tr/original/replacement/
 As should be obvious by now, the first form works on $_. In its simplest form,
original and replacement are sequences of characters of the same length.
 The tr operator scans its target string from left to right, replacing occurrences
of characters in original by the corresponding character in replacement:
characters not included in original are left unchanged. Thus, for example
$line =~ tr/A-Z/a-z/;
 forces the string in $line into lower case, leaving non-alphabetic characters
unchanged. If replacement is shorter than original, its last character is
replicated as many times as is necessary to fill it out.
Strings, Patterns and Regular expressions

 The behaviour of tr can be influenced by trailing modifier


characters as follows:
 tr / / / d- Do not replicate last character in replacement.
Characters in original with no corresponding replacement
characters are deleted.
 tr / / / c -Complement original, i.e. it includes all characters
except those specified.
 tr / / / s -Condense ('squash') duplicate replaced characters to a
single character.
Strings, Patterns and Regular expressions

Split and join:


 Used in a list context, the split function scans a string looking for delimiters
defined by a pattern (regular expressions) and returns a list of the sub-strings
so identified.
 E.g @words= split /\s+/, $line;
 if $line list of words separated with whitespaces, the above example returns
a list of the words in the array @words. If the second argument is omitted it
defaults to $_ if both arguments are omitted the delimiter is assumed to be
whitespace and the target is $_, thus making the common case easy.
 The join function does the reverse of split, converting a list of strings
in to a single string, using a separator string provided as an
argument. Thus
 $line= join “ ", @words;
 puts the words from the array @words into $line, separated spaces.
Subroutines

Subroutines (or subs) are named blocks of thus:


sub foobar {
Statements
}

sub foobar {
…..
return value;
}
Subroutines

Calling subroutines:
 Perl subroutine are better captured by the 'textual substitution' model. If foobar is
defined as a subroutine it can be called without arguments by
&foobar;
 or equivalently (remember that there's more than one way to do it)
&foobar( );
 The amperstand identifies foobar explicitly as the name of a subroutine, so this
form of call can be used even if the subroutine definition occurs later in the
script. If the subroutine has been defined earlier, the ampersand can be omitted: it
is common to provide a forward declarations of subroutines that are defined later
in a script, so that the amperstand hardly ever needs to be used.
 A forward declaration takes the form
sub foobar;
 i.e. it is a declaration without a subroutine body.
Subrouitnes

sub Hello {
print "Hello, World!\n";
}

# Function call
Hello();
Subroutines

Subroutine arguments:
 If a subroutine expects arguments, the call takes the form
&foobar(argl, arg2);
 or in the likely case that the subroutine is declared, we can omit the
amperstand
foobar(argl, arg2);
 The Perl subroutine model is based on the premise that subroutines
typically variadic, i.e. have a variable number of arguments, unlike
conventional in which the number and type of the arguments are defined
as part of the subroutine declaration.
 A subroutine expects to find its arguments as a flat list of scalars in the
anonymous array @_: they can be accessed in the body as $_ [0 ], $_
[1 ] etc.
Subroutines

 The value returned by a subroutine may be a scalar or a flat list of


scalars. It is important to note that in either case, the that
determines the return value is evaluated in the context (scalar or
list) in which the subroutine was called. Thus if we write
$x =foo{$y, $z)
 the return value will be evaluated in scalar context, but if we
write
@x= foo($y, $z) it will be evaluated in list context.
Subroutines

# Function definition
sub Average {
# get total number of arguments passed.
$n = scalar(@_);
$sum = 0;

foreach $item (@_) {


$sum += $item;
}
$average = $sum / $n;

print "Average for the given numbers : $average\n";


}

# Function call
Average(10, 20, 30);
Subroutines

Local and global variables:


 The text-substitution model implies that variables used in a subroutine
are global. Thus if a variable $x appears in the body it will take the
value of an existing $x if there is one: if not, a new variable will be
created and initialized and will be available for the rest of the script.
 This behavior can be modified by using the my declaration, or the
local declaration.
 The my declaration establishes a lexical scope; that is to say, a variable
so declared exists only in the block in which it is declared (and any
blocks lexically enclosed by this block): global variable with the same
name becomes temporarily invisible. More than one variable can be
declared in a my declaration by providing a list of variables enclosed
in brackets.
Subroutines

sub sl {
my $foo; my $bar;
my ($c , $m, $y, $k) ;
}
 The my declaration can also be used to initialize the local
variables, e.g.
sub s2 {
my ($red, $green, $blue)=(255, 127, 0);
}
Subroutines

# Function definition
sub PrintList {
my @list = @_;
print "Given list is @list\n";
}
$a = 10;
@b = (1, 2, 3, 4);

# Function call with list parameter


PrintList($a, @b);
Subroutines

# Function definition
sub PrintHash {
my (%hash) = @_;

foreach my $key ( keys %hash ) {


my $value = $hash{$key};
print "$key : $value\n";
}
}
%hash = ('name' => 'Tom', 'age' => 19);

# Function call with hash parameter


PrintHash(%hash);
Subroutines

# Function definition
sub Average {
# get total number of arguments passed.
$n = scalar(@_);
$sum = 0;

foreach $item (@_) {


$sum += $item;
}
$average = $sum / $n;

return $average;
}

# Function call
$num = Average(10, 20, 30);
print "Average for the given numbers : $num\n";

You might also like