Can we strict the subroutine in perl to receive number of arguments ?
Runtime? Compile time? Is
@a considered to be one argument, or as many arguments as
@a has elements?
Runtime is easy:
sub foo {
unless (@_ <= 4) {
die "foo allows at most 4 arguments";
}
...
}
Compile time, you can do:
sub foo (;$$$$) {
...
}
The disadvantage of the latter is that it evaluates all the arguments to the sub in scalar context, and hence there's no flattening of lists. That is,
@bar = qw[red blue green yellow brown pink];
foo @bar;
is not an error - it calls
foo with a single argument,
6; the number of elements in
@bar.