File Coverage

blib/lib/Module/Check_Args.pm
Criterion Covered Total %
statement 59 72 81.9
branch 19 30 63.3
condition 4 5 80.0
subroutine 13 15 86.6
pod 4 4 100.0
total 99 126 78.5


line stmt bran cond sub pod time code
1             ##==============================================================================
2             ## Module::Check_Args - a quick way to check argument counts
3             ##==============================================================================
4             ## $Id: Check_Args.pm,v 1.1 2000/11/04 18:46:06 kevin Exp $
5             ##==============================================================================
6             require 5.000;
7              
8             package Module::Check_Args;
9 1     1   176597 use strict;
  1         2  
  1         39  
10 1     1   6 use Exporter ();
  1         1  
  1         28  
11 1     1   6 use vars qw{@EXPORT @ISA $VERSION %_PROCS};
  1         6  
  1         136  
12             @ISA = qw{Exporter};
13             @EXPORT = qw{exact_argcount range_argcount atleast_argcount atmost_argcount};
14             ($VERSION) = q$Revision: 1.1 $ =~ /Revision:\s+([^\s]+)/;
15              
16 1     1   5 use constant no_dieproc => "no behavior set for Module::Check_Args - 'import' never called?";
  1         2  
  1         394  
17              
18             =head1 NAME
19              
20             Module::Check_Args - a quick way to check argument counts for methods
21              
22             =head1 SYNOPSIS
23              
24             use Module::Check_Args;
25              
26             exact_argcount I<$argcnt>;
27              
28             range_argcount I<$minargs>, I<$maxargs>;
29              
30             atleast_argcount I<$minargs>;
31              
32             atmost_argcount I<$maxargs>;
33              
34             =head1 DESCRIPTION
35              
36             When writing a complex program, some of the hardest problems to track down
37             are subroutines that aren't called with the right arguments. Perl provides
38             a means to check this at compile time, but there is no way to do this for
39             subroutines that take a variable number of arguments or for object methods.
40             C provides routines that check the number of arguments
41             passed to their callers and raise an exception if the number passed doesn't
42             match the number expected. It's possible to specify that the number of
43             arguments must be exactly I, at most I, at least I, or between
44             I and I.
45              
46             When using these routines from within a method, be sure to account for the
47             implicit first argument containing the object reference or class name!
48              
49             By default, the four _argcount routines are exported.
50              
51             By importing the following pseudo-symbols, you can request various
52             behaviors from C:
53              
54             =over 4
55              
56             =item use Module::Check_Args qw(-die);
57              
58             Specifies that an argument count mismatch is a fatal error. The message will
59             give the file and line number of the call containing the bad number of
60             arguments. This is the default.
61              
62             =item use Module::Check_Args qw(-warn);
63              
64             An argument mismatch is a warning only.
65              
66             =item use Module::Check_Args qw(-off);
67              
68             No argument-count checking is performed. The four checking routines are still
69             exported, so that you don't need to change code that contains them, but they
70             are dummy procedures.
71              
72             =back
73              
74             If you have multiple packages that use Module::Check_Args, each one can have
75             different behavior.
76              
77             =cut
78             ##------------------------------------------------------------------------------
79             ## import
80             ##------------------------------------------------------------------------------
81 0     0   0 sub _dummy_proc { };
82              
83             sub import {
84 1     1   7 my $module = shift;
85 1         2 my @options = grep { /^-/ } @_;
  0         0  
86 1 50       5 die "only one -option allowed to ${module}::import\n" if @options > 1;
87 1   50     8 my $option = shift(@options) || '-die';
88 1         3 @_ = grep { ! /^-/ } @_;
  0         0  
89 1         3 my ($calling_package, $filename, $line) = caller;
90             ##
91             ## Set up the procedures to handle various methods of reporting the error.
92             ##
93             my %subs = (
94             -die => sub {
95 5     5   17 my ($package, $filename, $line, $subroutine) = caller(2);
96 5         41 die "$filename($line): ", @_, "\n";
97             },
98             -warn => sub {
99 0     0   0 my ($package, $filename, $line, $subroutine) = caller(2);
100 0         0 warn "$filename($line): ", @_, "\n";
101             },
102 1         11 -off => 1
103             );
104 1 50       4 die "'$option' invalid in ${module}::import from $calling_package ($filename, line $line)\n"
105             unless exists $subs{$option};
106             ##
107             ## If the option is -off, export dummy subroutines that don't actually do anything.
108             ##
109 1 50       5 if ($option eq '-off') {
110 1     1   5 no strict 'refs';
  1         2  
  1         188  
111 0         0 *{"$calling_package\::exact_argcount"} = *_dummy_proc;
  0         0  
112 0         0 *{"$calling_package\::atleast_argcount"} = *_dummy_proc;
  0         0  
113 0         0 *{"$calling_package\::atmost_argcount"} = *_dummy_proc;
  0         0  
114 0         0 *{"$calling_package\::range_argcount"} = *_dummy_proc;
  0         0  
115             } else {
116 1         4 $_PROCS{$calling_package} = $subs{$option};
117 1         2 unshift(@_, $module);
118 1         2782 goto &Exporter::import;
119             }
120             }
121              
122             =head2 Routines
123              
124             =over 4
125              
126             =item exact_argcount I<$argcnt>;
127              
128             Specifies that the caller must have exactly I<$argcnt> arguments.
129              
130             =cut
131             ##------------------------------------------------------------------------------
132             ## exact_argcount
133             ##------------------------------------------------------------------------------
134             sub exact_argcount ($) {
135 2 50   2 1 227 my $dieproc = $_PROCS{scalar(caller)} or die no_dieproc;
136             package DB;
137 1     1   5 use Carp;
  1         1  
  1         64  
138 1     1   5 use vars qw(@args);
  1         2  
  1         665  
139 2 50       9 croak "wrong argument count to Module::Check_Args::exact_argcount" unless @_ == 1;
140 2         3 my $argcount = shift;
141 2         17 my (@callerdata) = caller(1);
142 2 100       11 unless (@args == $argcount) {
143 1         8 $dieproc->(
144             "wrong number of arguments to \&$callerdata[3] - was ",
145             scalar(@args),
146             ", should be $argcount"
147             );
148             }
149             }
150              
151             =item range_argcount I<$minargs>, I<$maxargs>;
152              
153             Specifies that the caller must have at least I<$minargs> arguments but no more
154             than I<$maxargs>.
155              
156             =cut
157             ##------------------------------------------------------------------------------
158             ## range_argcount
159             ##------------------------------------------------------------------------------
160             sub range_argcount ($$) {
161 3 50   3 1 173 my $dieproc = $_PROCS{scalar(caller)} or die no_dieproc;
162             package DB;
163 3 50       7 croak "wrong argument count to Module::Check_Args::range_argcount" unless @_ == 2;
164 3         5 my ($minargs, $maxargs) = @_;
165 3         18 my (@callerdata) = caller(1);
166 3 100 100     20 unless (@args >= $minargs && @args <= $maxargs) {
167 2         12 $dieproc->(
168             "wrong number of arguments to \&$callerdata[3] - was ",
169             scalar(@args),
170             ", should be between $minargs and $maxargs"
171             );
172             }
173             }
174              
175             =item atleast_argcount I<$minargs>;
176              
177             Specifies that the caller must have at least I<$minargs> arguments, but can have
178             any number more than that.
179              
180             =cut
181             ##------------------------------------------------------------------------------
182             ## atleast_argcount
183             ##------------------------------------------------------------------------------
184             sub atleast_argcount ($) {
185 2 50   2 1 122 my $dieproc = $_PROCS{scalar(caller)} or die no_dieproc;
186             package DB;
187 2 50       10 croak "wrong argument count to Module::Check_Args::atleast_argcount" unless @_ == 1;
188 2         3 my $minargs = shift;
189 2         13 my (@callerdata) = caller(1);
190 2 100       8 unless (@args >= $minargs) {
191 1         7 $dieproc->(
192             "not enough arguments to \&$callerdata[3] - was ",
193             scalar(@args),
194             ", should be at least $minargs"
195             );
196             }
197             }
198              
199             =pod
200              
201             =item atmost_argcount I<$maxargs>;
202              
203             Specifies that the caller must have at most I<$maxargs> arguments, but can have
204             any number up to that, including zero.
205              
206             =cut
207             ##------------------------------------------------------------------------------
208             ## atmost_argcount
209             ##------------------------------------------------------------------------------
210             sub atmost_argcount ($) {
211 2 50   2 1 117 my $dieproc = $_PROCS{scalar(caller)} or die no_dieproc;
212             package DB;
213 2 50       6 croak "wrong argument count to Module::Check_Args::atmost_argcount" unless @_ == 1;
214 2         3 my $maxargs = shift;
215 2         11 my (@callerdata) = caller(1);
216 2 100       9 unless (@args <= $maxargs) {
217 1         6 $dieproc->(
218             "too many arguments to \&$callerdata[3] - was ",
219             scalar(@args),
220             ", should be no greater than $maxargs"
221             );
222             }
223             }
224              
225             =back
226              
227             =head1 DIAGNOSTICS
228              
229             =over 4
230              
231             =item wrong argument count to Module::Check_Args::I
232              
233             One of the argument count checking routines was itself called with an invalid
234             argument count. This is always a fatal error regardless of the behavior
235             specified in the B declaration.
236              
237             =item I(I): too many arguments to I - was %d, should be no greater than %d
238              
239             =item I(I): not enough arguments to I - was %d, should be at least %d
240              
241             =item I(I): wrong number of arguments to I - was %d, should be between %d and %d
242              
243             =item I(I): wrong number of arguments to I - was %d, should be %d
244              
245             I was called with an invalid number of arguments at the indicated location.
246             These messages are either fatal errors or warnings depending on the behavior specified
247             in the B declaration.
248              
249             =item no behavior set for Module::Check_Args - 'import' never called?
250              
251             One of the argument count check routines was called, but no behavior
252             (-die, -warn) had ever been set. This can only happen if you use
253             something like the following combination of commands:
254              
255             use Module::Check_Args ();
256             ...
257             &Module::Check_Args::exact_argcount(3);
258              
259             Don't do that.
260              
261             =back
262              
263             =head1 SEE ALSO
264              
265             perlfunc -f caller
266              
267             =head1 AUTHOR
268              
269             Kevin Michael Vail
270              
271             =cut
272              
273             1;
274              
275             ##==============================================================================
276             ## $Log: Check_Args.pm,v $
277             ## Revision 1.1 2000/11/04 18:46:06 kevin
278             ## $module::import != ${module}::import
279             ##
280             ## Revision 1.0 2000/11/04 18:42:27 kevin
281             ## Initial revision
282             ##==============================================================================