blib/lib/Params/Validate/Array.pm

filter: Statementstmt 100% 41 / 41 Branchbran 83.3% 5 / 6 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 10 / 10 Podpod 0.0% 0 / 2 totaltotal 95.3% 61 / 64 CC 5 SCAR 5.4
CC
5
Cov
98%
validate
3.0
import
2.0
validation_options
2.0
CRAP 1.7
↑ Summary
1 package Params::Validate::Array;
2  
3 40 ▶use 5.008008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 40 ▶use base qw( Exporter );
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 40 ▶use vars qw($VERSION @EXPORT @EXPORT_OK %EXPORT_TAGS $DOC);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 40 our $VERSION = '0.03';
12  
13 40 ▶use Params::Validate ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 40 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 # copy Params::Validate's EXPORT* constants
17 40 @EXPORT = @Params::Validate::EXPORT;
18 40 @EXPORT_OK = @Params::Validate::EXPORT_OK;
19 40 %EXPORT_TAGS = %Params::Validate::EXPORT_TAGS;
20  
21 55 ▶sub import {
Subroutine import: called CC=2 SCAR=6.9
22 # import all of P::V except validate() and validation_options()
23 Params::Validate->import(
24 55 ▶ grep { $_ ne 'validate' and
Condition100%3/3
$_ ne 'validate' && $_ ne 'validation_options'
!ll&&!rl&&r
5555770
MC/DC100%2/2
$_ ne 'validate' && $_ ne 'validation_options'
$_ ne 'validate' $_ ne 'validation_options'
Truth table100%3/3
A && B
A= $_ ne 'validate'B= $_ ne 'validation_options'
ABresult
0X0
100
111
25 $_ ne 'validation_options' } @Params::Validate::EXPORT_OK
26 );
27  
28 # now export all that P::V would have exported
29 55 __PACKAGE__->export_to_level(1, @_);
30 }
31  
32 40 Params::Validate::validation_options(stack_skip => 2);
33  
34 442 ▶sub validate(\@$) {
Subroutine validate: called CC=3 SCAR=11.0
Pod undocumented
35 442 my $a = $_[0];
36  
37 442 my $spec = $_[1];
38  
39 442 ▶ return Params::Validate::validate(@$a, $spec)
Branch: 1 branch
BranchTrueFalseTotal
unless ref $spec eq "ARRAY" 280 162 442
40 unless ref $spec eq 'ARRAY';
41  
42 162 ▶ confess "validate(): odd number of args in specification"
Branch: 1 branch
BranchTrueFalseTotal
if @$spec % 2 0 162 162
43 if @$spec % 2;
44  
45 # get the "keys", the 0th, 2nd, 4th, ... values of the @$spec array
46 162 my $i = 1;
47 162 my @keys = grep { $i++ % 2 } @$spec;
48  
49 162 return @{Params::Validate::validate(@$a, { @$spec } )}{@keys};
50 }
51  
52 5 ▶sub validation_options {
Subroutine validation_options: called CC=2 SCAR=6.9
Pod undocumented
53 5 my %opts = @_;
54  
55 #print "validation_options() ", join(', ', ( caller(0) )[0,3] ), "\n";
56 #map { print "$_ => ", $opts{$_} || 'NULL', "\n"} keys %opts;
57  
58  
59 5 ▶ $opts{stack_skip}++ if exists $opts{stack_skip};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{"stack_skip"} 2 3 5
60  
61 5 Params::Validate::validation_options(%opts);
62 }
63  
64 40 1;
65  
66 =head1 NAME
67  
68 Params::Validate::Array - provide an alternative version of Param::Validate's
69 C<validate()> function which will return parameters as a list.
70  
71 =head1 VERSION
72  
73 Version 0.03
74  
75 =head1 SYNOPSIS
76  
77 This module's C<validate()> function is a replacement for the Params::Validate
78 module's C<validate()> function, returning the arguments as a list,
79 and not a hash as Params::Validate::validate() does.
80  
81 This replacement C<validate()> requires the argument descriptor to be an array
82 reference, not a hash reference as in Params::Validate::validate().
83  
84 =head1 Examples:
85  
86 use Params::Validate::Array qw(SCALAR ARRAYREF validate ...);
87  
88 sub foo1 {
89 my ($a, $b, $c) = validate( @_,
90 # note the arrayref, not a hashref as in Params::Validate
91 [ a => 1, # types of a, b, & c are
92 b => 0, #+ unspecified. a is mandatory,
93 c => 0, #+ b & c optional
94 ]
95 );
96  
97 print "a = $a\n";
98 print "b = ", $b // "undefined", "\n";
99 print "c = ", $c // "undefined", "\n";
100 }
101  
102 foo1(a => 'hello', c => 'there');
103 # prints:
104 # a = hello
105 # b = undefined
106 # c = there
107  
108 foo1(b => 1, c => 'foo');
109 # throws error:
110 # "Mandatory parameter 'a' missing in call to main::foo1 ..."
111  
112 }
113  
114 sub foo2 {
115 my ($x, $y) = validate( @_,
116 # arrayref, not hashref
117 [ x => {type => HASHREF, optional => 1}, # hashref 'x' is optional,
118 y => {type => SCALAR }, #+ scalar 'y' mandatory
119 ]
120 );
121  
122 $x->{$y} = 'foo'
123 if defined $x;
124 }
125  
126 Note that if this module's C<validate()> function is called with
127 a hashref argument descriptor, the behaviour reverts to that of
128 Params::Validate::validate():
129  
130 use Params::Validate::Array qw(SCALAR HASHREF validate);
131  
132 sub foo3 {
133 my %arg = validate( @_,
134 # Note hashref
135 { x => {type => HASHREF, optional => 1},
136 y => {type => SCALAR },
137 }
138 );
139  
140 print "y arg is ", $arg{y}, "\n";
141 ...
142 }
143  
144  
145 =head1 EXPORT
146  
147 Params::Validate::Array exports everything that Params::Validate
148 does, including C<validate()> and C<validate_pos()> by default. The
149 functions C<validate_with()> and C<validation_options()> as well as
150 constants C<SCALAR>, C<ARRAYREF>, etc are also available for export. See
151 L<Params::Validate> for details.
152  
153 Only the behaviour of C<validate()> is changed, and only when the argument
154 descriptor is an arrayref. All other routines are identical to those in
155 Params::Validate (Except for C<validation_options()> which will increment any stack_skip => ...
156 argument to hide the extra layer of call stack).
157  
158 =head1 SUBROUTINES/METHODS
159  
160 B<my @args = validate(@_, [ I<descriptor> ]);>
161  
162 In contrast to the C<validate()> subroutine in L<Params::Validate>,
163 which is called as:
164  
165 my %args = validate(@_, { ... } );
166  
167 (where the hashref argument C<{...}> is a descriptor for the subroutine
168 arguments to be validated), the C<Params::Validate::Array::validate()>
169 subroutine in this package is called as
170  
171 my ($arg1, $arg2, ...) = validate(@_, [ ... ] );
172  
173 where the contents of the descriptor C<[...]> are identical to those in the
174 hashref descriptor C<{...}> of the Params::Validate call.
175  
176 In fact Params::Validate::Array::validate() is little more than a wrapper, and
177 uses Params::Validate::validate() to do all the hard work.
178  
179 =head1 AUTHOR
180  
181 Sam Brain, C<< <samb at stanford.edu> >>
182  
183 =head1 BUGS
184  
185 Please report any bugs or feature requests to
186 C<bug-test at rt.cpan.org>, or through the web interface at
187 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test>. I will be
188 notified, and then you'll automatically be notified of progress on your
189 bug as I make changes.
190  
191 =head1 ACKNOWLEDGEMENTS
192  
193 Thanks to Dave Rolsky for the L<Params::Validate> and
194 L<MooseX::Params::Validate> modules, whose perl code and test suites I plagiarized
195 shamelessly for this module.
196  
197 =head1 LICENSE AND COPYRIGHT
198  
199 Copyright 2011 Sam Brain.
200  
201 This program is free software; you can redistribute it and/or modify it
202 under the terms of either: the GNU General Public License as published
203 by the Free Software Foundation; or the Artistic License.
204  
205 See http://dev.perl.org/licenses/ for more information.
206  
207 =cut
208  
209 ##### SUBROUTINE INDEX #####
210 # #
211 # gen by index_subs.pl #
212 # on 14 Mar 2014 21:53 #
213 # #
214 ############################
215  
216  
217 ####### Packages ###########
218  
219 # Params::Validate::Array ..................... 1
220 # foo1 ...................................... 2
221 # foo2 ...................................... 2
222 # foo3 ...................................... 3
223 # import .................................... 1
224 # validate .................................. 1
225 # validation_options ........................ 1
226  
↑ Summary