The subroutine is also called a function that takes inputs executes a sequence of a code block of statements and returns the output.
Subroutines are used and created when multiple code statements want to repeatedly execute in different places.
It helps code reusability and clean separation of code in logic and features.
In this tutorial, Learn
- subroutine definition
- subroutine return type
- subroutine parameters
How to create and define subroutines in Perl
a subroutine can be created with the keyword sub
, followed by the name of the subroutine and the body enclosed in {}
.
The subroutine contains the following syntax.
sub subroutine_name{
//body contains code block;
}
subroutine_name
: Name of the Subroutine.
the body is a Perl code statement that executes in sequential order
the subroutine body is enclosed in {}
Once the above code is defined, The subroutine is called with the below syntax. Subroutine Invocation syntax:
subroutine_name(arguments);
subroutine_name
: Subroutine name executed
arguments: parameters to the subroutine.
Here is a simple function definition.
## Check number is positive or not
sub negative
{
# Get the given number
$number = $_[0];
if($number<0){
return 1;
}
else{
return 0
}
}
## Calling subroutine
print( negative(-12));
Output:
1
Subroutine parameters
subroutine parameters are stored in a Perl special variable @_ as an array. Perl subroutines accept any number of parameters.
printParameters(1, 2, 3, 4, 5, 6);
printParameters(1..3);
printParameters(2,13,5,8,7);
printParameters("a".."z");
sub printParameters {
$args = @_ ;
print("Arguments Count: $args\n");
}
Output:
Arguments Count: 6
Arguments Count: 3
Arguments Count: 5
Arguments Count: 26
You can also assign an array into scalar variables as given
In the below example, subroutine arguments are assigned to individual variables, ($first, $secibd) = @_ ;
called scalar assignment
add(2, 3);
sub add {
($first, $secibd) = @_ ;
print("$first: $second\n");
}
Perl Subroutine examples types
- Subroutine without arguments and return type:
This example contains a subroutine with no arguments and no return type
sub getMsg{
print "hello";
}
getMsg();
getMsg;
- Functions without arguments and has return type:
This example shows a subroutine without arguments and returns a type. return keyword used to return the control to the calling subroutine.
sub getMsg{
return "hello";
}
print(getMsg());
print( getMsg);
- subroutine with arguments and without return type:
This example shows a subroutine with arguments and without return type.
arguments are stored in Perl special variable
$_
as an array. array values can be retrieved using an index.
## add numbers
sub add
{
# Get Given numbers
local $number1 = $_[0];
local $number2 = $_[1];
print "$number1 +$number2";
}
add(1,3);
- Subroutine with arguments and return type: The below subroutine contains arguments and returns the result to the caller subroutine
## add numbers
sub add
{
# Get the Given number
local $number1 = $_[0];
local $number2 = $_[1];
my $result=$number1 +$number2;
return $result;
}
print(add(1,3));
Nesting Subroutines
subroutines called inside another subroutine called nested subroutine. printCount() subroutine declared and called inside the same function.
printCount();
sub printCount{
print("$index\n");
$index++;
if($index<5){
printCount();
}
}
How to declare a private function
functions are declared without a name and assigned to a Perl variable, also called subroutine expression.
calling these function using $getMessage->( $self )
syntax.
private functions per example
my $getMessage = sub {
return 'hello world';
};
print($getMessage->( $self ))
Subroutine Advantages
The Subroutine has advantages in Perl.
- Code declared once and used and executed in multiple places, allows reusability
- Code separation into feature and logic wise and enhances code
- Unit testing is easy as you need to test functions only
- Development and productivity is improved
- Helps functions to debug and fix issues