Perl allows you to define your own functions, called subroutines. They are used for code reusability, so you don’t have to write the same code again and again. For example if you want to take input from user in several places of your program, then you can write the code in a subroutine and call the subroutine wherever you wanna take input. This way you do not have to write the same code again, this also improves code readability.
Advantages of subroutines:
1) Code re-usability
2) Improves code readability
Syntax:
defining a subroutine
The first thing you need to do is create a subroutine. sub keyword is used to define a subroutine in Perl program. You can choose any meaningful subroutine name.
sub subroutine_name { statement(s); return; }
calling a subroutine
A subroutine is called by using subroutine name prefixed with “&” character. You can pass arguments as well while calling the subroutine.
&subroutine_name; #calling without parameter &subroutine_name(10);
Simple example of subroutine
Lets take a simple example to understand this:
#!/usr/bin/perl my $msg; # defining three subroutines sub ask_user { printf "Please enter something: "; } sub get_input { $msg = <STDIN>; return $msg; } sub show_message { printf "You entered: $msg"; } #calling subroutines &ask_user; &get_input; &show_message;
Output:
Please enter something: Welcome to beginnersbook You entered: Welcome to beginnersbook
Passing Parameters to subroutines
In the above example, we did not pass any parameter while calling the subroutine, however we can pass various parameters while calling a subroutine. All the parameters (often referred as arguments) are stored in special array (@_). Lets have a look at the example below to understand this:
#!/usr/bin/perl # defining subroutine sub printparams { printf "@_\n"; return; } #calling subroutine &printparams("This", "is", "my", "blog");
Output:
This is my blog
In the above example, we have printed all the parameters using special array (@_). However if you want you can display selected parameters using “$_[n]” where n is the index of parameter, for example $_[0] would refer first parameter and $_[1] would refer second parameter. Lets take an example:
#!/usr/bin/perl # defining subroutine sub printparams { printf "First Parameter: $_[0]\n"; printf "Fourth Parameter: $_[3]\n"; return; } #calling subroutine &printparams("This", "is", "my", "blog");
Output:
First Parameter: This Fourth Parameter: blog
Passing arrays to subroutine
In the above example, we have passed few string parameters while calling subroutine. We can also pass arrays to subroutine. Here is an example:
#!/usr/bin/perl # defining subroutine sub printparams { printf "First Parameter: $_[0]\n"; printf "Third Parameter: $_[2]\n"; printf "Fourth Parameter: $_[3]\n"; printf "Sixth Parameter: $_[5]\n"; return; } @array1 = ("This", "is", "text"); $num = 100; @array2 = ("Welcome", "here"); #calling subroutine &printparams(@array1, @array2, $num);
Output:
First Parameter: This Third Parameter: text Fourth Parameter: Welcome Sixth Parameter: 100
Passing hashes to subroutine
#!/usr/bin/perl sub DisplayMyHash{ #copying passed hash to the hash my %hash = @_; for my $key (keys %hash) { print "Key is: $key and value is: $hash{$key}\n"; } } %hash = ('Item1', 'Orange', 'Item2', 'Apple', 'Item3', 'Banana'); # Function call with hash parameter DisplayMyHash(%hash);
Output:
Key is: Item1 and value is: Orange Key is: Item2 and value is: Apple Key is: Item3 and value is: Banana
Leave a Reply