Updated April 3, 2023
Definition of Perl Class
Perl class is very easy and simple to define, class in perl is corresponding to a package of perl in its simplest form. It is defined as it is a package that having constructor to create perl object, this object is having any type of variable but mostly it is a reference to array or hash is used. It is most important and useful in perl to define Perl object. To define we need to build a package first, package is nothing but sub-routines or user defined variables.
How to Declare Perl Class?
The procedure is as follows.
- It is very easy and simple to declare. To declare any class we need to mention package keyword before the class name.
Syntax:
Package class_name;
Parameter:
Below is a parameter description of Perl declare syntax.
- Package: Package is nothing but a specified namespace within the Perl code or program which contains sub-routines or user-defined variables. The package is very important at the time of declaring the class.
- Class name: We can define any class name to the program in Perl. But it is specific to the program which we have implemented.
-
- A package name in perl class is subroutines or a user-defined variable. We can reuse this again in the code.
- Package in perl will provide namespace within the program mostly which contains subroutines and variables.
- The scope of class or package will end of the file or until we cannot create another package in one file.
- Below is the example to declare per class is as follows.
Example:
Package declare_perlClass;
- In the above example, we have declared class name as declare_perlClass. This class corresponds to a package name.
- Before declaring declare_perlClass class firstly we need to load the package in Perl.
- A loaded package is used in throughout the program. The scope of the package is throughout the program or until we define another package.
How does Class Work in Perl?
The working is as follows:
- It is defined as it is a package that having constructor to create perl object, this object is having any type of variable but mostly it is a reference to array or hash is used.
- It is nothing but a concept of data structures. It defines the object of data in perl.
- To define we need to build a package first, the package is nothing but sub-routines or user-defined variables.
- The object is nothing but an instance of the class.
- It is very easy and simple to define, Itl is corresponding to a package of perl in its simplest form.
- It is most important and useful in Perl to define Perl’s object.
The pictorial representation of how class works in Perl are as follows:
- We have to take the example of class as an employee. We have defining member function and data members using the class as an employee.
- There may be an employee with different names and structures but all of them have some common characteristics like emp_id, emp_dept, and emp_name.
- Here we have to define an employee as a class with emp_id, emp_dept, and emp_name as data members.
- We have also used a bless function.
Syntax bless function:
bless name_of_object, name_of_class;
- We can also create an object by using a perl class. We have to create an object based on the class name.
- Data member in perl class is nothing but data variables and member function is a function which manipulated the variables.
- In the below example, we have to define member variable as emp_id, emp_dept and emp_name.
- We can define any class name to the program in perl. But it is specific to the program which we have implemented.
- In the above example we have to define class name as employee and data member as emp_id, emp_dept and emp_name.
Examples
The examples are as follows:
Example #1 – Using bless function in perl class:
- In the below example, we have created perl class using bless function. We have created a class name as an employee.
- Member variables of class name is ’emp_id’ and ’emp_salary’.
- We have defined a bless function. In bless function, we have to define object name and class name.
- We have also define constructor of employee class name as new_emp to define member variables.
Code:
use strict;
use warnings;
package employee;
## Constructor of employee class.
sub new_emp{
#the package name 'employee' is in the default array shift.
my $class = shift;
#object of a class.
my $self = {
'emp_id' => shift,
'emp_salry' => shift
};
#blessing with object name and class name
bless $self, $class;
#returning object from constructor
return $self;
} 1;
print "emp_id:- 101\n";
print "emp_salary:- 25000\n";
Output:
Example #2 – creating class and using objects
- In the below example we have created perl class using an object. We have created class name as an employee.
- Member variables of class name is ‘Employee_FirstName’ and ‘Employee_LastName’.
- We have defined a bless function. In bless function, we have to define object name and class name.
- We have also define constructor of employee class name as employee_data to define member variables.
Code:
use strict;
use warnings;
package employee; ## This is the class of employee.
sub employee_data ## Constructor of employee class.
{
# Shift is default array will used in 'employee' class and then assign
# it to employee class variables
my $class_name = shift;
my $self = {
'Employee_FirstName' => shift,
'Employee_LastName' => shift
};
# using bless function.
bless $self, $class_name;
# returning object from constructor of employee_data.
return $self;
}
# Object creating name as employee from employee_data and calling a constructor.
my $Data = employee_data employee("Emp_FirstName:- ABC", "Emp_LastName:- PQR");
# printing the data
print "$Data->{'Employee_FirstName'}\n";
print "$Data->{'Employee_LastName'}\n";
Output:
Recommended Articles
We hope that this EDUCBA information on “Perl Class” was beneficial to you. You can view EDUCBA’s recommended articles for more information.