The Perl (Almost) weekly manual By Ankit Fadia [email protected]



Until now, we learnt how to execute commands in a specific order. Other than conditions and loops, we pretty much did the same thing. Now, say you have a snippet of code, which does a small task by itself, and it contains code, which is needed to perform that particular task at different stages of the same program. What do you do?


Well, simply put the code within a ‘subroutine’ anywhere in the Perl program. This sub routine could then be called at any time and from anywhere with the help of a single line of code. Basically Subroutines are to Perl what functions are to C. Get it?


Now, using sub routines not only makes your code more efficient, it along with shortening your Perl program, makes it simpler to code. If this sub routine thing makes you a bit nervous, then let us consider a few examples to understand them better.


Well, before we move on to the Examples part, let us learn the syntax and other basics of using Sub Routines in Perl.


Subroutines are declared by using the ‘sub’ keyword. The following is the syntax of a typical sub routine declaration-:


sub NAME { Code } declares a Sub Routine called NAME. Here Code are the commands contained by the sub routine NAME which are executed each time it is called.


For example,


sub friendly_name {

print “Hello”;



Now, a sub routine declaration is pretty much useless without a call to it. By that what I mean to say is that, only declaring a Sub Routine is not enough to do something useful. A sub Routine becomes something efficient only when it is called or invoked after being declared.


So in the above example, the sub routine ‘friendly_name’ is not of any use unless we call it or invoke it. To call a sub routine in Perl, we use the following line of code:


sub_routine_name ();


This line called the sub Routine called sub_routine_name and executes all commands within the curly brackets.


So in our example, to call the ‘friendly_name’ sub routine, we use the following line of code:


friendly_name ();


Now that you understand the concept of Sub Routines, declaration and sub routine invoking, let us write a complete Perl program initializing both of the above:


sub hello_world {

                                print “Hello World\n”;




Output-:    c:\>perl sub.pl

                          Hello World


The one thing that you need to note is that if you are not passing any arguments to the sub routine, then we do not need to write the parentheses. This means that we can call a function in two different ways-


function_name(); or function_name;


Both of the above would produce the same results.


Returning Values


Say you have a number of scalars to which you want to assign values, which are calculated by calling the same sub routine. Now, if you have a single scalar, then you need not worry about how you would return the value, but if you have more than a single scalar, all of which have to assigned a value by performing the same calculations or in other words by calling the same sub routine, then you need to figure out a way of making your Sub routines return more than a single value.


HACKING TRUTH: Actually in Perl there is a slight difference between functions and Sub Routines. Most people assume that both of them are the same. But actually, a function is an inbuilt command, which is provided by Perl, while a sub routine is basically a group of commands that you right. Get it?


Well, to do just that we have the ‘return’ statement. The return statement works in the following manner: It causes an immediate exit from the Sub Routine while returning the value to be returned either in the variable that called it or either in the default Perl variable: $_ (If the Sub Routine was not called by using a variable.)


The usage of the ‘return’ statement will become clearer after reading the exact syntax of the ‘return’ statement.


return VALUE; exits from the Sub Routine while returning the VALUE.


To understand the ‘return’ statement more well, let us take up some examples:


print ‘My Fav No is:’, number(), “\n”;


sub number {

                                return (3*1+1);



Output-: c:\windows\perl number.pl

                My Fav Number is:



$letter = ‘b’;



print $letter;

sub addone{

                                $letter +=1;


sub minustwo{

                                $letter -=2;



Output: c:\windows\letter.pl



print “Type a Number between 0 and 4:\n”;

$num= shift;

print result;

sub result {

                                return 4 if $num ==4;

                                return 3 if $num ==3;

                                return 2 if $num ==2;

return 1 if $num ==1;



Output: c:\windows\get.pl

             Type a Number between 0 and 4:




Now, let us consider the following scenario: Say your program contains the following line of code:


$var_name= sub_name;


Now, if the Sub Routine, by the name of sub_name has already been declared earlier i.e. before this particular line, then the variable $var_name is assigned the value returned by the Sub Routine, sub_routine.


However, if the Sub Routine has not been declared already, i.e. before that particular line, then the variable var_name is assigned the string of characters- ‘sub_name’.


So you see, even the tiniest or typos of mistakes in a Perl program can really change the way your program works. So basically like most programming languages, Perl too has a very strict and complicated set of rules and regulations.


Anyway, to avoid making a kind of mistake that I described above, there is the need of special Sub Routine call identifiers.


Just like, all scalars are preceded by the ‘$’ sign, all arrays by the ‘@’ sign, all hashes by the ‘%’ sign, similarly, all calls to a sub routine should be preceded by a ‘&’ sign.


Note: This is by no means absolutely necessary to follow, but is certainly a very good and efficient programming practice.                                                


So now, we know that there are four different ways in which a sub routine can be called-;


sub_name; or

sub_name(); or

&Sub_name; or



Get it? If not, then read the following example to understand better:


sub printer{

                                print “Hi, I am the Mr. Printer”;




Output-: c:\windows\sb.pl

                Hi, I am the Mr. Printer


The usefulness of the ‘&’ sign also comes forward when your sub routine name is the same as an inbuilt Perl function. For Example, if you want to name your Sub Routine print, then we use ‘&’ so as to not confuse it with the inbuilt Perl function: print.


Well, that is all for this issue of the Perl almost Weekly Journal. Till the next Journal this is Ankit Fadia saying Keep ‘Perling’ ; )


Ankit Fadia

[email protected] (Yes, I answer all my mails.)


To receive tutorials on everything you Dreamt of written by Ankit Fadia, join his mailing list, by sending an email to: [email protected]