Module::Check_Args - a quick way to check argument counts for methods
use Module::Check_Args;
exact_argcount $argcnt;
range_argcount $minargs, $maxargs;
atleast_argcount $minargs;
atmost_argcount $maxargs;
When writing a complex program, some of the hardest problems to track down are subroutines that aren't called with the right arguments. Perl provides a means to check this at compile time, but there is no way to do this for subroutines that take a variable number of arguments or for object methods. Module::Check_Args provides routines that check the number of arguments passed to their callers and raise an exception if the number passed doesn't match the number expected. It's possible to specify that the number of arguments must be exactly n, at most n, at least n, or between n and m.
Module::Check_Args
When using these routines from within a method, be sure to account for the implicit first argument containing the object reference or class name!
By default, the four _argcount routines are exported.
By importing the following pseudo-symbols, you can request various behaviors from Module::Check_Args:
Specifies that an argument count mismatch is a fatal error. The message will give the file and line number of the call containing the bad number of arguments. This is the default.
An argument mismatch is a warning only.
No argument-count checking is performed. The four checking routines are still exported, so that you don't need to change code that contains them, but they are dummy procedures.
If you have multiple packages that use Module::Check_Args, each one can have different behavior.
Specifies that the caller must have exactly $argcnt arguments.
Specifies that the caller must have at least $minargs arguments but no more than $maxargs.
Specifies that the caller must have at least $minargs arguments, but can have any number more than that.
Specifies that the caller must have at most $maxargs arguments, but can have any number up to that, including zero.
One of the argument count checking routines was itself called with an invalid argument count. This is always a fatal error regardless of the behavior specified in the use declaration.
routine was called with an invalid number of arguments at the indicated location. These messages are either fatal errors or warnings depending on the behavior specified in the use declaration.
One of the argument count check routines was called, but no behavior (-die, -warn) had ever been set. This can only happen if you use something like the following combination of commands:
use Module::Check_Args (); ... &Module::Check_Args::exact_argcount(3);
Don't do that.
perlfunc -f caller
Kevin Michael Vail <kevin@vailstar.com>
To install Module::Check_Args, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Module::Check_Args
CPAN shell
perl -MCPAN -e shell install Module::Check_Args
For more information on module installation, please visit the detailed CPAN module installation guide.