Sat 11 Nov 2006
Some Perl Subroutine Writing Tips
Posted by Charles Clarkson under Perl, Programming
No Comments
In general, subroutines should receive data, process that data, and then output something. There are some exceptions. In void context, we have no output. In the case of information retrieval from a fixed source there may not be any input. An error logging routine might have input and output but no processing.
Good subroutines only affect and are affected by their own environment. If events exterior to the subroutine affect it, you have probably done something wrong. Here's an example of a poorly written subroutine. foo()
is using $bar
. $bar
has not been passed to it. Remember that a well written subroutine has data passed to it. Good subroutines only affect and are affected by their own environment. Deleting the $bar
variable outside this subroutine affects the subroutine.
-
sub foo {
-
return $bar;
-
}