Sei sulla pagina 1di 242

Practical Extraction and Reporting

Language

Introduction to PERL
Chapter 1

Introduction to Perl
Practical Extraction and Report Language
Developed by Larry Wall
Optimized for:
Scanning arbitrary text files
extracting information from those text files
printing Reports
Available in almost all OS platforms: UNIX, DOS,

Windows and Macintosh


Current Version 5.14.2 (V6 under development)
Backward compatibility

Introduction to Perl
Perl combines some of the best but simple features of
c
sh
awk
sed
Syntax corresponds quite closely to C expressions
Free-format language
Typically Perl program is indented much like C

How to get Perl


Home page: http://www.perl.org
The main distribution point of perl is CPAN (Comprehensive Perl

Archive Network). This archive contains all things related to perl.


You can use your web browser to access CPAN at
http://www.cpan.perl.org
Under the CPAN directory, youll see at least the following
subdirectories.
Documentation
Modules
Perl Source

Basic Concepts
Perl is both a Compiler and Interpreter
It is compiler because the program is completely read and parsed

before the first statement is executed.


It is an Interpreter because there is no object code gets generated
after compilation.

Basic Concepts
Shell script - a sequence of shell commands
$ echo date > somescript
$ echo who >> somescript
$ cat somescript
date
who
$ chmod +x somescript
$ somescript
[output of date and who commands will be displayed]

Basic Concepts
Perl program is a bunch of perl statements and definitions thrown

into a file
The first statement is
#! /usr/bin/perl
Perl comments are like shell comments:
# Anything from here till end of line
# NO C like multiline comments

First Example
#! /usr/bin/perl -w
print (Hello, World!\n);

Scalar Data
Chapter 2

Scalar Data

A scalar is the simplest form of data used in perl.

It can be a number or a string.

Perl does not differentiate between a number and string


and uses them interchangeably.

Scalar Variable

A scalar variable is a conventional variable and holds a scalar


value ( a number, a string or a reference ).

The variable name begins with a $ followed by a letter or more


letters or digits or underscores.

There is distinction between upper and lowercase variables.


Such that $A is different from $a.

Numbers

can specify both integers and floating-point numbers


perl computes only with double precision floating point
value.
There is no integer in perl
Perl accepts the complete set of floating-point literals like
numbers with or without decimals and exponential
notations.
Octal numbers start with leading 0, and hex numbers
start with leading 0x or 0X.
Therefore do not start a number with 0

Numbers
Ex.

1.25
# decimal number

7.25e12
# 7.25 time 10 to power of 12

-6.5e12
# 6.5 times 10 to the 12th power

-6.65
# negative decimal number

12
12 # simple integer
Octal & Hex
Ex.
0377 # 377 octal, same as 255 decimal
0xff # FF hex, same as decimal 255

Strings

Each char. is an 8 bit by default.


There is nothing like NULL.
String can be of any length, up to the end of memory.
String literals are enclosed in single quotes or double
quotes.
There is difference between single quotes and double
quotes.

Strings
In a single quote string
character within the quote is legal (including \n, \t etc.) except
when a quote is to be used, precede it with \ and for a
backslash precede it with another backslash.
Ex.
hello
# simple string of five chars.
don\t
# quote of dont is preceded with \
silly\\boy
# silly\boy, the backslash is preceded with \
hello\n
# a string as is hello backslash and n
hello
# word hello a newline and word there
there

Strings
In a double quoted string the backslash specifies the control
characters or any character through octal and hex
representations.
Ex.
hello world\n #string hello world and newline
new \127
#new, space and the del char. In octal.
coke\tsprite
#coke, a tab and sprite
cant
#cant word

Operators
Number operators :
Perl provides the normal addition, subtraction,
multiplication , division and the exponentiation
operators including the modulus.
Ex.
2+3
# 2 plus 3, or 5
5.1 2.4
3 * 12
14 /2
10.2 / 0.3
2 ** 3
10 % 3
10.5 % 3.2

# 5.1 minus 2.4 i.e., 2.7


# 3 times 12 = 36
# 14 divided by 2, or 7
# 10.2 divided by 0.3, or approx. 34
# 2 to the power of 3, or 8
# 10 divided by 3 with the remainder as the
modulus , or 1
# the decimals are first converted to
integers 10 & 3 and mod is 1

Operators
Logical comparison operator :
These are operators such as <, <=, = =, >=, !=, > which compare
two values numerically and return a true or false
String Operators :
String values can be concatenated with the . operator (period).
Ex.
hello . world
#same as helloworld
hello world . \n
# same as hello world\n

Operators
Numeric and String comparison operators :

Separate operators for string and numbers in perl, to keep


the two comparison different.
As 7 and 30 compares as 7 is less than 30 in numbers,
In string 7 and 30, the 30 is less than 7 as ascii value
of 3 is less than ascii value of 7.
These comparison operators are opposite to that as used
in Unix shells where eq is for numeric and = is for strings.

Numeric and String Operators


Numeric and String comparison operators :
Comaprison

Numeric

Equal
Not Equal
Less Than
Greater than
Less than or equal
Greater than or equal

==
!=
<
>
<=
>=

String
eq
ne
lt
gt
le
ge

Numeric and String Operators


String repetition operator :
The repetition operator is the lower case letter x.
Ex.
friend x 3
# is friendfriendfriend
boy x (4+1)
# is boy x 5, or
boyboyboyboyboy
(3+2) x 4
# is 5 x 4, x is a string
operator, thus 5 x 4 or 5555

Numeric and String Operators


Operator Precedence :

Perl uses the common mathematical definition in using the


precedence norm.
Those of the same precedence are in the order of left to right.
You can provide your precedence by using Parentheses.

Inputting Data from Standard Input


#! /usr/bin/perl -w
print What is your name? ;
$name = <STDIN>;
chomp($name);
# Last two statements can be combined as:
# chomp($name = <STDIN>);
print Hello $name! \n;

Conversion between Numbers and


Strings

A string used as an operand for a numeric operator ( say + ) ,


Perl automatically converts the string to numeric.
During conversion of string to numeric trailing non-numerics
and leading white spaces are ignored, thus 123.45fred
converts to 123.45.
Similarly, if a numeric is used in place of a string, the numeric
is converted to string.
Such as ;
X . (4 * 5)
# same as X . 20, or X20

Scalar operators and Functions


To assign a value to a variable, the equal sign is used.
$a = 17;
$b = $a + 3;
$b = $b * 2;

# assigns 17 to variable $a
# give $b the value of $a plus 3 , (20)
# give $b the value of $b * 2 (40)

In Perl you can assign a value in an operation too, as in:


$b = 4 + ($a = 3);
$d = ($c = 5);
$d = $c = 5;

# assign 3 to $a, then add 4 to store result


in $b
#assign 5 to $c and then assign $c to $d
#same as above without parenthesis

Binary assignment Operators


Instead of writing $a = $a + 5, perl makes use of binary operators
to do the same. As in;
$a += 5;
# same as $a = $a + 5, but with binary
operator +=
$b *= 3;
# same as $b = $b * 3, but with binary
operator *=
$str .= K; # same as $str = $str . K, concatenates a
K to value of $str
Other binary operators can be ;
$a **= 3;
# $a = $a ** 3
$b = ($a += 4);
# add 4 to $a, and assign $a to $b

Autoincrement and Autodecrement


The ++ operator (autoincrement operator) adds one to the
operand, and returns the incremented value.
$a += 1;
++$a;
$d = 17;
$e = ++$d;

#with increment operator


#same as above with prefix increment

# $d is incremented and assigned to $e,


both are 18 now.

Autoincrement and Autodecrement


The ++ operator to the left of the variable is the prefix operator. It
can also be used on the right as the suffix operator.
In the case of suffix, the value of the expression is the old value
before the variable is incremented.
Ex.
$c = 17;
$d = $c++; #$d is 17 and $c is 18. $c is incremented after
assigning old value to $d.
The -- is the autodecrement operator and functions the same as
the autoincrement operator ++.

The chop and the chomp functions


The chop function takes a single argument, which must be a
scalar variable, and removes the last char. From the string value
of the argument.
Ex.
$x = hello world;
chop($x);
# $x is now hello worl
The chomp function discards the newline char. of a string, only if
one exits.
Ex.
$a = hello world\n;
chomp ($a); # $a is now hello world
$b = hello world;
chomp ($b) # $b is still hello world

Interpolation of Scalars into Strings


A double quoted string is subject to variable interpolation. That is,
the string is checked for variable names ($ sign followed by
letters, digits and underscores) and when one is found, it is
replaced by its current value.
Ex.
$a = obelix;
$b = A fat guy called $a; # $b is now A fat guy
called obelix
$c = No such variable $what ; # $c is No such
variable

Interpolation of Scalars into Strings


A string is replaced only once. So if a string has a variable which
itself stores another, only one replacement takes place.
Ex.
$x = $name
# a literal string with $ followed by word
name
$y = name is $x # value of $y is name is $name : no
double substitution

Interpolation of Scalars into Strings


Perl searches a matching variable name from the environment for
the replacement.
Thus if words are joined as part variable and part text, and these
together exists as a variable, the variable is replaced instead.
To do so perl provides a delimiter as curly braces. You can also
separate them with string concatenating operator as;
Ex.
$your = its ${my}day;
# result is its payday
$your = its $my. day; # same as above
$your = its . $my. day # same as above

Interpolation of Scalars into Strings


Case shifting can also be used in the sting to convert cases. As;
Ex.
$cname = \Uasterix
#results in ASTERIX
$capname = \uasterix
# results in Asterix
$lname = \LOBELIX
# results in obelix
$lowname = \lobelix
# results in obelix
$name = \uganesh bhat # results in Ganesh bhat
$name = \lGanesh Bhat # results in ganesh Bhat
$name = \Uganesh bhat # results in GANESH BHAT
$name = \LGANESH BHAT # results in ganesh bhat
\u and \l is applied only on the immediate character whereas
\U and \L is applied on the entire string.

Defined OR
Use ? : operator to evaluate an expression and return a value based on
whether the expression is true or false.
$var1 = part1;
$var2 = part2;
$var3 = defined($var1) ? $var1 : $var2;
# defined function evaluates whether var1 is defined or not
# returns var1 if var1 is defined else returns var2
This can also be written using a simpler operator // (available from
V5.10)
$var3 = $var1 // $var2;

Inputting Data From Terminal

The <STDIN> in used to accept data from the terminal.


Perl waits for data to be input and reads the data line up to the
newline char. (carriage return).
Data read using <STDIN> has a newline (\n) at the end.

To get rid of it use the chomp function. As;


$a = <STDIN> ;
chomp($a);

# read a line of input into $a


# get rid of the \n at the end of line.

You can also use;


chomp($a = <STDIN>);

Output with print

The print function is used to print thing out. It takes parameters


and dumps them to the standard output.

Ex.
print (hello world\n);
print hello world\n;

# prints hello worlds followed


by newline
# same as above.

Undefined or uninitialized variables in perl have an undef value


to them. This value is empty string. You get a warning with w
switch on the perl call line.

Control Structures

Control Structures
A statement block is a sequence of statements,
enclosed in matching curly braces.
{
statement;
statement;
statement;
}

The if/unless statement


if (expression ) {
True statement;
True statement;
True statement;
} else {
false statement;
false statement;
false statement;
}
The true statements are executed if the expression
is true else the false statements are executed.

The if/unless statement

The control expression is evaluated for a string value


If it is numeric, it is converted to string.
If the value of the string is empty or 0, then the value of the
expression is false. Anything else is true.

Ex.
0
1-1
1

1
undef

# converts to 0, so false
# computes to 0, the converts to 0, so false
# converts to 1 , so true
# Blank string, converts to false
# not or 0, therefore true
# evaluates to , therefore false

The if-else statement


Ex.
print how old are you ?;
$age = <STDIN>;
chomp($a);
if ($age < 18) {
print Kid, you are not
old enough to vote\n;
}

Ex.
print how old are you ?;
$age = <STDIN>;
chomp($a);
if ($age < 18) {
print Kid, you are not old
enough to vote\n;
} else {
print Old enough. Go
vote !\n;
$voter++;
}

The if/unless statement


Ex.
print how old are you ?;
$age = <STDIN>;
chomp($a);
unless ($age < 18) {
print Old enough. Go vote !\n;
$voter++;
}
Replace if with unless, is in effect saying, if the control expression is false
do.
An unless can also have an else, just like an if.

The if/unless statement


In perl you use elsif like a case or switch statement;
if (expression1 ) {
One_true_statement_1;
One_true_statement_2;
} elsif ( expression2 ) {
two_true_statement_1;
two_true_statement_2;
} else {
all_false_statement_1;
all_false_statement_2;
}

If an expression is true, the


corresponding branch is executed, and
all remaining control expressions and
blocks are skipped.
If all expressions are false, the else
branch is executed, if one exists.

Condition checking and comparison


operators
#! /usr/bin/perl w
print What is your name? ;
$name = <STDIN>;
chomp($name);
if ($name eq Larry) {
print Hello, Larry, How are you! \n; }
else {
print Hello, $name!\n;
}

The While/until statement


The while statement is used for iteration (repeated execution
of block of statements ).
while ( expression ) {
statement_1;
statement_2;
}

Perl evaluates the control expression , if the result is true


the body of the while statement is executed.
This is repeated until the control expression becomes false,
at which point Perl goes on to the next statement after the
while loop.

The While/until statement


Ex.
$voter = 0;
while ( $voter <= 10 ) {
print how old are you ?;
$age = <STDIN>;
chomp($a);
unless ($age < 18) {
print Old enough. Go vote !\n;
$voter++;
}
}

You can also use until in the left


iteration as;
until ( expression ) {
statement_1;
statement_2;
}

Note that both in while and until form, the body is skipped entirely, if the first
time itself the expression is false.

The do {} while/until Statement


To test the condition at the bottom of the loop, Perl provides
the do {} while statement.
Here the loop is executed at least once, and the condition is
checked at the bottom.
do {
statement_1;
statement_2;
} while expression

You can invert the sense of the test by changing do


{} while to do {} until.
#stop = 0;
do {
$stop++;
print Next stop ? ;
chomp ($location = <STDIN>);
} until $stop > 5 | | $locatioin eq home;

Looping - Example
#! /usr/bin/perl -w
# Guessing a secret word
$secretword = Guess;
print What is your name? ;
chomp($name = <STDIN>);
print Hello $name! \n;
print what is the secret word? ;
chomp($guess = <STDIN>);
# This is a while loop
while ($guess ne $secretword) {
print Wrong, Try again. what is the secret word?;
chomp($guess = <STDIN>);
}

The for Statement


for ( initial_exp; test_exp; re-init_exp ) {
statement_1;
statement_2;
}
The initial_exp is executed first.
This expression provides an initial value to the iterator
variable.
Then the test_exp is evaluated for true or false.
If the value is true, the body is executed.
The re-init_exp is executed at the end of the block, which is
typically used to increment the iterator.
Perl then reevaluates the test_exp, repeating as necessary.

The for Statement


Ex.
for ( $i = 1; $i <= 10; $i++ ) {
print $i ;
}

Arrays and List Data

Arrays and List Data

A list is an ordered scalar data.

An array is a variable that holds a list.

Each element of an array is a scalar variable with a value.

Arrays can have any number of elements without any limits


(available memory).

List Data
A literal list consists of comma separated values enclosed in
parentheses.
Ex.
(1,2,3)
#list of three values 1,2 & 3
(dog,10.5)
# list of two values, dog and 10.5
The elements of a list can also be expressions. The expression is
reevaluated each time it is referred.
($a, 17);
($a+$c, $d+$e)

# two values: current value of $a and 17


# two expressions as values

Empty list is a pair of parenthesis as ().

List constructor operator

Two scalar values separated by two periods (..) is a list


constructor operator.
It creates a list of values starting from left value up through the
right value, with an increment of one.

Ex.
( 1 .. 5 )
# value as 1,2,3,4,5
(1.2 .. 5.2)
# values as 1.2, 2.2, 3.2, 4.2, 5.2
(2 .. 4, 10, 12) # values as 2,3,4,10,12
($a .. $b) # range determined by current value of $a and $b

Arrays in Perl
@words = (Gold, Silver, Pearl); or as
@words = qw(Gold Silver Pearl);

The @ symbol is used to indicate an array and to distinguish

from scalar variables $.


To access an element of an array, we use subscript as
$words[0], $words[1] etc.
The subscript can be a numeric constant or an expression
like $j as in $words[$j].

Array Variables
Array variable names are similar to scalar variable names, except
that the first char. $ is replaced by @.
Ex.

@myarray

# the array variable myarray

You can refer to the array as a whole, or as individual elements of


the array.

Initializing Arrays
Assigning a literal list to Arrays
@a = ( dog, cat, mouse, goat );
you can also write as
@a = qw(dog cat mouse goat ) ;
The quote word function uses the space character as the
delimiter.
You can also write as;

@a = qw(
dog
cat
mouse
goat );

More than one secret word


#!/usr/bin/perl w
@words = qw(Gold Silver Pearl);
print What is your Name ?;
name = <STDIN>;
chomp ($name);
if ($name eq Larry) {
print Hello, Larry!\n;
} else {
print Hello, $name!\n;
print What is the Password ?\n;
$pwd = <STDIN>;
chomp ($pwd);
$j = 0; # initializing count
$ok = No;

More than one secret word


while ($ok eq No) {
if ($pwd eq $words[$j]) {
$ok = Yes;
} elsif ($j < 2) {
$j = $j + 1;
} else {
print wrong password, try again!\n;
print What is the Password ?\n;
chomp ($pwd = <STDIN>);
$j = 0;
}
} # end of while not correct
} # end of not Larry

Array Operators and functions


The assignment operator (=) can be used to assign a scalar value
or an array.
Ex.
@myarr = (1,2,3);
# assigns list of 1,2,3 to a 3-elemet
array
@yourarr = @myarr
# an array is copied to another array
Assigning a scalar value to array variable, the array becomes a
single element array
Ex.@onearr = 1;
#array is single element array

Array Operators and functions


Array variable names may appear in an array list, whereby the
current value is used when the list is computed.
Ex.
@arr1 = qw(one, two);
@arr2 = (4,5, @arr1 ,6 ,7 ); # arr2 contains 4,5, one, two,
6, 7
@arr2 = (@arr2, last);# arr2 now contains
(4,5,one,two,6,7,last)

Array Operators and functions


A list containing only variables, not expressions, can be assigned
value in the order of the assignment. As;
Ex.
($a, $b, $c) = (1,2,3)
#assigns values 1 to $a, 2 to
$b and 3 to $c
($a, $b) = ($b, $a)
# swap $a and $b
($d, @arr1) = ($a, $b, $c)
# gives $a to $d and ($b, $c) to
array arr1
If the number of elements on the left (to be assigned) does not
match the number on the right (being assigned), excess values
(on the right) are dropped and excess variables(on the left) are
given the value of undef.

Array Operators and functions


To find the number of elements in the array, assign the array to a
scalar variable. The scalar variable would then contain the
number of elements in the array, as in;
@myarr = (4,5,6);
$cnt = @myarr;

#initialize array
# $cnt will get 3, the current length
of array

Array Element Access

To access an array element, Perl uses the conventional index


for subscripting.
For the subscripting function, array elements are numbered
using sequential integers, from 0 and increasing by one for
each element.
Like; $myarr[0], $myarr[1] and so on.
The @ changes to $ when referring to an element of the array,
which is a scalar.

Array elements can be assigned or used in expressions.


Ex.
@fred = (7,8,9);
$b = $fred[0];
# gives the value 7 to $b
$fred[0] = 11;
# now fred has (11,8,9)

Array Element Access


Other ways of accessing elements are;
$myarr[2]++; # increment the 3rd element of array
myarr
$myarr[1] += 4
# add 4 to the value of the 2nd element
( $myarr[0], $myarr[1] ) = ( $myarr[1], $myarr[0] ) # swap
the first two

Array Element Access


Accessing a list of elements from the same array is called slice.
There is a special representation for slice. Ex.
@myarr[0,1] = @myarr[1,0];
# swap first 2 elements
@myarr[0,1,2] = @myarr[1,1,1]; # make all 3 elements like the
2nd
@myarr[1,2] = (9,10);
# change the 2 element values to 9
and 10
Indexes can also be expression that return a number. Ex.
@myarr = (7,8,9);
$a = 2;
$b = $myarr[$a];
# like $myarr[2], or the value of 9
$c = $myarr[$a 1]
# like $myarr[1], or 8

Array Element Access


Accessing beyond the end or start of array index results in an
undef assignment as;
Ex.
@myarr = (1,2,3);
$a = $myarr[7];
# results in an undef assignment for $a
Assigning beyond the end of current array automatically extends
the array, and assigns undef to all intermediate values, if any.
Ex.
@myarr = (1,2,3);
$myarr[4] = hi;
# @myarr is now ( 1, 2, 3, undef, hi)

Array Element Access


Assignment to an array element less than 0 is a fatal error.
A ve subscript on an array counts from back to start.
To get the last element you can use a subscript of 1 as ;
$myarr[-1] # will return 3 from the above.
You can also use $#myarr to get the last index of array myarr. This
value can also be used in making the array grow or shrink, as;
$lenarr = $#myarr; # Gives the length of array
$#myarr = 2
# Redimensions array to 2 elements
preserving the 1st two elements, if any

The push and pop functions

The function push is used to add an element to the end of list


pop is used to remove the last element from end of list.

Ex.
push(@myarr, $newval);

# same as @myarr =
(@myarr, $newval);
$oldval = pop( @myarr );
# remove the last element
from myarr

The shift and unshift functions


The unshift and shift functions add/remove data to the start of the
list.
Ex.
@myarr = (5,6,7) ;
unshift ( @myarr ,2 ,3 ,4); # @myarr is now (2,3,4,5,6,7)
$x = shift(@myarr);
# $x gets 2 and @myarr is
now (3,4,5,6,7)

The reverse function


This function reverses the order of the elements of its argument.
Ex.
@a = (7,8,9);
@b = reverse( @a );
# gives @b the value of (9,8,7)
@b = reverse (7,8,9);
# same as above
The argument list is unaltered.
The reverse function works on a copy.
To reverse an array in place, assign it back to the same
variable.
@b = reverse (@b); give @b the reverse of itself

The sort function


The sort function takes arguments, and sorts in ascending ASCII
order.
Ex.
@x = sort( small, medium, large);
# @x gets
large, medium, small
@y = (1,2,4,8,16,32,64);
@y = sort( @y );
# @y gets 1,16,2,32,4,64,8
In the last example the sorting does not take place in the numeric
order, but in the string value of in ASCII order.

<STDIN> as an Array

The <STDIN> can be used with an array variable.


When used with an array variable, each line input becomes an
element for the array which is terminated with a newline.
Input to array can be stopped with a CTRL-Z to indicate endof-file or input.

Ex.
@a = <STDIN>; #reads standard input in a line
context

Variable Interpolation of Arrays


Array values can be interpolated in a double quoted string.
A single element of an array will be replaced by its value.
Ex.
@myarr = (hello, dolly);
$x = This is $myarr[1]s place;
$x = this is $myarr[1];
$x = this is ${myarr[1]};
$x = this is . $myarr[1];

# $x contains This is
dollys place
# returns this is dolly
# this returns correct because
of braces
# also correct

Variable Interpolation of Arrays

A list of values from an array variable can also be interpolated.


The interpolation can be the full array or part of it.
The elements are interpolated with a space char. between
them.

Ex.
@arr1 = (sue, sam, harry);
$all = The kids @arr1 are here;

# $all gets the kids sue


sam harry are here
You can also use portion of array with a slice .
Ex.
@arr1 = (sue, sam, harry, john);
$all = Now only @arr1[2,3] are here;
# $all gets Now
only harry john are here

The foreach statement

The foreach statement takes a list of values (array) and


assigns them one at a time to a scalar variable, executing a
block of code with each successive assignment.

foreach $i ( @some_list )
statement_1;
statement_2;
}

Here the original value of the scalar variable is automatically


restored when the loop exits, as the variable is local to the
loop.

The foreach statement


Ex.
@a = (1,2,3,4,5);
foreach $b ( reverse @a ) {
print $b;
}

The output is 5,4,3,2,1.


The list used in the foreach can be an
arbitrary list expression, not just an
array.

You can omit the scalar variable also,


and perl would use a dummy $_
variable instead and the print using this
$_.

Ex.
@a = (1,2,3,4,5);
foreach ( reverse @a ) {
print ;
}

Switch Operation
use feature qw (switch say);

# feature introduced in V5.10

my @guessed_num;
my $num = int (rand 100)+1;
while (my $guess = <STDIN> ) {
chomp $guess;
given($guess){
when(/\D/){say "Please enter an integer"}
when(@guessed_num){say "tried already"}
when($num){say "perfect"; last}
when(10){say "Tired?"; last}
push (@guessed_num, $_);
}
}

Switch Operation
use feature switch;

# feature introduced in V5.10

for each (@parts) {


when(/part1/){$part1++}
when(/part2/){$part2++}
when(/part3/){$part3++}
say This part is not required!;
}
# when automatically calls next at the end of its block.

Hashes

Hashes/Tables in Perl Program


A table in perl is called a hash.
A hash is like an array of scalar data
The elements are retrieved using an index
called the key value.
Unlike the array, the index in a hash can be
any arbitrary scalars.
The elements of an array have no particular
order and access is always through the key.

You cannot control the order.

Hashes/Tables in Perl Program


Each element of the table has a scalar value (like an array) and is

referenced by a key value.


The hash is designated as % and is assigned values as:
%words = qw (
asterix
clever
oblix
fat
dogmatix
dog );
Each pair of values in list represents one key and its corresponding
value in the hash.
To find the word for oblix, which is the key, we can write an
expression as $words{oblix}. The value referenced would be
fat.

Hash Variable
Hash variable names are indicated by a percent sign
(%) followed by the same characters as in defining a
scalar (letter, digits and underscore).
Thus $myvar and @myvar and %myvar are
unrelated to one other.
To refer to an element of a hash %myvar, use
$myvar{$key}

# where $key is any scalar


expression.

Hash Variable
To create a new element of a hash use the assignment as;
$myvar{aaa} = bbb;
$myvar{234.5} = 456.8;

# creates a key aaa with value


bbb
# creates key 234.5 with value
456.8

Now, to access the above;


print $myvar{aaa} ;
$myvar{234.5} += 3

# prints bbb
# makes the value 459.8

Referencing an element whose key does not exist returns


the undef value.

Literal Representation of a Hash

A hash can be accessed as a whole to initialize it or copy it to


another hash.
A hash is represented as a list
Each pair of elements form the key and the value.
The list can be assigned into another hash, which will then
recreate the same hash.

@my_list = %myvar;

# @my_list gets (aaa, bbb, 234.5,


456.8)
%newvar = @my_list;
# create %newvar like %myvar
%newvar = %myvar ;
# using hash to create a hash
%newvar = (aaa, bbb, 234.5, 456.8);
# create hash from
literals

Literal Representation of a Hash


You can construct a hash with keys and values swapped using
the reverse operator.
%backwards = reverse %normal;
If %normal has two identical values, those will end up as a
single element in backwards.

The keys function

The keys (%hashname) function lists all current keys in the


hash %hashname.
If there are no elements in the hash, the keys function returns
an empty string.
Ex.
$myhash{ aaa } = bbb;
$myhash{ 123 } = 456.8;
@list = keys(%myhash);
# @list gets (aaa, 123) or (123, aaa)
Ex.
foreach $key (keys (%myhash) )
{
# once for each key of %myhash
print at $key we have data as $myhash{$key} \n;
# show key and value
}

The Values function

The values (%hashname) function returns a list of all the


current values of the hash, in the order of the keys in the hash.

Example
%myhash = ();
#force empty %myhash
$myhash{one} = asterix;
$myhash{two} = obelix;
@lastname = values(%myhash);
#grab the values
The last statement will put (asterix,obelix) or (obelix, asterix)
in @lastname.

Giving each person a different secret


word
#!/usr/bin/perl w
%words = qw (
asterix
clever
oblix
fat
dogmatix
dog );
print What is your Name ?
chomp($name = <STDIN>);
if ($name eq Larry){
print Hello, Larry!\n;
} else {
print Hello, $name!\n;
$passwd = $words{$name};
print What is the Password ?\n;

Giving each person a different secret


word
chomp($pwd = <STDIN>);
while ($pwd ne $passwd) {
print wrong password, try again!\n;
print What is the Password ? ;
$pwd = <STDIN>;
chomp ($pwd);
}
} # end of not Larry

The each function

To iterate over an hash use each (%hashname) which returns


a key-value pair until all the values have been accessed.

Ex.
while (( $first, $last ) = each (%lastname)) {
print The last name of $first is $last\n;
}
Note: Assigning new value to the hash resets the each function to
the beginning. Adding or deleting elements of hash is likely to
confuse each.

The Delete function

Perl provides the delete function to remove hash elements.

Ex.
%myhash = (aaa, bbb, 123, 455.5 ); #assign two values
to hash
delete $myhash{aaa};

# now %myhash has only


123,455.5

Hash Slices

A hash can be sliced to access a collection of elements


instead of one element at a time.

$score {asterix} = 210;


$score{obelix} = 215;
$score{dogmatix} = 300;
This can be done better using slice as;
@score{asterix, obelix, dogmatix} = (210,215,300);
We can use hash slice with variable interpolation as well.
@players = qw(asterix obelix dogmatix);
print scores are : @score{@players}\n;

Miscellaneous Control Structures

The Last Statement

Like Cs break to get out of a loop, Perl provides the last


statement.
The last statement comes out of the innermost enclosing block
and continues with statement following the block.
While (something) {
:
if (somecondition)
{
:
last; #break out of loop
}
:
}
# last comes here
The last statement in the if condition terminates the while loop.
The last statement counts only looping blocks, not other
blocks like if else and also the ones for do { } while/until.

The Last Statement


The blocks that count are for, foreach, while, until
and naked blocks.
A naked block is a block that is not part of a larger
construct such as a loop, subroutine or an
if/then/else statement.

The Last Statement


Ex.

To check if a mail message in a file is from merlyn.


From: merlyn@stonehenge.com
To:
gbmalpe@gmail.com
Date: 01-may-09 10:30:15 AM
Subject:
A sample mail message
The body of the mail message.

To find if the mail message is from merlyn, we look for From line
and then see if the id is merlyn.

The Last Statement


while (<STDIN>)
{
# read the input lines
If (/^From: /)
{
# if the line begins with From :
If ( /merlyn/ )
{
# if its from merlyn
Print There is Email from merlyn \n;
}
last; #No need to keep looking for From ; so exit
}
# end if From
if (/^$/)
{
# if its a blank line ?
last;
# no need to check more lines
}
} # end while
Once the line with From is found, the main loop exits.
Since the mail header ends with a blank line we exit the main
loop there as well.

The Next Statement

The next statement causes execution to skip past the rest of


the innermost enclosing looping block without terminating the
block.

Ex.
while ( something )
{
:
if ( somecondition)
somepart;
:
next;
}
otherpart;
:
# next comes here
}

If somecondition is true,
then somepart is executed,
and otherpart is skipped.
Like last, even in next the
if statement does not count
as a looping block.

The Redo Statement


The redo construct causes a jump to the beginning of the
current block ( without reevaluating the control expression ).

Ex.
while ( something )
{
# redo comes here
:
if ( somecondition)
somepart;
:
redo;
}
otherpart;
:
}

Like last and next, even in redo the


if statement does not count as a
looping block.
With redo and last and a naked
block, you can have an infinite loop
(redo) and exit from an intermediate
point (last).

Labeled Blocks
To jump out of block, or exit from two nested blocks
at once, you can use last, next and redo on an
enclosing block by giving it a name with a label.
The label follows the same naming conventions and
is in a separate namespace.
It is better to use labels as uppercase consisting of
letter and digits.

Labeled Blocks
Ex. 1
SOMELABEL:

while ( condition ) {
Statement;
:
:
if ( othercondition ) {
last SOMELABEL;
}

}
The last statement with label as parameter, tells perl to exit
the block named SOMELABEL, rather than just innermost
block.

Labeled Blocks
Ex. In nested loops:
OUTER: for ($i = 1; $i <= 10 ; $i++ ) {
INNER: for ($j; $j <= 10; $j++ ) {
if ($i * $j ) == 63 {
Print $i times $j is 63 ! \n;
Last OUTER;
}
if ( $j >= $i ) {
next OUTER;
}
}

The loops iterate to find for every


outer number, an inner number
that when multiplied with outer
results in 63.
Once the inner number is found,
there is no point testing other
numbers, so the first if statement
exits both loops using last
OUTER label.

The 2nd if checks if inner loop number is greater than the outer loop numbers.
If so, it loops to the next iteration of the outer loop with next OUTER. Thus, only
9 * 7 = 63, is the result.

Expression Modifiers
Perl allows usage of an if modifier onto an expression that is
a standalone statement.
Some-expression if control_expression;
Here the control_expression is evaluated first for truth value,
and if true, some-expression is evaluated next. This is similar
to
If ( control_expression ) {
Some_expression;
}

Expression Modifiers
Ex.
LINE: while <STDIN> {
Last LINE if /^From: /;
}
Other variants include;
Exp2 unless exp1;
Exp2 while exp1;
Exp2 until exp1;

# if 1st word is From:


then exit block

Expression Modifiers
Ex.
To find first power of two greater than a given number;
Chomp($n = <STDIN>);
$i = 1;
$i *= 2 until $i > $n;
print The value of power of two Greater than $n is $i \n;
Note:
This form does not nest.
Exp2 consists of single statement.

&& and || as control structures


In statements like if this, then that, you can use logical
operators && ( logical AND ) and || (logical OR).
They evaluate from left to right, for a truth statement.
In a && operator, If the left operand is false, there is no point in
evaluating the right operand.
Similarly, the || operator, evaluates left operand and right, only
if the left return false.

&& and || as control structures


Ex.
Open (FILE, myfile) || die cannot open myfile: $!\n;
Here, the open function is evaluated. If it is successful, it
proceeds with next statement. If the open fails, the die
function statement prints error and terminates.
The message has the perl program name and line number.
When the $! is used, it prints the full OS error also.
Similar to die, perl also has a warn, which does everything die
does except terminate.
Ex.
Open (FILE, myfile) || warn cannot open myfile: \n;

Basic IO

Input from STDIN


The standard input is called STDIN.
Ex.
$a = <STDIN>;

# reads the next line

In a scalar form it gives one line of input or undef if there are


no more lines.
The inputs can also be taken in a list rather than a scalar as;
@a = <STDIN>;

Input from STDIN


Instead of taking the input to a $line variable, Perl copies the
input of <..> to a $_ variable, as;
While (<STDIN>)
Chomp;

# similar to the above


# similar to chomp ($_)
# other processings

You can use the $_ variable as default for many operation.

Input from Diamond Operator

This is similar to <STDIN>


It returns a single line in scalar, until an undef.
It differs from <STDIN>, in that the diamond operator gets
its data from a file or files, if specified on the command
line.

Ex.
Suppose we have a program name kitty.pl as;
#!/usr/bin/perl
while (<>) {
print $_;
}
and you invoke perl kitty.pl file1 file2

The diamond operator


reads each line of file1
followed by each line of
file2, returning undef at end
of all lines.
The function is like the Unix
cat command

Input from Diamond Operator

The <> operator works with the @ARGV array.


This array is a special array, initialized by the Perl
interpretor to the commmand-line arguments,
Each command line arguments goes into a separate
element of the @ARGV array.

This @ARGV list can be interpreted in the program, as;


@ARGV = (aaa, bbb, ccc);
while (<>) {
# process files aaa, bbb, ccc
print the line value input is: $_;
}

Output to STDOUT

Perl uses the print and printf function to write to standard


output.
The print function takes a list of strings and sends each
string to standard output as is, in turn.
The print function returns a true (1), if the printing was
successful.

Print (2+3), hello;


Print ((2+3), hello);
Print 2+3, hello;

# This print 5 and ignores hello


# prints 5hello
# prints 5hello also

Using Printf for formatted output

Similar to Cs printf to produces formatted outputs.


The printf function takes a list of arguments ( enclosed in
optional parentheses).
The arguments contains the format and width in quotes,
and the scalar variables to print.

Ex.
Printf %15s %5d %10.2f\n, $s, $n, $r;

This prints $s in as a 15 char. String, followed by $n as a


decimal integer of width 5 and $r as a floating point value
with 2 decimal places in a 10-Char field and last a new-line.

Using files in Perl


The data for the hash is stored in a file and read from the

file into a hash in the program. The program then uses


this hash.
Perl program automatically gets three file handles called
STDIN, STDOUT and STDERR, corresponding to three
standard I-O channels.
We had earlier used STDIN handle to read data from
standard input ( keyboard).

To read from a file


sub init_file {
open (DATAFILE, datafile);
while ($name = <DATAFILE> ) {
chomp ($name);
$pwd = <DATAFILE>; chomp ($pwd) ;
$words{$name} = $pwd;
}
close (DATAFILE);
}
The subroutine for file function is called from the main program. In
the datafile the data has to be stored on separate line for name and
pwd:
asterix
clever

To read from a file


The Open initializes a filehandle named DATAFILE by

linking it to datafile in the current directory.


Each record from the file is read and stored in scalar
variables $name and $pwd in a loop. After every two
reads the hash is created with the $name as key and
$pwd as data.
The loop is repeated and at end of file the value returned
is empty which is false for the loop and it terminates.

Reading a Paragraph at a time


The $/ variable controls the termination of read operation;

it is set to "\n" by default.


The file handle or <> operator will only return one line at a
time.
Set $/ variable to read a paragraph at a time.
This will terminate the read on Ctrl+d.
$/ = "";

PERL Flip Flop Operator


Use current input line number operator ($.) to control reading

the required lines.


while (<>) {
print if 5 .. undef;
}
# It skips lines 1..4 then prints the rest of the file.
If either operand of scalar ".." is a constant expression, that
operand is considered true if it is equal (==) to the current input
line number (the $. variable).
he use of a bare number in the flip-flop is treated as a test against
the line count variable, $.

PERL Flip Flop Operator


The same is not true for a scalar variable as it is not a

constant expression. This is why it is not tested against $.


my $start_line = 5;
while (<>) {
print if $start_line .. undef;
}
# It prints from line 1.

Checking File Attributes through Perl


There are operators in Perl to return the attributes of files. The

M operator returns the age in days since a file or filehandle has last
been modified.
If in our example we wanted to check whether the file has been
modified in the last 7 days, we need to check whether the M
operator return a value >= 7 as
sub init_words {
open (WORDLIST, wordlist) | | die cant open wordlist: $!;
if (-M WORDLIST >= 7.0 ) {
print The file wordlist is not modified for Seven days at least;
}
close ( WORDLIST ) | | die cant close file: $!;
}

Using a Wild Card to List files in a


Directory
Finding a file in the directory or listing files in directory using wild

cards is done using a glob function. It works like <STDIN>, in


that each time it is accessed, it returns the next value.
Sub init_words {
While (defined($filename = glob(*.pwd)) ) {
Open (FILELIST, $filename ) | | die cant open file: $!;
If (-M FILELIST >= 7.0 ) {
Print The file is not modified for Seven days at least;
}
close ( FILELIST ) | | die cant close file: $!;
}
}

Functions

Subroutines in Perl
Subroutines can be created and called from other places

in a perl code.
A subroutine has:
Name
Parameters
Return Value

Functions
A user function is called a subroutine or sub.
It is defined in a perl program as;
sub subname {
Statement_1;
Statement_2;
Statement_3;
}
The subname is the name of the subroutine. The name does
not have @ or $ or % so can have the same name as a
variable or array or hash.

Functions
Ex.
Sub say_hello{
Print hello, world \n;
}

Subroutines can be defined anywhere in the program


However, generally they are put at the end of the file.
Subroutines are global.
If two subroutines have the same name, the later one
overwrites the earlier one.
Generally, subroutines access global variables.

Invoking a user function


Ex.
Say_hello();
$a = 3 + say_hello();
for ($x = start_value(); $x < end_value(); $x +=
increment() {
:
:
}
A subroutine can invoke another in a nested fashion.

Return Values

The return value of a subroutine is the value of the return


statement or of the last expression evaluated in the
subroutine.

Ex.
$a = 3; $b = 4;
$c = sum_of_a_and_b();
$d = 3 * sum_of_a_and_b();
Sub sum_of_a_and_b {
return $a + $b;
}

A subroutine can also return a list of values (


multiple values ).
Ex.
$a = 5; $b = 6;
@c = list_of_a_and_b();
# @c gets 5 and 6
Sub list_of_a_and_b {
return($a, $b);
}

Function Arguments

A subroutine invocation followed by list within parameters,


is assigned to special variable @_ for the duration of the
subroutine.

Ex.
Sub say_hello_to {
Print hello, $_[0]!\n;
}

# prints first parameter

$_[0] is the 1st element of the @_ array. This is not the same
as $_.

Function Arguments
Less or more parameters in perl are ignored.
The @_ is private to the subroutine.
If there is global value for @_, it is saved away before the
subroutine is invoked and restored to previous value upon
return to previous value.
Therefore a subroutine can pass arguments to other
subroutines without losing its own @_ values.
Ex.
$a = add(4,5,6);
print add(1,2,3,4,5);
print add(1..5);

Sub add {
$sum = 0;
foreach $_ ( @_ ) {
$sum += $_;
}
return $sum;
}

Subroutines - Example
The example below is for finding the validity of the

password in our example. It takes the name and the


password, and return true if the password is correct and
false if not from the hash.

Subroutines - Example
sub pass_word {
my($key, $passwd) = @_; # name the parameters
$key =~ s/\W.*//; # get rid of everything after first word
$key =~ tr/A-Z/a-z/; # convert to lower case
if ($key eq larry) {
return 1;
} # return true
elsif ( ($words{$key} || guest) eq $passwd ) {
return 1 ;
}
else {
return 0;
} # return false
}

Subroutines - Example
#!/usr/bin/perl
%words = qw (
asterix
clever
oblix
fat
dogmatix
dog );
print What is your Name ?;
chomp($name = <STDIN>);
if ($name =~ /^larry\b/i ) {
print Hello, Larry!\n;
} else {
print Hello, $name!\n;
$passwd = $words{$name};

Subroutines - Example
if ($passwd eq ) { # not found in hash
$passwd = guest;
}
print What is the Password ?\n;
chomp($pwd = <STDIN>);
while ( ! pass_word($name, $pwd)) {
print wrong password, try again!\n;
print What is the Password ?\n;
chomp($pwd = <STDIN>);
} # end of while
} # end of not Larry

Private Variables in Functions

You can create private scalar, hash or array variables within


a subroutine with the my operator.
The my operator takes a list of variable names and creates a
local versions (instantiations) of them.

Ex.
Sub add {
my ($sum);
$sum = 0;
foreach $_ ( @_ ) {
$sum += $_;
}
return $sum;
}

The first declaration saves the global $sum, if


any, and a new variable $sum is created.
When the subroutine exits, local $sum is
discarded and the global $sum is restored.
This works even in nested local versions, though
you can access only one at a time.

Private Variables in Functions


Ex. Creating a list of all elements of an array greater than n;
sub bigger_than {
my ($n, @values);
( $n, @values) = @_;
my ( @result );

# creates some local variables


# spilt args into limit and values
# temporary for holding the
return value
# step through the arg list
# if eligible, add it

foreach $_ ( @values ) {
if ($_ > $n) {
push( @result, $_ );
}
}
return @result;
# return final list
}

Private Variables in Functions

The previous sub can be used with;


@new = bigger_than(100,@list);
100
@this = bigger_than(5,1,5,15,20);

# new gets all @list >


# this gets (15,20)

You can initialize a variable at the time of declaration also as;


my($n,@values) = @_
or
my($sum) = 0;

Semi Private Variable using Local

Perl allows creation of semi private variable with the local


function.

The my is local to the subroutine where it is declared and is


not visible outside it

local declaration causes the variable to be visible in


subroutines called by the subroutine where it was originally
declared.

Semi Private Variable using Local


Ex.
$value = original;
tellme();
spoof();
tellme();
sub spoof() {
local ($value) = temporary;
tellme();
}
sub tellme {
print Current value is $value \n;
}

This prints;
Current value is original
Current value is temporary
Current value is original

Semi Private Variable using Local

Using my in declaration can be used for scalar, array and


hash only.

Local can be used for the perls built-ins also, like $_, @_,
@ARGV.

Explicit Declaration of Variables

Perl allows use of variables as and when required.


However, this may cause wrong reference or assignment
Therefore, it is best to declare all variables before using them.

To force a variable declaration use the pragma use strict.


when use strict is used at the start of program, it gives error
for any undeclared variables during compilation.

Declaring a variable before using causes the perl program to


run faster and any typing error can be detected at compile
time.

Explicit Declaration of Variables


Ex.
use strict;
my $a;
my @b = qw(fred barney betty);
push @b, qw(wilma);
$c = sort @b; #Give compile error because $c is not
declared

AUTOLOAD
The AUTOLOAD mechanism, built into the definition of Perl packages,

is simple to use. If a subroutine named AUTOLOAD is declared within a


package, it is called whenever an undefined subroutine is called within
the package. AUTOLOAD is a special name, and must be capitalized as
shown, because Perl is designed that way. Don't use the subroutine
name AUTOLOAD (or DESTROY) for any other purpose.
Without an AUTOLOAD subroutine defined in a package, an attempt to
call some undefined subroutine simply produces an error when the
program runs. But if an AUTOLOAD subroutine is defined, it is called
instead and is passed the arguments of the undefined subroutine. At the
same time, the $AUTOLOAD variable is set to the name of the
undefined subroutine.

AUTOLOAD
use strict;
use warnings;
use vars '$AUTOLOAD';
# global variable declaration
print "I started the program\n";
report_magical_function("one", "two");
print "I got to the end of the program\n";
sub AUTOLOAD {
print "AUTOLOAD is set to $AUTOLOAD\n";
print "with arguments ", "@_\n";
}

BEGIN
BEGIN {
print Executed at the start of the process and before any other
code.\n";
}
print Programs execution sequence...\n";
die Terminate the program...\n";
END {
print " Executed at the end of the process even if there is an
error in normal program sequence.\n";
}

Typeglob
Variable of same name but different context can coexist at any point in

time. They all are stored together and can be accessed using one
reference.
Use the variable type (context) to access them individually.
$test = "This is a new line";
@test = (124, 464, "Whats the matter?");
%test = (987, "First", 394, "Second");
$a1 = *test{SCALAR};
$a2 = *test{ARRAY};
$a3 = *test{HASH};

Regular Expressions

Regular Expressions
A regular expression ( also called template) is a pattern to be
matched against a string.
The pattern can be used for replacement of a new pattern
etc.
Regular expression is used by programs such as grep, awk,
sed, vi etc.
Perl is a semantic superset of these.

Simple uses of regular expressions


Ex. grep:
To find all lines of a file that contain a string abc using grep as;
grep abc infilename > outfilename
In perl abc is the regular expression and it is used as;

Ex.
While (<>) {
If (/abc/) {
Print $_;
}
}

Here, $_ is tested against the regular


expression, and if it is a match it is printed.

Simple uses of regular expressions


For an expression like ab*c , which means ab followed by any number of
characters between b and c.
in Unix using grep;
grep ab*c infile > outfile

Ex.
In perl;
While (<>) {
If (/ab*c/) {
Print $_;
}
}

In perl, the regular expression has vast


and flexible templates which have many
operators.

String handling in Perl


Searching for a string beginning with Larry rather than exact match

would be as ^Larry. Thus we can check as


If ($name =~ /^Larry/ ) {
## Yes, a match
} else {
## No match
}

The above can handle strings beginning with Larry, but not larry.

Thus to ignore case, we use an i appended at end after the closing


slash as /^larry/i .
Suppose we also want to check that the spelling is Larry and not
Larryy, then we set a word boundary as \b in the expression as ;
/^larry\b/i.

This means larry at the beginning of the string and no letters or

digits following and any case.

Substituting a part of a String


To remove all the nonword char. (anything which is not a

letter, digit or underscore), we use:


/\W.*/

where the \W finds the first occurrence of a nonword

and .* is for all chars upto the end of line.


Now, to replace all chars from nonword to the end of the
line, we use:
$name =~ s/\W.*//;

where s is the substitute operator and the / is the

delimiter specifying search string (\W.*) to be replaced


with string (//).

Converting Case of a string


The substituted strings case can now be changed by the

translate operator tr as

$name =~ tr/A-Z/a-z/;
Therefore, $name =~ s/\W.*//; # gets rid of all chars

after first word


and $name = tr/A-Z/a-z/; # changes string name to
lowercase.
Now Larry, larry or Larry Wall all get converted to
larry

The substitute operator

This operator replaces the part of a string which matches the


regular expression with another string.
It is like the s command in the vi editor.

s/ab*c/def/;

The line ( $_ ) is matched against ( ab*c ) and if the match is


successful, the part is replaced by a new string (def).
Nothing happens if there is no match.

Patterns
A regular expression is a pattern.
Some patterns are for a single char.
Patterns are also for multiple chars.

Single Char. Pattern


A pattern-matching character class is represented by a list of characters between a
pair of square brackets.
Only one of these characters need to match for the pattern to match.
Ex.
/[abcde]/ or /[aeiouAEIOU]/.
Ranges of chars. can be given with a hyphen as [0-9].
Example of patterns:
[0-9\-]
# matches 0 to 9 or
[a-z0-9]
# matches any lowercase char or digit
[a-zA-Z_]
# matches a-z or A-Z or underscore

Single Char. Pattern

Negated character class (^), is the same as a character class, but the caret(^) is
immediately after the opening bracket.

It matches any single char. that is not in the list.

Example
[^0-9]
# any non digit char.
[^aeiouAEIOU]
# anything which is not a vowel
[^\^]
$ any single char which is not a caret (^).

Single Char. Pattern


Contruct

Equivalent
Class

Negated
Construct

Equivalent
Negated Class

\d ( a digit )

[0-9]

\D (digits not! )

[^0-9]

\w ( a word)

[a-zA-Z0-9_] \W (words, Not! )

\s ( space char, ) [ \r \t \n \f]

\S (space, not! )

[^a-zA-Z0-9_]
[^ \r \t \n ]

The \d matches one digit. The \w one word and the \s a space char. The
negated chars. are \D, \W and \S.
These abbreviated classes can be used as part of other char. classes as well;
like
[\da-fA-F]

# match one hex digit

Pattern Types
Grouping Patterns

In groups you can say one or more of these or up to five of those etc.
Sequence

In a sequence, all must be in the same order, As abc means a followed by b


followed by c.
Multipliers

A multipler can be asterisk (*) indicating zero or more of the previous char. or
char. class.
A plus (+), one or more of immediately previous char., and quistion mark (?)
meaning zero or one of the immidiately previous character.

Pattern Types
Ex. 1
/fo+ba?r/
which is an f followed by one or more of os followed by b,
followed by an optional a followed by r.
Ex. 2
$_ = home xxxxxxx home;
s/x+/sweet/;
Replaces all xs with sweet and results in home sweet
home.

General Multipliers
To say five to Ten xs, we use general multipliers as;

/x{5,10}/.

The general multiplier has curly braces with one or two numbers inside. In this case
letter x must have 5 to 10 repetitions.

leaving off the second number as /x{5,}, means 5 or more.


If its /x{5}/, it means exactly five xs.
To get five or less use /x{0,5}/.

Now, a regular expression /a.{5}b/ is interpreted as, a separated from b by any 5


non-newline chars. at a point in the string.

Now, seems you can get rid off *, + and ? as they are equivalent to {0, }, {1, } and {0,1},
however it is easier to type the equivalent single punctuation character.

Parentheses as memory

Another grouping operator is a pair of brackets around a


pattern.
This is used to remember the part of the string matched by
pattern to be stored and used later.

Example
([a]) matches an a, and
lowercase letter.

( [a-z] ) matches any single

Parentheses as memory
To recall a memorized string, you must include a backslash
followed by an integer.
Ex.
/Asterix(.)obelix\1/;
The above example matches a string consisting of Asterix,
followed by any char other than newline char. (dot is in
parentheses for any char and brackets for memorizing ),
followed by obelix and again any single char. ( i.e. repetition of
memorized 1st pattern (.) indicated by \1 ).

Parentheses as memory

In the previous example, the \1 indicated the first parenthesized part of a regular
expression.

If there are more than one, the second part is referred as \2, the third as \3 and so
on. Counting is from left to right
Example
/a(.)b(.)c\2d\1/;
Here the 1st group (.) after a is #1 and (.) after b is #2, and c is followed by repetition of
#2 and d that of #1.

The refernece part can also be multi char. as;


/a(.*)b\1c/;
This matches any chars after a as axxxbxxxc.

Alternation

Another grouping construct where a|b|c means a or b or c.

It can also be used for words as in /song|blue/.

However, for single class construct like /[abc]/ is definitely


better.

Anchoring Patterns

Matching a string to a pattern generally takes from left to right, at the first possible
opportunity.
Anchors allow you to ensure pattern line up with particular parts of the string.

The \b anchor is for a word boundary.


Ex.
/fred\b/;
/\bmo/;
/\bfred\b/;
/\b\+\b/;

# matches fred and not fredrick


# matches mo and mole, not elmo
# matches fred but not fredrick or alfred
# matches x+y but not xx or +

Anchoring Patterns

Likewise, \B requires that there be no boundary at the


indicated point in the string.

Example
/\bfred\B/;

matches fredrick but not fred flinstone

Anchoring Patterns

Ex.

Two anchors for beginning and end of string are ^ and $.


The ^ matches beginning of string.
^a matches an a if a is the first letter of the string.
Whereas a\^ matches the two chars a and ^ anywhere in the string.
To check for literal ^ put a backslash in front of it as \^.

The $, anchors the pattern at the end of a string.


Ex.
c$ matches a c only if it occurs at the end of the string. A $ anywhere else is interpreted
as a scalar value.

Thus to mean a literal $, precede it with \$.

Precedence of Anchors
Examples of regular expressions and effect of parenthesis
Abc*
(abc)*
^x|y
^(x|y)
a|bc|d
(a|b) (c|d)
(song|blue)bird

# matches ab, abc, abcc, abccc and so on


# matches , abc, abcabc, abcabcabc, and so on
# matches x at the beginning of line, or y anywhere
# matches either x or y at the beginning of a line
# a, or bc, or d
# ac, ad, bc or bd
# songbird or bluebird

The Matching operator


The matching operator ( an expression enclosed in slashes) can be used in many ways.
The =~ Operator : This operator takes a regular expression operator on the right side,
and changes the target of the operator to some value named on the left side of the
operator.
Example
$a = hello world;
$a =~ /^he/;
$a =~ /(.)\1/;
if ($a =~ /(.)\1/) {
:
}

# true
# also true (matches the double l)
# true, so yes
# some stuff

The Matching operator


The target of the =~ operator can be any expression that yields some scalar string value.
Ex, <STDIN> yields a scalar string value when used in a scalar context, so it can be
combined with the =~ operator and a regular expression match operator to get a check
for particular input, as in;
Print any last request ? ;
If (<STDIN> =~ /^[Yy]/) {
# input beginning with a Y or y
Print What might that be ? ;
<STDIN>;
print Sorry, Unable to comply\n;
}

Ignoring Case
In the previous case we used [Yy] to match either a lower or upper case y.
To match a string with ignore case, you can use /somestring/i, which will match with
either case.
Print any last request ? ;
If (<STDIN> =~ /^y/i) {
:
}

# input beginning with a Y or y

Variable Interpolation
A regular expression can be variable interpolated before it is considered for special
chars.
Thus, you can construct a regular expression from computed strings rather than just
literals.
Example
$what = bird;
$sentence = Every bird has wings;
if ($sentence =~ /\b$what\b/) { # use of variable reference for
/\bbird\b/
print the sentence contains the word $what!\n;
}

Special Read-only Variables


After a successful pattern match, the variables $1, $2, $3 and so on are set to same
values as \1, \2, \3 and so on. These can be used in later code.
Example
$_ = this is a test;
/ (\w+) \W+ (\w+)/;

# match first two words


# $1 is now this and $2 is now is

another way;
$_ = this is a test;
($first, $second) = / (\w+) \W+ (\w+)/; # match first two words
# $first is now this and $second is now is

Special Read-only Variables

Other predefined read-only variables are $&, which is the part that matched regular
expression

and $`, which is the part of string before the part that matched

and $ which is the part of string after the part that matched.

Example
$_ = "this is a sample string";
/sa.*le/;
# matches "sample" within the string
# $` is now "this is a "
# $& is now "sample"
# $ is now " string"

Substitution
Apart from the simple substitution operator s/old-string/new-string/, there are several
variations also.
To substitute multiple occurrences, use g at the end;
Example
$_ = "foot fool buffoon";
s/foo/bar/g;

# now $_ is "bart barl bufbarn"

An i before or after g, causes the regular substitute operator to ignore case.

Substitution
To have variable interpolation in the replacement string ;
Ex.
$_ = "Hello world";
$new = "Goodbye";
s/Hello/$new/;

# replaces Hello with Goodbye

Pattern chars can be used in regular expression to allow pattern matching, rather than
just fixed chars.
$_ = "this is a test";
s/(\w+)/<$1>/g; # $_ is now "<this> <is> <a> <test>"

Non Destructive Substitution


To preserve the original string and get the replaced string in a new variable, use
/r
Ex.
$old = "Hello world";
$new = $old =~ s/Hello/Goodbye/r;
# replaces Hello with Goodbye and stores it in $new
print $old;
print $new;
This feature was released in v5.14

The Split and Join Functions


The split function is used to break a string into fields, while join is used to form a string
from fields.
Spilt function
The split function takes a regular expression and looks for occurrences of the expression
in the string. The parts that do not match the expression are returned in sequence as a
list of values.
Example
$line = merlyn::118:10:randal:/home/merlyn:/usr/bin/perl;
@fields = split(/:/,$line);
# spilt $line, using : as delimiter
# now @fields is merlyn , , 118, 10,
randal, /home/merlyn, /usr/bin/perl
Note the empty 2nd field becomes empty string.

The Split and Join Functions


Join Function
The join function takes a list of values and glues them together with the glue string into
a string.
Example
$bigstring = join($glue, @list);
To join the example of split after the split you would use;
$outline = join(:, @fields);

Other Data Transformations

Finding a string

To locate a string (substring) within a string, you can use index function.
The index locates the first occurrence of the substring within the string.

Ex.
$x = index($string, $substring);

The $x returns the position of the substring within the string.


Here 0 means substring is at start i.e. at position n-1, and 1 is not found.

Finding a string
Ex.
$where = index(hello, e);
# $where gets 1
$name = barney;
$where = index (fred barney,$name);
# $where gets 5
@rockers = (fred, barney);
$where = index(join( ,@rockers),$name); #$where gets 5
$which = index(a very long long string, lame); # $which gets 1
To get the 2nd occurrence instead of the 1st, use the third parameter of index, which
is the start posn.
$x = index($string, $substring,$startpos);
Ex.
$which = index(a very long long string, long, 9);
#$which gets
12

Finding a string

You can do a reverse search using rindex.


The search starts from the right side, but the position returned is from left.

Example
$where = rindex(hello world, l);
$where = rindex(hello world, o);
$where = rindex(hello world, o,6);

# $where gets 9 for


rightmost l
# $where gets 7 for
rightmost o
# $where gets 4 for
first o before 6

Extracting and Replacing a substring

The substr function is used to pull out a part of a string from a string.
The substr function takes three arguments.
A string value, a start position (1st pos is 0 or pos-1 is the start pos ) and a length
(which can be longer than remaining chars in string).

Ex.
$s = substr($string, $start, $length);
$hello = hello;
$s = substr($hello, 3,2);
$s = substr($hello, 3,200);

# $s gets lo
# $s gets lo

Extracting and Replacing a substring

In perls substr function, the $start argument can be ve.


In case it is negative, the position is counted from the end of the string.
-1 for start posn is the last char of the string.
$s = substr($hello, -3,2);
$s = substr($hello, -1,1);

# $s gets ll
# $s gets o

If the start is big ve number (greater than length of string), the start posn. defaults
to the beginning i.e. 0.
Omitting the legth is same as including a large length, or up to the end.

Extracting and Replacing a substring

To replace a substr, you can have the substr in the left side of an expression as;
$hw = hello world;
substr($hw, 0,5) = howdy;

# replaces hello with howdy

Here the chars returned by substr function are removed and the assigned chars ( on
right ) are substitued in place.
The length of string being assigned (right side) is immaterial as in;
$hw = hello world;
substr($hw, 0,5) = hi;
# replaces hello with hi

Advanced Sorting

The sort function earlier sorted a list in ascending ASCII order.


In perl you can decide what kind of sort you want, descending order, numeric order,
or any general order.
Sort order is specified by how two elements compare to each other.

The comparison routine is a standard subroutine.


This subroutine is called repeatedly with two elements of the list to be sorted.
The subroutine returns one of three values;
If First < Second then return 1,
If First = Second then return 0
If First > Second then return 1.

The two values are passed as $a and $b.

Advanced Sorting

Here, the comparison is your comparison, only the return values are fixed.
It can be any logic which you choose and this makes the perl sort so flexible.

Ex. Sorting in Numeric order


Sub by_number {
If ($a < $b) {
Return 1;
} elsif ($a == $b) {
Return 0;
} elsif ($a > $b) {
Return 1;
}
}

Advanced Sorting
In ordinary sort ;
@somelist = (1,2,4,8,16,32,64,128,256);
@outlist = sort @somelist; # @outlist is now
(1,128,16,2,256,32,4,68,8)
Using sort with our subroutine;
@outlist = sort by_number @somelist;
# outlist is now (1,2,4,8,16,32,64,128,256)

Advanced Sorting
Since, numeric comparison is quite common in sort, perl has a special operator to do
this called the spaceship operator (<=>).
Using this the earlier by_number subroutine can be written as;
Sub by_number {
$a <=> $b;
}
In fact instead of writing as ;
@outlist = sort by_number @somelist;
You can now write as ;
@outlist = sort { $a <=> $b} @somelist;

Advanced Sorting

Just as the spaceship operator is for numeric comparison, perl has a string
comparison char cmp.

To sort a hash on names which contains ids as keys and names as values.

@sortedkeys = sort by_names keys(%names);


sub by_names {
($names{$a} cmp $names{$b} ) || ($a cmp $b );
}

# return cmp on ids


# if names are same

Directory Access

Changing Directory

Similar to cd command in MS-Dos and Unix to move around the directories, perl
used chdir function.

The chdir function takes a single argument/expression evaluating to a directory


name, to which the current directory will be set.

chdir returns true when you have successfully changed to the requested
directory, and false if not.

Example 1
chdir(/usr/bin/perl) || die cannot change directory :$!);

Changing Directory
Example 2
Print Enter directory to change to ?;
chomp ($where = <STDIN>);
If (chdir($where)) {
Print new directory is $where;
} else {
print cannot change to $where;
}
The chdir function without parameter defaults to your home directory.

Globbing

Asterisk (*) is the wild card char used to indicate a list of files.
The expansion of * into a list of matching filenames is called globbing.
Like /etc/host* means all files that begin with host in the /etc directory.
Perls globbing is by putting the * in a pair of angled brackets or to use the
function glob.

Ex.
@a = </etc/host*>;
@a = glob(/etc/host*);
The glob returns a list of all names that match a pattern or an empty list.

Globbing
Ex.
To look at one name at a time;
While ( defined($nextname = <C:\\Training\\*>)) {
Print listed file is $nextname\n;
# dir path is included in
$nextname
$nextname =~ s/.*\\//;
# removes part before last
back slash
print listed file is $nextname\n;
# only name is printed
}
glob function can have multiple patterns inside, which are constructed separately and
finally concatenated as one list.
@fred_barney_files = <fred* barney*>;

Globbing
Ex.
If (-d /usr/etc) {

# if dir /usr/etc exists


$where = /usr/etc;

} else {
$where = /etc;
}
@files = <$where/*>;

Directory Handles

If your OS provides the readdir library function, or its equivalent, perl provides
access to it using directory handles.
Directory handles are names in separate namespace and have the same general
restrictions.
Thus a filehandle FRED and a directory handle FRED are unrelated.
The directory handles is a connection to a particular directory which you use to
read a list of filenames within the directory.
Directory handles are read only.

Note : if your OS does not support readdir, then action with directory handle will result
in a fatal error.

Opening and Closing a Directory Handle

The opendir function takes a name for the directory handle and a string value of
the directory to open.
It returns true or false.

Ex.
opendir(ETC, /etc) || die cannot opendir /etc: $!;
To close a directory handle;
closedir(ETC);

Reading from a Directory Handle


The readdir function uses a directory file handle to read a list of file names.
Each invocation of readdir returns the next filename, in no particular order.
Ex.
opendir (ETC, /etc)
|| die no etc ? $!;
foreach $name (sort readdir(ETC)) {# sorted list context
Print $name\n;
# prints filename
}
closedir (ETC);

Removing a File

The unlink function of perl is used to delete one or more files.


This is similar to Unix rm command.

Ex.
unlink (fred);
# deletes file named fred
unlink (myfile, yourfile); #deleting multiple files
unlink (*.c);
# multi files delete with wildcard
Print Enter a file name to delete !;
Chomp($name = <STDIN>);
unlink $name;

Removing a File

The return value of unlink is the number of files successfully deleted.


unlink does not specify which files were deleted
In group delete, you have to delete individually in a loop and report failed
deletions as;

Ex.
Foreach $file (<*.c>)
{
# step through a list of files
unlink ($file) || warn cannot delete file $file: $!;
}

unlink with no arguments, the $_ variable is used as a default.


Ex.
Foreach (<*.c>) {
# step through a list of files
unlink || warn cannot delete file $_: $!;
}

Renaming a File

The Unix mv command to rename a file is done in perl using the rename($old,
$new).

Example
rename (fred, barney) || die cannot rename fred to barney: $!;

Rename returns true if successful.


To rename a file from one directory to another;

rename(file, some-dir/file);

Making and Removing Directories

Similar to the Unix mkdir command to create a directory, Perls equivalent is also
mkdir function.
Perls mkdir function takes the name of the directory and mode for the
permissions of the directory to be created.

Ex.
mkdir (mydir, 0777) || die cannot create directory mydir: $!;

The Unix command rmdir to remove a directory, is the same name used in perl
as a function.

Ex.
Rmdir (mydir)|| die cannot remove directory mydir: $!;

Modifying Permissions

Permissions on a file or directory, uses the chmod function,


The chmod takes an octal number for mode and a list of filenames.

Ex.
chmod (0666, fred, barney); #makes files fred and barney
read/write for all

The value 0666 is read/write for user, group and other.

The return value of chmod is the number of files successfully adjusted.


Octal Value & Meanings;
4
Read
2
Write
1
Execute

Modifying Permissions

The chown function is used to change the owner and group and works in the
same manner as unlink and chmod.
The chown takes as parameters for UID, GID, and files names.

Ex.
Chown (1234, 35,fred,barney); # changes files fred and
# barney to belong to
# uid 1234 of group 35.

Packages and Modules

References
References are like Pointers in C
A Reference is a Scalar Variable that holds the address of a Variable

or Function
There are various ways to Create References in Perl
Using the \ operator
Creating a reference to an unnamed Array
Creating a reference to an unnamed Hash
Creating a reference to an unnamed Subroutine
Creating a reference to an unnamed Scalar

References
USING THE \ OPERATOR

$a = 10;
$scalar_ref = \$a;

# Gives the Reference to $a.

@arr = (1, 2, 3, 4);


$array_ref = \@arr; # Gives the Ref to Array
%hash = ("Apples", 10, "Bananas", 20);
$hash_ref = \%hash; # The Ref itself is still a Scalar
$sub_ref = \&mySub;
# Gives a Ref to mySub.
sub mySub {
print("Hello World");
}

References
CREATING A REFERENCE TO AN UNNAMED ARRAY
$array_ref = [1, 2, 3, 4];
$new_ref = []
# Ref to an empty Array
CREATING A REFERENCE TO AN UNNAMED HASH
$hash_ref = {1, 2, 3, 4, 5};
$new_ref = {}
# Ref to an empty Hash

References
CREATING A REFERENCE TO AN UNNAMED SUBROUTINE
$sub_ref = sub # Same here. No SubName given. Just a Ref.
{
print "Hello World" ;
}
CREATING A REFERENCE TO AN UNNAMED SCALAR
$scalar_ref = \"Hello World";
THIS IS A CONSTANT IN PERL

Using a Reference
Scalar
$a = 10;
$scalar_ref = \$a;
print($$scalar_ref);
Array
@arr = (1, 2, 3, 4);
$array_ref = \@arr;
print(@$array_ref);
The First @ stands for De-reference as an ARRAY

Using a Reference
Hash
%hash = ("Apples", 10, "Bananas", 20);
$hash_ref = \%hash;
print(%$hash_ref);
The First % stands for "Deref as a HASH
Subroutine
$sub_ref = \&mySub;
# Gives a Ref to mySub
&$sub_ref("Hello", "World");
# The & stands for "Deref as CODE
sub mySub {
($a, $b) = @_;
# 2 parameters
print("$a, $b");
}

Objects
Objects are Special Modules and Modules in turn are Reusable

Packages
The idea of Packages comes from the implementation of Symbol
Tables in Perl
A Symbol Table of a Perl Program contains a list of all Global
Variables and 'Functions' Declared or Defined in the Program

Symbol Table
Each Package in Perl has its own Private Symbol Table
The Default Package is main and its Symbol Table is available

using the Hash %main::


(%packagename::)
Change the current package using the statement
package package_name;

This shifts the context of your application to the new package and

program can access only the symbols available in package_name


To switch back to main package use
package main;

Symbol Table
foreach $element(keys %main::)
{
print "$element \n";
# Will print all the Symbols available.
}

Creating Modules
Reusable Packages are Created through Perl Modules
Perl Modules are .pm files with the same name as the Package

package myPackage;
SAVE THE FILE AS myPackage.pm AND YOU HAVE A PERL MODULE
In this module you can create Variables and Subroutines which can then be

used by another Perl Program

Set an environment variable PERL5LIB to locate PERL modules

Creating Modules
# myModule.pm
package myModule;
require Exporter; # Need Exporter Module to help Export Symbols
@EXPORT = ('$sally', 'mySub'); # No var Substitution here!!
@EXPORT_OK = ('$harry');
@ISA = (Exporter);
$sally = 'Met Harry';
$harry = 'Met Sally';
sub mySub {
print("When Harry met Sally");
}

Using the Module


In Perl a Module can be used in two ways
The Module Exports Symbols (Functions and Variables) to the outside world
The outside world use's OR require's the Module
use myModule;
print $sally;
&mySub;

# This one's been EXPORTED TO %main::


# This one too...

Using the Module


require myModule;
myModule->import('$sally', '$harry', 'mySub');
print $sally; # Explicitly Imported.
&mySub; # Explicitly Imported.
print $harry; # Explicitly Imported.

Using the Module


If your Module does not Export any Symbol then the User of your

Module can still use the Subroutines in your Module using the
Module::subname() syntax
Moment you say require Module OR use Module
A reference of the Module's Symbol Table (Module::) is added to your

Symbol Table

Hence Exporting and Importing is relevant only for ensuring that

we don't have to type module::symbol and can use symbol directly

Database Access

Database Access Introduction


Three ways of Connecting to Database
Using DAO object model
Using ODBC
Using OLEDB Provider

Data Access Objects (DAO)


use DBI;

# Module DBI

$ConnObj = DBI->connect('dbi:ODBC:source_db', 'sa', '');


$sth = $ConnObj->prepare("SELECT au_fname, au_lname FROM authors
WHERE au_lname LIKE ?");
$sth->bind_param(1, 'R%');
$sth->execute();
while(@array = $sth->fetchrow_array()) {
$fname = $array[0];
$lname = $array[1];
print "$fname, $lname \n";
}

Open Database Connectivity (ODBC)


use Win32::ODBC;
$db = Win32::ODBC->new("DSN=source_db;UID=sa;PWD=");
$sql=SELECT au_fname, au_lname FROM authors WHERE au_name=" .
$name;
$db->Sql($sql); # Sql Function Executes the SELECT
while($db->FetchRow()) {
$fname = $db->Data('au_fname');
$lname = $db->Data('au_lname');
print "$fname, $lname \n";
}
$db->Close();

Object Linking and Embedding (OLEDB)


use Win32::OLE;

# Module Win32::OLE

$ConnObj = Win32::OLE->new("ADODB.Connection");
# Create Object
$ConnObj->open(source_db_conn');
# Open Connection
$sql = "SELECT au_fname, au_lname FROM authors";
$RstObj= $ConnObj->Execute($sql);
while(!$RstObj->EOF()) {
$fname = $RstObj->Fields('au_fname')->Value();
$lname = $RstObj->Fields('au_lname')->Value();
print "$fname, $lname \n";
$RstObj->MoveNext();
}
$RstObj->close();
$ConnObj->close();

Installing CPAN Modules


First, identify the module to be installed by searching for it in

CPAN site using CPAN Index


Using the PPM command, install the module
Verify using PPM GUI interface

Search for Module in CPAN

PPM GUI

PPM Command to Install a Module

Verify Module After Installation

Common Gateway Interface (CGI)


Chapter 12

Introduction

HTTP server works on Request/Response mechanism


Normally, HTTP server sends the document (Response) to the

Browser when the client requests for it


To execute the file on the server, instead of just sending,
application extension (MIME type) must be registered
For CGI applications, MIME type is application/cgi

Environment

Common Gateway Interface is a protocol not a language


As part of the CGI Protocol, the Server will set up all CGI

Variables as Environment Variables


STDOUT of the CGI Application is redirected to the Socket of the
Client's Connection
In the CGI application:
%ENV contains information about the Request
Use print function to send the Response to the Browser

Request

The client can use GET or POST method to submit the Request
The form contents will be appended to URL in Get method
The form contents will be sent as stream to the server in POST method
If the Request method is POST then the Query String is not

available in the Env Variable; instead it is available at the STDIN

Request

For GET method:


If the URL for a file request is
http://user45/Test.pl?fname=Ganesh&age=26&telno=234234
$ENV{QUERY_STRING} => fname=Ganesh&age=26&telno=234234
$ENV{REQUEST_METHOD} => GET/POST

For POST method:


Use read(STDIN, $noofbytes, $qs, 0); to extract the query string information since

query string does not terminate with \n


You can get the no of bytes from CONTENT_LENGTH ENV Variable

Response

CGI program must generate the output in HTML format so that

Browser can interpret and print

Every Response has two parts


The response header (which must compulsorily have at least one header
"Content-type:")
Followed by a blank line (this separates the header from the body)
The HTML Body (content of HTML Document)

Example

print Content-type : text/html\n\n;


print <HTML><BODY>;
print $ENV{QUERY_STRING} ;
print $ENV{REQUEST_METHOD} ;
@arr = %ENV ;
foreach $val(@arr){
print $val, \t ;
}
print </body></html> ;

Thank You.

Potrebbero piacerti anche