Basics of object orientation and inheritance in perl

By: Varghese Chacko 1 year, 3 months ago

Perl is an object oriented language. Perl has packages(class) that could be instanciated and used. At first, we need to decide the file namefor the package ans the perl class name carries the file name. For example, a package Student should be in the file Student.pm. If it shouldbe called Bright::Student, it should be saved asBright/Student.pm, ie Student.pm in a directory named Bright. There is no formal relationship between directory name and modules. That is just grouping for convenience.

For every package, we need a constructor. The developers with C/C++/Java or similar background wouldname it as new, butnew is not a keyword. The constructor gives you a brand new object of the class and it is achieved by the magick function bless. Bless makes a reference be the type of the class.

A data structure similar to we have in C could be made with an anonymous hash like

$rec = {
    NAME  => "Jason",
    AGE   => 23,
    PEERS => [ "Norbert", "Rhys", "Phineas"],
};


Letus create a student

package Student;
use strict;
 
#############################################
## The constructor                         ##
#############################################
sub new {
    my $class = shift;  # class is not a keyword
    my $self  = {};  # self is not a keyword
    $self->{NAME}   = undef;
    $self->{AGE}    = undef;
    $self->{PEERS}  = [];
    bless($self, $class);   
    return $self;
}
 
##############################################
## Methods to access per-object data        ##
##                                          ##
## With args, they set the value.  Without  ##
## any, they only retrieve it/them.         ##
##############################################
 
sub name {
    my $self = shift;
    if (@_) { $self->{NAME} = shift }
    return $self->{NAME};
}
 
sub age {
    my $self = shift;
    if (@_) { $self->{AGE} = shift }
    return $self->{AGE};
}
 
sub peers {
    my $self = shift;
    if (@_) { @{ $self->{PEERS} } = @_ }
    return @{ $self->{PEERS} };
}
 
1;  # All package files  should return truth.

Now let us have a student volunteer. SO we have a class Volunteer class and inherit student, ofcourse, in Volunteer.pm

package Volunteer
use Student
@ISA=("Student")
1;

Done. Yes, it is as simple as that. Now Volunteer is a Student. The magick variable @ISA makes the package Volunteer is a Student We can now invoke methods defined in Students from an object of Volunteer

use Volunteer;
my $volunteer = Volunteer->new();
$volunteer->name("Varghese");
$volunteer->age(23);
printf "%s is age %d.\n", $volunteer->name, $volunteer->age;


What is the difference?

Method Call              Resulting Function Call
-----------------        ------------------------
Student->new()           Student::new("Student")
Volunteer->new()         Student::new("Volunteer")

The class passed to the new will be different so that bless with create the instance accordingly after allrequired checks.

Mode details of the inheritance could be covered later :)