Perl Object Oriented

1) Class 
  1. Class is a blueprint or template of similar objects. 
  2. A package contains variables and subroutines which can be reused.
  3. To create a class, we need to create a package in Perl. 
  4. A class is the namespace created using package keyword.
  5. It is generally implemented in the module with the same name as class.
  6. Example: We are defining the Person class in Perl. The definition of the Person class is in Person.pm file.
  7. Here 1 at the end indicates successful loading of the file.
  8. The scope of a package extends to the file end or until another package keyword is encountered.
1
2
3
4
5
6
7
8
9
package Person;
 
use strict;
use warnings;

sub new{
}
 
1;


2) Object Creation
  1. We use a subroutine named new() i.e. a constructor to construct the object.
  2. This constructor is defined within the package.
  3. Mostly you can choose to name this constructor method new, but in Perl you can use any name.
  4. Let's create our constructor for our Person class using a Perl hash reference. 
  5. When creating an object, you need to supply a constructor, which is a subroutine within a package that returns an object reference.
  6. The object reference is created by blessing a reference to the package's class.
  7. Lets take a look into complete example: Keep Student package and helper functions into Student.pm file.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#Student.pm
use strict;
use warnings;

package Student;

#Defining a constructor
sub new{

  # The name of the package i.e. Student exists in the default array @_. 
  # Hense we are extracting it and stored it in a local variable 'class' by using my $class = shift;.
  my $class = shift;
  
  #object creation
  my $self = {
    'fname' => shift,
    'lname' => shift, 
    'id' => shift
  };

  #attaching object 'self' with class
  bless $self, $class;

  #returning object from constructor
  return $self;
}

1;
Description: When we call a constructor, the name of the package i.e. 'Student' exists in the default array @_. Hense we are extracting and storing it in a local variable 'class' by using my $class = shift;.

Hash Reference: After this, we created a reference to a hash which has 'fname', 'lname' and 'id' as its keys. Here we are creating our object. It means that our objects of the 'Student' class will be a reference to a hash and they will have 'fname', 'lname' and 'id' as their keys.

bless $self, $class - We have attached our object 'self' with our class 'class'

return $self - We returned the created object from the constructor 'new'.

Create object in student1.pl
1
2
3
4
5
6
7
8
9
use strict;
use warnings;

use Student;

my $obj = new Student("Vinod","Rane", 121532);
print "$obj->{'fname'}\n";
print "$obj->{'lname'}\n";
print "$obj->{'id'}\n";

DescriptionTo make an object of class 'Student', we have to write:
$obj = new Student("NAME","ROLL_NUMBER");

This will pass NAME and ROLL_NUMBER as arguments of the constructor 'new'.

3) Inheritance
  1. Inheritance is using the properties of a parent class by a child class. 
  2. It means that a child class inherits the parent class.
  3. In given example, we have package 'Engineering', we have used use parent 'Student', to use 'Student' as a parent class. After this statement, 'Engineering' becomes a subclass of 'Student' and will be able to access the properties and the methods of parent class.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#Student.pm
use strict;
use warnings;

package Student;

#Defining a constructor
sub new{

  # The name of the package i.e. Student exists in the default array @_. 
  # Hense we are extracting it and stored it in a local variable 'class' by using my $class = shift;.
  my $class = shift;
  
  #object creation
  my $self = {
    'fname' => shift,
    'lname' => shift, 
    'id' => shift
  };

  #attaching object 'self' with class
  bless $self, $class;

  #returning object from constructor
  return $self;
}

1;

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
#Engineering.pm

package Engineering;

use strict;
use warnings;

use parent 'Student';

1;

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#TestOops.pl
use strict;
use warnings;

use Engineering;

my $obj = new Engineering("Vinod","Rane", 121532);
#Another way to create object
#my $obj = Engineering->new("Vinod","Rane", 121532);

#Printing object details
print "$obj->{'fname'}\n";
print "$obj->{'lname'}\n";
print "$obj->{'id'}\n";

4) Method Overriding
  1. Method overriding means having two methods with the same name but doing different tasks. 
  2. It means that one of the methods overrides the other. 
  3. If there is any method in a superclass and a method with the same name in subclass, then by executing a method, the method from the corresponding class will be executed.
  4. Let's see an example of this:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#Rectangle.pm

use strict;
use warnings;

package Rectangle;

sub new{

  my $class = shift;

  my $self = {
    'length' =>shift,
    'breadth' =>shift
  };

  bless $self, $class;

  return $self;
}

sub getPerimeter{
  my $self = shift;
  print "Perimeter of rectangle is ",2*($self->{'length'}+$self->{'breadth'}),"\n";
}

sub getArea{
  my $self = shift;
  return $self->{'length'}*$self->{'breadth'};
}

1;

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#file is Square.pm

package Square;

use strict;
use warnings;

use parent 'Rectangle';

sub getPerimeter{
  my $self = shift;
  print "Perimeter of square is ",4*($self->{'length'}),"\n";
}

1;

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#Perimeter.pl
use strict;
use warnings;

use Square;

my $a = Rectangle->new(5,10);

my $b = Square->new(4,4);

$a->getPerimeter();
$b->getPerimeter();

2 comments: