http://qs1969.pair.com?node_id=948323

saranrsm has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks

Problem: Im trying to pre-compile a couple of patterns (Regular expression) and match it into the given text.

Done so far: I have tried doing this without pre-compiled regex, which work perfectly

$text="I have a problem with pre-compiled regex"; @words=qw(have regex); for $a(@words) { while ($text =~ m/$a/g) { print "Found $&\n"; } }

Stuck with: Now when I wanted to pre-compile these patterns (have, regex) and match with $text, I don't get the expected result. I have posted the script which doesn't work, below.

$text="I have a problem with pre-compiled regex"; @words=qw(qr/have/ qr/regex/); for $a(@words) { while ($text =~ /$a/g) { print "Found $&\n"; } }