summaryrefslogtreecommitdiff
path: root/doc/tools/checkargs.pm
blob: de52f69c457b187f9133a1526da2b61897da7c0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/uns/bin/perl

package checkargs;
require 5.004;			# uses "for my $var"
require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(check_args check_args_range check_args_at_least);
use strict;
use Carp;

=head1 NAME

checkargs -- Provide rudimentary argument checking for perl5 functions

=head1 SYNOPSIS

  check_args(cArgsExpected, @_)
  check_args_range(cArgsMin, cArgsMax, @_)
  check_args_at_least(cArgsMin, @_)
where "@_" should be supplied literally.

=head1 DESCRIPTION

As the first line of user-written subroutine foo, do one of the following:

  my ($arg1, $arg2) = check_args(2, @_);
  my ($arg1, @rest) = check_args_range(1, 4, @_);
  my ($arg1, @rest) = check_args_at_least(1, @_);
  my @args = check_args_at_least(0, @_);

These functions may also be called for side effect (put a call to one
of the functions near the beginning of the subroutine), but using the
argument checkers to set the argument list is the recommended usage.

The number of arguments and their definedness are checked; if the wrong
number are received, the program exits with an error message.

=head1 AUTHOR

Michael D. Ernst <F<mernst@cs.washington.edu>>

=cut

## Need to check that use of caller(1) really gives desired results.
## Need to give input chunk information.
## Is this obviated by Perl 5.003's declarations?  Not entirely, I think.

sub check_args ( $@ )
{
  my ($num_formals, @args) = @_;
  my ($pack, $file_arg, $line_arg, $subname, $hasargs, $wantarr) = caller(1);
  if (@_ < 1) { croak "check_args needs at least 7 args, got ", scalar(@_), ": @_\n "; }
  if ((!wantarray) && ($num_formals != 0))
    { croak "check_args called in scalar context"; }
  # Can't use croak below here: it would only go out to caller, not its caller
  my $num_actuals = @args;
  if ($num_actuals != $num_formals)
    { die "$file_arg:$line_arg: function $subname expected $num_formals argument",
      (($num_formals == 1) ? "" : "s"),
      ", got $num_actuals",
      (($num_actuals == 0) ? "" : ": @args"),
      "\n"; }
  for my $index (0..$#args)
    { if (!defined($args[$index]))
	{ die "$file_arg:$line_arg: function $subname undefined argument ", $index+1, ": @args[0..$index-1]\n"; } }
  return @args;
}

sub check_args_range ( $$@ )
{
  my ($min_formals, $max_formals, @args) = @_;
  my ($pack, $file_arg, $line_arg, $subname, $hasargs, $wantarr) = caller(1);
  if (@_ < 2) { croak "check_args_range needs at least 8 args, got ", scalar(@_), ": @_"; }
  if ((!wantarray) && ($max_formals != 0) && ($min_formals !=0) )
    { croak "check_args_range called in scalar context"; }
  # Can't use croak below here: it would only go out to caller, not its caller
  my $num_actuals = @args;
  if (($num_actuals < $min_formals) || ($num_actuals > $max_formals))
    { die "$file_arg:$line_arg: function $subname expected $min_formals-$max_formals arguments, got $num_actuals",
      ($num_actuals == 0) ? "" : ": @args", "\n"; }
  for my $index (0..$#args)
    { if (!defined($args[$index]))
	{ die "$file_arg:$line_arg: function $subname undefined argument ", $index+1, ": @args[0..$index-1]\n"; } }
  return @args;
}

sub check_args_at_least ( $@ )
{
  my ($min_formals, @args) = @_;
  my ($pack, $file_arg, $line_arg, $subname, $hasargs, $wantarr) = caller(1);
  # Don't do this, because we want every sub to start with a call to check_args*
  # if ($min_formals == 0)
  #   { die "Isn't it pointless to check for at least zero args to $subname?\n"; }
  if (scalar(@_) < 1)
    { croak "check_args_at_least needs at least 1 arg, got ", scalar(@_), ": @_"; }
  if ((!wantarray) && ($min_formals != 0))
    { croak "check_args_at_least called in scalar context"; }
  # Can't use croak below here: it would only go out to caller, not its caller
  my $num_actuals = @args;
  if ($num_actuals < $min_formals)
    { die "$file_arg:$line_arg: function $subname expected at least $min_formals argument",
      ($min_formals == 1) ? "" : "s",
      ", got $num_actuals",
      ($num_actuals == 0) ? "" : ": @args", "\n"; }
  for my $index (0..$#args)
    { if (!defined($args[$index]))
	{ warn "$file_arg:$line_arg: function $subname undefined argument ", $index+1, ": @args[0..$index-1]\n"; last; } }
  return @args;
}

1;				# successful import
__END__