Perl Learning
-
Upload
antony-pradeep -
Category
Documents
-
view
220 -
download
0
Transcript of Perl Learning
-
8/9/2019 Perl Learning
1/66
Practical Extraction and ReportLanguage
Motto: "theres more than one way to do it"
Antony Muthu Pradeep A,
Test Engineer,
Tessolve,
India.
-
8/9/2019 Perl Learning
2/66
Introduction
Perl is a general purpose programming languagedevelopedin 1987by Larry Wall. It has become the language of choicefor WWW development, text processing, Internet services,mail filtering, graphical programming, and every other taskrequiring portable and easily-developed solutions.
Perl is interpreted:This means that as soon as you write yourprogram, you can run it -- theres no mandatory compilationphase. The same Perl program can run on Unix, Windows,NT, Mac OS, DOS..
-
8/9/2019 Perl Learning
3/66
Perl is collaborative:The CPAN software archivecontains free utilities written by the Perl community, soyou save time.
Perl is free:Unlike most other languages, Perl is notproprietary. The source code and compiler are free,andwill always be free.
Perl is fast:The Perl interpreter is written in C, andmore than a decade of optimisations have resulted in afast executable.
-
8/9/2019 Perl Learning
4/66
Where we use Perl?
Text processing: Perls original main use was text processing. It isexceedingly powerful in this regard, and can be used to
manipulate textual data, reports, email, news articles, logfiles or just about any kind of text with great ease.
System administration tasksSystem administration is made easy with Perl. Its
particularly useful for tying together lots of smaller scripts,working with file systems, networking, and so on.
-
8/9/2019 Perl Learning
5/66
web programming:Since HTML is just text with built-in formatting, Perlcan be used to process and generate HTML. For many yearsPerl was the de facto language for web development, and is
still very heavily used today.
Database interaction:Perls DBI module makes interacting with all kinds of
databases --- from Oracle down to comma-separated variablefiles --- easy and portable. Perl is increasingly being used towrite large database applications, especially those whichprovide a database backend to a website.
-
8/9/2019 Perl Learning
6/66
Creating and running a Perl program
Open Perl IDE is used for writing and debug the
Perl script.
Script Area
-
8/9/2019 Perl Learning
7/66
Basic SyntaxBasic 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
-
8/9/2019 Perl Learning
8/66
output
compile
Run
-
8/9/2019 Perl Learning
9/66
Perl have three types of data types, they are
Perl Data Types
Data Type Starts with Pronounced
Scalar $Array @ atHash % percentage
dollor
-
8/9/2019 Perl Learning
10/66
Scalar Variables:
Scalar variables are simple variables containing onlyone 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;
-
8/9/2019 Perl Learning
11/66
Array Variables:Arrays contain a list of scalar data (singleelements). A list can hold an unlimited number ofelements. 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 thewombat", -300);
-
8/9/2019 Perl Learning
12/66
Using qw// to populate arrays: If youre working with lists and arrays a lot, youmight find that it gets very tiresome to be typing so manyquotes and commas. Lets 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 islonger,then????we can use qwmy @fruits = qw/apples oranges guavas mango grapes/;
-
8/9/2019 Perl Learning
13/66
Shift/unshift and push/pop functionsShift/unshift and push/pop functions shift() deletes the firstelement of the array and returns
that value
unshift() adds a new element or elements to thebeginningarray
pop() deletes the lastelement of the array and returnsthat value
push() adds an element or elements to the endof thearray
-
8/9/2019 Perl Learning
14/66
Hash:Hashes are complex lists with both a key and a
value part for each element of the list. We define a hashusing 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";
-
8/9/2019 Perl Learning
15/66
#!/usr/bin/perl -w
# this is my first program
print "What is your name? ";$name = ;
chomp ($name);print "Hello, $name!\n";
Simple Example:
-
8/9/2019 Perl Learning
16/66
Escaping Characters:
when you would like to print a dollar sign(specialcharacter) 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";
-
8/9/2019 Perl Learning
17/66
Assignment operators
a = a % ba %= b%=
a = a / ba /= b/=
a = a * ba *= b*=
a = a ba -= b-=
a = a + ba += b+=
a = ba = b=
Same asExampleOperator
-
8/9/2019 Perl Learning
18/66
Logical operatorsLogical operators
Logical NOT!
Logical OR||
Logical AND&&
DoesOperator
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
-
8/9/2019 Perl Learning
19/66
Logical operators examplesLogical 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
-
8/9/2019 Perl Learning
20/66
Numerical comparisonNumerical comparison
< = > returns -1, 0,or 1 if the left sideis less than, equalto, or greater than
the right side
Other operators re-turn TRUE if the
comparison is true,otherwise it will be
blank!Less than orequal?
=
Less than?Left-to-right comp< = >
Not equal?!=
Is equal?==ComparisonOperator
-
8/9/2019 Perl Learning
21/66
String comparisonString comparison
Comparison/ActionOperator
get ASCII num of charord(char)
get char for ASCII numchr(num)
convert to lower caselc(string)
convert to upper caseuc(string)
repeatx
concatenation.
-1, 0, or 1, dependingcmp
less than?Lt
greater than?gt
not equal?ne
is equal?eq
-
8/9/2019 Perl Learning
22/66
String comparison exampleString 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
-
8/9/2019 Perl Learning
23/66
Operators Operation Performed
++,-- Auto Increment and Auto Decrement
-,~,! Operators with single operand
* * Exponentiation
=~,!~ Pattern matching operators
*,/,%,x Multiplication,Division,Remainder,RepetitionAddition, Subtraction, Concatenation+,-, .
Operator precedence
-
8/9/2019 Perl Learning
24/66
Loops and conditions
-
8/9/2019 Perl Learning
25/66
If the test expression is true, then execute thestatement(s) following
#!/usr/bin/perl$major = chemistry;
if ($major eq chemistry) {print Welcome, chemistry student!\n;
}if ($major ne chemistry) {
print Youre not a chemistry student.\n;print Why not?\n;}# note: need the curly braces
IF statementsIF statements
-
8/9/2019 Perl Learning
26/66
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
IF/ELSE statements
-
8/9/2019 Perl Learning
27/66
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";}
ELSIF statements
-
8/9/2019 Perl Learning
28/66
While loops test one expression for truth, and will keep runningthe loop as long as the expression returns true. While loops aregood for iterating over arrays or through lines in a file.
Eg: copy a file content into another file.
open(pat,output.txt);$temp=;
while($temp){
print out $temp;$temp=;;
}
File Name File Handler Permission
pat readout write
datalog.txtoutput.txt
condition(It executeup to end of the file).
While
-
8/9/2019 Perl Learning
29/66
For StatementLoop (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
-
8/9/2019 Perl Learning
30/66
Foreach StatementThe foreach loop takes each element of the array and
uses it with your code.
Eg:
@name = ("Rose","Shan","Mani","Karthi");foreach $x (@name){print "$x\n";}
Output:RoseShanManiKarthi
First time of execution it assign
$x=Rose
second iteration it assign x=Shan
iteration going on till end of the array
Firstelement
of array
-
8/9/2019 Perl Learning
31/66
NEXT statementNEXT statement
Skip to next iteration of a loop Equivalent to Cs continue statement
for ($i=0; $i
-
8/9/2019 Perl Learning
32/66
LAST statementLAST statement
Skip out of loop and exit it completely Equivalent to Cs break statement
for ($i=0; $i
-
8/9/2019 Perl Learning
33/66
File Handling
-
8/9/2019 Perl Learning
34/66
Opening a fileOpening a file
Before reading from or writing into the file weneed to open that file first.
To open a file call the library function open
The syntax for open library function isopen(filehandler, filename);
while calling open function, we need to supply
two arguments:
--> filehandler represents the name that the perlinterpreter uses to refer to the file
--> filename represents the name of the file you want to
-
8/9/2019 Perl Learning
35/66
Chop( ) and chomp( ) operatorsChop( ) and chomp( ) operators
Chop() operatorused 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 thecharacter if it is a end-of-line character
Eg: $a=hello \n;
print chomp($a);
-
8/9/2019 Perl Learning
36/66
File access modesFile access modes Read mode:
Enables the program to read the existingcontents of the file but does not enable it towrite into the file
Write mode:
Destroys the current content of the file and
overwrites them with the output supplied by theprogram
Append mode:
Appends output supplied by the program
to the existing contents of the file
-
8/9/2019 Perl Learning
37/66
Reading a single line in a text fileReading 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, "
-
8/9/2019 Perl Learning
38/66
Reading a whole fileReading a whole file
To get all the lines, simply assign toan array variable
open(TXT, "
-
8/9/2019 Perl Learning
39/66
Writing to a text fileWriting 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 forwriting
print TXT "hello, testing!\n"; # write a line
print TXT "end of test.\n"; # write another line
close(TXT); # close file again
-
8/9/2019 Perl Learning
40/66
Appending to the text fileAppending to the text file
To append (add to the end of an existing file), usethe >> 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
-
8/9/2019 Perl Learning
41/66
Detecting read/write errorsDetecting read/write errors
If a file operation has an error, it typically returns an errormessage to the $! variable This example previews subroutinesopen(FP, "
-
8/9/2019 Perl Learning
42/66
DieDie
The die function is used in Perl to stop the interpreter in case of anerror 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,
-
8/9/2019 Perl Learning
43/66
Files in a directoryFiles 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";
}
-
8/9/2019 Perl Learning
44/66
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", "BigAvenue");
-
8/9/2019 Perl Learning
45/66
The join function takes a list of values and glues them togetherwith a glue string between each list element.
$bigstring = join($glue,$list); # The glue is not a regularexpression but just acharacter or strings
Eg:
@mylist = qw(mani shan ramesh);$outline =join(":",@mylist);print "$outline";
output:
mani:shan:ramesh
Join
-
8/9/2019 Perl Learning
46/66
Exercises:
1. Create a text file and and write 10 numbers in single roweg: 12,32,43,23,etc
read numbers from this file and create new file with
numbers are in column wise using perl.
12,32,43,54,76,15,26,76
12324354761526
row.txt column.txt
S b i
-
8/9/2019 Perl Learning
47/66
SubroutineSubroutine 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.
P i lPassing values
-
8/9/2019 Perl Learning
48/66
Passing valuesPassing values
Values are passed to a subroutine using a specialunderscore array @_
Use $_[0], $_[1], etc, to access each element
&print_args("One", "Two", "Three");
sub print_args{
$i = 1;foreach $arg(@_){
print "Argument $i is $arg.\n";$i++;}
}
Output:Argument 1 is OneArgument 2 is TwoArgument 3 is Three
R lR l
-
8/9/2019 Perl Learning
49/66
Return valuesReturn values
Subroutines can return values, like a function; in thisinstance, 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
-
8/9/2019 Perl Learning
50/66
-
8/9/2019 Perl Learning
51/66
Regular expressionsRegular 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 ina character string out
Regular expression is used for,
Complex string comparisons
-
8/9/2019 Perl Learning
52/66
=~ operator returns true if the pattern appears in the string
if $temp = Tessolve Services Pvt Ltd.,
then it returns True
$temp =~ /Tessolve/ PatternString
String Comparison
-
8/9/2019 Perl Learning
53/66
Some of the Example String are,
1. I am working in tessolve2. Tessolve is in Bangalore3. 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
-
8/9/2019 Perl Learning
54/66
Pattern Anchors
-
8/9/2019 Perl Learning
55/66
Pattern Matching Strings11 34time11 5609847:34time
47 90timeTime 47:34Am 5Am45
/\d\d\s\d\d/;
/\d\d[\s\:]\d\d/
/\w\w[\s\d]\d/
-
8/9/2019 Perl Learning
56/66
Eg:$string =~ m/^\S{1,8}\.\S{0,3}/;
Some match strings are,newfile.txtaa.mdi
-
8/9/2019 Perl Learning
57/66
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
S b tit ti
-
8/9/2019 Perl Learning
58/66
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
-
8/9/2019 Perl Learning
59/66
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 aserial number.$string =~ tr/[A,E,I,O,U,Y]/[1,2,3,4,5]/;
-
8/9/2019 Perl Learning
60/66
Exercise2:
Extract file name and modification date from the below example.
-
8/9/2019 Perl Learning
61/66
open("patt","
-
8/9/2019 Perl Learning
62/66
Output:
-
8/9/2019 Perl Learning
63/66
Write Excel
-
8/9/2019 Perl Learning
64/66
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 followingdownload path.http://search.cpan.org/CPAN/authors/id/J/JM/JMCNAMARA/Spre
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\
CreateExcel package
http://search.cpan.org/CPAN/authors/id/J/JM/JMCNAMARA/Spreadsheet-WriteExcel-2.37.tar.gzhttp://search.cpan.org/CPAN/authors/id/J/JM/JMCNAMARA/Spreadsheet-WriteExcel-2.37.tar.gz -
8/9/2019 Perl Learning
65/66
use Spreadsheet::WriteExcel;
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");
Createexample.xls
file
Excel package
Create new sheet withthe name hello inside
the example.xls file
Row Column
-
8/9/2019 Perl Learning
66/66
For More information about Perl Write Excel visit this site:http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-W
http://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-0.37/WriteExcel/doc/WriteExcel.htmlhttp://cpansearch.perl.org/src/JMCNAMARA/Spreadsheet-WriteExcel-0.37/WriteExcel/doc/WriteExcel.html