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


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.


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.


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.