224 10 Transforming with Traditional Programming Languages
use this list like any other. For example, the following program will print the
statistics for two of the columns of the database:
while (<>) {
chomp;
push(@table, [split]);
}
($mean, $var) = stats(1);
print("Statistics for column 1:");
print(" mean $mean variance $var\n");
($mean, $var) = stats(4);
print("Statistics for column 4:");
print(" mean $mean variance $var\n");
Of course, the program above has yet another opportunity for a procedure;
namely, one that prints the statistics:
sub printstats {
my $column = $_[0];
my ($mean, $var) = stats($column);
print("Statistics for column $column:");
print(" mean $mean variance $var\n");
}
One cannot help but notice that the scalar$_and the array@_are used
frequently in Perl. Because of this it is a good idea to assign the parameters
of a procedure to variousmyvariables belonging to the procedure as soon as
possible. It also makes it much easier for a person to understand what a pro-
cedure is supposed to do. In this case,$columnis a lot more understandable
than$_[0].
Putting all of this together, one obtains the solution to the task in pro-
gram 10.8.
The procedure definitions can go either before the main program or after it.
Summary
- A procedure is a collection of statements that can be performed as a unit.
- A procedure is invoked by using its name and giving a list of parameters.
•Amyvariable is limited to the part of the program in which it is declared.