In this modern world, where the use of programming has moved to its maximum and has its application in each and every work of our lives, we need to adapt ourselves to such programming paradigms that are directly linked to the real-world examples. There has been a drastic change in the competitiveness and the complexity of the real-world problems, thus the industry now requires some versatile approaches towards these complexities. The concept of Object-Oriented Programming language copes up comfortably with these complexities and is a perfect way of shaping up any programming language in a way that the real-world examples become very easy to solve. Object-Oriented Programming(OOP) uses the methodology of emphasizing more on objects rather than the procedure. The OOPs concept is about working in terms of objects and the interaction between objects.
Note: Object-oriented programming aims to implement real-world entities like inheritance, encapsulation, polymorphism, etc in programming. The main aim of OOP is to bind together the data and the functions that operate on them so that no other part of the code can access this data except that function.
Object -
An object is an instance of a class of the data structure with some characteristics and behavior. For Example, we say an "Apple" is an object. Its characteristics are:it is a fruit, its color is red, etc. Its behavior is:"it tastes good". In the concept of OOPs, the characteristics of an object are represented by its data and the behavior is represented by its associated functions. Thus an object is an entity that stores data and has its interface through functions.
Class -
A class is an expanded concept of data structures. It defines a prototype blueprint of objects comprising of data. And an object is an instance of a class. A class comprises of the data members and data functions and it is a pre-defined data type made as per the user's requirements which can be accessed and used by creating an instance of that class.
Example:
Consider a class of School. There may be schools with different names and structure but all of them share some common characteristics like students, teachers, staff, etc. So here school is the class with teachers, students, and Parents as the data members, and the member functions can be calculate_students_marks(), calculate_teachers_salary(), and Parents_Database().
Data Member: Data members are the data variables and member functions are the functions used to manipulate these variables and together these data members and member functions define the properties and behavior of the objects in a Class.

Defining a Class -
It is very easy to define a class in Perl. In Perl, a Class corresponds to a package. To define a class we first load and build a Package. A Package is a pre-contained unit of user-defined variables and subroutines, which can be used throughout the program.
Syntax:
package Class_name
Creating Class and Using Objects
A class in Perl can be created by using the keyword package but to create an object, a constructor is called. A constructor is defined in a class as a method.
Creating an instance of a class
A class name and a constructor name can be as per user's requirement. Most of the programmers prefer to use 'new' as a constructor name for their programs as it is easy to remember and is more feasible to use than any other complex constructor name.
Perl
package student // This is the class student
sub Student_data // Constructor to class
{
my $class = shift;
my $self = {
_StudentFirstName => shift;
_StudentLastName => shift;
};
print "Student's First Name is $self ->{_StudentFirstName}\n";
print "Student's Last Name is $self ->{_StudentLastName}\n";
bless $self, $class;
return $self;
}
In the above example code, a function named bless is used. This function is used to attach an object to a class which is passed to it as an argument.
Syntax:
bless Object_name, Class Name
Creating an Object
An object in Perl is instantiated by using the constructor defined in the class. An object name can be any variable as per user's requirement, but it is conventional to name it with respect to the relevancy with the class.
Perl
$Data = Student_data student( "Shikhar", "Mathur");
Example:
Perl
use strict;
use warnings;
package student;
# constructor
sub student_data
{
# shift will take package name 'student'
# and assign it to variable 'class'
my $class_name = shift;
my $self = {
'StudentFirstName' => shift,
'StudentLastName' => shift
};
# Using bless function
bless $self, $class_name;
# returning object from constructor
return $self;
}
# Object creating and constructor calling
my $Data = new student_data student("Geeks","forGeeks");
# Printing the data
print "$Data->{'StudentFirstName'}\n";
print "$Data->{'StudentLastName'}\n";
The concept of using classes in Object-Oriented Programming is very important as it completely depicts the real-world applications and can be adopted in real-world problems.
In Perl, classes can be defined using the package keyword, which is used to define a namespace for variables, subroutines, and other code constructs. Here's an example of defining a class in Perl:
Perl
# Define the Person class
package Person;
# Constructor method to create a new object of the class
sub new {
my $class = shift;
my $self = {
_firstName => shift,
_lastName => shift,
_ssn => shift,
};
# Bless the reference as an object of the class
bless $self, $class;
return $self;
}
# Accessor method to get the first name of the person
sub getFirstName {
my ($self) = @_;
return $self->{_firstName};
}
# Accessor method to set the first name of the person
sub setFirstName {
my ($self, $firstName) = @_;
$self->{_firstName} = $firstName if defined($firstName);
return $self->{_firstName};
}
# End of package declaration, required in Perl
1;
# Use the Person class
use Person;
# Create a new object of the Person class
my $person = Person->new("John", "Doe", "123-45-6789");
# Call the getFirstName method to get the first name of the person
my $firstName = $person->getFirstName();
print "First name: $firstName\n";
# Call the setFirstName method to change the person's first name
$person->setFirstName("Jane");
$firstName = $person->getFirstName();
print "New first name: $firstName\n";
Output:
First name: John
New first name: Jane
This code creates a new Person object with the properties firstName (set to "John"), lastName (set to "Doe"), and ssn (set to "123-45-6789"). It then calls the getFirstName() method to get the person's first name and prints it to the console.
Next, it calls the setFirstName() method to change the person's first name to "Jane". It then calls getFirstName() again to get the person's new first name and prints it to the console.
Similar Reads
Basics
Perl is a general purpose, high level interpreted and dynamic programming language. Perl supports both the procedural and Object-Oriented programming. Perl is a lot similar to C syntactically and is easy for the users who have knowledge of C, C++. Since Perl is a lot similar to other widely used lan
3 min read
Perl is a general-purpose, high level interpreted and dynamic programming language. It was developed by Larry Wall, in 1987. There is no official Full form of the Perl, but still, the most used expansion is "Practical Extraction and Reporting Language". Some of the programmers also refer Perl as the
9 min read
Prerequisite: Introduction to Perl Before, we start with the process of Installing Perl on our System, whether it be Windows, Linux or Macintosh. We must have first-hand knowledge of What the Perl Language is and what it actually does?. Perl is a general purpose, high level interpreted and dynamic p
3 min read
Perl is a general purpose, high level interpreted and dynamic programming language. Perl was originally developed for the text processing like extracting the required information from a specified text file and for converting the text file into a different form. Perl supports both the procedural and
10 min read
Perl programming language is exclusively designed for text processing purposes. Its abbreviation denotes Practical Extraction and Report Language. It is compatible on various platforms, such as Windows, Mac OS, and almost all versions of UNIX. Hello World! program in every programming language gives
3 min read
Arrays & Lists
In Perl, array is a special type of variable. The array is used to store the list of values and each object of the list is termed as an element. Elements can either be a number, string, or any type of scalar data including another variable. Example: @number = (50, 70, 46); @names = ("Geeks", "For",
6 min read
In Perl, array is a special type of variable. The array is used to store the list of values and each object of the list is termed as an element. Elements can either be a number, string, or any type of scalar data including another variable. Arrays can store any type of data and that data can be acce
3 min read
Perl provides various inbuilt functions to add and remove the elements in an array. .string-table { font-family: arial, sans-serif; border-collapse: collapse; border: 1px solid #5fb962; width: 100%; } .string-table td, th { background-color: #c6ebd9; border: 1px solid #5fb962; text-align: left; padd
3 min read
Introduction to Lists A list is a collection of scalar values. We can access the elements of a list using indexes. Index starts with 0 (0th index refers to the first element of the list). We use parenthesis and comma operators to construct a list. In Perl, scalar variables start with a $ symbol wher
4 min read
OOP Concepts
Object-oriented programming: As the name suggests, Object-Oriented Programming or OOPs refers to languages that uses objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc in programming. The main aim of OOP is to bind to
7 min read
In this modern world, where the use of programming has moved to its maximum and has its application in each and every work of our lives, we need to adapt ourselves to such programming paradigms that are directly linked to the real-world examples. There has been a drastic change in the competitivenes
6 min read
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
6 min read
Methods are used to access and modify the data of an object. These are the entities which are invoked with the use of objects of a class or a package itself. Methods are basically a subroutine in Perl, there is no special identity of a method. Syntax of a method is the same as that of a subroutine.
5 min read
Constructors Constructors in Perl subroutines returns an object which is an instance of the class. In Perl, the convention is to name the constructor "new". Unlike many other OOPs, Perl does not provide any special syntax for constructing an object. It uses Data structures(hashes, arrays, scalars) t
4 min read
In any object-oriented programming language, Overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes. When a method in a subclass has the same name, same parameters or signat
6 min read
Inheritance is a key concept in object-oriented programming that allows you to define a new class based on an existing class. The new class, called a subclass or derived class, inherits all of the properties and methods of the existing class, called the superclass or base class, and can also define
7 min read
Polymorphism is the ability of any data to be processed in more than one form. The word itself indicates the meaning as poly means many and morphism means types. Polymorphism is one of the most important concepts of object-oriented programming languages. The most common use of polymorphism in object
4 min read
Encapsulation in Perl is the process of wrapping up of data to protect it from the outside sources which need not have access to that part of the code. Encapsulation is a part of the Object-oriented programming, it is used to bind the data and the subroutines that are used to manipulate that data. I
6 min read
File Handling
In Perl, file handling is the process of creating, reading, writing, updating, and deleting files. Perl provides a variety of built-in functions and modules that make it easy to work with files. Here's an introduction to file handling in Perl: File modes:When opening a file in Perl, you need to spec
7 min read
A filehandle is an internal Perl structure that associates a physical file with a name. All filehandles have read/write access, so once filehandle is attached to a file reading/writing can be done. However, the mode in which file handle is opened is to be specified while associating a filehandle. Op
4 min read
A filehandle is a variable that is used to read and write to a file. This filehandle gets associated with the file. In order to write to the file, it is opened in write mode as shown below: open (FH, â>â, âfilename.txtâ); If the file is existing then it truncates the old content of file with the
3 min read
Perl was originally developed for the text processing like extracting the required information from a specified text file and for converting the text file into a different form. These operations can be performed by the use of various inbuilt file functions. Example: Perl #!/usr/bin/perl # Opening a
2 min read