File Coverage

blib/lib/Params/Validate/Array.pm
Criterion Covered Total %
statement 36 36 100.0
branch 7 8 87.5
condition n/a
subroutine 10 10 100.0
pod 0 2 0.0
total 53 56 94.6


line stmt bran cond sub pod time code
1             package Params::Validate::Array;
2              
3 40     40   324288 use 5.008008;
  40         143  
  40         2137  
4 40     40   210 use strict;
  40         71  
  40         1219  
5 40     40   197 use warnings;
  40         74  
  40         1249  
6              
7 40     40   199 use base qw( Exporter );
  40         73  
  40         5247  
8              
9 40     40   222 use vars qw($VERSION @EXPORT @EXPORT_OK %EXPORT_TAGS $DOC);
  40         79  
  40         17763  
10              
11             our $VERSION = '0.03';
12              
13 40     40   46968 use Params::Validate ();
  40         503695  
  40         1331  
14 40     40   359 use Carp qw(confess);
  40         89  
  40         16426  
15              
16             # copy Params::Validate's EXPORT* constants
17             @EXPORT = @Params::Validate::EXPORT;
18             @EXPORT_OK = @Params::Validate::EXPORT_OK;
19             %EXPORT_TAGS = %Params::Validate::EXPORT_TAGS;
20              
21             sub import {
22             # import all of P::V except validate() and validation_options()
23 880 100       9483 Params::Validate->import(
24 55     55   288311 grep { $_ ne 'validate' and
25             $_ ne 'validation_options' } @Params::Validate::EXPORT_OK
26             );
27              
28             # now export all that P::V would have exported
29 55         39342 __PACKAGE__->export_to_level(1, @_);
30             }
31              
32             Params::Validate::validation_options(stack_skip => 2);
33              
34             sub validate(\@$) {
35 442     442 0 452983 my $a = $_[0];
36              
37 442         665 my $spec = $_[1];
38              
39 442 100       9007 return Params::Validate::validate(@$a, $spec)
40             unless ref $spec eq 'ARRAY';
41              
42 162 50       455 confess "validate(): odd number of args in specification"
43             if @$spec % 2;
44              
45             # get the "keys", the 0th, 2nd, 4th, ... values of the @$spec array
46 162         217 my $i = 1;
47 162         282 my @keys = grep { $i++ % 2 } @$spec;
  540         1017  
48              
49 162         242 return @{Params::Validate::validate(@$a, { @$spec } )}{@keys};
  162         5593  
50             }
51              
52             sub validation_options {
53 5     5 0 5943 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 100       23 $opts{stack_skip}++ if exists $opts{stack_skip};
60              
61 5         30 Params::Validate::validation_options(%opts);
62             }
63              
64             1;
65              
66             =head1 NAME
67              
68             Params::Validate::Array - provide an alternative version of Param::Validate's
69             C 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 function is a replacement for the Params::Validate
78             module's C function, returning the arguments as a list,
79             and not a hash as Params::Validate::validate() does.
80              
81             This replacement C 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 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 and C by default. The
149             functions C and C as well as
150             constants C, C, etc are also available for export. See
151             L for details.
152              
153             Only the behaviour of C 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 which will increment any stack_skip => ...
156             argument to hide the extra layer of call stack).
157              
158             =head1 SUBROUTINES/METHODS
159              
160             B ]);>
161              
162             In contrast to the C subroutine in L,
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
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<< >>
182              
183             =head1 BUGS
184              
185             Please report any bugs or feature requests to
186             C, or through the web interface at
187             L. 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 and
194             L 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