blib/lib/Params/Validate/Micro.pm

filter: Statementstmt 97.5% 81 / 83 Branchbran 90.0% 27 / 30 Conditioncond 76.4% 13 / 17 MC/DCmcdc 60.0% 6 / 10 Subroutinesub 100% 8 / 8 Podpod 100% 2 / 2 totaltotal 91.3% 137 / 150 CC 24 SCAR 21.0
CC
24
Cov
93%
micro_translate
12.1
micro_validate
8.5
_pos_to_named
6.0
CRAP 8.2
↑ Summary
1 package Params::Validate::Micro;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Params::Validate qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Carp qw(croak confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 require Exporter;
10 4 our @ISA = qw(Exporter);
11 4 our %EXPORT_TAGS = (
12 all => [qw(micro_validate micro_translate)],
13 );
14 4 our @EXPORT_OK = (@{ $EXPORT_TAGS{all} });
15  
16 =head1 NAME
17  
18 Params::Validate::Micro - Validate parameters concisely
19  
20 =head1 VERSION
21  
22 Version 0.032
23  
24 =cut
25  
26 4 our $VERSION = '0.032';
27  
28 =head1 SYNOPSIS
29  
30 use Params::Validate::Micro qw(:all);
31 use Params::Validate::Micro qw(micro_validate micro_translate);
32  
33 =head1 DESCRIPTION
34  
35 Params::Validate::Micro allows you to concisely represent a
36 list of arguments, their types, and whether or not they are
37 required.
38  
39 Nothing is exported by default. Use C<< :all >> or the
40 specific function name you want.
41  
42 =head1 FORMAT
43  
44 Micro argument strings are made up of lists of parameter
45 names. Each name may have an optional sigil (one of C<< $@%
46 >>), which translate directly to the Params::Validate
47 constrations of SCALAR | OBJECT, ARRAYREF, and HASHREF, respectively.
48  
49 There may be one semicolon (C<< ; >>) in your argument
50 string. If present, any parameters listed after the
51 semicolon are marked as optional.
52  
53 Examples:
54  
55 =over 4
56  
57 =item Single scalar argument
58  
59 $text
60  
61 =item Hashref and optional scalar
62  
63 %opt; $verbose
64  
65 =item Two arrayrefs and an untyped argument
66  
67 @addrs @lines message
68  
69 =back
70  
71 You may also have an empty argument string. This indicates
72 that you want no parameters at all.
73  
74 =head1 FUNCTIONS
75  
76 =head3 C<< micro_translate >>
77  
78 my %spec = micro_translate($string, $extra);
79  
80 Turns C<< $string >> into a Params::Validate spec as
81 described in L</FORMAT>, then merges the resultant spec and
82 the optional C<< $extra >> hashref.
83  
84 This returns a list, which just happens to be a set of key
85 => value pairs. This matters because it means that if you
86 wanted to you could treat it as an array for long enough to
87 figure out what order the parameters were specified in. You
88 could use this to do your own optional positional
89 validation.
90  
91 =head3 C<< micro_validate >>
92  
93 my $arg = micro_validate(\%arg, $string, $extra);
94 my $arg = micro_validate(\@args, $string, $extra);
95  
96 Use C<< micro_translate >> with C<< $string >> and C<<
97 $extra >>, then passes the whole thing to Params::Validate.
98  
99 Named parameters should be passed in as a hashref, and
100 positional parameters as an arrayref. Positional parameters
101 will be associated with names in the order specified in C<<
102 $string >>. For example:
103  
104 micro_validate({ a => 1 }, q{$a; $b});
105 micro_validate([ 1 ], q{$a; $b});
106  
107 Both will return this:
108  
109 { a => 1 }
110  
111 When passing positional parameters, C<< micro_validate >>
112 will die if there are either too many for the spec or not
113 enough to fill all non-optional parameters.
114  
115 Returns a hashref of the validated arguments.
116  
117 =cut
118  
119 4 my $BARE_VAR = qr/[a-z_]\w*/i;
120  
121 4 my $SIGIL_VAR = qr/[%\$\@]?$BARE_VAR/i;
122  
123 4 my $EXTRACT_VARS = qr/\A
124 (
125 (?: \s* ; \s*)?
126 $SIGIL_VAR
127 (?:
128 (?: \s* ; )?
129 \s+ $SIGIL_VAR
130 )*
131 )?
132 \z/x;
133  
134 4 my %PVSPEC = (
135 '%' => {
136 type => HASHREF,
137 },
138 '@' => {
139 type => ARRAYREF,
140 },
141 '$' => {
142 type => SCALAR | OBJECT,
143 },
144 );
145  
146 4 my ($SIGIL) = map { qr/$_/ } '[' . join("", keys %PVSPEC) . ']';
147  
148 21 ▶sub micro_translate {
Subroutine micro_translate: called CC=12 SCAR=24.9
Pod documented
149 21 my ($string, $extra) = @_;
150 21 $string =~ s/^\s*//;
151 21 $string =~ s/\s*$//;
152 21 ▶ croak "'$string' does not appear to be a micro-spec"
Branch: 1 branch
BranchTrueFalseTotal
unless $string =~ /$EXTRACT_VARS/ 0 21 21
153 unless $string =~ $EXTRACT_VARS;
154  
155 # maybe they want to say "no args at all"
156 21 ▶ return unless defined $1;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $1 4 17 21
157  
158 my @vspecs = grep {
159 52 length($_)
160 } map {
161 # make sure that semicolons are their own 'word'
162 17 s/;/ ; /g;
163 17 split /\s+/;
164 } $string =~ $EXTRACT_VARS;
165  
166 17 my $optional;
167 my @spec;
168 17 for my $vspec (@vspecs) {
169 50 ▶ if ($vspec eq ';') {
Branch: 1 branch
BranchTrueFalseTotal
if ($vspec eq ";") 7 43 50
170 7 ▶ if ($optional++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($optional++) 1 6 7
171 1 croak "micro-spec '$string' contains multiple semicolons";
172 }
173 6 next;
174 }
175 43 my $vname = $vspec;
176 43 my $spart = {};
177 43 ▶ while ($vname =~ s/^($SIGIL)//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vname =~ s/^($SIGIL)//) 29 43 72
178 29 my $sigil = $1;
179 29 ▶ $spart = { %$spart, %{$PVSPEC{$sigil} || {}} };
Condition50%1/2
$PVSPEC{$sigil} || {}
l!l
290
MC/DC0%0/1
$PVSPEC{$sigil} || {}
$PVSPEC{$sigil}
Truth table50%1/2
A
A= $PVSPEC{$sigil}
Aresult
11
00
180 }
181 43 ▶ unless ($vname =~ /\A$BARE_VAR\z/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($vname =~ /\A$BARE_VAR\z/) 0 43 43
182 0 croak "illegal parameter name: '$vname'";
183 }
184 43 ▶ if ($optional) {
Branch: 1 branch
BranchTrueFalseTotal
if ($optional) 10 33 43
185 10 $spart->{optional} = 1;
186 }
187 43 ▶ if ($extra->{$vname}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($extra->{$vname}) 3 40 43
188 # as of now, the only things that may be already set in $spart are 'type'
189 # and 'optional'. it is therefore safe to naively join the hashes, since
190 # we don't need to worry about more complex cases like merging nested
191 # 'callbacks' entries. re-evaluate this if more complex specs are
192 # being generated automatically. -- hdp, 2007-04-10
193 3 %$spart = (%$spart, %{$extra->{$vname}});
194 }
195 43 ▶ unless (%$spart) {
Branch: 1 branch
BranchTrueFalseTotal
unless (%$spart) 11 32 43
196 11 $spart = 1;
197 }
198 43 push @spec, $vname => $spart;
199 }
200  
201 16 return @spec;
202 }
203  
204 10 ▶sub _pos_to_named {
Subroutine _pos_to_named: called CC=6 SCAR=17.9
205 10 my ($string, $args, $spec) = @_;
206 10 my @tmpspec = @$spec;
207 10 my @tmpargs = @$args;
208 10 my $return = {};
209 10 ▶ while (my ($key, $val) = splice @tmpspec, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@tmpspec, 0, 2)) 26 6 32
210 26 ▶ unless (@tmpargs) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@tmpargs) 4 22 26
211 4 ▶ if (ref($val) eq 'HASH' and $val->{optional}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq 'HASH' and $val->{'optional'}) { } 2 2 4
Condition100%3/3
ref $val eq "HASH" and $val->{"optional"}
!ll&&!rl&&r
112
MC/DC100%2/2
ref $val eq "HASH" and $val->{"optional"}
ref $val eq "HASH" $val->{"optional"}
Truth table100%3/3
A and B
A= ref $val eq "HASH"B= $val->{"optional"}
ABresult
0X0
100
111
212 2 last;
213 } else {
214 2 confess "not enough arguments for '$string' (only got @$args)";
215 }
216 }
217 22 $return->{$key} = shift(@tmpargs);
218 }
219 8 ▶ if (@tmpargs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@tmpargs) 1 7 8
220 1 confess "too many arguments for '$string' (leftover: @tmpargs)";
221 }
222 7 return $return;
223 }
224  
225 15 ▶sub micro_validate {
Subroutine micro_validate: called CC=8 SCAR=21.3
Pod documented
226 15 my ($args, $string, $extra) = @_;
227 15 ▶ $args ||= {};
Condition100%2/2
$args ||= {}
l!l
141
MC/DC100%1/1
$args ||= {}
$args
Truth table100%2/2
A
A= $args
Aresult
11
00
228 15 ▶ $string ||= "";
Condition100%2/2
$string ||= ""
l!l
123
MC/DC100%1/1
$string ||= ""
$string
Truth table100%2/2
A
A= $string
Aresult
11
00
229 15 ▶ $extra ||= {};
Condition100%2/2
$extra ||= {}
l!l
213
MC/DC100%1/1
$extra ||= {}
$extra
Truth table100%2/2
A
A= $extra
Aresult
11
00
230  
231 15 my $spec = [ micro_translate($string, $extra) ];
232  
233 15 ▶ if ($args and reftype($args) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($args and builtin::reftype($args) eq "ARRAY") 10 5 15
Condition66%2/3
$args and builtin::reftype($args) eq "ARRAY"
!ll&&!rl&&r
0510
MC/DC50%1/2
$args and builtin::reftype($args) eq "ARRAY"
$args builtin::reftype($args) eq "ARRAY"
Truth table66%2/3
A and B
A= $argsB= builtin::reftype($args) eq "ARRAY"
ABresult
0X0
100
111
234 10 $args = _pos_to_named($string, $args, $spec);
235 }
236 12 ▶ unless ($args and reftype($args) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args and builtin::reftype($args) eq "HASH") 0 12 12
Condition33%1/3
$args and builtin::reftype($args) eq "HASH"
!ll&&!rl&&r
0012
MC/DC0%0/2
$args and builtin::reftype($args) eq "HASH"
$args builtin::reftype($args) eq "HASH"
Truth table33%1/3
A and B
A= $argsB= builtin::reftype($args) eq "HASH"
ABresult
0X0
100
111
237 0 croak "first argument to micro_validate must be hashref or arrayref";
238 }
239  
240 return {
241 12 validate_with(
242 params => $args,
243 spec => { @$spec },
244 )
245 };
246 }
247  
248 =head1 SEE ALSO
249  
250 L<Params::Validate>
251  
252 =head1 AUTHOR
253  
254 Hans Dieter Pearcey, C<< <hdp@cpan.org> >>
255  
256 =head1 BUGS
257  
258 Please report any bugs or feature requests to
259 C<bug-params-validate-micro@rt.cpan.org>, or through the web interface at
260 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Params-Validate-Micro>.
261 I will be notified, and then you'll automatically be notified of progress on
262 your bug as I make changes.
263  
264 =head1 COPYRIGHT & LICENSE
265  
266 Copyright 2005 Hans Dieter Pearcey, all rights reserved.
267  
268 This program is free software; you can redistribute it and/or modify it
269 under the same terms as Perl itself.
270  
271 =cut
272  
273 4 1; # End of Params::Validate::Micro
↑ Summary