Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base: 931151120f
...
compare: cc29aa5b59
Checking mergeability… Don't worry, you can still create the pull request.
  • 2 commits
  • 1 file changed
  • 0 commit comments
  • 1 contributor
Commits on Apr 27, 2012
@vpit vpit Inline _sanity_check_and_defaults()
This will help further cleanups and optimizations.

Note that some caller frame numbers have been decremented so that stack
frames on errors are still correct.
63dd3e6
@vpit vpit Don't iterate over all keys to honor 'store' clauses
'store' clauses in templates are relatively rare, so it is wasteful to
iterate over all the returned members just to check that relevant key in
the template wants to store the value in a ref.

Instead, we note which keys want to 'store' while setting up the default
values, and only iterate over them at the end of check().
cc29aa5
Showing with 58 additions and 62 deletions.
  1. +58 −62 lib/Params/Check.pm
View
120 lib/Params/Check.pm
@@ -280,9 +280,58 @@ sub check {
}
}
+ my %defs;
+
+ ### which template entries have a 'store' member
+ my @want_store;
+
### sanity check + defaults + required keys set? ###
- my $defs = _sanity_check_and_defaults( $utmpl, $args, $verbose )
- or return;
+ my $fail;
+ for my $key (keys %$utmpl) {
+ my $tmpl = $utmpl->{$key};
+
+ ### check if required keys are provided
+ ### keys are now lower cased, unless preserve case was enabled
+ ### at which point, the utmpl keys must match, but that's the users
+ ### problem.
+ if( $tmpl->{'required'} and not exists $args->{$key} ) {
+ _store_error(
+ loc(q|Required option '%1' is not provided for %2 by %3|,
+ $key, _who_was_it(), _who_was_it(1)), $verbose );
+
+ ### mark the error ###
+ $fail++;
+ next;
+ }
+
+ ### next, set the default, make sure the key exists in %defs ###
+ $defs{$key} = $tmpl->{'default'}
+ if exists $tmpl->{'default'};
+
+ if( $SANITY_CHECK_TEMPLATE ) {
+ ### last, check if they provided any weird template keys
+ ### -- do this last so we don't always execute this code.
+ ### just a small optimization.
+ map { _store_error(
+ loc(q|Template type '%1' not supported [at key '%2']|,
+ $_, $key), 1, 0 );
+ } grep {
+ not $known_keys{$_}
+ } keys %$tmpl;
+
+ ### make sure you passed a ref, otherwise, complain about it!
+ if ( exists $tmpl->{'store'} ) {
+ _store_error( loc(
+ q|Store variable for '%1' is not a reference!|, $key
+ ), 1, 0 ) unless ref $tmpl->{'store'};
+ }
+ }
+
+ push @want_store, $key if $tmpl->{'store'};
+ }
+
+ ### errors found ###
+ return if $fail;
### flag to see if anything went wrong ###
my $wrong;
@@ -298,7 +347,7 @@ sub check {
### but we'll allow it anyway ###
if( $ALLOW_UNKNOWN ) {
- $defs->{$key} = $arg;
+ $defs{$key} = $arg;
### warn about the error ###
} else {
@@ -360,7 +409,7 @@ sub check {
}
### we got here, then all must be OK ###
- $defs->{$key} = $arg;
+ $defs{$key} = $arg;
}
@@ -375,13 +424,13 @@ sub check {
### check if we need to store any of the keys ###
### can't do it before, because something may go wrong later,
### leaving the user with a few set variables
- for my $key (keys %$defs) {
- if( my $ref = $utmpl->{$key}{'store'} ) {
- $$ref = $NO_DUPLICATES ? delete $defs->{$key} : $defs->{$key};
- }
+ for my $key (@want_store) {
+ next unless exists $defs{$key};
+ my $ref = $utmpl->{$key}{'store'};
+ $$ref = $NO_DUPLICATES ? delete $defs{$key} : $defs{$key};
}
- return $defs;
+ return \%defs;
}
=head2 allow( $test_me, \@criteria );
@@ -462,59 +511,6 @@ sub allow {
### helper functions ###
-sub _sanity_check_and_defaults {
- my ($utmpl, $args, $verbose) = @_;
-
- my %defs; my $fail;
- for my $key (keys %$utmpl) {
- my $tmpl = $utmpl->{$key};
-
- ### check if required keys are provided
- ### keys are now lower cased, unless preserve case was enabled
- ### at which point, the utmpl keys must match, but that's the users
- ### problem.
- if( $tmpl->{'required'} and not exists $args->{$key} ) {
- _store_error(
- loc(q|Required option '%1' is not provided for %2 by %3|,
- $key, _who_was_it(1), _who_was_it(2)), $verbose );
-
- ### mark the error ###
- $fail++;
- next;
- }
-
- ### next, set the default, make sure the key exists in %defs ###
- $defs{$key} = $tmpl->{'default'}
- if exists $tmpl->{'default'};
-
- if( $SANITY_CHECK_TEMPLATE ) {
- ### last, check if they provided any weird template keys
- ### -- do this last so we don't always execute this code.
- ### just a small optimization.
- map { _store_error(
- loc(q|Template type '%1' not supported [at key '%2']|,
- $_, $key), 1, 1 );
- } grep {
- not $known_keys{$_}
- } keys %$tmpl;
-
- ### make sure you passed a ref, otherwise, complain about it!
- if ( exists $tmpl->{'store'} ) {
- _store_error( loc(
- q|Store variable for '%1' is not a reference!|, $key
- ), 1, 1 ) unless ref $tmpl->{'store'};
- }
- }
- }
-
- ### errors found ###
- return if $fail;
-
- ### return references so we always return 'true', even on empty
- ### defaults
- return \%defs;
-}
-
sub _safe_eq {
### only do a straight 'eq' if they're both defined ###
return defined($_[0]) && defined($_[1])

No commit comments for this range

Something went wrong with that request. Please try again.