- Getting Started
- Perl Variables
- Control Flow Constructs
- Altering Loop Control Flow
- A Few Special Perl Variables
- Regular Expressions
- Writing Your Own Functions
1.7 Writing Your Own Functions
Functions in a programming language serve two important purposes. They allow you to reuse code and they serve to generalize an algorithm that can be used on different data. Perl has several hundred builtin functions. We've seen a few thus far: print, open, chomp, push, pop, etc. In this section of the tutorial, you will learn how to write your own functions.
Functions are typically written once and stored in a library so that they can be used in a wide variety of programs. Later in the book, we will demonstrate how that can be done. For now, we will assume that any function you write will be coded at the beginning of the file in which you wish to use the function. Perl functions are defined with the word "sub", followed by the name of the function, followed by a pair of curly braces that enclose the body of the function.
sub functionname { body of function }
To invoke a Perl function, give the name of the function preceded by the & symbol. If the function has arguments, list them within a set of parentheses following the function name. Inside the function, the arguments are accessed through the special Perl builtin array named @_. Here's an example that demonstrates a function that sums the elements of the array passed to it. Note that we've just given the bare essentials of writing your own functions. Later, we will have much more to say about this topic.
#!/usr/bin/perl # # function.pl # sub total { $sum = 0; foreach $item (@_) { $sum += $item; # add $item to $sum } return $sum; } @data1 = (1,2,3,4); $result = total(@data1); print "Sum of @data1 is $result\n"; @data2 = (1,2,3,4,5); $result = total(@data2); print "Sum of @data2 is $result\n"; $result = total(@data1, @data2); print "Sum of @data1 and @data2 is $result\n"; % functions.pl Sum of 1 2 3 4 is 10 Sum of 1 2 3 4 5 is 15 Sum of 1 2 3 4 and 1 2 3 4 5 is 25 %
The function treats all arguments sent to it as one array. Thus, you can send an actual array, a list of values, or combinations of these. Inside the function, there is simply a loop that traverses the builtin parameter array, @_. We will give the full details about arguments, parameters, and local variables in a later chapter.
Perl functions are versatile. They may return scalars, arrays, hashes, or just about anything else, but they all return something. Of course, if you must, you can disregard the returned value. If you want to return a value from a function, then you can either use the return statement or you can rely on the fact that the last expression that is evaluated inside the function will be returned. Thus, the above function could have been written as:
sub total { $sum = 0; foreach $item (@_) { $sum += $item; } $sum; # last expression evaluated }
Do not fall into the following trap by coding the function as follows:
sub total { $sum = 0; foreach $item (@_) { $sum += $item; } }
It may appear that the last expression evaluated is the statement inside the loop, but this is not the case. To terminate the foreach loop, Perl had to evaluate the elements in the list to determine that the loop was finished.