Understanding subroutines in Perl with example

Understanding subroutines in Perl

When you find meaning of subroutines in dictionary , it is described as "A set of instructions designed to perform a frequently used operation within a program". Similar to it Perl has subroutines which are user defined functions which allows Programmer to reuse one chunk of code at multiple places of the program.

Few Important points about subroutines



1. User can define their own subroutines.

2. Subroutines can be located anywhere in the program.

3. Name of the subroutine can contain String, Number or underscore but must not start with a digit.

4. When we need to load subroutine from another file it can be done using do, require or use keywords.

5. We can use @_ to print the arguments passed to the subroutine.

6. Subroutine name comes from different namespace, subroutine defined as &test is not have any relation with the $test scalar defined, both are different.

7. If you have two subroutine definitions with the same name, the later one overwrites the first one.

8. Subroutine are always Global.

9. Subroutines are never private.

Defining Subroutine



Defining a subroutine is easy, we need to use keyword as sub followed by the name of the subroutine and then the block of code in curly braces as shown in following code.

sub subtest {

print "This is a first subroutine\n";

}


Invoking or calling a Subroutine



To invoke a subroutine we need to prefix ampersand with it

&subtest will print This is a first subroutine

Returning value from a Subroutine



The return value of any subroutine or block inside the subroutine is the value of the last evaluated expression. In the following example the addition of two variables is the return value of the subroutine.


sub subtest {

$_[0]+$_[1];

}


But in following example the print statement is the return value.


sub subtest {

$_[0]+$_[1];

print "This is return statement\n";

}


We can explicitly return the value using return statement as shown below :

sub subtest {

return $_[0]+$_[1];

}


Important thing to note is whenever we call the return it terminates the current subroutine and returns the value. If we don’t specify the return value it is undef.

Subroutine can return any type of variable such as Scalar, Array or Hash.

Arguments and subroutines



Passing Array to subroutine




#!/usr/bin/perl

use strict;

my @arrayTest = (1,2,3);

subtest( @arrayTest );

sub subtest {

print "Printing array contents @_\n";

}

Output :
Printing array contents 1 2 3

In above program if we do not declare arrayTest with my keyword then due to use strict it throws following errors

Global symbol "@arrayTest" requires explicit package name at subtest.pl line 3.

Global symbol "@arrayTest" requires explicit package name at subtest.pl line 4.

Execution of subtest.pl aborted due to compilation errors.
Share on Google Plus

About Pranav

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment