Object Oriented Programming in Perl

                                                                                                                         

 

OOPs

  • Object Oriented concept in Perl is very much based on references and anonymous array and hashes.

Three Important Rules:

  • These three declarations are extremely important to understanding how objects, classes, and methods work in Perl.
    • A class is a Perl package. This package for a class provides the methods for objects.
    • A method is simply a Perl subroutine. The only catch with writing such methods is that the name of the class is the first argument.
    • An object in Perl is simply a reference to some data item within the class.
    • Perl provides a BLESS() FUNCTION which is used to return a reference which ultimately becomes an object.

 

Creating a Class:

  • Create a package file & save it as a file.pm.
  • Now a module is a package, and a package is a class for all practical purposes.
  • Before you do anything else, place a 1 ;

Example:

Package pack;

# add your method here

1;

 

Creating and Using Objects :

  • 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 you can use any name.
  • You can use any kind of Perl variable as an object in Perl.
  • Most Perl programmers choose either references to arrays or hashes.

Blessing a Constructor:

  • A constructor is a Perl subroutine in a class that returns a reference to something that has the class name attached to it.
  • Connecting a class name with a reference is referred to as “blessing” an object because the function to establish the connection is called bless.
  • Let ‘s create constructor class using a Perl hash reference.
  • When ever you want to create an object, simply use constructor .
  • which is a subroutine within a package that returns an object reference.
  • The object reference is created by blessing a reference to the package’s class. Example:

Package pack1;

Sub new{

my $class=shift;

my $self ={

_firstName=>shift,

_lastName =>shift,

};

print “First Name is $self->{_firstName}\n”;

print” Last Name is $self ->{_lastName}\n”;

bless $self,$class;

return $self;

}

 

  • Creating an Object :

$object = new pack1( “salman” ,”khan”);

  • You can use simple hash in your constructor if you don’t want to assign any value to any class variable.

Example :

package pack1;

sub new {

my $class = shift;

my $self = {};

bless $self, $class;

return $self;

}

 

Inheritance:

  • Object-oriented programming has very good and useful concept called inheritance.
  • Inheritance simply means that properties and methods of a parent class will be available to child classes.
  • So you don’t have to write the same code again and again, you can just inherit a parent class.

Example:

  • Suppose we can have a class Employee which inherits from Person.
  • This is referred to as an “isa” relationship because an employee is a person.
  • Perl has a special variable, @ISA, to help with this. @ISA governs (method) inheritance.

Following are important points to be considered while using inheritance:

  • Perl searches the class of the specified object for the given method or attribute ie. variable.
  • Perl searches the classes defined in the object class’s @ISA array.
  • If no method is found in steps 1 or 2, then Perl uses an AUTOLOAD subroutine, if one is found in the @ISA tree.
  • If a matching method still cannot be found, then Perl searches for the method within the UNIVERSAL class (package) that comes as part of the standard Perl library.
  • If the method still has not found, then Perl gives up and raises a runtime exception.
  • Create a new Employee class that will inherit methods and attributes from our Person class,
  • we simply code as follows: Keep this code into Employee.pm

#!/usr/bin/perl

package Employee;

use Person;

use strict;

our @ISA = qw(Person); # inherits from Person

  • Now Employee Class has all the methods and attributes inherited from Person class and you can use them as follows: Use main.pl file to test it

#!/usr/bin/perl

use Employee;

$object = new Employee( “Jason”, “watson”, 23234345);

# Get first name which is set using constructor.

$firstName = $object->getFirstName();

print “Before Setting First Name is : $firstName\n”;

#Now Set first name using helper function.

$object->setFirstName( “jon” );

#Now get first name set by helper function.

$firstName = $object->getFirstName();

print “After Setting First Name is : $firstName\n”;

Result:

First Name is jason

Last Name is watson

SSN is 23234345

Before Setting First Name is : jason

Before Setting First Name is : jon.

                                                                                                                         

 

Leave a Reply

Your email address will not be published. Required fields are marked *