Perl Script : Subroutines

If you have written programs longer than one hundred lines or so, I don't need to tell you how important it is to organize programs into subroutines. But for some reason, many Perl programmers seem to be allergic to them.

Well, different authors will recommend different styles, but I tend to use a lot of subroutines. In fact, when I start a new project, I usually write a subroutine with the same name as the program, and start the program by invoking it.

sub echo {
print "@_\n";
}
echo @ARGV


This program does the same thing as the previous one; it's just more complicated.

All subroutine declarations start with sub followed by the name of the subroutine and the body. The body of the subroutine is a block of statements enclosed
in squiggly-braces. In this case, the block contains a single statement.
The variable @ is a built-in variable that refers to the array of values the subroutine got as parameters.

No comments:

Post a Comment