Open In App

Perl | Objects in OOPs

Improve
Improve
Like Article
Like
Save
Share
Report

Perl is an Objected Oriented, dynamic and interpreter based programming language. In object-oriented programming, we have three main aspects, which are, object, class, and methods. An object is a data type which can be specifically called as an instance of the class to which it belongs. It can be a collection of data variables of different data types and as well as a collection of different data structures. Methods are functions which work on these objects of the class.   Following is a basic example to better understand how objects can be used in Perl: First, we need to define the class. In Perl, it is done by building the package of the class. Package is an encapsulated entity which has all the data members and the methods of the concerned class.

Perl is a high-level, general-purpose programming language that was originally designed for text manipulation and system administration tasks. It was created in the late 1980s by Larry Wall and has since evolved into a popular language for a wide range of applications, including web development, network programming, and data analysis.

Perl is known for its flexibility, expressive syntax, and powerful string manipulation capabilities. It supports both procedural and object-oriented programming paradigms, and includes a wide range of built-in functions and modules that make it easy to handle complex tasks.

One of the key features of Perl is its regular expression support, which allows for advanced text processing and pattern matching. Perl also has a large and active community of developers who contribute to the language’s development and support its various modules and tools.

Overall, Perl is a versatile and powerful language that can be used for a wide range of programming tasks, particularly those involving text and data processing.

package Employee;

Here, Employee is the class name. Second task, is to create an instance of the package(i.e the object). For this, we need a constructor. A constructor is a subroutine in Perl which generally is given the name ‘new’. However, the name is user defined hence not restricted to ‘new’. 

PERL




package Employee;
 
# Constructor with name new
sub new
{
    my $class = shift;
    my $self = {
                  _serialNum => shift,
                  _firstName => shift,
                  _lastName  => shift,
              };
     
    bless $self, $class;
    return $self;
}


In the constructor we are defining a simple hash reference $self to design our object. Here, the object will have three values serialNum, firstName, and lastName of an Employee, which means every employee concerned to this will have their own set of serial number, firstname, and lastname. The my keyword is an access specifier which is localizing $class and $self to be within the enclosed block. shift keyword takes the package name from the default array “@_” and pass it on to the bless function. bless function is used to return a reference which ultimately becomes an object. And in the end, the constructor will finally return the instance of the class Employee(here). Finally, the main part is how to initialize an object. It can be done in the following way:

$object = new Employee(1, "Geeks", "forGeeks");

Here, $object is a scalar variable which is a reference to the hash defined in the constructor. Following is the example program for the creation and implementation of Objects in OOPs: 

perl




use strict;
use warnings;
 
# class with the name Employee
package Employee;
 
# constructor with the name new
sub new
{           
    # shift will take package name
    # and assign it to variable 'class'
    my $class = shift;   
     
    # defining the hash reference
    my $self = {                        
                _serialNum => shift,
                _firstName => shift,
                _lastName => shift,
               };
     
    # Attaching object with class
    bless $self, $class;
     
    # returning the instance of class Employee
    return $self;                        
}
 
# Object creation of the class
my $object = new Employee(1, "Geeks", "forGeeks");
 
# object here is a hash to a reference
print("$object->{_firstName} \n");            
print("$object->{_serialNum} \n");   


An object in Perl works in the same way as in other languages like C++, Java, etc. Above program shows the working of an object in Perl, its creation and its use in the class.

In Perl, objects are instances of classes. A class is a blueprint or a template for creating objects, which defines a set of attributes (properties) and methods (functions) that the objects of that class can have.

Here’s an example code that demonstrates the creation of a class and the creation of objects from that class:

Perl




#!/usr/bin/perl
# your code here
# Define a class called "Person"
package Person;
 
# Define the constructor method for the class
sub new {
    my $class = shift;
    my $self = {
        name => shift,
        age => shift,
        gender => shift,
    };
    bless $self, $class;
    return $self;
}
 
# Define a method for the class that returns the person's name
sub get_name {
    my ($self) = @_;
    return $self->{name};
}
 
# Define a method for the class that returns the person's age
sub get_age {
    my ($self) = @_;
    return $self->{age};
}
 
# Define a method for the class that returns the person's gender
sub get_gender {
    my ($self) = @_;
    return $self->{gender};
}
 
# Create two objects of the "Person" class
my $person1 = new Person("John", 25, "Male");
my $person2 = new Person("Jane", 30, "Female");
 
# Print the properties of the objects to the console
print "Person 1: " . $person1->get_name() . ", " . $person1->get_age() . ", " . $person1->get_gender() . "\n";
print "Person 2: " . $person2->get_name() . ", " . $person2->get_age() . ", " . $person2->get_gender() . "\n";


Output

Person 1: John, 25, Male
Person 2: Jane, 30, Female

In this code, a class called “Person” is defined using the package keyword. The new() method is defined as the constructor for the class, which initializes a new object of the class with the given properties. Three properties (name, age, and gender) are defined for the objects, which are passed as arguments to the constructor method.

Three methods (get_name(), get_age(), and get_gender()) are also defined for the class, which return the corresponding property value for an object of the class.

Two objects ($person1 and $person2) are then created from the Person class using the new() method, and their properties are printed to the console using the get_name(), get_age(), and get_gender() methods.



Last Updated : 13 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads