Beruflich Dokumente
Kultur Dokumente
PERL Part IV
1
Associative Arrays
Introduction
2
Specifying Hash Array
3
Conversion Array <=> Hash
4
Modifying a Value
By simple assignment:
$directory{Sruti} = 453322;
$directory{Chandan} ++;
Deleting an Entry
5
Swapping Keys and Values
Why needed?
Suppose we want to search for a person,
given the phone number.
6
An Example
Subroutines
7
Introduction
A subroutine ..
Is a user-defined function.
Allows code reuse.
Define ones, use multiple times.
How to use?
Defining a subroutine
sub test_sub {
# the body of the subroutine goes here
# ..
}
Calling a subroutine
Use the & prefix to call a subroutine.
&test_sub;
&gcd ($val1, $val2); # Two parameters
However, the & is optional.
8
Subroutine Return Values
Example 1
$name = Indranil';
welcome(); # call the first sub
welcome_namei(); # call the second sub
exit;
sub welcome {
print "hi there\n";
}
sub welcome_name {
print "hi $name\n";
# uses global $name variable
}
9
Example 2
# Return a non-scalar
sub return_alpha_and_beta {
return ($alpha, $beta);
}
$alpha = 15;
$beta = 25;
@c = return_alpha_and_beta;
# @c gets (5,6)
Passing Arguments
10
Example 3
sub add_ver2 {
return $_[0] + $_[1];
# $_[0] and $_[1] are the first two
# elements of @_
}
Example 4
sub find_total {
# adds all numbers passed to the sub
$sum = 0;
for $num (@_) {
$sum += $num;
}
return $sum;
}
11
my variables
Example 5
$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;
for my $num (@_ ) {
$sum += $num;
}
return $sum;
}
12
Writing CGI Scripts in Perl
Introduction
#!/usr/bin/perl
use CGI qw (:standard);
13
Some of the functions included in the
CGI.pm (.pm is optional) are:
header
This prints out the Content-type header.
With no arguments, the type is assumed to be
text/html.
start_html
This prints out the <html>, <head>, <title> and
<body> tags.
Accepts optional arguments.
end_html
This prints out the closing HTML tags,
</body>, >/html>.
14
Example 1 (without using CGI.pm)
#!/usr/bin/perl
print <<TO_END;
Content-type: text/html
#!/usr/bin/perl -wT
use CGI qw(:standard);
15
Example 3: Decoding Form Input
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;
}
16
Using CGI.pm
Example 4
#!/usr/bin/perl -wT
use CGI qw(:standard);
my %form_data;
foreach my $name (param() ) {
$form_data {$name} = param($name);
}
17
Example 5: sending mail
#!/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;
18