Well, you hit the pro on the head... it's much easier to interpolate variables into m//.
There is a major con, though. If you plan on using this regular expression in a loop, it will need to be recompiled every time it is used. With qr//, you will only compile once... You may want to benchmark them to find out the difference:
#!/usr/bin/perl -w
use Benchmark;
my $txt = 'find this regex';
my $str = 'regex?';
my $rgx = qr/$str/i;
timethese
(
1000000,
{
match => sub{ $txt =~ m/$str/i },
qreg => sub{ $txt =~ $rgx },
},
);
Gives me the following:
% test.pl
Benchmark: timing 1000000 iterations of match, qreg...
match: 1 wallclock secs ( 0.82 usr + 0.00 sys = 0.82 CPU) @ 12
+19512.20/s (n=1000000)
qreg: 0 wallclock secs ( 0.73 usr + 0.00 sys = 0.73 CPU) @ 13
+69863.01/s (n=1000000)
As you can see, for a regex this simple, it doesn't matter too much... but more complicated regexs wll show a bigger difference. |