in reply to Limit number of Sub routine argument list

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.

Replies are listed 'Best First'.
Re^2: Limit number of Sub routine argument list
by ganeshwari (Sexton) on Oct 25, 2009 at 15:26 UTC
    thanks for your replies. I need to handle this in compile time only. But as Alexander told we can bypass this by calling the subroutine as &subroutine(..). Is there way to handle this ?
      But as Alexander told we can bypass this by calling the subroutine as &subroutine(..). Is there way to handle this ?
      Not on the level of Perl itself. You may be able to do something by inspecting the op-tree. Perhaps perlcritic can help you as well, but that requires a separate stage.