Thursday, September 19, 2024

Camel POOP

Object Oriented Programming in Perl
Most people are not aware of the fact that Perl has support for object-oriented programming. If you’ve used another object-oriented programming language such as Java or C++ or been exposed to object-orientation then object oriented programming in Perl is nothing like that. To do real useful object-oriented programming in Perl you only need to use three simple rules as put forth by Larry Wall in Object Oriented Perl .

Object oriented programmers are familiar with the concept of object and classes but I will review that here quickly. An object is a thing that provides access to or modification of data. A class is description of the attributes of a particular kind of object and the manner in which those objects can be accessed and modified. A method is a means by which an object’s data is accessed or modified. An object is an instance of a class.

An example would be an Person class in an HR system. The Person class describes the attributes of an Person such as name, address, title, social security number, id etc. A particular class instance or object would encapsulate data about a particular Person e.g. name, title, social security number, address, etc. Some methods to access that object’s data would be name, address etc.

Package delivery

To create a class in Perl we first build a package. A package is a self-contained unit of user-defined variables and subroutines, which can be re-used over and over again. They provide a separate namespace within a Perl program that keeps subroutines and variables from conflicting with those in other packages.

To declare a class named Person in Perl we do:

-==-

That’s it. The scope of the package definition extends to the end of the file, or until another package keyword is encountered. Not very useful yet but on to the next section.

There’s a method to this madness

A method is a means by which an object’s data is accessed or modified. In Perl a method is just a subroutine defined within a particular package. So to define a method to print our Person object we do:

-==-

The subroutine print is now associated with the package Person. To call the method print on an Person object we use the Perl “arrow” notation. If the variable $khurt contains an Person object we would call print on that object by writing:

-==-

When the object method is invoked a reference to the object is passed in along with any other arguments. This is important since the method now has access to the object on which it to operate.

How do we create the invoking object?

Bless me father

To create an instance of a class (an object) we need an object constructor. This constructor is a method defined within the package. Most programmers choose to name this object constructor method new but in Perl one can use any name.

One can use any kind of Perl variable as an object in Perl. Most Perl programmers choose either references to arrays or hashes.

Let’s create our constructor for our Person class using a Perl hash reference;

-==-

What have we done? We created a subroutine called new associated with the package Person. The entries of the hash reference $self become the attributes of our object. We then use the bless function on the hash reference. The bless function take two arguments; a reference to the variable to be marked and a string containing the name of the class. This indicates that the variable now belongs to the class Person.

To create an instance of our Person object:

-==-

We have not defined accessor methods or done any error checking on the input values or keys or the anonymous hash reference but we have the start of a Perl Person OO framework. To make our constructor more flexible and to make our class inheritable (more on that later) we can define it to use the $class variable to bless the hash reference.

-==-

Other object-oriented languages have the concept of security of the data to prevent a programmer from changing an objects data directly and so provide accessor methods to modify object data. Perl does not have private variables but we can still use the concept of accessor methods and ask programmers to not mess with our object innards.

For our Person class we should provides accessor methods for our object attributes; name, address, title, ssn.

-==-

Making babies

Object-oriented programming sometimes involves inheritance. Inheritance simply means allowing one class called the Child to inherit methods and attributes from another, called the Parent one so you don’t have to write the same code again and again. For example, we can have a class Employee which inherits from Person. This is an referred to as an “isa” relationship because an Employee is a Person. Perl has a special variable, @ISA, to help with this. @ISA which governs (method) inheritance. So to create a new Employee class that will inherit methods and attributes from our Person class we simply code:

-==-

What we have done is load the Person class and declare that Employee class inherits methods from it. We have declared no methods for Employee but an Employee object will behave just like a Person object. We should be able to write code:

-==-

without any other changes.

Now Let’s add some methods.

-==-

Looking at the code you will notice that we have a new method and a print method. Both a Child class and its Parent class have the same method defined. We have overridden the Parent class’ methods with the ones from the Child. When those methods are called on an Employee object we will get the Employee class’ version of the method. This concept of using the methods of and existing object and modifying them is known as polymorphism.

Putting it together

So now we have a complete set of classes we can write a small program to test them.

-==-

Let’s execute our code and see the output

-==-

It works! We covered the basics of object oriented programing in Perl and I hope this article was informative and useful.

First appeared at CodeProject. Reprinted with permission from the author.

Khurt Williams is founder and principal of Williams Interactive, Inc.
Williams Interactive, Inc. is a web application development firm that
provides customized software solutions for businesses by leveraging open
source software and programming languages. You can contact Khurt at
khurt@williamsinteractive.com or visit http://www.williamsinteractive.com

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles