An Introduction To Perl PDF
An Introduction To Perl PDF
• Descendant of
– C, Lisp, shell scripting (sh), …
documentation: perldoc.perl.org
control an application
typically not strongly typed
typically interpreted
scripts can be created, modified, executed at run-time
• Many features
– "There's more than one way to do it"
– "The Swiss Army Chainsaw of Programming Languages"
– "No unnecessary limits”
Perl Features
scalars,
arrays,
and hashes.
Scalars
A scalar represents a single value, begins with a $ sign
1.print $animal;
2.print "The animal is $animal\n";
3.print "The square of $answer is ", $answer * $answer, "\n“;
Arrays
An array represents a list of values, begin with @ sign
1.my @animals = ("camel", "llama", "owl");
%sequences=
(
'Vipin'=>'ATGC','Sachin'=>'AAGC','Sudeep'=>'AAAA',
);
– @ array
my @names = (“Vipin”,”Sachin”,”Sudeep”);
my @sequences= (“ATGC”,”AAGC”,”AAAA”);
print “$name[1],$sequence[1]”;
– % hash
my %sequences = (‘Vipin’ => ‘ATGC’, ‘Sachin’ => ‘AAGC’, ‘Sudeep’
=>’AAAA’);
• Variables "interpolate" into strings - print "$DNA"; #prints the value of $DNA
Operators
• If-not-clause
unless (condition) {... #same as if (!
condition)
}
Loops
• while and until loops Loops are used
while (condition) {...
} when a part of the
until (condition) {... program is to be
}
executed multiple
• for loop
times – until a
for ($i=0; $i <= $max; $i++) {... condition is
}
satisfied – 3 loops
• foreach loop in Perl
foreach (@array) { # default variable $_ contains an
element
print "This element is $_\n";
}
Split – converting a string into an array
split /PATTERN/,EXPR
FILE HANDLING –
Reading from a file
In Perl the file is read through a File Handle
File handle is a sort of buffer, from where the file can be read, written or
appended.
It is always good to confirm whether such a file exists and to display a message if it
does not
print FH “………….”;
Regular Expressions
A regular expression is a pattern - a template - to be matched
against a string.
Match m//
Substitute s///
Translate tr///
s/PATTERN/REPLACEMENT