my $c = Module->new( { filterX => XXXX } ); where XXXX could be: - a hash ref --> checks for exists $hashref->{$value} - a regex --> checks if the regex matches - an array ref of regexes --> checks if any regex matches - a coderef --> the code is called #### my %callbacks = ( CODE => \&_init_code_callback, HASH => \&_init_hash_callback, ARRAY => \&_init_array_callback, ); #=================================== sub _init_callback { #=================================== my ( $self, $callback, $filter ) = @_; # If nothing set, use default or subclassed version unless ($filter) { $self->{$callback} = $self->can($callback); return; } # If not in callbacks, then assume it is a single regex $filter = [$filter] unless exists $callbacks{ref $filter}; return $callbacks{ref $filter}->($filter); } #=================================== sub _init_code_callback { #=================================== return $_[0] }; #=================================== sub _init_hash_callback { #=================================== my $filter = shift; return sub { my $self = shift; my $param = shift; return exists $filter->{$param}; }; } #=================================== sub _init_array_callback { #=================================== my $filter = shift; foreach my $value (@$filter) { $value ||= ''; die "'$value' is not a regular expression" unless ref $value eq 'Regexp'; } return sub { my $self = shift; my $value = shift; foreach my $regex (@$filter) { return 1 if $value =~ m/$regex/; } return 0; }; }