190 likes | 438 Views
Using Perl Modules. What are Perl modules?. Modules are collections of subroutines Encapsulate code for a related set of processes End in .pm so Foo.pm would be used as Foo Can form basis for Objects in Object Oriented programming. Using a simple module.
E N D
What are Perl modules? • Modules are collections of subroutines • Encapsulate code for a related set of processes • End in .pm so Foo.pm would be used as Foo • Can form basis for Objects in Object Oriented programming
Using a simple module • List::Util is a set of List utilities functions • Read the perldoc to see what you can do • Follow the synopsis or individual function examples
List::Util use List::Util;my @list = 10..20;my $sum = List::Util::sum(@list);print “sum (@list) is $sum\n”; use List::Util qw(shuffle sum);my $sum = sum(@list);my @list = (10,10,12,11,17,89);print “sum (@list) is $sum\n”;my @shuff = shuffle(@list);print “shuff is @shuff\n”;
Module naming • Module naming is to help identify the purpose of the module • The symbol :: is used to further specify a directory name, these map directly to a directory structure • List::Util is therefore a module called Util.pm located in a directory called ‘List’
Module Naming (contd.) • Does not require inheritance or specific relationship between modules that all start with the same directory name • Case MaTTerS! List::util will not work • Read more about a module by doing “perldoc Modulename”
Modules as objects • Modules are collections of subroutines • Can also manage data • Multiple instances can be created (instantiated) • Can access module routines directly on object
Object creation • To instantiate a module call ‘new’ • Sometimes there are initialization values • Objects are registered for cleanup when they are set to undefined (or when they go out of scope) • Methods are called using -> because we are dereferencing object.
Simple Module as Object example • #!/usr/bin/perl -wuse strict;use MyAdder;my $adder = new MyAdder;$adder->add(10);print $adder->value, “\n”;$adder->add(10);print $adder->value, “\n”;my $adder2 = new MyAdder(12);$adder2->add(17);print $adder2->value, “\n”;my $adder3 = MyAdder->new(75);$adder3->add(7);print $adder3->value, “\n”;
Writing a module: instantiation • Starts with package to define the module name • multiple packages can be defined in a single module file - but this is not recommended at this stage • The method name new is usually used for instantiation • bless is used to associate a datastructre with an object
Writing a module: subroutines • The first argument to a subroutine from a module is always a reference to the object - we usually call it ‘$self’ in the code. • This is an implicit aspect Object-Oriented Perl • Write subroutines just like normal, but data associated with the object can be accessed through the $self reference.
Writing a module package MyAdder;use strict;sub new { my ($package, $val) = @_; $val ||= 0; my $obj = bless { ‘value’ => $val}, $package; return $obj; } sub add { my ($self,$val) = @_; $self->{’value’} += $val; }sub value { my $self = shift; return $self->{’value’};}
Writing a module II (array) package MyAdder;use strict;sub new { my ($package, $val) = @_; $val ||= 0; my $obj = bless [$val], $package; return $obj; } sub add { my ($self,$val) = @_; $self->[0] += $val; }sub value { my $self = shift; return $self->[0];}
Using the module • Perl has to know where to find the module • Uses a set of include paths • type perl -V and look at the @INC variable • Can also add to this path with the PERL5LIB environment variable • Can also specify an additional library path in script use lib ‘/path/to/lib’;
Your script using adder Assuming you put the module in your homedir/lib/perl #!/usr/bin/perl -wuse lib “$ENV{HOME}/lib/perl”;# can also do this using the Env # module to import all the # environmnet vars# use Env;# use lib “$HOME/lib/perl”; my $adder = MyAdder->new(18);print $adder->add(-7); # add returned the sum as well
Using a module as an object • LWP is a perl library for WWW processing • Will initialize an ‘agent’ to go out and retrieve web pages for you • Can be used to process the content that it downloads
LWP::UserAgent #!/usr/bin/perl -wuse strict;use LWP::UserAgent;my $url = 'http://us.expasy.org/uniprot/P42003.txt';my $ua = LWP::UserAgent->new(); # initialize an object$ua->timeout(10); # set the timeout valuemy $response = $ua->get($url); if ($response->is_success) {# print $response->content; # or whatever if( $response->content =~ /DE\s+(.+)\n/ ) { print "description is '$1'\n"; } if( $response->content =~ /OS\s+(.+)\n/ ) { print "species is '$1'\n"; }}else { die $response->status_line;}