Академический Документы
Профессиональный Документы
Культура Документы
(PERL)
Introduction
• What is PERL?
– Practical Report and Extraction Language.
– It is an interpreted language optimized for scanning
arbitrary text files, extracting information from them, and
printing reports based on that information.
– Very powerful string handling features.
– Available on all platforms.
#!/usr/bin/perl
print “Good day\n”;
print “This is my first Perl program \n”;
• A powerful feature
– Variable names are automatically replaced by values when
they appear in double-quoted strings.
• An example:
$stud = “Rupak”;
$marks = 75;
print “Marks obtained by $stud is $marks\n”;
print ‘Marks obtained by $stud is $marks\n’;
– What do we see:
• If we need to do variable interpolation, use double
quotes; otherwise, use single quotes.
$Expense = ‘$100’;
print “The expenditure is $Expense.\n”;
$abc = 10;
$abc++;
$total- -;
$a = $b ** 10; # exponentiation
$a = $b % 10; # modulus
$balance = $balance + $deposit;
$balance += $deposit;
$num = 20;
print “Value of \$num is $num\n”;
print “<HTML>\n”;
print “<HEAD><TITLE>Test page </TITLE></HEAD>\n”;
print “<BODY>\n”;
print “<H2>This is a test document.<H2>\n”;
print “</BODY></HTML>”;
• Examples:
(10, 20, 50, 100)
(‘red', “blue", “green")
(“a", 1, 2, 3, ‘b')
($a, 12)
() # empty list
(10..20) # list constructor function
(‘A’..’Z’) # same, for lettere\s
– Use the quote words (qw) function, that uses space as the
delimiter:
@color = qw (red green blue black);
– Combination of above:
@allcolors = (“white”, @colors, “brown”);
@xyz = (2..5);
• Two ways:
$size = scalar @colors;
$size = @colors;
$first = $list[0];
$fourth = $list[3];
• Example:
print @colors;
# prints without spaces – redgreenblue
print “@colors”;
# prints with spaces – red green blue
$fname = “/home/isg/report.txt”;
open XYZ , $fname;
while (<XYZ>) {
print “Line number $. : $_”;
}
$fname = “/home/isg/report.txt”;
open XYZ, $fname or die “Error in open: $!”;
while (<XYZ>) {
print “Line number $. : $_”;
}
$out = “/home/isg/out.txt”;
open XYZ , “>$out” or die “Error in write: $!”;
for $i (1..20) {
print XYZ “$i :: Hello, the time is”,
scalar(localtime), “\n”;
}
$out = “/home/isg/out.txt”;
open XYZ , “>>$out” or die “Error in write: $!”;
for $i (1..20) {
print XYZ “$i :: Hello, the time is”,
scalar(localtime), “\n”;
}
$input = “/home/isg/report.txt”;
open IN, $input or die “Error in open: $!”;
while (<IN>) {
print;
}
close IN;
foreach (@ARGV) {
print “$_ \n”;
}
if (year == 2000) {
print “You have entered new millenium.\n”;
}
if (test expression) {
# if TRUE, do this
}
else {
# if FALSE, do this
}
if ($name eq ‘isg’) {
print “Welcome Indranil. \n”;
} else {
print “You are somebody else. \n”;
}
if ($flag == 1) {
print “There has been an error. \n”;
}
# The else block is optional
$your_choice = ‘ ‘;
$secret_word = ‘India’;
while ($your_choice ne $secret_word) {
print “Enter your guess: \n”;
chomp ($your_choice = <STDIN>);
}
Equal == eq
Not equal != ne
$_=‘Red:Blue:Green:White:255';
@details = split /:/, $_;
foreach (@details) {
print “$_\n”;
}
$new = join ' ', $x1, $x2, $x3, $x4, $x5, $x6;
$sep = ‘::’;
$new = join $sep, $x1, $x2, $w3, @abc, $x4, $x5;
$found = 0;
$_ = “Hello good morning everybody”;
$search = “every”;
foreach $word (split) {
if ($word eq $search) {
$found = 1;
last;
}
}
if ($found) {
print “Found the word ‘every’ \n”;
}
if ($_ =~ /every/) {
print “Found the word ‘every’ \n”;
}
if (/IIT K/) {
print “’IIT K’ is present in the string\n”;
{
if (/Kharagpur students/) {
print “This will not match\n”;
}
if ($string =~ m/day/) {
print “Match successful \n";
}
if ($string =~ /day/) {
print “Match successful \n";
}
if ($string =~ m@day@) {
print “Match successful \n";
}
if ($string =~ m[day[ ) {
print “Match successful \n";
}
if ($string =~ /d..s/) {
print "Found something like days\n";
}
if ($string =~ /\w\w\w\w\s/) {
print "Found a four-letter word!\n";
}
if ($string =~ /\d$/)
:: does string end with a digit?
if ($string =~ /\bGood\b/)
:: Does string contain the word “Good”?
$xyz =~ s/Lakshman/Bharat/;
$xyz =~ s/R\w+a/Bharat/;
$xyz =~ s/[aeiou]/i/;
$abc =~ s/\d+/12/;
$abc =~ s /\n$/ /;
$string =~ s/m/j/g;
# Ram -> Raj, Shyam -> Shyaj
$string =~ /^(\w+)/;
print $1, "\n"; # prints “Ra\n”
$string =~ /(\w+)$/;
print $1, "\n"; # prints “st\n”
$string =~ /^(\w+)\s+(\w+)/;
print "$1 $2\n";
# prints “Ramnd Shyam are honest”;
if ($string =~ /(\w)\1/) {
print "found 2 in a row\n";
}
if ($string =~ /(\w+).*\1/) {
print "found repeat\n";
}
if ($age =~ /\D/) {
print "$age is a non-number!\n";
}
$_ = 'abcdefghi';
/def/;
print "$\`:$&:$'\n";
# prints abc:def:ghi
%directory = (
“Rabi”, “258345”,
“Chandan”, “325129”,
“Atul”, “445287”,
“Sruti”, “237221”
);
%directory = (
Rabi => “258345”,
Chandan => “325129”,
Atul => “445287”,
Sruti => “237221”
);
@list = %directory;
$directory{Sruti} = “453322”;
$directory{‘Chandan’} ++;
• Calling a subroutine
– Use the ‘&’ prefix to call a subroutine.
&test_sub;
&gcd ($val1, $val2); # Two parameters
– However, the ‘&’ is optional.
sub welcome {
print "hi there\n";
}
sub welcome_name {
print "hi $name\n";
# uses global $name variable
}
$alpha = 15;
$beta = 25;
@c = return_alpha_and_beta;
# @c gets (5,6)
sub add_ver2 {
return $_[0] + $_[1];
# $_[0] and $_[1] are the first two
# elements of @_
}
sub find_total {
# adds all numbers passed to the sub
$sum = 0;
for $num (@_) {
$sum += $num;
}
return $sum;
}
$sum = 7;
$total = add_any (20, 10, -15);
# $total gets 15
sub add_any {
# local variable, won't interfere
# with global $sum
my $sum = 0;
#!/usr/bin/perl
use CGI qw (:standard);
#!/usr/bin/perl
print <<TO_END;
Content-type: text/html
#!/usr/bin/perl -wT
use CGI qw(:standard);
sub parse_form_data {
my %form_data;
my $name_value;
my @nv_pairs = split /&/, $ENV{QUERY_STRING};
if ( $ENV{REQUEST_METHOD} eq ‘POST’ ) {
my $query = “”;
read (STDIN, $query, $ENV{CONTENT_LENGTH});
push @nv_pairs, split /&/, $query;
}
$name =~ tr/+/ /;
$name =~ s/%([\da-f][\da-f])/chr (hex($1))/egi;
$value =~ tr/+/ /;
$value =~ s/%([\da-f][\da-f])/chr (hex($1))/egi;
$form_data{$name} = $value;
}
return %form_data;
}
#!/usr/bin/perl -wT
use CGI qw(:standard);
my %form_data;
foreach my $name (param() ) {
$form_data {$name} = param($name);
}
#!/usr/bin/perl -wT
use CGI qw(:standard);
print header;
print start_html (“Response to Guestbook”);
$ENV{PATH} = “/usr/sbin”; # to locate sendmail
open (MAIL, “| /usr/sbin/sendmail –oi –t”);
# open the pipe to sendmail
my $recipient = ‘xyz@hotmail.com’;
print MAIL “To: $recipient\n”;
print MAIL “From: isg\@cse.iitkgp.ac.in\n”;
print MAIL “Subject: Submitted data\n\n”;
close (MAIL);
print <<EOM;
<h2>Thanks for the comments</h2>
<p>Hope you visit again.</p>
EOM
print end_html;