Perl Module
A Perl module is a collection of reusable subroutines and variables.
Module Documentation
The Perl documentation can accessed from the perldoc command:
Example: The File::Basename Module
The File::Basename module is a standard module that is distributed with Perl.
When you load the File::Basename module, you get two new functions:
#!/usr/bin/perl use strict; use File::Basename;
my $path = '/ta/mie453/tutorial/tut7/index.htm'; my $base = basename($path); my $dir = dirname($path);
print "The directory is: $dir\n"; print "The base is: $base \n";
Example: The Env Module
The Env module is a standard module that provides access to the environment variables.
When you load it, it imports a set of scalars corresponding to your environment variables.
#!/usr/bin/perl
use strict; use Env;
print "My home is $HOME\n"; print "My path is $PATH\n"; print "My username is $USER\n";
More About Importing
Example: Importing Module (what's wrong?)
#!/usr/bin/perl
use strict; use Env qw($HOME $PATH);
print "My home is $HOME\n"; print "My path is $PATH\n"; print "My username is $USER\n";
By default the Env module will import all the environment variables. But if qw($HOME $PATH) tell Perl only to import these two variables.
To make this right:
#!/usr/bin/perl
use strict; use Env qw($HOME $PATH $USER);
print "My home is $HOME\n"; print "My path is $PATH\n"; print "My username is $USER\n";
Recall: qw($HOME $PATH $USER) is the abbreviated way for the list ('$HOME', '$PATH', '$USER').
Example: Importing Module
The CGI::Pretty module has a group called :standard, which imports a bunch of standard functions for creating HTML pages.
#!/usr/bin/perl # file: html.pl use strict; use CGI::Pretty qw(:standard);
print h1('This is a level one header');
print p('This is a paragraph.');
print p('Here is some',i('italicized'),'text.');
Demo for Installing modules in ActivePerl ...
Module Search Path
#!/usr/bin/perl
use lib '/home/lstein/lib';
use MyModule;
...
Syntax
package MODULE_NAME;
...
1;
A module begins with the keyword package and ends with 1;.
Example: MySequence (module, program)
package MySequence;
use strict; our $EcoRI = 'ggatcc';
sub reversec {
my $sequence = shift;
$sequence = reverse $sequence;
$sequence =~ tr/gatcGATC/ctagCTAG/;
return $sequence;
}
sub seqlen {
my $sequence = shift;
$sequence =~ s/[^gatcnGATCN]//g;
return length $sequence;
}
1;
Notice:
To use this module:
#!/usr/bin/perl use strict; use MySequence; my $sequence = 'gattccggatttccaaagggttcccaatttggg'; my $complement = MySequence::reversec($sequence);
print "original = $sequence\n"; print "complement = $complement\n";
Exporting Functions and Variables
Example: MySequence V2 (module, program)
To make your module export variables and/or functions like a "real" module, use the Exporter module.
package MySequence2; use strict; # this module is a subclass of the "Exporter" module. # so it inherit properties (functions and variables) the "Exporter" module. use base 'Exporter'; # to export the functions reversec and seqlen automatically. # i.e., they are the default functions our @EXPORT = qw(reversec seqlen); # it is OK for the user to import the $EcoRI variable, # but not to export it automatically # i.e., it is the optional variable our @EXPORT_OK = qw($EcoRI);
our $EcoRI = 'ggatcc';
sub reversec {
my $sequence = shift;
$sequence = reverse $sequence;
$sequence =~ tr/gatcGATC/ctagCTAG/;
return $sequence;
}
sub seqlen {
my $sequence = shift;
$sequence =~ s/[^gatcnGATCN]//g;
return length $sequence;
}
1;
To use this module:
#!/usr/bin/perl use strict; use MySequence2 qw(:DEFAULT $EcoRI);
my $sequence = 'gattccggatttccaaagggttcccaatttggg'; my $complement = reversec($sequence);
print "original = $sequence\n"; print "complement = $complement\n";
if ($complement =~ /$EcoRI/) {
print "Contains an EcoRI site.\n";
} else {
print "Doesn't contain an EcoRI site.\n";
}
Object-oriented module
Some modules are object-oriented.
Example: The Math::Complex Module
The Math::Complex module is a standard module that implements complex numbers.
You work with it by creating one or more Math::Complex objects.
You can then manipulate these objects mathematically by adding them, subtracting them, multiplying, and so on.
#!/usr/bin/perl # file: complex.pl
use strict; use Math::Complex;
# create two objects named $a and $b. # the first argument is the real part of the complex number, # and the second is the imaginary part. # The return value from make() is the complex number object. my $a = Math::Complex->make(5,6); my $b = Math::Complex->make(10,20); my $c = $a * $b;
print "$a * $b = $c\n";
Objects in Perl
An object is a collection of functions and data. It models a thing, which has attributes, and is capable of doing certain things
Classes vs Instances
Object Reference
A variable can refer to an object (called an object reference).
Objects vs Modules
Object Instantiation
my_object->new('attr1'=>'value1', 'attr2'=>'value2');
Example: MySequence V3 - Object-Oriented
use strict;
# Define the MySequence class package MySequence;
our $EcoRI = 'ggatcc';
sub new {
# when you make a method call on an OO module
# the first argument is the class
my $class = shift;
my %args = @_;
# create a new hash reference which will store all the object data
# it is the actual object you're calling this method from
my $self = {};
# stores object data in the hash reference $self
foreach my $key (keys %args) {
$self->{$key} = $args{$key};
}
# associate this new bit of memory with a class type # and turn the hashref into an object bless $self, $class;
# return the newly created object return($self); }
sub reversec {
# when called in an object context, the object is
# automatically passed in at the first argument
my $self = shift;
my $sequence = shift; $sequence = reverse $sequence; $sequence =~ tr/gatcGATC/ctagCTAG/; return $sequence; }
sub seqlen {
# when called in an object context, the object is
# automatically passed in at the first argument
my $self = shift;
my $sequence = shift; $sequence =~ s/[^gatcnGATCN]//g; return length $sequence; } 1;
# create a new sequence object my $seq_obj = MySequence->new();
my $sequence = 'gattccggatttccaaagggttcccaatttggg'; my $complement = $seq_obj->reversec($sequence);
print "original = $sequence\n"; print "complement = $complement\n";
Hash Reference
A hash reference is just a reference variable referring to a hash
%my_hash = ('dna1'=>'ccttgga', 'dna2'=>'actgaaga', 'dna3'=>'tcattgattt');
$hash_ref = \%my_hash;
To access the values in the original hash
$hash_ref->{'KEY'};
To create an anonymous hash (i.e., creating hash reference from scratch)
$hash_ref = {'dna1'=>'ccttgga',
'dna2'=>'actgaaga',
'dna3'=>'tcattgattt'}
This tutorial is adapted from http://stein.cshl.org/genome_informatics/using_perl_modules/index.html, http://stein.cshl.org/genome_informatics/perl_refs/ and http://stein.cshl.org/genome_informatics/perl_objects.pdf