4.7 SubroutinesSubroutines are declared using one of these forms: Prototypes allow you to put constraints on the arguments you provide to your subroutines.sub name { block } sub name ( proto ) { block } You can also create anonymous subroutines at run-time, which will be available for use through a reference: $ subref = sub { block }; 4.7.1 Calling Subroutines
The ampersand ( To call subroutines indirectly (by name or by reference):name ( args ); # & is optional with parentheses name args ; # Parens optional if predeclared/imported & name; # Passes current @_ to subroutine &$subref( args ); # & is not optional on indirect call &$subref; # Passes current @_ to subroutine 4.7.2 Passing ArgumentsAll arguments to a subroutine are passed as a single, flat list of scalars, and return values are returned the same way. Any arrays or hashes passed in these lists will have their values interpolated into the flattened list.
Any arguments passed to a
subroutine come in as the array
You may use the explicit 4.7.3 Passing ReferencesIf you want to pass more than one array or hash into or out of a function and have them maintain their integrity, then you will want to pass references as arguments. The simplest way to do this is to take your named variables and put a backslash in front of them in the argument list: This sends references to the three arrays to the subroutine (and saves you the step of creating your own named references to send to the function). The references to the arrays are passed to the subroutine as the three-member@returnlist = ref_conversion( \@temps1, \@temps2, \@temps3 );
@_
array. The subroutine will have to dereference the arguments
so that the data values may be used.
Returning references is a simple matter of returning scalars that are references. This way you can return distinct hashes and arrays. 4.7.4 Private and Local VariablesAny variables you use in the function that aren't declared private are global variables. In subroutines, you'll often want to use variables that won't be used anywhere else in your program, and you don't want them taking up memory when the subroutine is not being executed. You also might not want to alter variables in subroutines that might have the same name as global variables.
The
To scope multiple variables at once, use a list in parentheses. You can
also assign a variable in a Dynamic variables are visible to other subroutines called from within their scope. Dynamic variables are defined withmy @list = (44, 55, 66); my $cd = "orb";
local
, and they are not private variables, but rather they are
global variables with temporary values. When a subroutine is executed, the
global value is hidden away, and the local value is used. Once the
scope is exited, the original global value is used. Most of the
time you will want to use
my
to localize parameters in a subroutine.
4.7.5 PrototypesPrototypes allow you to design your subroutines to take arguments with constraints on the number of parameters and types of data. To declare a function with prototypes, use the prototype symbols in the declaration line, like this: In this case, the function expects two scalar arguments. The following table gives the various prototype symbols:sub addem ($$) { ... }
A backslash placed before one of these symbols forces the argument to be that exact variable type. For instance, a function that requires a hash variable would be declared like this: Unbackslashedsub hashfunc (\%);
@
or
%
symbols act exactly alike, and will
eat up all remaining arguments, forcing list context. A
$
likewise
forces scalar context on an argument, so taking an array or hash variable for that
parameter would probably yield unwanted results.
A semicolon separates mandatory arguments from optional arguments. For example: requires two arguments: an array variable and a scalar. The third scalar is optional. Placing a semicolon beforesub newsplit (\@$;$);
@
and
%
is not necessary since
lists can be null.
A typeglob prototype symbol ( Copyright © 2001 O'Reilly & Associates. All rights reserved. |
|