package ApplyTable; # apply a hashtable of regexp/function pairs to an input string, executing # the first function that matches (case insensitive) the given text. # returns whatever the executed function returns, or else undef if no match. # Usage: applytable(string, reference-to-hashtable, arglist) # this will call function(arglist), where function is the right # one depending on the hash table, arglist is passed through unmodified. # Note: $& can be used to extract which string matched you. # Examples # using an existing hashtable, with a two element arglist # applytable($_, \%functionmap, 'lala', 'mama'); # using an anonymous hash, with one anonymous sub, no arglist # applytable("Foobar", {'xyzzy' => \&subNotUsed, # 'bar' => sub { print "Bar detected\n" }}); use Exporter; @ISA = qw(Exporter); @EXPORT = qw(applytable); sub applytable { my $inputString = shift; my %fmap = %{shift()}; my @arglist = @_; my $k; foreach $k (keys(%fmap)) { if ($inputString =~ /$k/i) { return &{$fmap{$k}}(@arglist); } } return undef; } 1;