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.
In reply to Re: How to apply modifiers to a substitution via variable?
by ikegami
in thread How to apply modifiers to a substitution via variable?
by lokiloki
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |