The problem is that Perl's function prototypes don't do what people think they do. Their purpose is to allow you to write functions that will be parsed like Perl's built-in functions.
First of all, method calls completely ignore prototypes. If you're doing OO programming, it doesn't matter what prototype your methods have. (So they shouldn't have any prototype.)
Second, prototypes aren't strictly enforced. If you call a subroutine with &function(...)
, the prototype is ignored. So they don't really provide any type safety.
Third, they're spooky action-at-a-distance. (Especially the $
prototype, which causes the corresponding parameter to be evaluated in scalar context, instead of the default list context.)
In particular, they make it hard to pass parameters from arrays. For example:
my @array = (1,2,3);
foo(@array);
foo(@array[0..1]);
foo($array[0], $array[1], $array[2]);
sub foo ($;$$) { print "@_\n" }
foo(@array);
foo(@array[0..1]);
foo($array[0], $array[1], $array[2]);
prints:
1 2 3
1 2
1 2 3
3
2
1 2 3
along with 3 warnings about main::foo() called too early to check prototype
. The problem is that an array (or array slice) evaluated in scalar context returns the length of the array.
If you need to write a function that acts like a built-in, use a prototype. Otherwise, don't use prototypes.
Note: Perl 6 will have completely revamped and very useful prototypes. This answer applies only to Perl 5.