File Coverage

blib/lib/Params/CheckCompiler.pm
Criterion Covered Total %
statement 13 14 92.8
branch n/a
condition n/a
subroutine 5 6 83.3
pod 2 2 100.0
total 20 22 90.9


line stmt bran cond sub pod time code
1             package Params::CheckCompiler;
2              
3 9     9   761037 use strict;
  9         14  
  9         191  
4 9     9   28 use warnings;
  9         8  
  9         239  
5              
6             our $VERSION = '0.06';
7              
8 9     9   3072 use Params::CheckCompiler::Compiler;
  9         14  
  9         214  
9              
10 9     9   39 use Exporter qw( import );
  9         7  
  9         608  
11              
12             our @EXPORT_OK = qw( compile source_for );
13              
14             sub compile {
15 23     23 1 70757 return Params::CheckCompiler::Compiler->new(@_)->subref;
16             }
17              
18             sub source_for {
19 0     0 1   return Params::CheckCompiler::Compiler->new(@_)->source_for;
20             }
21              
22             1;
23              
24             # ABSTRACT: Build an optimized subroutine parameter validator once, use it forever
25              
26             __END__
27              
28             =pod
29              
30             =encoding UTF-8
31              
32             =head1 NAME
33              
34             Params::CheckCompiler - Build an optimized subroutine parameter validator once, use it forever
35              
36             =head1 VERSION
37              
38             version 0.06
39              
40             =head1 SYNOPSIS
41              
42             use Types::Standard qw( Int Str );
43             use Params::CheckCompiler qw( compile );
44              
45             {
46             my $check = compile(
47             params => {
48             foo => { type => Int },
49             bar => {
50             type => Str,
51             optional => 1,
52             },
53             baz => {
54             type => Int,
55             default => 42,
56             },
57             },
58             );
59              
60             sub do_something {
61             my %args = $check->(@_);
62             }
63             }
64              
65             =head1 DESCRIPTION
66              
67             B<This is very alpha. The module name could change. Everything could
68             change. You have been warned.>
69              
70             Create a customized, optimized, non-lobotomized, uncompromised, and thoroughly
71             specialized parameter checking subroutine.
72              
73             =head1 EXPORTS
74              
75             This module has two options exports, C<compile> and C<source_for>. Both of
76             these subs accept the same options:
77              
78             =over 4
79              
80             =item * params
81              
82             An arrayref or hashref containing a parameter specification.
83              
84             If you pass an arrayref, the check will expect positional params. Each member
85             of the arrayref represents a single parameter to validate.
86              
87             If you pass a hashref then it will expect named params. For hashrefs, the
88             parameters names are the keys and the specs are the values.
89              
90             The spec can contain either a boolean or hashref. If the spec is a boolean,
91             this indicates required (true) or optional (false).
92              
93             The hashref accepts the following keys:
94              
95             =over 8
96              
97             =item * type
98              
99             A type object. This can be a L<Moose> type (from L<Moose> or
100             L<MooseX::Types>), a L<Type::Tiny> type, or a L<Specio> type.
101              
102             If the type has coercions, those will always be used.
103              
104             =item * default
105              
106             This can either be a simple (non-reference) scalar or a subroutine
107             reference. The sub ref will be called without any arguments (for now).
108              
109             =item * optional
110              
111             A boolean indicating whether or not the parameter is optional. By default,
112             parameters are required unless you provide a default.
113              
114             =back
115              
116             =item * slurpy
117              
118             If this is a simple true value, then the generated subroutine accepts
119             additional arguments not specified in C<params>. By default, extra arguments
120             cause an exception.
121              
122             You can also pass a type constraint here, in which case all extra arguments
123             must be values of the specified type.
124              
125             =back
126              
127             =head2 compile(...)
128              
129             This returns a subroutine that implements the specific parameter
130             checking. Pass this the arguments in C<@_> and it will return a hash of
131             parameters or throw an exception. The compiled subroutine accepts either a
132             hash or a single hashref.
133              
134             For now, you must shift off the invocant yourself.
135              
136             This subroutine accepts an additional parameter:
137              
138             =over 4
139              
140             =item * name
141              
142             If this is given, then the generated subroutine will be named using
143             L<Sub::Name>. This is strongly recommended as it makes it possible to
144             distinguish different check subroutines when profiling or in stack traces.
145              
146             =back
147              
148             =head2 source_for(...)
149              
150             This returns a two element list. The first is a string containing the source
151             code for the generated sub. The second is a hashref of "environment" variables
152             to be used when generating the subroutine. These are the arguments that are
153             passed to L<Eval::Closure>.
154              
155             =head1 SUPPORT
156              
157             Bugs may be submitted through L<the RT bug tracker|http://rt.cpan.org/Public/Dist/Display.html?Name=Params-CheckCompiler>
158             (or L<bug-params-checkcompiler@rt.cpan.org|mailto:bug-params-checkcompiler@rt.cpan.org>).
159              
160             I am also usually active on IRC as 'drolsky' on C<irc://irc.perl.org>.
161              
162             =head1 DONATIONS
163              
164             If you'd like to thank me for the work I've done on this module, please
165             consider making a "donation" to me via PayPal. I spend a lot of free time
166             creating free software, and would appreciate any support you'd care to offer.
167              
168             Please note that B<I am not suggesting that you must do this> in order for me
169             to continue working on this particular software. I will continue to do so,
170             inasmuch as I have in the past, for as long as it interests me.
171              
172             Similarly, a donation made in this way will probably not make me work on this
173             software much more, unless I get so many donations that I can consider working
174             on free software full time (let's all have a chuckle at that together).
175              
176             To donate, log into PayPal and send money to autarch@urth.org, or use the
177             button at L<http://www.urth.org/~autarch/fs-donation.html>.
178              
179             =head1 AUTHOR
180              
181             Dave Rolsky <autarch@urth.org>
182              
183             =head1 COPYRIGHT AND LICENCE
184              
185             This software is Copyright (c) 2016 by Dave Rolsky.
186              
187             This is free software, licensed under:
188              
189             The Artistic License 2.0 (GPL Compatible)
190              
191             =cut