1b39c5158Smillertpackage Params::Check; 2b39c5158Smillert 3b39c5158Smillertuse strict; 4b39c5158Smillert 5b39c5158Smillertuse Carp qw[carp croak]; 6b39c5158Smillertuse Locale::Maketext::Simple Style => 'gettext'; 7b39c5158Smillert 8b39c5158SmillertBEGIN { 9b39c5158Smillert use Exporter (); 10b39c5158Smillert use vars qw[ @ISA $VERSION @EXPORT_OK $VERBOSE $ALLOW_UNKNOWN 11b39c5158Smillert $STRICT_TYPE $STRIP_LEADING_DASHES $NO_DUPLICATES 12b39c5158Smillert $PRESERVE_CASE $ONLY_ALLOW_DEFINED $WARNINGS_FATAL 13b39c5158Smillert $SANITY_CHECK_TEMPLATE $CALLER_DEPTH $_ERROR_STRING 14b39c5158Smillert ]; 15b39c5158Smillert 16b39c5158Smillert @ISA = qw[ Exporter ]; 17b39c5158Smillert @EXPORT_OK = qw[check allow last_error]; 18b39c5158Smillert 19*6fb12b70Safresh1 $VERSION = '0.38'; 20b39c5158Smillert $VERBOSE = $^W ? 1 : 0; 21b39c5158Smillert $NO_DUPLICATES = 0; 22b39c5158Smillert $STRIP_LEADING_DASHES = 0; 23b39c5158Smillert $STRICT_TYPE = 0; 24b39c5158Smillert $ALLOW_UNKNOWN = 0; 25b39c5158Smillert $PRESERVE_CASE = 0; 26b39c5158Smillert $ONLY_ALLOW_DEFINED = 0; 27b39c5158Smillert $SANITY_CHECK_TEMPLATE = 1; 28b39c5158Smillert $WARNINGS_FATAL = 0; 29b39c5158Smillert $CALLER_DEPTH = 0; 30b39c5158Smillert} 31b39c5158Smillert 32b39c5158Smillertmy %known_keys = map { $_ => 1 } 33b39c5158Smillert qw| required allow default strict_type no_override 34b39c5158Smillert store defined |; 35b39c5158Smillert 36b39c5158Smillert=pod 37b39c5158Smillert 38b39c5158Smillert=head1 NAME 39b39c5158Smillert 40b39c5158SmillertParams::Check - A generic input parsing/checking mechanism. 41b39c5158Smillert 42b39c5158Smillert=head1 SYNOPSIS 43b39c5158Smillert 44b39c5158Smillert use Params::Check qw[check allow last_error]; 45b39c5158Smillert 46b39c5158Smillert sub fill_personal_info { 47b39c5158Smillert my %hash = @_; 48b39c5158Smillert my $x; 49b39c5158Smillert 50b39c5158Smillert my $tmpl = { 51b39c5158Smillert firstname => { required => 1, defined => 1 }, 52b39c5158Smillert lastname => { required => 1, store => \$x }, 53b39c5158Smillert gender => { required => 1, 54b39c5158Smillert allow => [qr/M/i, qr/F/i], 55b39c5158Smillert }, 56b39c5158Smillert married => { allow => [0,1] }, 57b39c5158Smillert age => { default => 21, 58b39c5158Smillert allow => qr/^\d+$/, 59b39c5158Smillert }, 60b39c5158Smillert 61b39c5158Smillert phone => { allow => [ sub { return 1 if /$valid_re/ }, 62b39c5158Smillert '1-800-PERL' ] 63b39c5158Smillert }, 64b39c5158Smillert id_list => { default => [], 65b39c5158Smillert strict_type => 1 66b39c5158Smillert }, 67b39c5158Smillert employer => { default => 'NSA', no_override => 1 }, 68b39c5158Smillert }; 69b39c5158Smillert 70b39c5158Smillert ### check() returns a hashref of parsed args on success ### 71b39c5158Smillert my $parsed_args = check( $tmpl, \%hash, $VERBOSE ) 72b39c5158Smillert or die qw[Could not parse arguments!]; 73b39c5158Smillert 74b39c5158Smillert ... other code here ... 75b39c5158Smillert } 76b39c5158Smillert 77b39c5158Smillert my $ok = allow( $colour, [qw|blue green yellow|] ); 78b39c5158Smillert 79b39c5158Smillert my $error = Params::Check::last_error(); 80b39c5158Smillert 81b39c5158Smillert 82b39c5158Smillert=head1 DESCRIPTION 83b39c5158Smillert 84b39c5158SmillertParams::Check is a generic input parsing/checking mechanism. 85b39c5158Smillert 86b39c5158SmillertIt allows you to validate input via a template. The only requirement 87b39c5158Smillertis that the arguments must be named. 88b39c5158Smillert 89b39c5158SmillertParams::Check can do the following things for you: 90b39c5158Smillert 91b39c5158Smillert=over 4 92b39c5158Smillert 93b39c5158Smillert=item * 94b39c5158Smillert 95b39c5158SmillertConvert all keys to lowercase 96b39c5158Smillert 97b39c5158Smillert=item * 98b39c5158Smillert 99b39c5158SmillertCheck if all required arguments have been provided 100b39c5158Smillert 101b39c5158Smillert=item * 102b39c5158Smillert 103b39c5158SmillertSet arguments that have not been provided to the default 104b39c5158Smillert 105b39c5158Smillert=item * 106b39c5158Smillert 107b39c5158SmillertWeed out arguments that are not supported and warn about them to the 108b39c5158Smillertuser 109b39c5158Smillert 110b39c5158Smillert=item * 111b39c5158Smillert 112b39c5158SmillertValidate the arguments given by the user based on strings, regexes, 113b39c5158Smillertlists or even subroutines 114b39c5158Smillert 115b39c5158Smillert=item * 116b39c5158Smillert 117b39c5158SmillertEnforce type integrity if required 118b39c5158Smillert 119b39c5158Smillert=back 120b39c5158Smillert 121b39c5158SmillertMost of Params::Check's power comes from its template, which we'll 122b39c5158Smillertdiscuss below: 123b39c5158Smillert 124b39c5158Smillert=head1 Template 125b39c5158Smillert 126b39c5158SmillertAs you can see in the synopsis, based on your template, the arguments 127b39c5158Smillertprovided will be validated. 128b39c5158Smillert 129b39c5158SmillertThe template can take a different set of rules per key that is used. 130b39c5158Smillert 131b39c5158SmillertThe following rules are available: 132b39c5158Smillert 133b39c5158Smillert=over 4 134b39c5158Smillert 135b39c5158Smillert=item default 136b39c5158Smillert 137b39c5158SmillertThis is the default value if none was provided by the user. 138b39c5158SmillertThis is also the type C<strict_type> will look at when checking type 139b39c5158Smillertintegrity (see below). 140b39c5158Smillert 141b39c5158Smillert=item required 142b39c5158Smillert 143b39c5158SmillertA boolean flag that indicates if this argument was a required 144b39c5158Smillertargument. If marked as required and not provided, check() will fail. 145b39c5158Smillert 146b39c5158Smillert=item strict_type 147b39c5158Smillert 148b39c5158SmillertThis does a C<ref()> check on the argument provided. The C<ref> of the 149b39c5158Smillertargument must be the same as the C<ref> of the default value for this 150b39c5158Smillertcheck to pass. 151b39c5158Smillert 152b39c5158SmillertThis is very useful if you insist on taking an array reference as 153b39c5158Smillertargument for example. 154b39c5158Smillert 155b39c5158Smillert=item defined 156b39c5158Smillert 157b39c5158SmillertIf this template key is true, enforces that if this key is provided by 158b39c5158Smillertuser input, its value is C<defined>. This just means that the user is 159b39c5158Smillertnot allowed to pass C<undef> as a value for this key and is equivalent 160b39c5158Smillertto: 161b39c5158Smillert allow => sub { defined $_[0] && OTHER TESTS } 162b39c5158Smillert 163b39c5158Smillert=item no_override 164b39c5158Smillert 165b39c5158SmillertThis allows you to specify C<constants> in your template. ie, they 166b39c5158Smillertkeys that are not allowed to be altered by the user. It pretty much 167b39c5158Smillertallows you to keep all your C<configurable> data in one place; the 168b39c5158SmillertC<Params::Check> template. 169b39c5158Smillert 170b39c5158Smillert=item store 171b39c5158Smillert 172b39c5158SmillertThis allows you to pass a reference to a scalar, in which the data 173b39c5158Smillertwill be stored: 174b39c5158Smillert 175b39c5158Smillert my $x; 176b39c5158Smillert my $args = check(foo => { default => 1, store => \$x }, $input); 177b39c5158Smillert 178b39c5158SmillertThis is basically shorthand for saying: 179b39c5158Smillert 180b39c5158Smillert my $args = check( { foo => { default => 1 }, $input ); 181b39c5158Smillert my $x = $args->{foo}; 182b39c5158Smillert 183b39c5158SmillertYou can alter the global variable $Params::Check::NO_DUPLICATES to 184b39c5158Smillertcontrol whether the C<store>'d key will still be present in your 185b39c5158Smillertresult set. See the L<Global Variables> section below. 186b39c5158Smillert 187b39c5158Smillert=item allow 188b39c5158Smillert 189b39c5158SmillertA set of criteria used to validate a particular piece of data if it 190b39c5158Smillerthas to adhere to particular rules. 191b39c5158Smillert 192b39c5158SmillertSee the C<allow()> function for details. 193b39c5158Smillert 194b39c5158Smillert=back 195b39c5158Smillert 196b39c5158Smillert=head1 Functions 197b39c5158Smillert 198b39c5158Smillert=head2 check( \%tmpl, \%args, [$verbose] ); 199b39c5158Smillert 200b39c5158SmillertThis function is not exported by default, so you'll have to ask for it 201b39c5158Smillertvia: 202b39c5158Smillert 203b39c5158Smillert use Params::Check qw[check]; 204b39c5158Smillert 205b39c5158Smillertor use its fully qualified name instead. 206b39c5158Smillert 207b39c5158SmillertC<check> takes a list of arguments, as follows: 208b39c5158Smillert 209b39c5158Smillert=over 4 210b39c5158Smillert 211b39c5158Smillert=item Template 212b39c5158Smillert 213b39c5158SmillertThis is a hash reference which contains a template as explained in the 214b39c5158SmillertC<SYNOPSIS> and C<Template> section. 215b39c5158Smillert 216b39c5158Smillert=item Arguments 217b39c5158Smillert 218b39c5158SmillertThis is a reference to a hash of named arguments which need checking. 219b39c5158Smillert 220b39c5158Smillert=item Verbose 221b39c5158Smillert 222b39c5158SmillertA boolean to indicate whether C<check> should be verbose and warn 223b39c5158Smillertabout what went wrong in a check or not. 224b39c5158Smillert 225b39c5158SmillertYou can enable this program wide by setting the package variable 226b39c5158SmillertC<$Params::Check::VERBOSE> to a true value. For details, see the 227b39c5158Smillertsection on C<Global Variables> below. 228b39c5158Smillert 229b39c5158Smillert=back 230b39c5158Smillert 231b39c5158SmillertC<check> will return when it fails, or a hashref with lowercase 232b39c5158Smillertkeys of parsed arguments when it succeeds. 233b39c5158Smillert 234b39c5158SmillertSo a typical call to check would look like this: 235b39c5158Smillert 236b39c5158Smillert my $parsed = check( \%template, \%arguments, $VERBOSE ) 237b39c5158Smillert or warn q[Arguments could not be parsed!]; 238b39c5158Smillert 239b39c5158SmillertA lot of the behaviour of C<check()> can be altered by setting 240b39c5158Smillertpackage variables. See the section on C<Global Variables> for details 241b39c5158Smillerton this. 242b39c5158Smillert 243b39c5158Smillert=cut 244b39c5158Smillert 245b39c5158Smillertsub check { 246b39c5158Smillert my ($utmpl, $href, $verbose) = @_; 247b39c5158Smillert 248898184e3Ssthen ### clear the current error string ### 249898184e3Ssthen _clear_error(); 250898184e3Ssthen 251b39c5158Smillert ### did we get the arguments we need? ### 252898184e3Ssthen if ( !$utmpl or !$href ) { 253898184e3Ssthen _store_error(loc('check() expects two arguments')); 254898184e3Ssthen return unless $WARNINGS_FATAL; 255898184e3Ssthen croak(__PACKAGE__->last_error); 256898184e3Ssthen } 257b39c5158Smillert 258b39c5158Smillert ### sensible defaults ### 259b39c5158Smillert $verbose ||= $VERBOSE || 0; 260b39c5158Smillert 261b39c5158Smillert ### XXX what type of template is it? ### 262b39c5158Smillert ### { key => { } } ? 263b39c5158Smillert #if (ref $args eq 'HASH') { 264b39c5158Smillert # 1; 265b39c5158Smillert #} 266b39c5158Smillert 267b39c5158Smillert ### clean up the template ### 26891f110e0Safresh1 my $args; 26991f110e0Safresh1 27091f110e0Safresh1 ### don't even bother to loop, if there's nothing to clean up ### 27191f110e0Safresh1 if( $PRESERVE_CASE and !$STRIP_LEADING_DASHES ) { 27291f110e0Safresh1 $args = $href; 27391f110e0Safresh1 } else { 27491f110e0Safresh1 ### keys are not aliased ### 27591f110e0Safresh1 for my $key (keys %$href) { 27691f110e0Safresh1 my $org = $key; 27791f110e0Safresh1 $key = lc $key unless $PRESERVE_CASE; 27891f110e0Safresh1 $key =~ s/^-// if $STRIP_LEADING_DASHES; 27991f110e0Safresh1 $args->{$key} = $href->{$org}; 28091f110e0Safresh1 } 28191f110e0Safresh1 } 28291f110e0Safresh1 28391f110e0Safresh1 my %defs; 28491f110e0Safresh1 28591f110e0Safresh1 ### which template entries have a 'store' member 28691f110e0Safresh1 my @want_store; 287b39c5158Smillert 288b39c5158Smillert ### sanity check + defaults + required keys set? ### 28991f110e0Safresh1 my $fail; 29091f110e0Safresh1 for my $key (keys %$utmpl) { 29191f110e0Safresh1 my $tmpl = $utmpl->{$key}; 292b39c5158Smillert 29391f110e0Safresh1 ### check if required keys are provided 29491f110e0Safresh1 ### keys are now lower cased, unless preserve case was enabled 29591f110e0Safresh1 ### at which point, the utmpl keys must match, but that's the users 29691f110e0Safresh1 ### problem. 29791f110e0Safresh1 if( $tmpl->{'required'} and not exists $args->{$key} ) { 29891f110e0Safresh1 _store_error( 29991f110e0Safresh1 loc(q|Required option '%1' is not provided for %2 by %3|, 30091f110e0Safresh1 $key, _who_was_it(), _who_was_it(1)), $verbose ); 30191f110e0Safresh1 30291f110e0Safresh1 ### mark the error ### 30391f110e0Safresh1 $fail++; 30491f110e0Safresh1 next; 30591f110e0Safresh1 } 30691f110e0Safresh1 30791f110e0Safresh1 ### next, set the default, make sure the key exists in %defs ### 30891f110e0Safresh1 $defs{$key} = $tmpl->{'default'} 30991f110e0Safresh1 if exists $tmpl->{'default'}; 31091f110e0Safresh1 31191f110e0Safresh1 if( $SANITY_CHECK_TEMPLATE ) { 31291f110e0Safresh1 ### last, check if they provided any weird template keys 31391f110e0Safresh1 ### -- do this last so we don't always execute this code. 31491f110e0Safresh1 ### just a small optimization. 31591f110e0Safresh1 map { _store_error( 31691f110e0Safresh1 loc(q|Template type '%1' not supported [at key '%2']|, 31791f110e0Safresh1 $_, $key), 1, 0 ); 31891f110e0Safresh1 } grep { 31991f110e0Safresh1 not $known_keys{$_} 32091f110e0Safresh1 } keys %$tmpl; 32191f110e0Safresh1 32291f110e0Safresh1 ### make sure you passed a ref, otherwise, complain about it! 32391f110e0Safresh1 if ( exists $tmpl->{'store'} ) { 32491f110e0Safresh1 _store_error( loc( 32591f110e0Safresh1 q|Store variable for '%1' is not a reference!|, $key 32691f110e0Safresh1 ), 1, 0 ) unless ref $tmpl->{'store'}; 32791f110e0Safresh1 } 32891f110e0Safresh1 } 32991f110e0Safresh1 33091f110e0Safresh1 push @want_store, $key if $tmpl->{'store'}; 33191f110e0Safresh1 } 33291f110e0Safresh1 33391f110e0Safresh1 ### errors found ### 33491f110e0Safresh1 return if $fail; 335b39c5158Smillert 336b39c5158Smillert ### flag to see if anything went wrong ### 337b39c5158Smillert my $wrong; 338b39c5158Smillert 339b39c5158Smillert ### flag to see if we warned for anything, needed for warnings_fatal 340b39c5158Smillert my $warned; 341b39c5158Smillert 34291f110e0Safresh1 for my $key (keys %$args) { 34391f110e0Safresh1 my $arg = $args->{$key}; 344b39c5158Smillert 345b39c5158Smillert ### you gave us this key, but it's not in the template ### 34691f110e0Safresh1 unless( $utmpl->{$key} ) { 347b39c5158Smillert 348b39c5158Smillert ### but we'll allow it anyway ### 349b39c5158Smillert if( $ALLOW_UNKNOWN ) { 35091f110e0Safresh1 $defs{$key} = $arg; 351b39c5158Smillert 352b39c5158Smillert ### warn about the error ### 353b39c5158Smillert } else { 354b39c5158Smillert _store_error( 355b39c5158Smillert loc("Key '%1' is not a valid key for %2 provided by %3", 356b39c5158Smillert $key, _who_was_it(), _who_was_it(1)), $verbose); 357b39c5158Smillert $warned ||= 1; 358b39c5158Smillert } 359b39c5158Smillert next; 360b39c5158Smillert } 361b39c5158Smillert 36291f110e0Safresh1 ### copy of this keys template instructions, to save derefs ### 36391f110e0Safresh1 my %tmpl = %{$utmpl->{$key}}; 36491f110e0Safresh1 365b39c5158Smillert ### check if you're even allowed to override this key ### 36691f110e0Safresh1 if( $tmpl{'no_override'} ) { 367b39c5158Smillert _store_error( 368b39c5158Smillert loc(q[You are not allowed to override key '%1']. 369b39c5158Smillert q[for %2 from %3], $key, _who_was_it(), _who_was_it(1)), 370b39c5158Smillert $verbose 371b39c5158Smillert ); 372b39c5158Smillert $warned ||= 1; 373b39c5158Smillert next; 374b39c5158Smillert } 375b39c5158Smillert 376b39c5158Smillert ### check if you were supposed to provide defined() values ### 37791f110e0Safresh1 if( ($tmpl{'defined'} || $ONLY_ALLOW_DEFINED) and not defined $arg ) { 378b39c5158Smillert _store_error(loc(q|Key '%1' must be defined when passed|, $key), 379b39c5158Smillert $verbose ); 380b39c5158Smillert $wrong ||= 1; 381b39c5158Smillert next; 382b39c5158Smillert } 383b39c5158Smillert 384b39c5158Smillert ### check if they should be of a strict type, and if it is ### 385b39c5158Smillert if( ($tmpl{'strict_type'} || $STRICT_TYPE) and 38691f110e0Safresh1 (ref $arg ne ref $tmpl{'default'}) 387b39c5158Smillert ) { 388b39c5158Smillert _store_error(loc(q|Key '%1' needs to be of type '%2'|, 389b39c5158Smillert $key, ref $tmpl{'default'} || 'SCALAR'), $verbose ); 390b39c5158Smillert $wrong ||= 1; 391b39c5158Smillert next; 392b39c5158Smillert } 393b39c5158Smillert 394b39c5158Smillert ### check if we have an allow handler, to validate against ### 395b39c5158Smillert ### allow() will report its own errors ### 396b39c5158Smillert if( exists $tmpl{'allow'} and not do { 397b39c5158Smillert local $_ERROR_STRING; 39891f110e0Safresh1 allow( $arg, $tmpl{'allow'} ) 399b39c5158Smillert } 400b39c5158Smillert ) { 401b39c5158Smillert ### stringify the value in the error report -- we don't want dumps 402b39c5158Smillert ### of objects, but we do want to see *roughly* what we passed 403b39c5158Smillert _store_error(loc(q|Key '%1' (%2) is of invalid type for '%3' |. 404b39c5158Smillert q|provided by %4|, 40591f110e0Safresh1 $key, "$arg", _who_was_it(), 406b39c5158Smillert _who_was_it(1)), $verbose); 407b39c5158Smillert $wrong ||= 1; 408b39c5158Smillert next; 409b39c5158Smillert } 410b39c5158Smillert 411b39c5158Smillert ### we got here, then all must be OK ### 41291f110e0Safresh1 $defs{$key} = $arg; 413b39c5158Smillert 414b39c5158Smillert } 415b39c5158Smillert 416b39c5158Smillert ### croak with the collected errors if there were errors and 417b39c5158Smillert ### we have the fatal flag toggled. 418b39c5158Smillert croak(__PACKAGE__->last_error) if ($wrong || $warned) && $WARNINGS_FATAL; 419b39c5158Smillert 420898184e3Ssthen ### done with our loop... if $wrong is set, something went wrong 421b39c5158Smillert ### and the user is already informed, just return... 422b39c5158Smillert return if $wrong; 423b39c5158Smillert 424b39c5158Smillert ### check if we need to store any of the keys ### 425b39c5158Smillert ### can't do it before, because something may go wrong later, 426b39c5158Smillert ### leaving the user with a few set variables 42791f110e0Safresh1 for my $key (@want_store) { 42891f110e0Safresh1 next unless exists $defs{$key}; 42991f110e0Safresh1 my $ref = $utmpl->{$key}{'store'}; 430b39c5158Smillert $$ref = $NO_DUPLICATES ? delete $defs{$key} : $defs{$key}; 431b39c5158Smillert } 432b39c5158Smillert 433b39c5158Smillert return \%defs; 434b39c5158Smillert} 435b39c5158Smillert 436b39c5158Smillert=head2 allow( $test_me, \@criteria ); 437b39c5158Smillert 438b39c5158SmillertThe function that handles the C<allow> key in the template is also 439b39c5158Smillertavailable for independent use. 440b39c5158Smillert 441b39c5158SmillertThe function takes as first argument a key to test against, and 442b39c5158Smillertas second argument any form of criteria that are also allowed by 443b39c5158Smillertthe C<allow> key in the template. 444b39c5158Smillert 445b39c5158SmillertYou can use the following types of values for allow: 446b39c5158Smillert 447b39c5158Smillert=over 4 448b39c5158Smillert 449b39c5158Smillert=item string 450b39c5158Smillert 451b39c5158SmillertThe provided argument MUST be equal to the string for the validation 452b39c5158Smillertto pass. 453b39c5158Smillert 454b39c5158Smillert=item regexp 455b39c5158Smillert 456b39c5158SmillertThe provided argument MUST match the regular expression for the 457b39c5158Smillertvalidation to pass. 458b39c5158Smillert 459b39c5158Smillert=item subroutine 460b39c5158Smillert 461b39c5158SmillertThe provided subroutine MUST return true in order for the validation 462b39c5158Smillertto pass and the argument accepted. 463b39c5158Smillert 464b39c5158Smillert(This is particularly useful for more complicated data). 465b39c5158Smillert 466b39c5158Smillert=item array ref 467b39c5158Smillert 468b39c5158SmillertThe provided argument MUST equal one of the elements of the array 469b39c5158Smillertref for the validation to pass. An array ref can hold all the above 470b39c5158Smillertvalues. 471b39c5158Smillert 472b39c5158Smillert=back 473b39c5158Smillert 474b39c5158SmillertIt returns true if the key matched the criteria, or false otherwise. 475b39c5158Smillert 476b39c5158Smillert=cut 477b39c5158Smillert 478b39c5158Smillertsub allow { 479b39c5158Smillert ### use $_[0] and $_[1] since this is hot code... ### 480b39c5158Smillert #my ($val, $ref) = @_; 481b39c5158Smillert 482b39c5158Smillert ### it's a regexp ### 483b39c5158Smillert if( ref $_[1] eq 'Regexp' ) { 484b39c5158Smillert local $^W; # silence warnings if $val is undef # 485b39c5158Smillert return if $_[0] !~ /$_[1]/; 486b39c5158Smillert 487b39c5158Smillert ### it's a sub ### 488b39c5158Smillert } elsif ( ref $_[1] eq 'CODE' ) { 489b39c5158Smillert return unless $_[1]->( $_[0] ); 490b39c5158Smillert 491b39c5158Smillert ### it's an array ### 492b39c5158Smillert } elsif ( ref $_[1] eq 'ARRAY' ) { 493b39c5158Smillert 494b39c5158Smillert ### loop over the elements, see if one of them says the 495b39c5158Smillert ### value is OK 496898184e3Ssthen ### also, short-circuit when possible 497b39c5158Smillert for ( @{$_[1]} ) { 498b39c5158Smillert return 1 if allow( $_[0], $_ ); 499b39c5158Smillert } 500b39c5158Smillert 501b39c5158Smillert return; 502b39c5158Smillert 503b39c5158Smillert ### fall back to a simple, but safe 'eq' ### 504b39c5158Smillert } else { 505b39c5158Smillert return unless _safe_eq( $_[0], $_[1] ); 506b39c5158Smillert } 507b39c5158Smillert 508b39c5158Smillert ### we got here, no failures ### 509b39c5158Smillert return 1; 510b39c5158Smillert} 511b39c5158Smillert 512b39c5158Smillert### helper functions ### 513b39c5158Smillert 514b39c5158Smillertsub _safe_eq { 515b39c5158Smillert ### only do a straight 'eq' if they're both defined ### 516b39c5158Smillert return defined($_[0]) && defined($_[1]) 517b39c5158Smillert ? $_[0] eq $_[1] 518b39c5158Smillert : defined($_[0]) eq defined($_[1]); 519b39c5158Smillert} 520b39c5158Smillert 521b39c5158Smillertsub _who_was_it { 522b39c5158Smillert my $level = $_[0] || 0; 523b39c5158Smillert 524b39c5158Smillert return (caller(2 + $CALLER_DEPTH + $level))[3] || 'ANON' 525b39c5158Smillert} 526b39c5158Smillert 527b39c5158Smillert=head2 last_error() 528b39c5158Smillert 529b39c5158SmillertReturns a string containing all warnings and errors reported during 530b39c5158Smillertthe last time C<check> was called. 531b39c5158Smillert 532b39c5158SmillertThis is useful if you want to report then some other way than 533b39c5158SmillertC<carp>'ing when the verbose flag is on. 534b39c5158Smillert 535b39c5158SmillertIt is exported upon request. 536b39c5158Smillert 537b39c5158Smillert=cut 538b39c5158Smillert 539b39c5158Smillert{ $_ERROR_STRING = ''; 540b39c5158Smillert 541b39c5158Smillert sub _store_error { 542b39c5158Smillert my($err, $verbose, $offset) = @_[0..2]; 543b39c5158Smillert $verbose ||= 0; 544b39c5158Smillert $offset ||= 0; 545b39c5158Smillert my $level = 1 + $offset; 546b39c5158Smillert 547b39c5158Smillert local $Carp::CarpLevel = $level; 548b39c5158Smillert 549b39c5158Smillert carp $err if $verbose; 550b39c5158Smillert 551b39c5158Smillert $_ERROR_STRING .= $err . "\n"; 552b39c5158Smillert } 553b39c5158Smillert 554b39c5158Smillert sub _clear_error { 555b39c5158Smillert $_ERROR_STRING = ''; 556b39c5158Smillert } 557b39c5158Smillert 558b39c5158Smillert sub last_error { $_ERROR_STRING } 559b39c5158Smillert} 560b39c5158Smillert 561b39c5158Smillert1; 562b39c5158Smillert 563b39c5158Smillert=head1 Global Variables 564b39c5158Smillert 565b39c5158SmillertThe behaviour of Params::Check can be altered by changing the 566b39c5158Smillertfollowing global variables: 567b39c5158Smillert 568b39c5158Smillert=head2 $Params::Check::VERBOSE 569b39c5158Smillert 570b39c5158SmillertThis controls whether Params::Check will issue warnings and 571b39c5158Smillertexplanations as to why certain things may have failed. 572b39c5158SmillertIf you set it to 0, Params::Check will not output any warnings. 573b39c5158Smillert 574b39c5158SmillertThe default is 1 when L<warnings> are enabled, 0 otherwise; 575b39c5158Smillert 576b39c5158Smillert=head2 $Params::Check::STRICT_TYPE 577b39c5158Smillert 578b39c5158SmillertThis works like the C<strict_type> option you can pass to C<check>, 579b39c5158Smillertwhich will turn on C<strict_type> globally for all calls to C<check>. 580b39c5158Smillert 581b39c5158SmillertThe default is 0; 582b39c5158Smillert 583b39c5158Smillert=head2 $Params::Check::ALLOW_UNKNOWN 584b39c5158Smillert 585b39c5158SmillertIf you set this flag, unknown options will still be present in the 586b39c5158Smillertreturn value, rather than filtered out. This is useful if your 587b39c5158Smillertsubroutine is only interested in a few arguments, and wants to pass 588b39c5158Smillertthe rest on blindly to perhaps another subroutine. 589b39c5158Smillert 590b39c5158SmillertThe default is 0; 591b39c5158Smillert 592b39c5158Smillert=head2 $Params::Check::STRIP_LEADING_DASHES 593b39c5158Smillert 594b39c5158SmillertIf you set this flag, all keys passed in the following manner: 595b39c5158Smillert 596b39c5158Smillert function( -key => 'val' ); 597b39c5158Smillert 598b39c5158Smillertwill have their leading dashes stripped. 599b39c5158Smillert 600b39c5158Smillert=head2 $Params::Check::NO_DUPLICATES 601b39c5158Smillert 602b39c5158SmillertIf set to true, all keys in the template that are marked as to be 603b39c5158Smillertstored in a scalar, will also be removed from the result set. 604b39c5158Smillert 605b39c5158SmillertDefault is false, meaning that when you use C<store> as a template 606b39c5158Smillertkey, C<check> will put it both in the scalar you supplied, as well as 607b39c5158Smillertin the hashref it returns. 608b39c5158Smillert 609b39c5158Smillert=head2 $Params::Check::PRESERVE_CASE 610b39c5158Smillert 611b39c5158SmillertIf set to true, L<Params::Check> will no longer convert all keys from 612b39c5158Smillertthe user input to lowercase, but instead expect them to be in the 613b39c5158Smillertcase the template provided. This is useful when you want to use 614b39c5158Smillertsimilar keys with different casing in your templates. 615b39c5158Smillert 616898184e3SsthenUnderstand that this removes the case-insensitivity feature of this 617b39c5158Smillertmodule. 618b39c5158Smillert 619b39c5158SmillertDefault is 0; 620b39c5158Smillert 621b39c5158Smillert=head2 $Params::Check::ONLY_ALLOW_DEFINED 622b39c5158Smillert 623b39c5158SmillertIf set to true, L<Params::Check> will require all values passed to be 624b39c5158SmillertC<defined>. If you wish to enable this on a 'per key' basis, use the 625b39c5158Smillerttemplate option C<defined> instead. 626b39c5158Smillert 627b39c5158SmillertDefault is 0; 628b39c5158Smillert 629b39c5158Smillert=head2 $Params::Check::SANITY_CHECK_TEMPLATE 630b39c5158Smillert 631b39c5158SmillertIf set to true, L<Params::Check> will sanity check templates, validating 632b39c5158Smillertfor errors and unknown keys. Although very useful for debugging, this 633b39c5158Smillertcan be somewhat slow in hot-code and large loops. 634b39c5158Smillert 635b39c5158SmillertTo disable this check, set this variable to C<false>. 636b39c5158Smillert 637b39c5158SmillertDefault is 1; 638b39c5158Smillert 639b39c5158Smillert=head2 $Params::Check::WARNINGS_FATAL 640b39c5158Smillert 641b39c5158SmillertIf set to true, L<Params::Check> will C<croak> when an error during 642b39c5158Smillerttemplate validation occurs, rather than return C<false>. 643b39c5158Smillert 644b39c5158SmillertDefault is 0; 645b39c5158Smillert 646b39c5158Smillert=head2 $Params::Check::CALLER_DEPTH 647b39c5158Smillert 648b39c5158SmillertThis global modifies the argument given to C<caller()> by 649b39c5158SmillertC<Params::Check::check()> and is useful if you have a custom wrapper 650b39c5158Smillertfunction around C<Params::Check::check()>. The value must be an 651b39c5158Smillertinteger, indicating the number of wrapper functions inserted between 652b39c5158Smillertthe real function call and C<Params::Check::check()>. 653b39c5158Smillert 654b39c5158SmillertExample wrapper function, using a custom stacktrace: 655b39c5158Smillert 656b39c5158Smillert sub check { 657b39c5158Smillert my ($template, $args_in) = @_; 658b39c5158Smillert 659b39c5158Smillert local $Params::Check::WARNINGS_FATAL = 1; 660b39c5158Smillert local $Params::Check::CALLER_DEPTH = $Params::Check::CALLER_DEPTH + 1; 661b39c5158Smillert my $args_out = Params::Check::check($template, $args_in); 662b39c5158Smillert 663b39c5158Smillert my_stacktrace(Params::Check::last_error) unless $args_out; 664b39c5158Smillert 665b39c5158Smillert return $args_out; 666b39c5158Smillert } 667b39c5158Smillert 668b39c5158SmillertDefault is 0; 669b39c5158Smillert 670b39c5158Smillert=head1 Acknowledgements 671b39c5158Smillert 672b39c5158SmillertThanks to Richard Soderberg for his performance improvements. 673b39c5158Smillert 674898184e3Ssthen=head1 BUG REPORTS 675898184e3Ssthen 676898184e3SsthenPlease report bugs or other issues to E<lt>bug-params-check@rt.cpan.orgE<gt>. 677898184e3Ssthen 678898184e3Ssthen=head1 AUTHOR 679898184e3Ssthen 680898184e3SsthenThis module by Jos Boumans E<lt>kane@cpan.orgE<gt>. 681898184e3Ssthen 682b39c5158Smillert=head1 COPYRIGHT 683b39c5158Smillert 684898184e3SsthenThis library is free software; you may redistribute and/or modify it 685898184e3Ssthenunder the same terms as Perl itself. 686b39c5158Smillert 687b39c5158Smillert 688b39c5158Smillert=cut 689b39c5158Smillert 690b39c5158Smillert# Local variables: 691b39c5158Smillert# c-indentation-style: bsd 692b39c5158Smillert# c-basic-offset: 4 693b39c5158Smillert# indent-tabs-mode: nil 694b39c5158Smillert# End: 695b39c5158Smillert# vim: expandtab shiftwidth=4: 696