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;
}