b2sing4u has asked for the wisdom of the Perl Monks concerning the following question:

When I use s/FindPattern/ReplacePattern/Options , I can change FindPattern & ReplacePattern within a program like below.
$source = "abc"; $find = "a"; $replace = "x"; $source =~ s/$find/$replace/g; print $source;
In this case, is there any method to change Options within a program?
That is changing
s/$find/$replace/g;
to
s/$find/$replace/gi;
within a program.
Like... "using variable for Options" or something

Replies are listed 'Best First'.
Re: How do I change substitution options within a program?
by ikegami (Patriarch) on Oct 31, 2006 at 08:06 UTC

    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.

Re: How do I change substitution options within a program?
by davido (Cardinal) on Oct 31, 2006 at 08:09 UTC

    Build up your regular expression programatically, and use the (?i-xsm) or (?-xism) constructs (documented in perlre).

    In other words....

    my $insensitive = '(?i-xsm)'; my $sensitive = '(?-xism)'; my $target = "Abcde"; foreach my $re_switch ( $insensitive, $sensitive ) { if( $target =~ m/$re_switch([a-z])/ ) { print "$1\n"; } }

    And the output...

    A b

    Dave

Re: How do I change substitution options within a program?
by holcapek (Sexton) on Oct 31, 2006 at 18:55 UTC
    See perlop, namely qr/PATTERN/options. With qr// you can change imosx options as you wish within dynamically generated patterns.

      See perlop, namely qr/PATTERN/options. With qr// you can change imosx options as you wish within dynamically generated patterns.
      I read your comment as meaning you can set the regex options from variables when using qr//. However, none of the following work for me:
      $mods = "im"; $test = qr/xyz/$mods; # Syntax error. $test = qr/xyz/"$mods"; # Ditto. $test = qr/xyz/\$mods; # Ditto. # ... out of ideas.

      Could you clarify your post? What can you do with qr// and modifiers that you can't with m// or s///?

        Nothing. Maybe he was thinking of the following:

        # XXX WRONG DON'T USE THIS $re = qr/$re/i if $opt{i}; $re = qr/$re/m if $opt{m}; $re = qr/$re/s if $opt{s}; $re = qr/$re/x if $opt{x};

        On the other hand, the following does work:

        $re = "(?i:$re)" if $opt{i}; $re = "(?m:$re)" if $opt{m}; $re = "(?s:$re)" if $opt{s}; $re = "(?x:$re)" if $opt{x};
Re: How do I change substitution options within a program?
by gaspodethewonderdog (Monk) on Nov 01, 2006 at 14:58 UTC
    $source = "abc"; $find = "a"; $replace = "x"; $opt = "g"; eval "\$source =~ s/$find/$replace/$opt"; print $source;

      Better:

      eval "\$source =~ s/\$find/\$replace/$opt";

      Even better:

      $opt =~ /[^imsx]/ and die(...); eval "\$source =~ s/\$find/\$replace/$opt";