Starting Object Oriented Programming with Perl

How to Create and Use Classess in a Perl Application

© Mark Alexander Bain

Jul 19, 2009
Starting Object Oriented Programming with Perl, Mark Alexander Bain
Perl is not just a scripting language for CGI web pages. It is also a very powerful object oriented programming. Fortunately it is also very simple to use.

Object oriented programming is a very important aspect of most modern programming languages, and Perl is no different. This technique is important because the objects encapsulate blocks of code meaning that the programmer needs to know very little about any background processes - they just need to understand how to interface with the object. In order to do that they need to know two terms:

  • class - in Perl this is simply a module that contains the methods required for the object to operate correctly
  • method - these are subroutines stored in the Perl module that provide the interfaces to the object

And, fortunately, the Perl programmer will find these are very easy to work with.

Creating a Perl Class

A Perl class is a module. In other words it is just a text file. This text file contains a Perl package, and a Perl package is a set of Perl subroutines and variables which are grouped together. So the first task is to create a module (for example "person.pm") and then defining the package:

package person;

Any class needs a constructor method. This is always call "new" and creates the object reference (by blessing it):

sub new {
my $person = {};
return bless $person;
}

Next the package needs to contain any private variables (a private variable is denoted by the $_ prefix):

$_age = 21;
$_firstname = "john";
$_surname = "doe";

Finally the methods that will form the object's interface are required. This will either set or return the values of the private variables:

sub age {
if ($_[1]) {$_age = $_[1]}
else {return $_age}
}
sub firstname {
if ($_[1]) {$_firstname = $_[1]}
else {return $_firstname}
}
sub surname {
if ($_[1]) {$_surname = $_[1]}
else {return $_surname}
}

Or they can be used for more complex operations:

sub username {
my $fl = substr(firstname, 0, 1);
return surname . $fl;
}

And, of course, the module must end by returning a value to the script calling it:

1;

With the class in place it can be used to create an object in any Perl script.

Using a Perl Object

The Perl class is loaded just like any other Perl module:

use person;

However, the new method turns it into a usable object:

$customer = new person;

The programmer then has access to the object's methods for either obtaining information:

print
$customer->username
. " " . $customer->firstname . " " . $customer ->surname
. " " . $customer->age . "\n";

Or to change the object parameters:

$customer->age(40);
$customer->firstname("fred");
$customer->surname("smith");

And, just as important, the programmer can add their own custom variables to the object:

$customer->{'account_number'} = 9988776655;
print
$customer->username
. " " . $customer->firstname . " " . $customer ->surname
. " " . $customer->age
. " " . $customer->{'account_number'} ."\n";

Here the script has turned the person (with age, firstname and surname) into a customer (by adding an account number). However, rather than doing this in the script, a more efficient use of the code is to create a new class to do the job.

Perl Classes and Inheritance

In this example the only difference between a person and a customer is that the customer has an account number. This, therefore, can be incorporated into a new class and, in this case, that's stored in "customer.pm":

package customer;

This will load the person module:

use person;

And then the programmer uses the ISA array so that the customer class inherits the person class methods:

@ISA = qw/person/;

Apart from that, it is created in the same way as the person class:

sub new {
my $customer = {};
return bless $customer;
}
$_account_number = 0;
sub account_number {
if ($_[1]) {$_account_number = $_[1]}
else {return $_account_number}
}
1;

The customer class now has its own methods as well as the methods of the person class.

Using Inherited Methods from a Perl Class

This time the customer module is loaded instead of the person module:

use customer;
$customer = new customer;
However, the programmer now has access to all of the methods defined in the the customer class, as well as all of the methods inherited from the person class:
print
$customer->username
. " " . $customer->firstname . " " . $customer ->surname
. " " . $customer->age
. " " . $customer->account_number . "\n";
$customer->age(40);
$customer->firstname("fred");
$customer->surname("smith");
$customer->account_number(9988776655);
print
$customer->username
. " " . $customer->firstname . " " . $customer ->surname
. " " . $customer->age
. " " . $customer->account_number . "\n";
And so, by using this simple technique, the Perl programmer can build highly complex classes which can then be used in applications that they build.

The copyright of the article Starting Object Oriented Programming with Perl in Computer Programming Languages is owned by Mark Alexander Bain. Permission to republish Starting Object Oriented Programming with Perl in print or online must be granted by the author in writing.


Starting Object Oriented Programming with Perl, Mark Alexander Bain
Figure 1: Using a Perl Class, Mark Alexander Bain
 Figure 2: Perl Class Inheritance, Mark Alexander Bain
   


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo