User Defined Functions in Perl
What is a User Defined Function in Perl?
How to call a function using user defined functions?
Explanation
The User Defined Function is a set of code that will be executed when the function is called. In Perl user defined function does'nt require arguments, but if needed arguments can be passed.
Syntax:
sub function_name
{
function_body
}
In the above syntax "function_name" specifies a unique name for the function, in the "function body" contains a set of code to be executed when there is a function call.
Example for user defined func, using function call:
#! C:programfilesperlbinperl
print "content-type: text/htmlnn";
sub present
{
print "The Key is present.n";
}
sub notpresent
{
print "The Key not present.n";
}
%emp = ('empno' => '12422' , 'empname' => 'Kate Anderson',
'empph' => '9167552148', 'empcode' => 'ADS212');
if (exists $emp{'empcode'})
{
present();
}
else
{
notpresent();
}
Result :
The Key is present.
In the above example using exists() function a value is checked in an array, if present, it calls a function "present()" otherwise, it calls a function "notpresent()". Since the "empcode" is present, it displays "The Key is present".