From: Vincent Pit Date: Thu, 26 Feb 2009 14:40:39 +0000 (+0100) Subject: Cleanup ->_validate X-Git-Tag: v1.03~5 X-Git-Url: http://git.vpit.fr/?a=commitdiff_plain;h=9375fddef45f4884a1e447e6d31f608ce18b22a2;p=perl%2Fmodules%2FRegexp-Wildcards.git Cleanup ->_validate --- diff --git a/lib/Regexp/Wildcards.pm b/lib/Regexp/Wildcards.pm index b70b593..f0b471a 100644 --- a/lib/Regexp/Wildcards.pm +++ b/lib/Regexp/Wildcards.pm @@ -104,31 +104,36 @@ sub _validate { my $valid = shift; my $old = shift; $old = { } unless defined $old; - my $c; + + my %opts; if (@_ <= 1) { - $c = { set => $_[0] }; + $opts{set} = defined $_[0] ? $_[0] : { }; } elsif (@_ % 2) { croak 'Arguments must be passed as an unique scalar or as key => value pairs'; } else { - my %args = @_; - $c = { map { (exists $args{$_}) ? ($_ => $args{$_}) : () } qw/set add rem/ }; + %opts = @_; } + + my %checked; for (qw/set add rem/) { - my $v = $c->{$_}; - next unless defined $v; + my $opt = $opts{$_}; + next unless defined $opt; my $cb = { '' => sub { +{ ($_[0] => 1) x (exists $valid->{$_[0]}) } }, 'ARRAY' => sub { +{ map { ($_ => 1) x (exists $valid->{$_}) } @{$_[0]} } }, 'HASH' => sub { +{ map { ($_ => $_[0]->{$_}) x (exists $valid->{$_}) } keys %{$_[0]} } } - }->{ ref $v }; + }->{ ref $opt }; croak 'Wrong option set' unless $cb; - $c->{$_} = $cb->($v); + $checked{$_} = $cb->($opt); } - my $config = (exists $c->{set}) ? $c->{set} : $old; - $config->{$_} = $c->{add}->{$_} for grep $c->{add}->{$_}, - keys %{$c->{add} || {}}; - delete $config->{$_} for grep $c->{rem}->{$_}, keys %{$c->{rem} || {}}; + + my $config = (exists $checked{set}) ? $checked{set} : $old; + $config->{$_} = $checked{add}->{$_} for grep $checked{add}->{$_}, + keys %{$checked{add} || {}}; + delete $config->{$_} for grep $checked{rem}->{$_}, + keys %{$checked{rem} || {}}; + $config; }