Friday, August 05, 2011

Somewhere between "use re 'eval'" and "no re 'eval'" | Abigail [blogs.perl.org]

Somewhere between "use re 'eval'" and "no re 'eval'" | Abigail [blogs.perl.org]: "So, we need an additional stringification:

sub example {
my ($str, $pattern) = @_;
no re 'eval';
$pattern = '$pattern';
$pattern = qr /$pattern/;
use re 'eval';
$str =~ /$pattern(?{ 1; })/;
}
This does what I want it to do: run the code block, but die if $pattern contains a code block, regardless whether it's passed as a string or a compiled regexp.

But I wonder, is there a less cluncky way?"