[Israel.pm] regex objects - a better example

Eitan Schuler eitan.schuler at gmail.com
Fri Feb 18 02:11:28 PST 2005

Dear mongers,
In my YAPC-talk I gave a bad example on using the regex objects (qr//).
qr compiles the pattern only once.
Here is a better example taken out of "Programming Perl".

Let's say we have a slew of patterns which we want to match each like this:

foreach $item(@data){
  foreach $pat(@patterns){
    if ($item =~ /$pat/){...}

The regex will compile each time. Not very efficient.
/o we cannot use here because $pat varies in the inner loop.

The solution is qr//

@regexes=map {qr/$_/} @patterns;
foreach $item(@data){
  foreach $pat(@regexes){
    if ($item =~ $pat){...}


More information about the Perl mailing list