Perl OOP basics
Date: Jan 03 2012 | Tags: classes, objects, perlNote: This is not an exhaustive manual on Perl OOP. This post should give a reader some basic information about the usage of classes.
As most programming languages, Perl supports object-oriented programming (OOP). It allows to create classes which contain logically grouped variables (here called attributes) and functions (here called methods). Then you can create an object, which is an instance of the class. This means that you can have multiple objects of the same class, each with their own memory space.
Packages
A package is a separate namespace. You can create a package by using package command.
package Alien;
Classes
A class is a package. To extend the definition, a class is a package that has methods and variables that enables an object to be instantiated and used.
Class constructor
Usual constructor name is new, though it can be anything. The first argument passed to the constructor is a class reference. In its simplest form, no other arguments need to be passed.
Object can be any kind of variable, though it is usually a hash reference. We will name this reference as $self and in this example, leave it empty.
To make this reference an object, function bless() is used. Finally this reference is returned.
Now our simplest constructor looks like this:
sub new {
my ($class) = @_;
my $self = {};
bless ($self, $class);
return $self;
}
Methods
Methods are another name for procedures in classes. The first argument passed to a method is always an object reference. Here are two methods, one that sets an object name and one that returns it.
sub set_name {
my ($self, $name) = @_;
$self->{name} = $name;
}
sub get_name {
my ($self) = @_;
if (exists $self->{name}) {
return $self->{name};
}
return;
}
Note that method get_name() checks if object (which is a hash reference, as set in the constructor) has key named 'name' before returning it.
Using a package
Now a package named Alien is complete. It can be created as a module, which will be discussed in one of the next posts, or combined with package main, which is a default package that gets executed. The latter will be used in this example.
Package main usually does not need to be explicitly stated, but as we have two namespaces in the same file, package main command will delimit the main part of the source from the package Alien.
The full example includes the package Alien, and in the main part creates two objects (two Aliens). Then their name is set, and finally printed to output.
use strict;
use warnings;
use 5.010;
package Alien;
# Constructor.
sub new {
my ($class) = @_;
my $self = {};
bless ($self, $class);
return $self;
}
# Sets name property for an object.
sub set_name {
my ($self, $name) = @_;
$self->{name} = $name;
}
# Returns name.
sub get_name {
my ($self) = @_;
if (exists $self->{name}) {
return $self->{name};
}
return;
}
# MAIN
package main;
# Create an alien and set its name.
my $alien1 = Alien->new();
$alien1->set_name('Qargl');
# Create another alien, name it too.
my $alien2 = Alien->new();
$alien2->set_name('Woondrg');
# Output to screen.
say "The first guy is called ".$alien1->get_name();
say "and the other one is called ".$alien2->get_name();
exit 0;
0 comments