Object oriented programming in Perl

Walden Systems Geeks Corner Object oriented programming in Perl tutorial developer Rutherford NJ New Jersey NYC New York North Bergen County
Perl 5 is a highly capable, feature-rich programming language with over 30 years of development. Perl 5 runs on over 100 platforms from portables to mainframes and is suitable for both rapid prototyping and large scale development projects.

Object Oriented concept in Perl is based on references, anonymous array and hashes. here are three main terms, explained from the point of view of how Perl handles objects. The terms are object, class, and method. An object in Perl is just a reference to a data type that knows what class it belongs to. The object is stored as a reference in a scalar variable. Because a scalar only contains a reference to the object, the same scalar can hold different objects in different classes. A class in Perl is a package that contains the corresponding methods required to create and manipulate objects. A method in Perl is a subroutine, defined in the package. The first argument to the method is an object reference or a package name, depending on whether the method affects the current object or the class. Perl provides a bless() function, which is used to return a reference and becomes an object.

Defining a class

It is very simple to define a class in Perl. To create a class in Perl, we first build a package. A package is a self-contained unit of user-defined variables and subroutines, which can be re-used over and over again. Perl packages provide a separate namespace within a Perl program which keeps subroutines and variables independent from conflicting with those in other packages. The scope of the package definition extends to the end of the file, or until another package keyword is encountered.


Creating and using objects

To create an instance of a class, we need an object constructor. This constructor is a method defined within the package. Most programmers choose to name the constructor method new, but in Perl we can use any name. We can use any kind of Perl variable as an object in Perl. Most Perl programmers choose either references to arrays or hashes. When creating an object, we need to supply a 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.

package Person;
sub new {
   my $class = shift;
   my $self = {
      _firstName => shift,
      _lastName  => shift,
      _ssn       => shift,
   };

   print "First Name is $self->{_firstName}
";
   print "Last Name is $self->{_lastName}
";
   print "SSN is $self->{_ssn}
";
   bless $self, $class;
   return $self;
}

Defining methods

Other object-oriented languages have the concept of security of data to prevent a programmer from changing an object data directly and they provide accessor methods to modify object data. Perl does not have private variables but we can still use the concept of helper methods to manipulate object data.

#!/usr/bin/perl 

package Person;

sub new {
   my $class = shift;
   my $self = {
      _firstName => shift,
      _lastName  => shift,
      _ssn       => shift,
   };
   print "First Name is $self->{_firstName}
";
   print "Last Name is $self->{_lastName}
";
   print "SSN is $self->{_ssn}
";
   bless $self, $class;
   return $self;
}
sub setFirstName {
   my ( $self, $firstName ) = @_;
   $self->{_firstName} = $firstName if defined($firstName);
   return $self->{_firstName};
}

sub getFirstName {
   my( $self ) = @_;
   return $self->{_firstName};
}
1;

Inheritance

Object-oriented programming has concept called inheritance. Inheritance means that properties and methods of a parent class will be available to the child classes. So we don't have to write the same code again and again, we can just inherit a parent class. Perl searches the class of the specified object for the given method or attribute. Perl searches the classes defined in the object class's @ISA array. If no method is found, Perl uses an AUTOLOAD subroutine, if one is found in the @ISA tree. If a matching method still can't be found, Perl searches for the method within the UNIVERSAL class that comes as part of the standard Perl library. If the method still has not found, Perl raises a runtime exception.