The match operator will happily interpolate a pattern stored as a string. Compiled patterns have only existed since 5.6. qr// is not necessary.
In fact, using qr// makes no sense here.
If there's no reason for the pattern to be a string, you might as well just do
$string = 'My Test Data';
print $string =~ m/\btest\b/i ? "Match\n" : "No match\n";
But I bet there's a reason it's a string here. If so, you'd do
$string = 'My Test Data';
$regex = '(?i)\btest\b';
print $string =~ $regex ? "Match\n" : "No match\n";
If you were planning on using $regex repeatedly, then qr// could become useful. |