Flylib.com

Books Software

 
 
 

Chapter 3. Object-Oriented Programming in Perl


Chapter 3. Object-Oriented Programming in Perl

In Chapter 1, you saw how modules are defined and used, and in Chapter 2, how references and data structures work. Now, it's time to introduce the important concepts and techniques of object-oriented programming in Perl that are based on modules and references.

Object-oriented (OO) programming is one of the most important approaches to writing programs, and it is an approach that has been well supported by Perl for quite a while. Other OO languages of interest include Java, C++, and Smalltalk. Many Perl modules are written in an OO style, and their proper use requires some fundamental understanding of the OO approach. Luckily, the key concepts are fairly simple.

Perl easily supports both declarative and OO programming. (Perl was originally a declarative language only; the OO style was added fairly early on.) Declarative programming is characterized by code that declares variables and subroutines, conditional tests, if-else branches, and loops , and various arithmetic, logical, and string operators. It is up to you to manage the definition and use of the variables and subroutines so that they interact in appropriate ways. (You'll see shortly how object-oriented programming imposes additional constraints that help you create well-behaved programs.) Many declarative programming languages are well established, including Perl and such stalwarts as C, FORTRAN, and BASIC, to name just a few. By this point, assuming you have some experience programming in Perl, you should be fairly comfortable with the declarative style.

The first part of this chapter is an overview of OO programming and how OO Perl modules are used. If you're a beginning Perl programmer, you'll find them easy to use because they rarely require you to know how to write OO Perl code. Depending on your needs and goals, this might be all the information you'll require from this chapter.

As a more advanced programmer, you'll sometimes need to write your own OO bioinformatics software. If you're such a programmer, the second part of this chapter will be of greatest interest to you. However, because the material is developed incrementally, you will most likely want to read the chapter in order from beginning to end.

Perl makes clever and simple use of existing mechanisms to support OO programming. Perl packages and modules are used to define OO classes, Perl references define OO objects, and Perl subroutines define OO methods. The definitions of these terms will become clear as you read the chapter, but in brief, OO software is organized into classes that contain data called objects. Subroutines called methods operate on the objects.

Over the course of this chapter, I'll develop a small example object module, Gene.pm , to demonstrate the essentials of OO Perl. Gene.pm is developed in four stages so you can learn the OO style gradually. The final code for Gene.pm serves as a template from which you can begin developing your own OO software.


3.1 What Is Object-Oriented Programming?

Object-oriented programming is a way to organize code so it interacts in certain prescribed ways, obeying certain rules about how the data and subroutines are organized. In other words, it imposes a certain programming discipline that can lead to better and more reliable code.

The key idea of OO programming is that all data is stored and modified with special data structures called objects , and each kind of object can be accessed only by its defined subroutines called methods . The user of an OO class is typically spared the effort of directly manipulating data, and can use class methods for this instead.

The promise of this OO structure of program code is that it makes the resulting programs cleanly designed, more reliable, easier to reuse in other programs, and easier to modify and improve. In essence, the approach imposes certain restrictions on what a programmer can do with the data and subroutines at hand.

Proponents of the OO approach cite the benefits this extra discipline provides. It is certainly true that you can follow good programming practices without using an OO approach. However, OO does provide a well-defined framework for encouraging discipline and good programming practices. In a very flexible language such as Perl, good practices can sometimes be easier to enforce in the framework of OO. We'll see how this comes about in the examples that follow.

3.1.1 Why Object-Oriented Programming?

It is often important and necessary to weigh the costs and benefits of a given system against the alternatives in an applied engineering discipline such as programming. The decision to use OO programming, declarative programming, or some other paradigm, is often subject to religious debates, with some enthusiasts promoting their favorite approach against all comers. This is especially relevant to the Perl programmer, because Perl allows you to write in the declarative or in the OO style. You should know that OO programming isn't always the correct choice for a programming project. Despite the real benefits it can confer upon a software development project, it can also have certain costs; these costs and benefits should be weighed against each other.

For instance, some types of software lend themselves more readily to abstracting with OO techniques than others. Object-oriented software development can sometimes take longer due to the overhead associated with its level of abstraction. OO software sometimes runs slower than other approaches; this has certainly been true in Perl, and although not usually a deal breaker, it is sometimes an important consideration. (Current work on the upcoming Perl 6 is addressing this performance issue.)

In spite of these strictures , OO programming is often an excellent choice; it has become a key approach to writing software in the Perl language.

3.1.2 Terminology

Object

An object is a collection of data that logically belongs together.

For instance, you might have a genome object that would have such attributes (or parts ) as the name of the organism, the DNA sequence data, the start and end points for each exon, the genes with their associated lists of exons, and so forth. The exact nature of an object is a matter of logic and convenience, and in the end, it depends on the judgment of the programmer as to what collection of attributes makes an object that will accomplish the goals at hand.

A standard term in computer science for a collection of data is a data structure . Data structures are often studied in terms of accomplishing certain algorithms . It is often the case that the fastest , cleverest way to compute something relies upon putting the data into a special data structure that makes a fast algorithm possible. [1]

[1] This point really highlights the importance of spending some time on design at the beginning of your programming, and the importance of using a good efficiency tester such as Benchmark.pm to evaluate alternate solutions.

In Perl, objects are usually implemented as references to hashes and are marked with their class name. The Perl function bless marks a reference with a class name, as will be explained in this chapter. The Perl function ref can be used to see what class name an object is marked with.

Method

In OO programming, a subroutine called a method is associated with an object. Each type of object has one or more methods that it can call. In this style of programming, the only way to access the data in an object is by the defined methods of the class. This restriction is meant to increase reliability, reusability, and maintainability.

In Perl, methods are implemented as Perl subroutines, although they behave slightly differently from other subroutines. Methods are called from an object, and they know what object they're called on; they don't need to be explicitly imported, and they exist in a hierarchy of classes. If you know how to use Perl subroutines, methods are an easy next step.

Class

Together, the object definitions and the collection of methods for them defines a class. A specific object (say, a genome object for C. elegans) is called an instance of a class .

Classes can be related to each other so methods can be inherited into a class from another class. In Perl, classes are implemented as namespaces, by means of the package directive.

Now that you're familiar with the language of OO programming, let's see how it's used.