my $mystring = "hello, I'm key2"; my %dispatcher = ( key1 => sub { print "1" }, key2 => sub { print "2" }, #... ); #... for my $key ( keys %dispatcher ) { $dispatcher{$key}->(), last if index($mystring, $key) >= 0; }