1 / 17

Using Perl Modules

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.

alden
Download Presentation

Using Perl Modules

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Using Perl Modules

  2. 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

  3. 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

  4. 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”;

  5. 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’

  6. 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”

  7. 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

  8. 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.

  9. 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”;

  10. 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

  11. 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.

  12. 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’};}

  13. 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];}

  14. 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’;

  15. 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

  16. 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

  17. 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;}

More Related