Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Limit number of Sub routine argument list

by JavaFan (Canon)
on Oct 25, 2009 at 10:57 UTC ( [id://803138]=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://803138]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (5)
As of 2024-04-23 17:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found