in reply to Substitution with regexes in variables
s/// is an operator, so that means your variable contains Perl code. eval EXPR can be used to execute it.
But that's rather unsafe. I assume you don't want to allow arbitrary code execution (variable lookups, (?{}), (??{}) and /e). If so, you can use the following:
my ($pat, $repl, $mods) = $substitution =~ m{ ^s/ ( (?:[^/\\]+|\\.)* ) / ( .* ) / ( [a-z]* ) \z }xs or die("Bad substitution\n"); my %mods = map { $_ => 1 } $mods =~ /./sg; my $global = delete($mods{g}); $mods = join('', delete(@mods{qw( i m s x )})) and $pat = "(?$mods:$pat)"; die("Unknown modifiers \"", keys(%mods), "\"\n") if %mods; no re 'eval'; if ($global) { $candidate =~ s/$pat/$repl/g; } else { $candidate =~ s/$pat/$repl/; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Substitution with regexes in variables
by JadeNB (Chaplain) on Nov 15, 2009 at 07:38 UTC | |
by ikegami (Patriarch) on Nov 15, 2009 at 07:56 UTC | |
by JadeNB (Chaplain) on Nov 15, 2009 at 07:59 UTC |