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";
OutputPerson 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.
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