File Coverage

blib/lib/Params/Check.pm
Criterion Covered Total %
statement 117 119 98.3
branch 58 64 90.6
condition 41 58 70.6
subroutine 13 13 100.0
pod 3 3 100.0
total 232 257 90.2


line stmt bran cond sub pod time code
1             package Params::Check;
2              
3 1     1   35997 use strict;
  1         1  
  1         41  
4              
5 1     1   6 use Carp qw[carp croak];
  1         2  
  1         88  
6 1     1   1857 use Locale::Maketext::Simple Style => 'gettext';
  1         1797  
  1         7  
7              
8             BEGIN {
9 1     1   498 use Exporter ();
  1         2  
  1         30  
10 1         474 use vars qw[ @ISA $VERSION @EXPORT_OK $VERBOSE $ALLOW_UNKNOWN
11             $STRICT_TYPE $STRIP_LEADING_DASHES $NO_DUPLICATES
12             $PRESERVE_CASE $ONLY_ALLOW_DEFINED $WARNINGS_FATAL
13             $SANITY_CHECK_TEMPLATE $CALLER_DEPTH $_ERROR_STRING
14 1     1   5 ];
  1         1  
15              
16 1     1   19 @ISA = qw[ Exporter ];
17 1         2 @EXPORT_OK = qw[check allow last_error];
18              
19 1         2 $VERSION = '0.38';
20 1 50       5 $VERBOSE = $^W ? 1 : 0;
21 1         2 $NO_DUPLICATES = 0;
22 1         1 $STRIP_LEADING_DASHES = 0;
23 1         2 $STRICT_TYPE = 0;
24 1         2 $ALLOW_UNKNOWN = 0;
25 1         1 $PRESERVE_CASE = 0;
26 1         2 $ONLY_ALLOW_DEFINED = 0;
27 1         2 $SANITY_CHECK_TEMPLATE = 1;
28 1         1 $WARNINGS_FATAL = 0;
29 1         1619 $CALLER_DEPTH = 0;
30             }
31              
32             my %known_keys = map { $_ => 1 }
33             qw| required allow default strict_type no_override
34             store defined |;
35              
36             =pod
37              
38             =head1 NAME
39              
40             Params::Check - A generic input parsing/checking mechanism.
41              
42             =head1 SYNOPSIS
43              
44             use Params::Check qw[check allow last_error];
45              
46             sub fill_personal_info {
47             my %hash = @_;
48             my $x;
49              
50             my $tmpl = {
51             firstname => { required => 1, defined => 1 },
52             lastname => { required => 1, store => \$x },
53             gender => { required => 1,
54             allow => [qr/M/i, qr/F/i],
55             },
56             married => { allow => [0,1] },
57             age => { default => 21,
58             allow => qr/^\d+$/,
59             },
60              
61             phone => { allow => [ sub { return 1 if /$valid_re/ },
62             '1-800-PERL' ]
63             },
64             id_list => { default => [],
65             strict_type => 1
66             },
67             employer => { default => 'NSA', no_override => 1 },
68             };
69              
70             ### check() returns a hashref of parsed args on success ###
71             my $parsed_args = check( $tmpl, \%hash, $VERBOSE )
72             or die qw[Could not parse arguments!];
73              
74             ... other code here ...
75             }
76              
77             my $ok = allow( $colour, [qw|blue green yellow|] );
78              
79             my $error = Params::Check::last_error();
80              
81              
82             =head1 DESCRIPTION
83              
84             Params::Check is a generic input parsing/checking mechanism.
85              
86             It allows you to validate input via a template. The only requirement
87             is that the arguments must be named.
88              
89             Params::Check can do the following things for you:
90              
91             =over 4
92              
93             =item *
94              
95             Convert all keys to lowercase
96              
97             =item *
98              
99             Check if all required arguments have been provided
100              
101             =item *
102              
103             Set arguments that have not been provided to the default
104              
105             =item *
106              
107             Weed out arguments that are not supported and warn about them to the
108             user
109              
110             =item *
111              
112             Validate the arguments given by the user based on strings, regexes,
113             lists or even subroutines
114              
115             =item *
116              
117             Enforce type integrity if required
118              
119             =back
120              
121             Most of Params::Check's power comes from its template, which we'll
122             discuss below:
123              
124             =head1 Template
125              
126             As you can see in the synopsis, based on your template, the arguments
127             provided will be validated.
128              
129             The template can take a different set of rules per key that is used.
130              
131             The following rules are available:
132              
133             =over 4
134              
135             =item default
136              
137             This is the default value if none was provided by the user.
138             This is also the type C will look at when checking type
139             integrity (see below).
140              
141             =item required
142              
143             A boolean flag that indicates if this argument was a required
144             argument. If marked as required and not provided, check() will fail.
145              
146             =item strict_type
147              
148             This does a C check on the argument provided. The C of the
149             argument must be the same as the C of the default value for this
150             check to pass.
151              
152             This is very useful if you insist on taking an array reference as
153             argument for example.
154              
155             =item defined
156              
157             If this template key is true, enforces that if this key is provided by
158             user input, its value is C. This just means that the user is
159             not allowed to pass C as a value for this key and is equivalent
160             to:
161             allow => sub { defined $_[0] && OTHER TESTS }
162              
163             =item no_override
164              
165             This allows you to specify C in your template. ie, they
166             keys that are not allowed to be altered by the user. It pretty much
167             allows you to keep all your C data in one place; the
168             C template.
169              
170             =item store
171              
172             This allows you to pass a reference to a scalar, in which the data
173             will be stored:
174              
175             my $x;
176             my $args = check(foo => { default => 1, store => \$x }, $input);
177              
178             This is basically shorthand for saying:
179              
180             my $args = check( { foo => { default => 1 }, $input );
181             my $x = $args->{foo};
182              
183             You can alter the global variable $Params::Check::NO_DUPLICATES to
184             control whether the C'd key will still be present in your
185             result set. See the L section below.
186              
187             =item allow
188              
189             A set of criteria used to validate a particular piece of data if it
190             has to adhere to particular rules.
191              
192             See the C function for details.
193              
194             =back
195              
196             =head1 Functions
197              
198             =head2 check( \%tmpl, \%args, [$verbose] );
199              
200             This function is not exported by default, so you'll have to ask for it
201             via:
202              
203             use Params::Check qw[check];
204              
205             or use its fully qualified name instead.
206              
207             C takes a list of arguments, as follows:
208              
209             =over 4
210              
211             =item Template
212              
213             This is a hash reference which contains a template as explained in the
214             C and C