in reply to How to apply modifiers to a substitution via variable?
I find the suggestions to use eval EXPR unwise and suggestions to use s///ee (eval EXPR's disguised form) deplorable. Don't generate code for eval that you can't easily validate. Fortunately, eval EXPR is not needed. Here's a copy of my earlier answer to the same question.
For imsx, you can use (?switches:pattern). For g, you'll have to use an if.
my $source = "abc"; my $find = "a"; my $replace = "x"; my %opts = ( i => 1, m => 0, s => 0, x => 0, g => 1, ); my $opts = join '', map { $opts{$_}?$_:'' } qw( i m s x ); my $re = "(?$opts:$find)"; if ($opts{g}) { $source =~ s/$re/$replace/g; } else { $source =~ s/$re/$replace/; } print $source;Update: Fixed where %opts was referenced as %opt.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How to apply modifiers to a substitution via variable?
by liverpole (Monsignor) on Feb 01, 2007 at 00:16 UTC | |
|
Re^2: How to apply modifiers to a substitution via variable?
by liverpole (Monsignor) on Feb 01, 2007 at 00:18 UTC | |
by ikegami (Patriarch) on Feb 01, 2007 at 00:53 UTC |