Professional Documents
Culture Documents
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.
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
Script Area
Basic Syntax
Put the following in the first line of the script
#!/usr/bin/perl ( optional)
Run
output
Perl Data Types
Perl have three types of data types, they are
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
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
#!/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.
− Syntax:
for (initializer, test, increment/decrement)
{statement block}
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
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
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
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
$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
1. I am working in tessolve
2. Tessolve is in Bangalore
3. Tessolve
Eg:
$temp =~ /(\d\d)\-(\d\d)\-(\d\d)/;
$1 $2 $3
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");