Jan 27, 2022

Object Oriented PERL Programming



In this article we will cover the Object Oriented PERL through the below mentioned subtopics :

  • Package & Module in PERL
  • Package/Module Example
  • Object Oriented Programming (OOP) Concept
  • When to use OOP 
  • Object/Class/Blessing/Constructor/Destructor/Methods/Inheritance
  • Coding Example : Class & Object Creation in PERL
  • Coding Example : Inheritance Class & Object Creation in PERL 
  • Coding Example : Class & Object Creation in PERL using Moose.pm


Perl Package & Module :

  • A namespace stores names of variables, subroutines, filehandles, and formats belonging to its scope. 
  • Each namespace has its own symbol table, which is basically a hash table.
  • Perl Namespaces are called "packages".
  • The default namespace for any PERL program is main.
  • Package declaration tells the compiler which namespace to prefix to our variables.
  • A module is a package defined in a file whose name is the same as the package.
  • All Perl modules are contained in global @INC array, which contains a list of library directories/paths.
  • A module/package can be included in your program with use or require functions of PERL.


A Perl Module :

  • A module is just a set of related functions in a library file! 
  • Perl package with the same name as the file (FileName=SomeModule.pm). 
  • File extension is .pm and NOT .pl

The module definition starts with : 

package SomeModule;  

use warning;

use strict;

# Code of this module goes here

# Subroutines 

# end of this module is declared by

1;  # don't forget to return a true value from the file


Usage of Custom Perl Module :

Keep the PM file in your current working directory

For any other directory , specify it in your .cshrc file & source it:

setenv PERL5LIB '/home/<userName>/PERL_Modules'

setenv PERLLIB '/home/<userName>/PERL_Modules'

Then use in your present myProg.pl as

use SomeModule;

or

require SomeModule;

or

require "SomeModule.pm";


Package/Module Example :

Here is how we create a PERL Module abc.pm :

And here is the example how we use it :


Object Oriented Concept :



  • Most object systems share a number of common concepts.
  • Understanding the concepts will make it much easier to read and write object-oriented code. 
  • Perl's OO system is class-based. Class-based OO is fairly common. 
  • It's used by Java, C++, C#, Python, Ruby, and many other languages. 

When to Use OOP ?

  • The system being designed is large, or is likely to become large over time.
  • The data can be put into obvious structures.
  • There is prominent of inheritance and polymorphism.
  • There is a piece of data on which many different operations are applied.
  • You may need to perform the same general operations on related types of data, but with slight variations depending on the specific type of data the operations are applied to.
  • Chances are there that you may add new data types later.
  • The implementation of individual components of the system is likely to change over time.
  • The system design is already object-oriented.
  • Large numbers of other programmers from multi site will be using your code modules.

Objects :

  • An object is a data structure that bundles together data and subroutines which operate on that data.
  • An object's data is called attributes, and its subroutines are called methods.
  • An object can be thought of as a noun (a person, a web service, a computer).
  • An object represents a single discrete thing.
  • For example, an object might represent a file. 
  • The attributes for a file object might include its path, content, and last modification time. 

Class :

  • A class defines the behavior of a category of objects.
  • A class is a name for a category and a class also defines the behavior of objects in that category.
  • A specific object is often referred to as an instance of a class.

In Perl, any package can be a class.

The difference between a package which is a class and one which isn't is based on how the package is used. Here's our "class declaration" for the File package: 

Blessing :

Most Perl objects are hashes, but an object can be an instance of any Perl data type (scalar, array, etc.). Turning a plain data structure into an object is done by blessing that data structure using Perl's bless function.

We sometimes say that an object has been "blessed into a class".

Constructor :

A constructor creates a new object belonging to a class. In Perl, a class's constructor is just another method, unlike some other languages, which provide syntax for constructors. Most Perl classes use new as the name for their constructor:

my $file = File→new(...);

Methods :

Method is a subroutine that operates on an object. 

In Perl, methods are simply subroutines that is defined inside a class's package. 

Methods are always written to receive the object as their first argument.

Attributes :

Each class can define its attributes. When we instantiate an object, we assign values to those attributes. For example, every File object has a path. Attributes are sometimes called properties.

Inheritance : 

Inheritance lets you create a specialized version of an existing class. Inheritance lets the new class reuse the methods and attributes of another class.

For example, we could create an File::MP3 class which inherits from File. An File::MP3 is-a more specific type of File. All mp3 files are files, but not all files are mp3 files.

We often refer to inheritance relationships as parent-child or super-class/subclass relationships. Sometimes we say that the child has an is-a relationship with its parent class.

File is a super-class of File::MP3, and File::MP3 is a subclass of File.

package File::MP3;

use parent 'File';

Overriding :

  • Overriding methods and method resolution
  • Inheritance allows two class to share code. By default, every method in the parent class is also available in the child. 
  • The child can explicitly override a parent's method to provide its own implementation. 

Object Oriented PERL by Example :


So far we have walked through the theories , now we will do the object oriented programming using PERL .
In the below example we create the class "Student" . The "new" is the constructor and "DESTROY" is the destructor of the class.
We have three methods : setName , setSurname and setRoll to assign Name, Surname and Roll-No to each student.
The method "display" is to printout the rerecords of a student . 
Here goes the code :

In the below code we create two student objects from the above class :

Once we execute the code we get the below output :




Inheritance Example :


In the next code we will exemplify the Inheritance. Here the derived class is "intern" whose parent class is "student".

Here goes the inheritance class :

Now we create the objects of this class in the below example :
Once we execute the code we get the below output :




Creating Class Using Moose Package :


We have a PERL package , Moose.pm which is available from CPAN Network . We can use the module and create PERL objects in the easy way. Using moose we can declare the class i the below way.

 

And we can use it to create objects in the below fashion :


Once we execute the codes we get the below output :




The Moose.pm can be Installed in your Ubuntu System Easily . See the below Video :



The Entire Article is Well Narrated in the below video :




Courtesy : Image by Jae Rue from Pixabay