next up previous contents
Next: Return Values Up: Functions Previous: A Simple Example   Contents

Function Arguments

We've already seen that perl passes arguments to functions as most languages do; namely by providing a comma-separated list of the arguments after the function name. But when we're writing a function, how do we accept these arguments? In perl, all of the function arguments are collapsed into one big array known as @_ . If any of the elements of the argument list are arrays or hashes, they are ``rolled out'' into scalars before being placed in the @_ array. This array is local to each function call, so you can freely call functions from within functions, until the memory capacity of your computer is exceeded, and each one will always remember its own arguments. However, remember that, like the dummy variable used in a foreach loop (Section 5.6), the variables in the @_ array are truly aliases for the arguments being passed, so that if you modify elements of the array, the arguments themselves will be changed when the function call is completed.

Let's rewrite the write_email_line function, this time using two arguments. Regardless of how many arguments are being passed to a function, you don't specify them after the function's name in the definition; instead, you extract them into their desired locations from the @_ array inside the function body. When you extract these arguments, it's always a good idea to make them local by surrounding the name of the variable with a call to the my function. This function ``overwrites'' any global variables with the same name as your variables for the duration of the function call, and then disposes of the variables when you leave the function. (Note that this is the default behavior of variables inside of functions in many computer languages.) Thus, providing we use the my function, we can name our function variables $user and $email, with no danger of altering the global versions of these variables.

 sub write_email_line{
    my($user,$email) = @_;
    print("<hr><p>Please send comments to");
    print(qq{ <a href="mailto:$email">$user</a><br><hr>}); 
 }
Alternatively, you could use the shift function (Section 4.4), one time for each argument. Inside a function, a call to shift without arguments operates on the argument array @_.
     sub write_email_line{
      my($user) = shift;
      my($email) = shift;
      print("<hr><p>Please send comments to");
      print(qq{ <a href="mailto:$email">$user</a><br><hr>}); 
     }

In this particular example, no harm would have been done by using the global $user and $email variables, because they weren't modified within the function. However, since functions often evolve over time, it's still a good idea to use the my function for arguments inside a function. You can also call the my function for other variables that you use inside a function. This is a very good idea, especially for variables with names that might occur somewhere else in the program which calls your function.


next up previous contents
Next: Return Values Up: Functions Previous: A Simple Example   Contents
Phil Spector 2002-10-18