The Anatomy of a Module File

Here is a very simple module file named "MySequence.pm":

package MySequence;
#file: MySequence.pm

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;

A module begins with the keyword package and ends with "1;". package gives the module a name, and the 1; is a true value that tells Perl that the module compiled completely without crashing.

The our keyword declares a variable to be global to the module. It is similar to my, but the variable can be shared with other programs and modules ("my" variables cannot be shared outside the current file, subroutine or block). This will let us use the variable in other programs that depend on this module.

To install this module, just put it in the Perl module path somewhere, or in the current directory.

Using the MySequence.pm Module

Using this module is very simple:
  #!/usr/bin/perl
  #file: sequence.pl

  use strict;
  use MySequence;

  my $sequence = 'gattccggatttccaaagggttcccaatttggg';
  my $complement = MySequence::reversec($sequence);

  print "original   = $sequence\n";
  print "complement = $complement\n";

% sequence.pl
original   = gattccggatttccaaagggttcccaatttggg
complement = cccaaattgggaaccctttggaaatccggaatc

Unless you explicitly export variables or functions, the calling function must explicitly qualify each MySequence function by using the notation:

MySequence::function_name

For a non-exported variable, the notation looks like this:

$MySequence::EcoRI

<< Previous
Contents >> Next >>

Lincoln D. Stein, lstein@cshl.org
Cold Spring Harbor Laboratory
Last modified: Thu Oct 18 22:17:38 EDT 2001