Perl Subroutines
Subroutines are used to divide your program into smaller more manageable modules. A subroutine is a separate body of code that performs a specific task Subroutines serve two purposes
Defining | sub salaryCheck { if ( $salary > 50000) { print("You are promoted to manager\n"); } } |
Invoking | ##################### ##################### print("Checking your salary\n"); Note: the comments making it clear where the subroutines are, this helps any programmer when he/she has to change your code. |
You use the keyword sub to define a subroutine, it does not matter where in the program you define your subroutine (I personally like to put subroutines at the top, that's just me). You use the & (ampersand) followed by the name of the subroutine to invoke a subroutine, the & tells the perl interpreter that the following name is a subroutine. You can remove the & (ampersand) when calling the subroutine only if the subroutines has been defined already ( I personally use the & (ampersand)).
remove & (ampersand) when invoking a subroutine | salaryCheck; ## This would fail as the subroutine has not been defined yet ##################### print("Checking your salary\n"); Note: you can only obmit the & (ampersand) if the subroutines are defined before calling them |
Returning a value from a Subroutine
The last value of the last expression evaluated by the subroutine is automatically considered to be the subroutine's return value
Subroutines return value | #################### ## getNumbers SUB ## #################### sub getNumbers { $line = <STDIN>; $line =~ s/^\s+|\s*\n$//g; split(/\s+/, $line); ## this is the return value } #################### |
Return statement | #################### ## getDummy SUB ## #################### sub getDummy { $dummy_value = 1000000; $line = <STDIN>; $line =~ s/^\s+|\s*\n$//g; split(/\s+/, $line); return $dummy_value; ## We return a value other than the last expression value } #################### Note: we can use the keyword return to return a different value than the last expression |
Local Variables
There are two statements that define local variables
my | #################### |
local | you can use local keyword instead of the my keyword. The only difference between the two is that my variables are not know outside the subroutine. |
Passing values to a subroutine
You make subroutines more flexible by allowing them to accept values passed from the main or any other subroutine program; these values passed are known as arguments. When passing the arguments to a subroutine they are placed in the system array variable @_, it is create whenever you pass arguments to a subroutine.
Passing values to subroutines | ######################## print ("system array variable first element is " . $_[0]); # acts just like any normal array ################## &whoDoYouSupport($footballTeam, $ground, $capacity); # pass the arguments to the subroutine |
Passing lists to subroutines | ##################### ### getLists SUB ### ##################### sub getLists { my (@localList) = @_; # all the lists will be merged into a single array @_ foreach $number (@localList) { print("$number\n"); } } ################## &getLists($num_one, @num_list, $num_six); # pass all the lists as arguments |
Calling subroutines from subroutines |
#################### ### routine1 SUB ### #################### sub routine1 { &routine2; # call subroutine routine2 } #################### ################## |
Recursive subroutines | ##################### ################## |
Passing arrays by name using aliases | ################## foreach $number (@subarray) { ################## |
BEGIN and END subroutines | exit; |
AUTLOAD (non-existent subroutines | @list = qw( 1 2 3 4); AUTOLOAD { print("subroutine $AUTOLOAD does not exist check your code\n"); } Note: useful if you have very large scripts or have many perl modules |