Vous êtes sur la page 1sur 66

Practical Extraction and Report Language

Motto: "there’s more than one way to do it"

Antony Muthu Pradeep A,


Test Engineer,
Tessolve,
India.
Introduction

Perl is a general purpose programming language developed


in 1987 by Larry Wall. It has become the language of choice
for WWW development, text processing, Internet services,
mail filtering, graphical programming, and every other task
requiring portable and easily-developed solutions.

Perl is interpreted: This means that as soon as you write your


program, you can run it -- there’s no mandatory compilation
phase. The same Perl program can run on Unix, Windows,
NT, Mac OS, DOS..
Perl is collaborative: The CPAN software archive
contains free utilities written by the Perl community, so
you save time.

Perl is free: Unlike most other languages, Perl is not


proprietary. The source code and compiler are free,and
will always be free.

Perl is fast: The Perl interpreter is written in C, and


more than a decade of optimisations have resulted in a
fast executable.
Where we use Perl?

Text processing:
Perl’s original main use was text processing. It is
exceedingly powerful in this regard, and can be used to
manipulate textual data, reports, email, news articles, log
files or just about any kind of text with great ease.

System administration tasks


System administration is made easy with Perl. It’s
particularly useful for tying together lots of smaller scripts,
working with file systems, networking, and so on.
web programming:
Since HTML is just text with built-in formatting, Perl
can be used to process and generate HTML. For many years
Perl was the de facto language for web development, and is
still very heavily used today.

Database interaction:
Perl’s DBI module makes interacting with all kinds of
databases --- from Oracle down to comma-separated variable
files --- easy and portable. Perl is increasingly being used to
write large database applications, especially those which
provide a database backend to a website.
Creating and running a Perl program

Open Perl IDE is used for writing and debug the


Perl script.

Script Area
Basic Syntax
 Put the following in the first line of the script
#!/usr/bin/perl ( optional)

 # is used for commenting the line

 Each line should end with a semicolon

 Save the program in any name with a .pl


extension
compile

Run

output
Perl Data Types
Perl have three types of data types, they are

Data Type Starts with Pronounced


Scalar $ dollor
Array @ at
Hash % percentage
Scalar Variables:
Scalar variables are simple variables containing only
one element. a string, a number, or a reference.

Eg:
my $name = "Arthur";
my $whoami = ’Just Another Perl Hacker’;
my $meaning_of_life = 42;
my $number_less_than_1 = 0.000001;
my $very_large_number = 3.27e17;
my $value= \$ref;
Array Variables:
Arrays contain a list of scalar data (single
elements). A list can hold an unlimited number of
elements. In Perl, arrays are defined with the at (@)
symbol

@days = ("Monday", "Tuesday", "Wednesday"); or


$days[0]=”monday”;
$days[1]=”Tuesday”;

my @magic_numbers = (23, 42, 69);


my @random_scalars = ("mumble", 123.45, "willy the
wombat", -300);
Using qw// to populate arrays:
If you’re working with lists and arrays a lot, you
might find that it gets very tiresome to be typing so many
quotes and commas. Let’s take fruit example:

my @fruits = ("apples", "oranges", "guavas", "mango",


"grapes");

We had to type the quotes character ten times, along with


four commas, and that was only for a short list. If your list is
longer,then????
we can use qw
my @fruits = qw/apples oranges guavas mango grapes/;
Shift/unshift and push/pop functions
 shift() deletes the first element of the array and returns
that value

 unshift() adds a new element or elements to the


beginning array

 pop() deletes the last element of the array and returns


that value

 push() adds an element or elements to the end of the


array
Hash:
Hashes are complex lists with both a key and a
value part for each element of the list. We define a hash
using the percent symbol (%).
special arrays with words as index.

Eg:
%ages = ('Martin' => 28, 'Sharon' => 35,'Rikke' => 29,);
print "Rikke is $ages{Rikke} years old\n";
Simple Example:
#!/usr/bin/perl -w
# this is my first program
print "What is your name? ";
$name = <STDIN>;
chomp ($name);
print "Hello, $name!\n";
Escaping Characters:
when you would like to print a dollar sign(special
character) rather than use one to define a variable. To do
this you must "escape" the character using a backslash
(\).
some of the special char are: . , ; $ # @ \ / * ' “ < >
Eg:
$string = "David paid \$4.34 for Larry\'s shirt.";
$email = "youremail\@youremail.com";
Assignment operators

Operator Example Same as


= a=b a=b
+= a += b a=a+b
-= a -= b a=a–b
*= a *= b a=a*b
/= a /= b a=a/b
%= a %= b a=a%b
Logical operators
Operator Does
&& Logical AND
|| Logical OR
! Logical NOT
•These logical operators are very similar to those in C

•Used with operands that have boolean values TRUE


and FALSE, or which can be converted to these values;
typically 1 means TRUE and 0 means FALSE
Logical operators examples
$x = 1; $y = 0;
# example of AND
$z = $x && $y;
print "$x && $y = $z\n";
# prints 1 && 0 = 0
# example of OR
$z = $x || $y;
print "$x || $y = $z\n";
# prints 1 || 0 = 1
# example of NOT
$z = !$y;
print "!$y = $z\n";
# prints !0 = 1
Numerical comparison
Operator Comparison  < = > returns -1, 0,
== Is equal? or 1 if the left side
!= Not equal? is less than, equal
to, or greater than
<=> Left-to-right comp the right side
> Greater?
< Less than?  Other operators re-
>= Greater or equal? turn TRUE if the
comparison is true,
<= Less than or otherwise it will be
equal? blank!
String comparison
Operator Comparison/Action
eq is equal?
ne not equal?
gt greater than?
Lt less than?
cmp -1, 0, or 1, depending
. concatenation
x repeat
uc(string) convert to upper case
lc(string) convert to lower case
chr(num) get char for ASCII num
ord(char) get ASCII num of char
String comparison example
$a = "hi";
$b = "hello";
$equal = $a eq $b;
print "$a eq $b = $equal\n";
$equal = $a ne $b;
print "$a ne $b = $equal\n"; # $equal=1
$compare = $a cmp $b;
print "$a cmp $b = $compare\n"; #$compare=-1
$compare = $b cmp $a;
print "$b cmp $a = $compare\n"; #$compare=1
Operator precedence
Operators Operation Performed
++,-- Auto Increment and Auto Decrement
-,~,! Operators with single operand
** Exponentiation
=~,!~ Pattern matching operators
*,/,%,x Multiplication,Division,Remainder,Repetition
+,-, . Addition, Subtraction, Concatenation
Loops and conditions
IF statements
− If the test expression is true, then execute the
statement(s) following

#!/usr/bin/perl
$major = “chemistry”;

if ($major eq “chemistry”) {
print “Welcome, chemistry student!\n”;
}
if ($major ne “chemistry”) {
print “You’re not a chemistry student.\n”;
print “Why not?\n”;
}
# note: need the curly braces
IF/ELSE statements
− Sometimes more convenient than just “IF” statements
#!/usr/bin/perl
$major = "chemistry";
if ($major eq "chemistry")
{
print "Welcome, chemistry student!\n";
}
else
{
print "You're not a chemistry student.\n";
print "Why not?\n";
}
# note: need the curly braces
ELSIF statements
− useful in picking one possibility out of a list of several
#!/usr/bin/perl
$grade = "F";
if ($grade eq "A") {
print "Excellent!\n";
}
elsif ($grade eq "B") {
print "Good work.\n";
}
elsif ($grade eq "C") {
print "Needs improvement.\n";
}
else {
print "I suggest you start coming to office hours.\n";
}
While
While loops test one expression for truth, and will keep running
the loop as long as the expression returns true. While loops are
good for iterating over arrays or through lines in a file.

Eg: copy a file content into another file.

open(pat,”<datalog.txt”); File Name File Handler Permission


open(out,”>output.txt”); datalog.txt pat read
$temp=<pat>; output.txt out write
while($temp)
{ condition(It execute
print out “$temp; up to end of the file).
$temp=<pat>;;
}
For Statement
Loop (repeatedly execute a statement block) until a given
condition is met

− Syntax:
for (initializer, test, increment/decrement)
{statement block}

Eg: find sum of 10 no's


$sum=0;
for($i=1;$i<11;$i++)
{
$sum=$sum+$i;
}
print $sum;
Foreach Statement
The foreach loop takes each element of the array and
uses it with your code.

Eg: First
element
@name = ("Rose","Shan","Mani","Karthi"); of array
foreach $x (@name)
{ ●First time of execution it assign

print "$x\n";
} $x=”Rose”
Output: ●second iteration it assign x=“Shan”
Rose
●iteration going on till end of the array
Shan
Mani
Karthi
NEXT statement
• Skip to next iteration of a loop
• Equivalent to C’s “continue” statement

for ($i=0; $i<3; $i++)


{
if ($i = = 1) { next }
print "i = $i\n";
}
# prints the following:
i=0
i=2
LAST statement
• Skip out of loop and exit it completely
• Equivalent to C’s “break” statement

for ($i=0; $i<3; $i++)


{
if ($i == 1) { last }
print "i = $i\n";
}

# prints the following:


i=0
File Handling
Opening a file
 Before reading from or writing into the file we
need to open that file first.
 To open a file call the library function “open”

 The syntax for open library function is

open(filehandler, filename);
while calling open function, we need to supply
two arguments:
--> filehandler represents the name that the perl
interpreter uses to refer to the file
--> filename represents the name of the file you want to
Chop( ) and chomp( ) operators
 Chop() operator
used to remove the last character from the string
Eg: $a=”hello”;
print chop($a);
#it prints hell by terminating the last character
 Chomp() operator

Also similar to chop() but only removes the


character if it is a end-of-line character
Eg: $a=”hello \n”;
print chomp($a);
File access modes
 Read mode:
Enables the program to read the existing
contents of the file but does not enable it to
write into the file
 Write mode:

Destroys the current content of the file and


overwrites them with the output supplied by the
program
 Append mode:

Appends output supplied by the program


to the existing contents of the file
Reading a single line in a text file
 Use “open” and “close” functions
 Need a “file handle” to represent the file
 Use equality operator to read a line or an array of (all) lines

# Note: file random.txt must be in same directory, or else


# must specify an absolute path

open(TXT, "<random.txt"); # open the file for reading


$line = <TXT>; # get the first line(note scalar)
close(TXT); # close file again

print "The first line of the file is: $line\n";


Reading a whole file
 To get all the lines, simply assign <filehandle> to
an array variable

open(TXT, "<random.txt"); # open the file


for reading
@lines = <TXT>; # get all the lines
close(TXT); # close file again

print "The file contains:\n";


print @lines;
Writing to a text file
 Use the > symbol in front of the filename to write,
instead of < to read

open(TXT, ">written.txt"); # open the file for


writing
print TXT "hello, testing!\n"; # write a line
print TXT "end of test.\n"; # write another line
close(TXT); # close file again
Appending to the text file
 To append (add to the end of an existing file), use
the >> symbol before the filename instead of >

open(TXT, ">>written.txt"); # open the file for writing

print TXT "Add a line!\n"; # write an additional line

close(TXT); # close file again


Detecting read/write errors
• If a file operation has an error, it typically returns an error
message to the $! variable
• This example previews subroutines
open(FP, "<junk.txt") || &pr_error($!);
@lines = <FP>;
close(FP);
foreach $line(@lines)
{
print "$line";
}
sub pr_error
{
print "Received error on opening file.\n";
print "Error message: $_[0]\n";
exit;
}
Die
 The die function is used in Perl to stop the interpreter in case of an
error and print a meaningful error message.
 A special variable in Perl $! Is always set to the error message of the
last requested operation of the system(such as disl i/o).
 Always used in a string context

Example:
open(MYFILE,”<mytext.txt”) || die print “Can not open because $!\n”;

$!
Files in a directory
• Can get all the files in a given directory using the opendir()
function

opendir(CDIR, "."); # gives current directory


@filenames = readdir(CDIR); # get all the filenames
@filenames = sort(@filenames); # sort them!
closedir(CDIR);
foreach $filename(@filenames)
{
print "$filename\n";
}
Split
split is used to split up the string and place it in an array

$info = "Caine:Michael:Actor:14, Leafy Drive";


@personal = split(/:/, $info);

output: @personal = ("Caine", "Michael", "Actor", "14,


Leafy Drive");

$_ = "Capes:Geoff::Shot putter:::Big Avenue";


@personal = split(/:+/);

Output: @personal = ("Capes", "Geoff","Shot putter", "Big


Avenue");
Join
The join function takes a list of values and glues them together
with a glue string between each list element.

$bigstring = join($glue,$list); # The glue is not a regular


expression but just a
character or strings
Eg:

@mylist = qw(mani shan ramesh);


$outline =join(":",@mylist);
print "$outline";

output:
mani:shan:ramesh
Exercises:
1. Create a text file and and write 10 numbers in single row
eg: 12,32,43,23,etc
read numbers from this file and create new file with
numbers are in column wise using perl.
row.txt column.txt

12,32,43,54,76,15, 12
26,76 32
43
54
76
15
26
Subroutine
• Subroutines are called using the name of the subroutine
preceded by an ampersand
&name
Eg:
print "The start of the main body.\n";
&great_perl;
print "The end of the main body.\n";
sub great_perl
{
print "From the subroutine; PERL is great!\n";
}
# The start of the main body.
# From the subroutine; PERL is great!
# The end of the main body.
Passing values
• Values are passed to a subroutine using a special
“underscore array” @_
• Use $_[0], $_[1], etc, to access each element

&print_args("One", "Two", "Three");


sub print_args
{
$i = 1;
foreach $arg(@_) Output:
{ Argument 1 is One
print "Argument $i is $arg.\n"; Argument 2 is Two
$i++; Argument 3 is Three
}
}
Return values
• Subroutines can return values, like a function; in this
instance, we do not use the & prefix

$x = 5;
$y = square($x);
print "$x squared is $y\n";

sub square
{
return $_[0] * $_[0];
}

Output:
5 squared is 25
Regular expressions
 Perl defines a special operators that test whether a
particular pattern appears in the character string

 Pattern is a sequence of characters to be searched for in


a character string out

Regular expression is used for,

Complex string comparisons


String Comparison

$temp =~ /Tessolve/ Pattern


String
− =~ operator returns true if the pattern appears in the string

if $temp = “Tessolve Services Pvt Ltd.,”

then it returns True


Some of the Example String are,

1. I am working in tessolve
2. Tessolve is in Bangalore
3. Tessolve

Pattern True Examples

/tessolve/ 1,2,3 true


/^tessolve/ 2,3 only true Begging of the string
/tessolve$/ 1,3 only true End of the string
/^tessolve$/ 3 only true Exact word
/tessolve/i 1,2 only true case sensitive
Pattern Anchors
Pattern Matching Strings
11 34time
/\d\d\s\d\d/;
11 56098
47:34time
/\d\d[\s\:]\d\d/ 47 90time
Time 47:34
Am 5
/\w\w[\s\d]\d/
Am45
Eg:
$string =~ m/^\S{1,8}\.\S{0,3}/;

Some match strings are,


newfile.txt
aa.mdi
String Selections
We can select a string using Regular expression

( ) is used for grouping and to store variable.

Eg:
$temp =~ /(\d\d)\-(\d\d)\-(\d\d)/;

$1 $2 $3

If $temp value is “24-01-10” then


$1=24
$2=01
$3=10
Substitutions
Replace every "Bill Clinton" with an "Al Gore"
$string =~ s/Bill Clinton/Al Gore/;

Now do it ignoring the case of bIlL ClInToN.


$string =~ s/Bill Clinton/Al Gore/i;
Translations
Change all vowels to upper case vowels:
$string =~ tr/[a,e,i,o,u,y]/[A,E,I,O,U,Y]/;

Change everything to upper case:


$string =~ tr/[a-z]/[A-Z]/;

Change everything to lower case


$string =~ tr/[A-Z]/[a-z]/;

Change all vowels to numbers to avoid "4 letter words" in a


serial number.
$string =~ tr/[A,E,I,O,U,Y]/[1,2,3,4,5]/;
Exercise2:
Extract file name and modification date from the below example.
open("patt","<perl_exercise2.txt");
my $temp=<patt>;
while($temp)
{
if( $temp !~ /DIR/)
{
if( $temp =~ /.{28}(\d\d-\d\d-\d\d).{8}(.*)$/ )
{
my $date=$1; my $file=$2;
print "$file\t$date\n";
}
}
$temp=<patt>;
}
Output:
Write Excel
Write excel perl module is not come along with default Perl IDE.

We need to install it. Installation procedure is,

1. Download Perl Write Excel module from the following


download path.
http://search.cpan.org/CPAN/authors/id/J/JM/JMCNAMARA/Spread

2.Extract downloaded zip file.

3.copy Spreadsheet folder from the extracted file “Spreadsheet-


WriteExcel-2.37\lib”

3.Paste Spreadsheet folder into the following path,


c:\perl\site\lib\
Excel package Create
example.xls
use Spreadsheet::WriteExcel; file

my $workbook = Spreadsheet::WriteExcel->new("example\.xls");

$worksheet{Sheet1} = $workbook->add_worksheet('Sheet1');
$worksheet{hello} = $workbook->add_worksheet('hello');
$worksheet{Sheet1}->write(0 , 0 ,"new cell");

Row Column Create new sheet with


the name “hello” inside
the example.xls file
For More information about Perl Write Excel visit this site:
http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteE

Vous aimerez peut-être aussi