Perl | Multiple Subroutines
Last Updated : 29 May, 2019
Prerequisite: Perl | Subroutines or Functions
A Perl function or subroutine is a group of statements that together perform a specific task. In every programming language user want to reuse the code. So the user puts the section of code in function or subroutine so that there will be no need to write code again and again.
In Perl, the terms function, subroutine, and method are the same but in some programming languages, these are considered different. The word subroutines are used most in Perl programming because it is created using keyword sub. Whenever there is a call to the function, Perl stops executing all its program and jumps to the function to execute it and then returns back to the section of code that it was running earlier. One can avoid using the return statement.
Defining Subroutines:
The general form of defining the subroutine in Perl is as follows-
sub subroutine_name { # body of method or subroutine }
In Perl, a program can hold multiple subroutines with the same name without generating an error, because Perl allows to write multiple subroutines with the same name unless they have different Signatures. This can be defined by using different arity for each subroutine having the same name.
Arity of a Subroutine: Perl subroutines can have the same name unless they have a different set of Arity. Arity refers to the number of arguments that a subroutine contains. These arguments may or may not be of the different datatype. As long as the arity of subroutines differs from each other, the Perl program will not generate any error.
Use of ‘multi’ keyword:
Multiple subroutines in Perl can be created by using the keyword ‘multi’. This helps in the creation of multiple subroutines with the same name.
Example:
multi Func1($var){statement}; multi Func1($var1, $var2){statement1; statement2;}
Use of Multiple subroutines is very common in the creation of built-in functions and most of the operators in a Programming language like Perl. This helps in reducing the complexity of the program by not using different names for every other subroutine. Whatever code statement that is required, just pass the number of arguments required for that function and the work will be done. In this case, the compiler will pick the version of subroutine whose Function signature matches the one called for execution.
Various programs like Factorial of a number, Fibonacci series, etc. require more than one function to solve the problem. Use of Multiple subroutines will help reducing the complexity of such programs.
Example 1: Sum of Fibonacci series.
multi Fibonacci_func(0) { 1; } multi Fibonacci_func(1) { 1; } multi Fibonacci_func(Int $n where $n > 1) { Fibonacci_func( $n - 2) + Fibonacci_func( $n - 1); } print Fibonacci_func(17); |
Output:
2584
Above example uses multiple subroutines to calculate the Sum of Fibonacci Series.
Example 2: Factorial of a Number
multi Factorial(0) { 1; } multi Factorial(Int $n where $n > 0) { $n * Factorial( $n - 1); } print Factorial(15); |
Output:
3628800
In the above-given Examples, the program uses the ‘multi’ keyword to declare multiple subroutines with the same name but with different arity.
Similar Reads
Perl | Subroutines or Functions
A Perl function or subroutine is a group of statements that together perform a specific task. In every programming language user want to reuse the code. So the user puts the section of code in function or subroutine so that there will be no need to write code again and again. In Perl, the terms func
2 min read
Perl | Recursive Subroutines
Prerequisite: Recursion in PerlRecursive means about or using a rule or procedure that can be applied repeatedly. It is the process of defining a function or calculating a number by the repeated application of an algorithm. Recursive Subroutine is a type of subroutine that calls itself as part of it
7 min read
Perl | Scope of a Subroutine
Subroutines in Perl, are reusable unit of code. It is a dynamic concept. Functions and subroutines are the two terms that can be used interchangeably. If you want to be strict on the semantics, small pieces of named blocks of code that accept arguments and return values are called subroutines. The b
7 min read
Perl | Slurp Module
The File::Slurp module is used to read contents of a file and store it into a string. It is a simple and efficient way of Reading/Writing/Modifying complete files. Just like its name, it allows you to read or write entire files with one simple call. By importing this module to your program, the user
4 min read
Perl | substitution Operator
Substitution Operator or 's' operator in Perl is used to substitute a text of the string with some pattern specified by the user. Syntax: s/text/pattern Returns: 0 on failure and number of substitutions on success Example 1: #!/usr/bin/perl -w # String in which text # is to be replaced $string =
1 min read
Perl | rindex() Function
rindex() function in Perl operates similar to index() function, except it returns the position of the last occurrence of the substring (or pattern) in the string (or text). If the position is specified, returns the last occurrence at or before that position. Syntax: # Searches pat in text from given
2 min read
Perl | substr() function
substr() in Perl returns a substring out of the string passed to the function starting from a given index up to the length specified. This function by default returns the remaining part of the string starting from the given index if the length is not specified. A replacement string can also be passe
2 min read
Perl | References to a Subroutine
Prerequisite: Perl references Declaring References to a Subroutine In Perl, a reference is, exactly as the name suggests, a reference or pointer to another object. References actually provide all sorts of abilities and facilities that would not otherwise be available and can be used to create sophis
6 min read
Perl | sprintf() Function
sprintf() function in Perl uses Format provided by the user to return the formatted string with the use of the values in the list. This function is identical to printf but it returns the formatted string instead of printing it. Syntax: sprintf Format, List Returns: a formatted scalar string Example
1 min read
Perl | Passing Complex Parameters to a Subroutine
Prerequisite: Perl | Subroutines or Functions A Perl function or subroutine is a group of statements that together perform a specific task. In every programming language, the user wants to reuse the code. So the user puts the section of code in a function or subroutine so that there will be no need
4 min read