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;
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;
package Employee;
sub new
{
my $class = shift ;
my $self = {
_serialNum => shift ,
_firstName => shift ,
_lastName => shift ,
};
bless $self , $class ;
return $self ;
}
my $object = new Employee(1, "Geeks", "forGeeks");
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
package Person;
sub new {
my $class = shift ;
my $self = {
name => shift ,
age => shift ,
gender => shift ,
};
bless $self , $class ;
return $self ;
}
sub get_name {
my ( $self ) = @_ ;
return $self ->{name};
}
sub get_age {
my ( $self ) = @_ ;
return $self ->{age};
}
sub get_gender {
my ( $self ) = @_ ;
return $self ->{gender};
}
my $person1 = new Person( "John" , 25, "Male" );
my $person2 = new Person( "Jane" , 30, "Female" );
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.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
13 Apr, 2023
Like Article
Save Article