The most common control statement used in CGIRoutines that are general enough
that they are used several times in the same application
should usually be placed in a subroutine.
A subroutine encapsulates a routine
so that other parts of the program can refer to it by its
subroutine name.
Consider the addition algorithm;
what if we needed to add various numbers several times in our
program but did not want to create a separate piece of code
for each instance of addition? If the algorithm were four or
five lines long, it would be annoying to type the similar lines
over and over again. Even worse, if you ended up changing the
logic of the algorithm you would have to hunt down every
occurrence and change each one. Maintaining such a program
could become a nightmare, because many errors could arise
if you forgot to change one of the lines of code in any of
the duplicate routines or changed one of them incorrectly.
When faced with such a circumstance,
a programmer can create a subroutine that can be used again
and again by other parts of the program.
To create and use subroutines in Perl,
we need three things: a subroutine reference, a subroutine
identifier, and a parameter list of variables to pass to the
subroutine. The & symbol precedes the name of the routine,
telling Perl to look for the subroutine and call it. For
example, &AddNumbers would direct Perl to execute the
AddNumbers subroutine.
We also need to be able to send the
subroutine some information. Specifically, we need to send the
subroutine parameters that it will use to customize its output.
If we want to sum 2 and 3, for example, we pass 2 and 3 to the
subroutine using the following format:
&AddNumbers(2,3);
The & marker tells Perl to look for the
subroutine in the program in order to call it. However, the
definition of the subroutine is marked off in the program
using a sub marker. The code belonging to the routine is
delimited with curly brackets ({}). The following example
shows what the AddNumber subroutine definition would look
like:
sub AddNumbers
{
local($first_number, $second_number) = @_;
print $first_number + $second_number;
}
Note the third line above. We use the
local keyword to make sure that the $first_number and
$second_number variables will be considered local to only that
subroutine. The subroutine will not affect any other variables
that may be called $first_number or $second_number in other
subroutines within the program.
In Perl, the @_ is a list of parameters
that have been passed to the function. $first_number is set
equal to the first parameter, and $second_number is set equal
to the second parameter in the @_ list of parameters. If the
routine is called by &AddNumbers(2,3), 2 and 3 will be assigned
to $first_number and $second_number, respectively.
Whenever you want to add numbers, you
can simply use the subroutine call &AddNumbers(x,y) instead of
writing each addition individually. As a bonus, if you need to
change the logic of the addition algorithm, you need only change
it in the subroutine.