Perl OOP Programming - Part 1
Object-Oriented Programming (OOP) in Perl is a like riding on the back of a strange imaginary beast. You eventually arrive at the destination, but explaining to someone how you got there takes some creative doing. In Part 1 of this article we look at how Perl can be used to create code that mimics Object-Oriented Programming. If we squint hard, we might even fool ourselves into thinking we’re really doing OOP, as long as we don’t look closely. So grab onto your saddle…here we go!
Creating Objects
Perl does not support true Object-Oriented Programming (OOP), but an OOP programming style is supported through the use of packages. The declaration for a package named Object is:
package Object;
Object methods are normal Perl subroutines accessed by using a right arrow pointer.
Object->method;
Object->method(arguments);
Object properties are typically stored in a hash and accessed through the right arrow pointer (infix notation). The first example below is correct; the second is shorthand that can only be used to get a value, not to set it.
# setting an attribute
Object->{'property'} = value; # ok
Object->{property} = value; # ok
Object->property = value; # wrong
# getting an attribute
value = Object->{property}; # ok
value = Object->property; # ok for gets only!
This works because Perl Objects are hash references. The bless command is used to bind the hash reference to the object name. When an object is created the bless command is placed within the constructor method and bound to the object instance. The Perl code looks like this:
package Object;
sub new {
my ($class) = shift;
my ($self) = {};
bless $self, $class;
}
We can create an instance of an Object in two ways:
my $obj = new Object;
my $obj = Object->new();
We can initialize instance variables from inside the constructor:
package Object;
sub new {
my ($class) = shift;
my ($self) = {
name => 'total',
type => 'integer',
value => 23
};
bless $self, $class;
}
Passing Parameters
We might choose to pass parameters to the class constructor as the object is created. The traditional way of passing parameters is to list them in the constructor’s method call.
my $obj = new Object($name, $type, $value);
This becomes unwieldy when there are a lot of parameters. In Perl a common solution is to use an anonymous hash reference to contain the parameters. The Object constructor is called using a hash reference to hold all of the parameters. This has a number of advantages, as we will see.
# using a actual hash
my %args = (
name => 'objectname',
type => 'objecttype',
value => 'objectvalue',
);
my $obj = new Object(\\%args);
# using an anonymous hash
my $args = {
name => 'objectname',
type => 'objecttype',
value => 'objectvalue',
};
my $obj = new Object($args);
Perl OOP programming typically uses a hash reference to send parameters to an object method, and to return values from it. This frees the programmer from worrying about the correct order of the parameters, and the possibility of breaking the code by adding new parameters. In fact, parameters can be passed around as a bundle. An object might change the parameters that it uses, and pass the remainder back, with the changes included. This makes for a very relaxed style of programming.
The downside is the confusion it creates when calling a method. An object reference that is passed in a hash could be used to call a method, process the result, and place the resulting data back into the hash. What does the syntax look like?
$args->{'value'} = $args->{$object}->getparameter();
In the example above we are storing the value returned from the Object method called getparameter() into the $args hash using the name ‘value’ as the tag. The %args hash contains a reference to $object, and $object’s return value from getparameter() is then saved into the %args hash with the tag value.
Class and Instance Variables
The hash stores the instance variables, but what about a class variable? Where does that go? Place class variables and constants in the package, but outside the class methods. When a new object is created, instance variables are initialized from inside the new method.
package Object;
my classcounter;
sub new {
my ($class) = shift;
my ($self) = {};
bless $self, $class;
$self->{'name'} = 'newobject';
$classcounter++;
}
In the example above the class variable $classcounter is incremented each time a new instance of Object is created. This class static-like variable is accessible to all instances of the Object class.
Stay tuned for Part 2 - Riding the Unicorn
Tags: oop, Perl, Programming

























