Introduction to Perl bless function
In Perl, bless function is a built-in function which can be defined as a function for marking any variable in the program as an object of a particular class using this function which takes a class name and reference to which this object refers to which class and internally informs the Perl compiler as the variable that is passed to this bless function is marked variable as an object of any particular class. In general, we can define bless function() as a function for making to know the program the marked variable with this bless a function is an object that belongs to a particular class where the name of class and the reference to this variable is passed to this function.
Working of bless() function in Perl with examples
In this article, we will discuss a built-in function where to make the program understand that the variable can be made an object of a particular class in Perl where this function is known as the bless() function. Unlike in other programming languages, Perl creates an object similar to the creation or declaring of variables. So to create an object we do create it the same as how variables are declared but how would the program distinguish between variable and object. Therefore the Perl programming language provides a built-in function called bless() function, which makes the variable look like an object of the particular class that it belongs to.
This bless() function usually class name and a reference to a variable that needs to be made as an object as arguments so that it can internally set a flag on the variable which makes the program to know that this variable is identified as an object of some particular class and this object belong to that class.
Now let us see the syntax and examples of how the bless() function works in the below example.
Syntax:
bless var_ref, class_name;
Parameters:
- var_ref: this parameter is used for referring to the variable as an object of the class whose class name is specified.
- class_name: this parameter is used for specifying the class name to which the object that is created by marking the variable reference as an object of this class specified in this parameter.
This function can take the only var_ref as an argument also and this function returns the reference of marked variable as a reference to an object which is blessed into a particular or specified class name as specified in the arguments that are passed to the function.
In general, this function tells the thing that is referenced by the parameter var_ref is now not a variable but an object of the particular class which is specified again as the parameter and which makes this reference variable as an object of this class and it belongs to this class only. And if there is no class name specified as an argument to the bless function where only var_ref reference parameter is passed then by default the bless() function takes the current class package as the class for the object to be created and mark it as the object of that current class.
Examples
Now let us see a simple example of demonstrating the bless() function in the below section.
Example #1
Code:
use strict;
use warnings;
print "Demonstration of bless() function in Perl.";
print "\n";
print "\n";
package student_data;
sub stud
{
my $class_name = shift;
my $var_ref = {
'stud_first_name' => shift,
'stud_course_name' => shift,
'stud_age' => shift,
};
print "The bless() function is now implemented:";
print "\n";
bless $var_ref, $class_name;
return $var_ref;
}
print "Object creation";
print "\n";
print "\n";
my $info = stud student_data("Alen","Python",32);
print "The student's name is :";
print "\n";
print "$info->{'stud_first_name'}\n";
print "\n";
print "The course name student taken is:";
print "\n";
print "$info->{'stud_course_name'}\n";
print "\n";
print "The student's age is :";
print "\n";
print "$info->{'stud_age'}\n";
print "\n";
Output:
In the above program, we can see we have defined class “stud” using keyword package in Perl. Then we have created a subroutine in which we are printing the student’s details. This function or subroutine can also be said as a constructor of a class where we are creating the objects for the class “stud”. And the variable $var_ref is created similar to creating the scalar variables in Perl. This variable we are referring each variable to the class name using the variable reference to object creation and class name as arguments in the bless() function in the above program. Then we are returning a reference value of which object is created using var_ref and it belongs and points to the class stud. Then we are printing each value of the objects created.
Now we will see bless function taking only one argument and also two arguments in the below example.
Example #2
Code:
use strict;
use warnings;
print "Demonstration of bless() function in Perl.";
print "\n";
print "\n";
package student_data;
sub stud
{
my $class_name = shift;
my $var_ref = {
'stud_first_name' => shift,
'stud_course_name' => shift,
'stud_age' => shift,
};
print "The bless() function is now implemented:";
print "\n";
bless $var_ref;
return $var_ref;
}
package Employee;
sub emp {
my $class2 = shift;
my $var_ref2 = {
'emp_firstName' => shift,
'emp_lastName' => shift,
'emp_epn' => shift,
};
bless $var_ref2, $class2;
return $var_ref2;
}
print "Object creation";
print "\n";
print "\n";
print "Bless function takes only one argument:";
print "\n";
print "\n";
my $info = stud student_data("Alen","Python",32);
print "The student's name is :";
print "\n";
print "$info->{'stud_first_name'}\n";
print "\n";
print "The course name student taken is:";
print "\n";
print "$info->{'stud_course_name'}\n";
print "\n";
print "The student's age is :";
print "\n";
print "$info->{'stud_age'}\n";
print "\n";
print "Bless() function takes two argument:";
print "\n";
print "\n";
my $per_info = emp Employee("Ram", "Shukla", 343);
print "The employee's name is :";
print "\n";
print "$per_info->{'emp_firstName'}\n";
print "\n";
print "The employee's second name student taken is:";
print "\n";
print "$per_info->{'emp_lastName'}\n";
print "\n";
print "The employee's employee number is :";
print "\n";
print "$per_info->{'emp_epn'}\n";
print "\n";
Output:
In the above program, we can see we have declared two classes “student_data” and “Employee” wherein the class “Student_data” we have defined function bless() with the single argument so when the variables reference is only passed it will by default take only the values of its current class but not of the “employee” class. Therefore the values of student data and employee data are printed respectively.
Conclusion
In this article, we conclude that the bless() function is a built-in function in Perl for marking the variables reference to object creation which belongs to the particular class with its class name specified as an argument in the bless() function. In this article, we have seen examples of bless() function taking two arguments and what happens if there are two classes and the class name is not specified as an argument in the function which will by default take the current class name.
Recommended Articles
This is a guide to Perl bless. Here we discuss the introduction and Working of bless() function in Perl with examples for better understanding. You may also have a look at the following articles to learn more –