Packages Modules

Packages and Modules

T. M. Murali

Jan 17, 2005

T. M. Murali: Packages and Modules Packages Modules

Reason for Packages

I First step in good software design.

I How do you protect different pieces of code from inadvertently tampering with each others variables?

I Each named variable and named belongs to a particular package.

I Default package is main.

I Different packages can have variables/ with the same name.

I Each package has its own symbol table or .

T. M. Murali: Packages and Modules Packages Modules

Declaring Packages

I Package declaration:

I package ; I Changes namespace until another package declaration or until the end of the current block, file, subroutine, or eval. I Say package main; to go back to main package.

I Ability to assess the algorithm/data structure needed to solve a problem.

I Ability to implement a small software system in to solve bioinformatics tasks.

T. M. Murali: Packages and Modules Packages Modules

Referring to Variables in Another Package

I Prefix variable/subroutine name with name of the package.

I $Expression::number_of_genes I Expression::Cluster::k_means() I $::number_of_genes ≡$main::number_of_genes I The identifier ($, @, %) comes before the name of the package.

I No package name means that you are accessing a variable in the current package.

I Good design dictates that we should only access the subroutines in another package.

I There is no relationship between the Expression and Expression::Cluster packages.

T. M. Murali: Packages and Modules Packages Modules

Lexical Variables

I Defined using my.

I Do not belong to a package’s namespace.

I is within the block that the variable is defined.

I Variable is not visible to any subroutines that are called.

I Scope can cross package boundaries.

T. M. Murali: Packages and Modules Packages Modules

Perl’s Module System

I Package code into a separate file.

I Put the file in a well-defined place.

I Allow the compiler to add the file into other programmes semi-automatically.

I Read perlmod and perlmodlib documentation for gory details.

T. M. Murali: Packages and Modules Packages Modules

Perl Module

I A Perl module is a file with the .pm suffix that contains some Perl code.

I Fundamental unit of code reuse.

I Typically, one module contains one package.

I Reuse a module with the use ; statement.

I use statement causes compiler to look for the corresponding module in a set of pre-defined locations, find the matching file, and evals (parse and executes) the code in that file.

I Module name is related to the file name: the module Gene::Expression::Cluster is in the file Gene/Expression/Cluster.pm.

T. M. Murali: Packages and Modules Packages Modules

Setting Up the Gene::Expression::Cluster Module

I Choose a standard directory, for example, /home/murali/lib/perl5.

I Tell Perl about this directory:

I (in tcsh) setenv PERL5LIB {$PERL5LIB}:/home/murali/lib/perl5 I (in the file that will use a module)

I BEGIN { push(@INC, "/home/murali/lib/perl5"); } I use lib "/home/murali/lib/perl5";

I Create the Gene and Gene/Expression subdirectories in /home/murali/lib/perl5.

I Create the file Cluster.pm in the Gene/Expression subdirectory.

I Insert your code in Cluster.pm.

I Add a statement that evaluates to true at the end of file, e.g. 1;

I Now any programme can access this module with the statement use Gene::Expression::Cluster;

T. M. Murali: Packages and Modules Packages Modules

Exporting Subroutines

I How does the code useing a module access the module’s subroutines?

I By using the fully-qualified name, e.g., Gene::Expression::Cluster::k_means(); I Without using the fully-qualified name if the module exports the subroutine.

I require Exporter; I our @ISA = ("Exporter"); I our @EXPORT = (k_means, hierarchical, bicluster);

I See Programming Perl, Chapter 11, for use of @EXPORT_OK and @EXPORT_TAGS.

I We will very rarely export variables in this class. I Once we start object-oriented Perl, we will very rarely export variables or subroutines from classes, since exporting violates the principle of encapsulation.

T. M. Murali: Packages and Modules Packages Modules

Autoloading

I How does a module handle a call to an undefined subroutine in that module?

I Before issuing a fatal error, Perl attempts to call the module’s AUTOLOAD subroutine.

I Perl calls AUTOLOAD with the same arguments passed to the unimplemented subroutine.

I Within AUTOLOAD, the special variable $AUTOLOAD contains the fully-qualified name of the subroutine.

I AUTOLOAD can now decide what to do based on the value in $AUTOLOAD.

I AUTOLOAD is especially useful in object-oriented programming.

T. M. Murali: Packages and Modules Packages Modules

The CPAN

I The CPAN is the Comprehensive Perl Archive Network at http://www.cpan.org

I Contains Perl modules, scripts, binary distributions, source code, documentation.

I Explore it by hand.

I Search it at http://search.cpan.org

T. M. Murali: Packages and Modules Packages Modules

Installing Code from CPAN

I perl -MCPAN -e shell;

I install Clone;

I That is it! (If there are no errors ...).

T. M. Murali: Packages and Modules