Perl OOP

Object oriented paradigm is one of the most useful technique to organize code in effectly. Generally that is a way to combine primitive data and implement new custom functionality under the protection. There are main two terms are used in object oriented programming. Class and its objects. Methods (subroutine) is also important which is perform some specific task. In perl programming, class is like a regular pack.

Objects

Object is an variable which are hold the reference of an instance. This instance can be contain information of primitive and custom defined variables. This variable is called instance variable. Each object of a class is capable to access class methods. And method is capable to modify class instance variable stages.

bless is a subroutine which are used to create instance (reference) of particular class. This are accepts two parameters first is an data which is form of a hash and second is the name of class. There are several way to create object in perl programming.

#!/usr/bin/perl
package Employee;
use strict;  
use warnings; 

sub new {
   my $class = shift;
   my $self = {
       name => shift,
       employee_id  => shift,
       age    => shift,
       salary =>shift
   };

   bless $self, $class;
   return $self; 
}
our $diver = new Employee("devid","oo7",25,50000);
our $salesman = new Employee("jack","oo11",22,45907);

print $diver->{"name"},"\n";
print $diver->{"employee_id"},"\n";
print $diver->{"age"},"\n";
print $diver->{"salary"},"\n\n";


print $salesman->{"name"},"\n";
print $salesman->{"employee_id"},"\n";
print $salesman->{"age"},"\n";
print $salesman->{"salary"},"\n";
Create Objects in Perl
devid
oo7
25
50000

jack
oo11
22
45907
#!/usr/bin/perl
package Fruits;
use strict;  
use warnings; 


sub new{
    my ($class,$args) = @_;
    my $self = bless $args,  $class;
}
our $apple = Fruits->new({
        name =>"Apple",
        price => 500, 
        weight => 'By Per Kg'
});
#create objects using different new instance variable
our $banana = Fruits->new({
        name =>"Banana",
        price => 50,
        location=>"Local", 
        quantity=> 'By Per Dozen'
});



print $apple->{"name"},"\n";
print $apple->{"price"},"\n";
print $apple->{"weight"},"\n\n\n";

print $banana->{"name"},"\n";
print $banana->{"price"},"\n";
print $banana->{"location"},"\n";
print $banana->{"quantity"},"\n\n\n";
Create object with different instance variable
Apple
500
By Per Kg


Banana
50
Local
By Per Dozen

Object reference

#!/usr/bin/perl
package Record;
use strict;  
use warnings; 

sub new {
   my $class = shift;
   my $self = {
       image => shift,
       pdf   => shift
   };

   bless $self, $class;
   return $self; 
}
our $data = new Record(50,100);

#assigning the reference of an instance
our $reference = $data;

$data->{"image"}=77;

print $data->{'image'},"\n";
print $reference->{'image'},"\n";
Assigning Reference to an instance object
77
77
#!/usr/bin/perl
package Record;
use strict;  
use warnings; 

sub new {
   my $class = shift;
   my $self = {
       image => shift,
       pdf   => shift
   };

   bless $self, $class;
   return $self; 
}
our $data = new Record(50,100);

#assigning the reference of $data
our $reference =\$data;

print $data->{'image'},"\n";
print $$reference->{'image'},"\n";
Referencing a instance object
50
50

Inheritance in Perl

How to achieve inheritance.

#file A.perl
#!/usr/bin/perl

use strict;
use warnings;

package A;
sub new {
   my $class = shift;
   my $self = {
       x => shift,
       y   => shift
   };

   bless $self, $class;
   return $self; 
}
1;
#file B.pl
#!/usr/bin/perl
package B;
use parent A;
$obj= new A(10,20);
print $obj->{"x"},"\n";
10

Example of Inheritance

#file A.pm

#!/usr/bin/perl

use strict;
use warnings;

package A;
sub new {
   my $class = shift;
   my $self = {
       x => shift,
       y   => shift
   };

   bless $self, $class;
   return $self; 
}
1;
#file B.pl
#!/usr/bin/perl
package B;
use parent A;
sub new {
   my $class = shift;
   my $self = $class->SUPER::new(shift,shift);
   $self->{'z'} = shift;

   bless $self, $class;
   return $self; 
}

$obj= new B(10,20,30);
print $obj->{"x"},"\n";
print $obj->{"y"},"\n";
print $obj->{"z"},"\n";
Perl Class inheritance
10
20
30


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved