27 Sep 2002 (updated 1 Oct 2002 at 21:09 UTC)
»
I've been trying to use prototypes to make new utility functions in perl. The "perlsub" documentation says this:
Perl supports a very limited kind of compile-time argument
checking using function prototyping. If you declare
sub mypush (\@@)
then "mypush()" takes arguments exactly like "push()"
does.
But that doesn't seem to be my experience. I wrote a function called linsert. The definition is:
sub linsert (\@$@) {
my ($aref, $index, @elts) = @_;
splice(@{$aref}, $index, 0, @elts);
}
Then I tried to write mypush using linsert:
sub mypush (\@@) {
my ($aref, @elts) = @_;
linsert(@{$aref}, $#{$aref}, @elts);
}
It doesn't work; the original array is not altered. But if I use the built-in push instead of linsert, it magically works. It seems that this makes it impossible to build a pyramid of utility functions, as the base functions don't seem work as expected unless they're built in.
I also found out that $a and $b are special when "strict" is concerned. I had good luck making a "foreachpair" utility function, but I got tripped up when making "foreachtriplet". Ugh.