Subroutines and SortingSubroutines (a.k.a. procedures or functions or methods) are (as you might expect by now) rather simple and straightforward in Perl.
Declaring a new subroutine
The name can be any valid Perl identifier.
Note that the indentation style is similar
to that of the 'if' or 'while'.
Calling a SubroutineSubroutines are very useful for breaking up a long program up into manageable pieces. Giving a sequence of lines a meaningful name will also add to the clarity of your program.hello; # prints "hello how are you?" If you declare the subroutine BELOW the place where you call it you need to either precede the name with an ampersand '&' or use parentheses:init; process; show_results; clean_up;
Input parametersYou can pass input parameters to the subroutine in the same way that you pass parameters to the Perl built-in functions like 'substr' or 'index'. The parameters get magically assigned to a special array very cryptically named @_. Bizzare, huh? The first element of the array @_ is referenced (as usual) with $_[0]. Looks odd, huh?
Alternatively:
The above is the normal and recommended way to handle parameters.
It gets copies of the parameters and names them.
Local variablesAn even better way to handle input parameters is to copy them to variables that are entirely private to the subroutine:
With the 'my' keyword the variables $x and $y can be seen ONLY
within the scope of the subroutine. This
prevents the subroutine from clobbering any
similarily named variables outside of itself.
This can easily happen - especially when a
program grows in size and you tend to use
the same variable name a lot - such as $i.
NOTE - be sure to put the 'my' variables within parentheses. This makes the assignment happen in list (or array) context. You can often (but not always) omit the parentheses on a subroutine call because the semi-colon ';' will tell Perl where the parameters end. How many parameters?What if you are passing a variable number of parameters? How do you know how many you are receiving? Well, they are put in the array @_. What is the size of that array? As usual you can get the size of an array by assigning @_ to a scalar or by putting it in scalar context.
More cryptically you could have done this:
Ponder on that one! Sometimes what at first appears overly concise and cryptic can eventually become very clear and clean.@_ >= 2 or die "need two or more parameters for 'show'!\n"; Returning a ValueSubroutines often want to return a value to the caller in the same way that the 'index' built-in function does. It makes for a clean and clear syntax. Here are some fairly complex examples. Study them carefully.
Sorting ArraysSorting arrays is done with the keyword 'sort'.
This sorts the array alphabetically (or ASCIIbetically).
Capital letters come before lower case. This may not
always be what you want. For example it will not work
for sorting a series of numbers.
To sort in a different order you must supply the sort function with a different comparison routine.
Whenever 'sort' needs to compare two elements
of the array it will call the subroutine 'numerically'.
The elements are 'magically' passed by way of
the names $a and $b. It is the job of the sort subroutine
to look carefully at $a and $b and return -1, 0 or 1.
It will:
You can see the process of sorting in action by inserting a print in the sort subroutine:
'numerically' will be called many many times. This is a different
way of using a subroutine. You do not call it yourself.
It is called for you by the sort function whenever it needs
to compare two numbers.
Exercises
|