Approaches to Web Development for Bioinformatics

Previous  Contents  Next
References

Object Oriented Programming in Perl

Object oriented programming is valuable because it can be used to hide program complexity and aids in development between multiple people.  It adds some complexity to the language by itself but object oriented principles enable a programmer to code a complex task and only expose the necessary application programming interfaces (API's) for other people to make use of the code as a library.  Many of the core Perl modules and two of the modules discussed later, CGI and BioPerl, use object oriented interfaces.  For these reasons I will take a little space to explain some of the basics of object oriented programming with Perl.

Object oriented concepts were added to Perl in version 5.  Because the concepts were not a part of the language to begin with and were built on top of other concepts, including packages and references, the implementation is not as clean as with other purely object oriented languages, such as Java.

The fundamental work unit in object oriented programming are classes, which are roughly equivalent to modules in Perl (depending which other object oriented paradigm you are comparing Perl to). Classes are accessed with the :: operator.  Classes have methods, which are similar to functions.  Methods are accessed with the -> operator.  An example of the use of an object oriented interface with the built in class File::Spec is


#!/bin/perl -w
# Example use of object oriented interfaces from File::Spec

# Load the File::Spec class
use File::Spec;

# Invoke the method rootdir(), which gives the root directory of the file system.
print File::Spec->rootdir();

The use keyword (similar to import in Java) tells the Perl interpreter to make the module available in to the program it is running.  To create a class that can be used from another Perl program define a package in a Perl module file.  The name of the class must be the same as the name of the package (a class really is a package in Perl) and must be in a file with the same name as the package, appending '.pm'.  Here is an example:


#!/usr/bin/perl -w
# An example Perl module to demonstrate object oriented features using
# a gene_info record

package MedicalComputing::GeneInfo;

# A constructor for a GeneInfo instance
# Required parameters
# Id the NCBI identifier field
# The gene symbol
# The name of the gene
sub new {
my $class = shift;
my $id = shift;
my $symbol = shift;
my $name = shift;
bless {ID => $id, Symbol => $symbol, Name => $name}, $class;
}

# An accessor method for the NCBI identifier field
sub id {
my $self = shift;
$self->{ID};
}

# A method to print all the fields to standard out.
sub printGeneInfo {
my $self = shift;
print "GeneInfo\n",
"\tID:\t", $self->{ID}, "\n",
"\tSymbol:\t", $self->{Symbol}, "\n",
"\tName:\t", $self->{Name}, "\n";
}

# Required for Perl implementation reasons
1;

The code should be saved in a directory called MedicalComputing under the current directory in a file called GeneInfo.pm.  The class defines a constructor called new for creating instances.  The instance is created with the keyword bless on the last line of new.  The constructor takes several parameters as listed in the comments and saves them in a hashtable.

The class also defines a method (an object oriented function, more or less) called id that returns the value of the ID (hence the term 'accessor' or 'getter').  The instance fields (member variables that remain for the lifetime of the instance) are passed into the methods as the first parameter.  This happens because of the form of the bless statement in the constructor.  Finally, the class defines a method called printGeneInfo that prints all the data to standard out.

Here's how the class can be used:


#!/usr/bin/perl -w
# An example Perl program to demonstrate object oriented features

use MedicalComputing::GeneInfo;

my $geneInfo1 = MedicalComputing::GeneInfo->new( "3064", "HD", "Huntington Disease" );
print "The ID is ", $geneInfo1->id(), "\n";
$geneInfo1->printGeneInfo();

$geneInfo1 is the instance variable.  The methods, including the constructor are accessed with the -> operator. Running the program gives this output:


>testGeneInfo.pl
The ID is 3064
GeneInfo
ID: 3064
Symbol: HD
Name: Huntington Disease

Why would we do this instead of just using subroutines and hash tables?  One scenario is that the data could be collected from a user with a user interface, such as HTML form, kept around for a while to allow the user to do stuff with the data, and then saved to a file or a database.  With a bit more work by the person writing the class, the details of manipulating and saving the data can be hidden from the programmer writing the user interface.  In my mind that is the main benefit of object oriented programming: the person writing the class has the expertise of what is inside the class allowing other programmers just to use the methods.  This allows a development project to scale to more than one person.


Previous  Contents  Next
References

Contributed Comments and NotesAdd a comment.

There are no user comments.

Google

Please send ideas and opinions by email at alexamies@gmail.com.

© 2006-2007 Alex Amies