my %searchs = ( stringa => sub { print "matched on stringa\n"; }, stringb => sub { print "regex on string b\n"; }, stringc => sub { print "C match\n"; }, ); my %compiled = map { $_ => qr/\Q$_/ } keys %searchs ; while () { chomp; if (/^\*{3} (.+)/) { print "Found the following strings within body $1\n"; next; } for my $search_string (keys %searchs) { if (/$compiled{ $search_string }/) { $searches{ $search_string }->(); } } }