< Perl Tutorial

Perl subroutines

← Previous Next →


Perl Subroutines

Subroutines (or just subs) give us the ability to give a name to a section of code. Then when we need to use that code in our program, we just call it by name. We can divide up your code into separate subroutines. How we divide up our code among different subroutines is up to us, but logically the division usually is such that each subroutine performs a specific task.

In perl there is a difference bet ween functions and subroutines. We can understand the difference in the following context:

  • If someone mentions a function in Perl, they almost certainly mean something built into Perl. The main reference documentation for Perl built-ins is called perlfunc.
  • An operator in Perl can have a name instead of a symbol, so it can look very much like a function. Hence, we can use the terms interchangeably. Those built-ins that have symbols instead of names are documented in perlop, which also refers to 'named operators'. perl itself speaks about the 'print operator'. However, we're equally likely to hear Perl people talk about 'the print function'.
  • Subroutines in Perl are akin to C's functions – they are sections of code that can take arguments, perform some operations with them, and may return a meaningful value, but don't have to. However, they're always user-defined rather than built-ins

Defining a Subroutine

We can give Perl some code, and then we name that block of code and that's our subroutine. An example of a subroutine is:

sub marine {
...
}

There are three sections to this declaration:

  1. The keyword sub. This is case-sensitive and needs to be in lower case.
  2. The name we're going to give it. The rules for naming a subroutine are exactly those for naming variables; names must begin with an alphabetic character or an underscore, to be followed by one or more alphanumerics or underscores. Upper case letters are allowed, but we tend to reserve all-uppercase names for special subroutines.
  3. A block of code delimited by curly brackets, just as we saw when we were using while and if. We don't need a semicolon after the closing brace.

Calling a Subroutine

A subroutine can be called by simply refering to its name in any part of the code and passing the relevant parameters in parentheses. A typical subroutine call is as follows:

marine( list of arguments );

The following example shows the complete process of defining and calling a subroutine:

# Function definition
sub Hello{
   print "Hello, World!\n";
}

# Function call
Hello();

The above code produces the following output:

Hello, World!

Passing Arguments to a Subroutine

We can pass various arguments to a subroutine like we do in any other programming language and they can be acessed inside the function using the special array @_.Thus the first argument to the function is in $_[0], the second is in $_[1], and so on. The arguments are passed in brackets after the name of the subroutine. We can also pass arrays and hashes as arguments apart from scalars.

The following demonstrates subroutine with arguments:

#Subroutine definition
sub total {
my $total = 0;
$total += $_ for @_;
print "The total is $total\n";
}

#Subroutine Call
total(111, 107, 105, 114, 69);
total(1...100);

The above code produces the following output:

The total is 506
The total is 5050

Returning values from Subroutine

Sometimes we don't want to perform an action like printing out the total, but instead we want to return a result. We may also want to return a result to indicate whether what we were doing succeeded. There are two ways to do this - implicitly and explicitly. The implicit way is nice and easy. We just make the value we want to return the last thing in our subroutine. The following example shows how to do this:

sub total {
my $total = 0;
$total += $_ for @_;
$total;
}

$total = total(111, 107, 105, 114, 69);
$sum_of_100 = total(1...100);

Here the last value is automatically returned and stored in the scalar variables.

The explicit method of returning something from a subroutine is to say return(...). The first return statement we come across will immediately return that list to the caller. The following example shows how to do this:

sub secs2hms {
($h,$m);
$seconds = shift;
$h = int($seconds/(60*60)); $seconds %= 60*60;
$m = int($seconds/60); $seconds %= 60;
return ($h,$m,$seconds);
print "This statement is never reached.";
}

 

← Previous Next →