Scripting Languages Advanced Perl: Course: 67557 Hebrew University Lecturer: Elliot Jaffe - הפי טוילא
Scripting Languages Advanced Perl: Course: 67557 Hebrew University Lecturer: Elliot Jaffe - הפי טוילא
Advanced Perl
Course: 67557
Hebrew University
Lecturer: Elliot Jaffe – אליוט יפה
The principals of a programmer
• Laziness is the mother of invention
• Impatience makes us automate repetitive
boring tasks
• Hubris makes us remember to write code
that other people can read
Perl
• -w turns on warnings
– Warn about lots of questionable PERL
constructs
• use strict
– don’t allow undefined global variables
The list expansion problem
Remember this?
$a = 1;
$b = 2;
@c = (3,4);
sub MyFunction {
($arg1, @list, $arg2) = @_;
}
MyFunction($a, @c, $b);
References
• Similar to pointers in C
• Are treated as scalar values
$readable = -r "myfile.txt";
/^Subject:/
$_ =~ /^Subject:/
tr/a-z/A-Z/
$_ =~ tr/a-z/A-Z/
chomp
chomp($_)
$_ (2)
• Where Perl implies $_
– Pattern matching operations m//, s///, and tr///
when used without an =~ operator.
– Default iterator variable in a foreach loop if no
other variable is supplied.
– Implicit iterator variable in the grep() and map
() functions.
Map
map BLOCK LIST or map EXPR, LIST
• Apply the BLOCK to each element in LIST
%hash = map { getkey($_) => $_ } @array;
• is just a funny way to write
%hash = {};
foreach $_ (@array) {
$hash{getkey($_)} = $_;
}
Grep
grep BLOCK LIST or grep EXPR, LIST
• Apply the BLOCK to each element in LIST
returning only elements which were TRUE
@foo = grep(!/^#/, @bar);
# weed out comments
or equivalently,
package MyPackage;
our ($bar);
sub foo {};
package main;
MyPackage::foo($MyPackage::bar);
Modules
use Module; or require module;
– Same implementation
Consider this
• Dynamic scoping
– Search for the variable at run-time
sub fred {
return $a++;
}
• Where does $a come from?
What happens to $a
{
my ($a) = 0;
sub fred {
return $a++;
}
}
print defined($a);
Iterators
{
my $a = 0;
sub initCounter { $a = @_; }
sub nextCounter { return $a++; }
}
while (100 < ($b = nextCounter())){
# do something
}
Fibonachi
{
my $prev = 0;
my $current = undef;
sub fibonachi {
if (!defined($current)) {
$current = 1;
return 0;
}
my $tmp = $current + $prev;
$prev = $current;
$current = $tmp;
return $tmp;
}
}
Iterator Pattern
• create_iterator()
use Data::Dumper;
print Dumper($foo);
LWP
• The World Wide Web library for Perl
• Supports HTTP, HTTPS, FTP, News,
Gopher
# Create a request
my $req = HTTP::Request->new(POST => 'http://search.cpan.org/search');
$req->content_type('application/x-www-form-urlencoded');
$req->content('query=libwww-perl&mode=dist');
XYZ XYZ
Driver Database
Perl DBI
Scripts API DBI
Oracle Oracle
Driver Database
DBI
use DBI;
while( $sth->fetch() ) {
print "$name, $title, $phone\n";
}
$sth->finish();
$dbh->disconnect();
Strange modules
• SCUBA::Table::NoDeco
– Compute no-decompression limits on repetitive
dives
• Games::Backgammon
– Play Backgammon (Sheysh-Beysh)
• LEGO::RCX
– Control you Lego Mindstorm RCX computer
Object Oriented Perl
• Classes are Packages, Methods are functions
package Person;
sub print {
my ($self) = shift;
printf( "Name:%s %s\n\n“,
$self->firstName,
$self->lastName );
}
Bless me Father
#constructor
sub new {
my $self = {
_firstName => undef,
_lastName => undef,
_ssn => undef,
_address => undef };
bless $self, 'Person';
return $self;
}
Accessors
#accessor method for Person first name
sub firstName {
my ( $self, $firstName ) = @_;
$self->{_firstName} = $firstName
if defined($firstName);
return $self->{_firstName};
}
Calling
use Person;
$p1 = new Person;
$p1->firstName = “Elliot”;
$p1->print;
Making babies
# class Employee
package Employee;
use Person;
use strict;
our @ISA = qw(Person); # inherits from Person