10.11. Prototyping FunctionsProblemYou want to use function prototypes so the compiler can check your argument types. Solution
Perl has something of a prototype facility, but it isn't what you're thinking. Perl's function prototypes are more like a context coercion used to write functions that behave like some of Perl's built-ins, such as DiscussionManually checking the validity of a function's arguments can't happen until run-time. If you make sure the function is declared before it is used, you can tickle the compiler into using a very limited form of prototype checking to help you here. Don't confuse Perl's function prototypes with those found in any other language. Perl prototypes serve only to emulate the behavior of built-in functions. A Perl function prototype is zero or more spaces, backslashes, or type characters enclosed in parentheses after the subroutine definition or name. A backslashed type symbol means that the argument is passed by reference, and the argument in that position must start with that type character.
A prototype forces context on the arguments to the prototyped function call. This is done when Perl compiles your program, and in most cases this does not necessarily mean that Perl checks the number or type of the arguments to your function. If Perl sees This is so important that it bears repeating: don't use Perl prototypes expecting the compiler to check type and number of arguments for you. So what use are they? They have two main uses, although as you experiment with them you may find others. The first use is to tell Perl how many arguments your subroutine has, so you can leave off parentheses when you call the function. The second is to create a subroutine that has the same calling syntax as a built-in. Omitting parenthesesOrdinarily your subroutines take a list of arguments, and you can omit parentheses on the function call if you like: @results = myfunc 3, 5; Without prototypes, this is the same as: @results = myfunc(3, 5); In the absence of parentheses, Perl will put the right hand side of the subroutine call into list context. You can use prototypes to change this behavior: sub myfunc($); @results = myfunc 3, 5; Now this is the same as: @results = ( myfunc(3), 5 );
You can also provide an empty prototype to indicate the function takes no arguments, like the built-in function sub LOCK_SH () { 1 } sub LOCK_EX () { 2 } sub LOCK_UN () { 4 } Mimicking built-ins
The other common use of prototypes is to give the convenient pass-without-flattening behavior of built-in functions like sub mypush (\@@) { my $array_ref = shift; my @remainder = @_; # ... }
The mypush( $x > 10 ? @a : @b , 3, 5 ); # WRONG Instead, you must play games with references: mypush( @{ $x > 10 ? \@a : \@b }, 3, 5 ); # RIGHT
Here's an sub hpush(\%@) { my $href = shift; while ( my ($k, $v) = splice(@_, 0, 2) ) { $href->{$k} = $v; } } hpush(%pieces, "queen" => 9, "rook" => 5); See Also
The |
|