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
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:
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:
$geneInfo1 is the instance variable. The methods,
including the constructor are accessed with the ->
operator. Running the program gives this output:
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.
There are no user comments.
Please send ideas and opinions by email at alexamies@gmail.com.