| 1 | package Params::ValidationCompiler; | ||
| 2 | |||
| 3 | 17 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 17 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 17 | our $VERSION = '0.30'; | |
| 7 | |||
| 8 | 17 | ▶ | use Params::ValidationCompiler::Compiler; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 17 | ▶ | use Exporter qw( import ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | 17 | our @EXPORT_OK = qw( compile source_for validation_for ); | |
| 13 | |||
| 14 | sub validation_for { | ||
| 15 | 57 | ▶ | return Params::ValidationCompiler::Compiler->new(@_)->subref; |
|
Subroutine
validation_for: called CC=1 SCAR=0.0
Pod
documented
| |||
| 16 | } | ||
| 17 | |||
| 18 | ## no critic (TestingAndDebugging::ProhibitNoWarnings) | ||
| 19 | 17 | ▶ | no warnings 'once'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 20 | 17 | *compile = \&validation_for; | |
| 21 | ## use critic | ||
| 22 | |||
| 23 | sub source_for { | ||
| 24 | 1 | ▶ | return Params::ValidationCompiler::Compiler->new(@_)->source; |
|
Subroutine
source_for: called CC=1 SCAR=0.0
Pod
documented
| |||
| 25 | } | ||
| 26 | |||
| 27 | 17 | 1; | |
| 28 | |||
| 29 | # ABSTRACT: Build an optimized subroutine parameter validator once, use it forever | ||
| 30 | |||
| 31 | __END__ | ||
| 32 | |||
| 33 | =pod | ||
| 34 | |||
| 35 | =encoding UTF-8 | ||
| 36 | |||
| 37 | =head1 NAME | ||
| 38 | |||
| 39 | Params::ValidationCompiler - Build an optimized subroutine parameter validator once, use it forever | ||
| 40 | |||
| 41 | =head1 VERSION | ||
| 42 | |||
| 43 | version 0.30 | ||
| 44 | |||
| 45 | =head1 SYNOPSIS | ||
| 46 | |||
| 47 | use Types::Standard qw( Int Str ); | ||
| 48 | use Params::ValidationCompiler qw( validation_for ); | ||
| 49 | |||
| 50 | { | ||
| 51 | my $validator = validation_for( | ||
| 52 | params => { | ||
| 53 | foo => { type => Int }, | ||
| 54 | bar => { | ||
| 55 | type => Str, | ||
| 56 | optional => 1, | ||
| 57 | }, | ||
| 58 | baz => { | ||
| 59 | type => Int, | ||
| 60 | default => 42, | ||
| 61 | }, | ||
| 62 | }, | ||
| 63 | ); | ||
| 64 | |||
| 65 | sub foo { | ||
| 66 | my %args = $validator->(@_); | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | { | ||
| 71 | my $validator = validation_for( | ||
| 72 | params => [ | ||
| 73 | { type => Int }, | ||
| 74 | { | ||
| 75 | type => Str, | ||
| 76 | optional => 1, | ||
| 77 | }, | ||
| 78 | ], | ||
| 79 | ); | ||
| 80 | |||
| 81 | sub bar { | ||
| 82 | my ( $int, $str ) = $validator->(@_); | ||
| 83 | } | ||
| 84 | } | ||
| 85 | |||
| 86 | { | ||
| 87 | my $validator = validation_for( | ||
| 88 | params => [ | ||
| 89 | foo => { type => Int }, | ||
| 90 | bar => { | ||
| 91 | type => Str, | ||
| 92 | optional => 1, | ||
| 93 | }, | ||
| 94 | ], | ||
| 95 | named_to_list => 1, | ||
| 96 | ); | ||
| 97 | |||
| 98 | sub baz { | ||
| 99 | my ( $foo, $bar ) = $validator->(@_); | ||
| 100 | } | ||
| 101 | } | ||
| 102 | |||
| 103 | =head1 DESCRIPTION | ||
| 104 | |||
| 105 | This module creates a customized, highly efficient parameter checking | ||
| 106 | subroutine. It can handle named or positional parameters, and can return the | ||
| 107 | parameters as key/value pairs or a list of values. | ||
| 108 | |||
| 109 | In addition to type checks, it also supports parameter defaults, optional | ||
| 110 | parameters, and extra "slurpy" parameters. | ||
| 111 | |||
| 112 | =for Pod::Coverage compile | ||
| 113 | |||
| 114 | =head1 PARAMETERS | ||
| 115 | |||
| 116 | This module has two options exports, C<validation_for> and C<source_for>. Both | ||
| 117 | of these subs accept the same options: | ||
| 118 | |||
| 119 | =head2 params | ||
| 120 | |||
| 121 | An arrayref or hashref containing a parameter specification. | ||
| 122 | |||
| 123 | If you pass a hashref then the generated validator sub will expect named | ||
| 124 | parameters. The C<params> value should be a hashref where the parameter names | ||
| 125 | are keys and the specs are the values. | ||
| 126 | |||
| 127 | If you pass an arrayref and C<named_to_list> is false, the validator will | ||
| 128 | expect positional params. Each element of the C<params> arrayref should be a | ||
| 129 | parameter spec. | ||
| 130 | |||
| 131 | If you pass an arrayref and C<named_to_list> is true, the validator will | ||
| 132 | expect named params, but will return a list of values. In this case the | ||
| 133 | arrayref should contain a I<list> of key/value pairs, where parameter names | ||
| 134 | are the keys and the specs are the values. | ||
| 135 | |||
| 136 | Each spec can contain either a boolean or hashref. If the spec is a boolean, | ||
| 137 | this indicates required (true) or optional (false). | ||
| 138 | |||
| 139 | The spec hashref accepts the following keys: | ||
| 140 | |||
| 141 | =over 4 | ||
| 142 | |||
| 143 | =item * type | ||
| 144 | |||
| 145 | A type object. This can be a L<Moose> type (from L<Moose> or | ||
| 146 | L<MooseX::Types>), a L<Type::Tiny> type, or a L<Specio> type. | ||
| 147 | |||
| 148 | If the type has coercions, those will always be used. | ||
| 149 | |||
| 150 | =item * default | ||
| 151 | |||
| 152 | This can either be a simple (non-reference) scalar or a subroutine | ||
| 153 | reference. The sub ref will be called without any arguments (for now). | ||
| 154 | |||
| 155 | =item * optional | ||
| 156 | |||
| 157 | A boolean indicating whether or not the parameter is optional. By default, | ||
| 158 | parameters are required unless you provide a default. | ||
| 159 | |||
| 160 | =back | ||
| 161 | |||
| 162 | =head2 slurpy | ||
| 163 | |||
| 164 | If this is a simple true value, then the generated subroutine accepts | ||
| 165 | additional arguments not specified in C<params>. By default, extra arguments | ||
| 166 | cause an exception. | ||
| 167 | |||
| 168 | You can also pass a type constraint here, in which case all extra arguments | ||
| 169 | must be values of the specified type. | ||
| 170 | |||
| 171 | =head2 named_to_list | ||
| 172 | |||
| 173 | If this is true, the generated subroutine will expect a list of key-value | ||
| 174 | pairs or a hashref and it will return a list containing only values. The | ||
| 175 | C<params> you pass must be a arrayref of key-value pairs. The order of these | ||
| 176 | pairs determines the order in which values are returned. | ||
| 177 | |||
| 178 | You cannot combine C<slurpy> with C<named_to_list> as there is no way to know | ||
| 179 | how to order the extra return values. | ||
| 180 | |||
| 181 | =head2 return_object | ||
| 182 | |||
| 183 | If this is true, the generated subroutine will return an object instead of a | ||
| 184 | hashref. You cannot set this option to true if you set either or C<slurpy> or | ||
| 185 | C<named_to_list>. | ||
| 186 | |||
| 187 | The object's methods correspond to the parameter names passed to the | ||
| 188 | subroutine. While calling methods on an object is slower than accessing a | ||
| 189 | hashref, the advantage is that if you typo a parameter name you'll get a | ||
| 190 | helpful error. | ||
| 191 | |||
| 192 | If you have L<Class::XSAccessor> installed then this will be used to create | ||
| 193 | the class's methods, which makes it fairly fast. | ||
| 194 | |||
| 195 | The returned object is in a generated class. Do not rely on this class name | ||
| 196 | being anything in specific, and don't check this object using C<isa>, C<DOES>, | ||
| 197 | or anything similar. | ||
| 198 | |||
| 199 | When C<return_object> is true, the parameter spec hashref also accepts to the | ||
| 200 | following additional keys: | ||
| 201 | |||
| 202 | =over 4 | ||
| 203 | |||
| 204 | =item * getter | ||
| 205 | |||
| 206 | Use this to set an explicit getter method name for the parameter. By default | ||
| 207 | the method name will be the same as the parameter name. Note that if the | ||
| 208 | parameter name is not a valid sub name, then you will get an error compiling | ||
| 209 | the validation sub unless you specify a getter for the parameter. | ||
| 210 | |||
| 211 | =item * predicate | ||
| 212 | |||
| 213 | Use this to ask for a predicate method to be created for this parameter. The | ||
| 214 | predicate method returns true if the parameter was passed and false if it | ||
| 215 | wasn't. Note that this is only useful for optional parameters, but you can ask | ||
| 216 | for a predicate for any parameter. | ||
| 217 | |||
| 218 | =back | ||
| 219 | |||
| 220 | =head1 EXPORTS | ||
| 221 | |||
| 222 | The exported subs are: | ||
| 223 | |||
| 224 | =head2 validation_for(...) | ||
| 225 | |||
| 226 | This returns a subroutine that implements the specific parameter | ||
| 227 | checking. This subroutine expects to be given the parameters to validate in | ||
| 228 | C<@_>. If all the parameters are valid, it will return the validated | ||
| 229 | parameters (with defaults as appropriate), either as a list of key-value pairs | ||
| 230 | or as a list of just values. If any of the parameters are invalid it will | ||
| 231 | throw an exception. | ||
| 232 | |||
| 233 | For validators expected named params, the generated subroutine accepts either | ||
| 234 | a list of key-value pairs or a single hashref. Otherwise the validator expects | ||
| 235 | a list of values. | ||
| 236 | |||
| 237 | For now, you must shift off the invocant yourself. | ||
| 238 | |||
| 239 | This subroutine accepts the following additional parameters: | ||
| 240 | |||
| 241 | =over 4 | ||
| 242 | |||
| 243 | =item * name | ||
| 244 | |||
| 245 | If this is given, then the generated subroutine will be named using | ||
| 246 | L<Sub::Util>. This is strongly recommended as it makes it possible to | ||
| 247 | distinguish different check subroutines when profiling or in stack traces. | ||
| 248 | |||
| 249 | This name will also be used in some exception messages, even if L<Sub::Util> | ||
| 250 | is not available. | ||
| 251 | |||
| 252 | Note that you must install L<Sub::Util> yourself separately, as it is not | ||
| 253 | required by this distribution, in order to avoid requiring a compiler. | ||
| 254 | |||
| 255 | =item * name_is_optional | ||
| 256 | |||
| 257 | If this is true, then the name is ignored when C<Sub::Util> is not | ||
| 258 | installed. If this is false, then passing a name when L<Sub::Util> cannot be | ||
| 259 | loaded causes an exception. | ||
| 260 | |||
| 261 | This is useful for CPAN modules where you want to set a name if you can, but | ||
| 262 | you do not want to add a prerequisite on L<Sub::Util>. | ||
| 263 | |||
| 264 | =item * debug | ||
| 265 | |||
| 266 | Sets the C<EVAL_CLOSURE_PRINT_SOURCE> environment variable to true before | ||
| 267 | calling C<Eval::Closure::eval_closure()>. This causes the source of the | ||
| 268 | subroutine to be printed before it's C<eval>'d. | ||
| 269 | |||
| 270 | =back | ||
| 271 | |||
| 272 | =head2 source_for(...) | ||
| 273 | |||
| 274 | This returns a two element list. The first is a string containing the source | ||
| 275 | code for the generated sub. The second is a hashref of "environment" variables | ||
| 276 | to be used when generating the subroutine. These are the arguments that are | ||
| 277 | passed to L<Eval::Closure>. | ||
| 278 | |||
| 279 | =head1 SUPPORT | ||
| 280 | |||
| 281 | Bugs may be submitted at L<https://github.com/houseabsolute/Params-ValidationCompiler/issues>. | ||
| 282 | |||
| 283 | I am also usually active on IRC as 'autarch' on C<irc://irc.perl.org>. | ||
| 284 | |||
| 285 | =head1 SOURCE | ||
| 286 | |||
| 287 | The source code repository for Params-ValidationCompiler can be found at L<https://github.com/houseabsolute/Params-ValidationCompiler>. | ||
| 288 | |||
| 289 | =head1 DONATIONS | ||
| 290 | |||
| 291 | If you'd like to thank me for the work I've done on this module, please | ||
| 292 | consider making a "donation" to me via PayPal. I spend a lot of free time | ||
| 293 | creating free software, and would appreciate any support you'd care to offer. | ||
| 294 | |||
| 295 | Please note that B<I am not suggesting that you must do this> in order for me | ||
| 296 | to continue working on this particular software. I will continue to do so, | ||
| 297 | inasmuch as I have in the past, for as long as it interests me. | ||
| 298 | |||
| 299 | Similarly, a donation made in this way will probably not make me work on this | ||
| 300 | software much more, unless I get so many donations that I can consider working | ||
| 301 | on free software full time (let's all have a chuckle at that together). | ||
| 302 | |||
| 303 | To donate, log into PayPal and send money to autarch@urth.org, or use the | ||
| 304 | button at L<http://www.urth.org/~autarch/fs-donation.html>. | ||
| 305 | |||
| 306 | =head1 AUTHOR | ||
| 307 | |||
| 308 | Dave Rolsky <autarch@urth.org> | ||
| 309 | |||
| 310 | =head1 CONTRIBUTORS | ||
| 311 | |||
| 312 | =for stopwords Gregory Oschwald Tomasz Konojacki | ||
| 313 | |||
| 314 | =over 4 | ||
| 315 | |||
| 316 | =item * | ||
| 317 | |||
| 318 | Gregory Oschwald <goschwald@maxmind.com> | ||
| 319 | |||
| 320 | =item * | ||
| 321 | |||
| 322 | Gregory Oschwald <oschwald@gmail.com> | ||
| 323 | |||
| 324 | =item * | ||
| 325 | |||
| 326 | Tomasz Konojacki <me@xenu.pl> | ||
| 327 | |||
| 328 | =back | ||
| 329 | |||
| 330 | =head1 COPYRIGHT AND LICENSE | ||
| 331 | |||
| 332 | This software is Copyright (c) 2016 - 2018 by Dave Rolsky. | ||
| 333 | |||
| 334 | This is free software, licensed under: | ||
| 335 | |||
| 336 | The Artistic License 2.0 (GPL Compatible) | ||
| 337 | |||
| 338 | The full text of the license can be found in the | ||
| 339 | F<LICENSE> file included with this distribution. | ||
| 340 | |||
| 341 | =cut | ||