3.5 Gene1: An Example of a Perl Class


This first example of Perl code defines a very small Perl class. This code does several new things, which I will explain in detail after the code.

This first version of the class is called Gene1 , and it demonstrates the essential features needed to implement a simple class. Gene1 looks similar to the last module definition in Chapter 1, but with a few new wrinkles that transform it into OO software. I progress from Gene1.pm to Gene2.pm , then to Gene3.pm , and then to the final version, Gene.pm .

The methods of the Gene1 class will permit creating Gene1 objects and finding out what the values of a Gene1 object's attributes are.

Here's the module that implements a Gene1 class. I put the module into a file called Gene1.pm and place it into a directory on my computer that can be found when Perl needs it. I continue putting my code into my own development library directory, which on my Linux system is the directory /home/tisdall/MasteringPerlBio/development/lib . This directory is pointed out to Perl at the beginning of the testGene1 program that appears later as an example of how to use the Gene1.pm module definition. You will probably use a different directory on your computer, in which case you'll have to change this line:

 use lib "/home/tisdall/MasteringPerlBio/development/lib"; 

You can also put the directory on the command line or set the PERL5LIB environmental variable, as described in Chapter 1. Setting the PERL5LIB variable is the easiest because you don't have to change the use lib lines in the programs.

A Gene1 object consists of a gene name , an organism represented by genus and species, a chromosome, and a reference to a protein structure in the PDB:

 package Gene1; use strict; use warnings; use Carp; sub new {         my ($class, %arg) = @_;         return bless {                 _name       => $arg{name}         croak("no name"),                 _organism   => $arg{organism}     croak("no organism"),                 _chromosome => $arg{chromosome}   "????",                 _pdbref     => $arg{pdbref}       "????",         }, $class; } sub name        { $_[0] -> {_name}     } sub organism    { $_[0] -> {_organism} } sub chromosome  { $_[0] -> {_chromosome}} sub pdbref      { $_[0] -> {_pdbref}    } 1; 

That's the whole thing!

Hash keys that are simple words such as "name," "organism," and so on, don't need to have quotes around them when they appear within their surrounding curly braces: $arg{name} means the same thing as $arg{'name'} .

Here's a small program that uses the Gene1.pm class:

 use strict; use warnings; use lib "/home/tisdall/MasteringPerlBio/development/lib"; use Gene1; print "Object 1:\n\n"; my $obj1 = Gene1->new(         name            => "Aging",         organism        => "Homo sapiens",         chromosome      => "23",         pdbref          => "pdb9999.ent" );  print $obj1->name, "\n"; print $obj1->organism, "\n"; print $obj1->chromosome, "\n"; print $obj1->pdbref, "\n"; print "Object 2:\n\n"; my $obj2 = Gene1->new(         organism        => "Homo sapiens",         name            => "Aging", );  print $obj2->name, "\n"; print $obj2->organism, "\n"; print $obj2->chromosome, "\n"; print $obj2->pdbref, "\n"; print "Object 3:\n\n"; my $obj3 = Gene1->new(         organism        => "Homo sapiens",         chromosome      => "23",         pdbref          => "pdb9999.ent" );  print $obj3->name, "\n"; print $obj3->organism, "\n"; print $obj3->chromosome, "\n"; print $obj3->pdbref, "\n"; 

If I put that demonstration program code into file testGene1 , I get this output from running that demonstration program with perl testGene1 :

 Object 1: Aging Homo sapiens 23 pdb9999.ent Object 2: Aging Homo sapiens ???? ???? Object 3: no name at testGene line 35 

Now, let's take a look at how this Perl code works.



Mastering Perl for Bioinformatics
Mastering Perl for Bioinformatics
ISBN: 0596003072
EAN: 2147483647
Year: 2003
Pages: 156

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net