in reply to Escaping special characters

I think that the way you are trying to do it, it is modifying the original string each time. I think you should copy the result of the regex into a variable of its own like:
use strict; use warnings; my $string = '123!'; my $new_string = $string =~ s/!/4/r; print "original - $string\nreplacement - $new_string";
This way you leave the original string untouched and can modify it 20 different ways from tuesday if needed :)

Replies are listed 'Best First'.
Re^2: Escaping special characters
by AnomalousMonk (Archbishop) on Aug 12, 2015 at 02:00 UTC
    my $new_string = $string =~ s/!/4/r;

    Note that the  /r substitution modifier is only available with Perl versions 5.14+. Prior to that version, one can use the following trick (and using the  /g modifier is handy also):

    c:\@Work\Perl\monks>perl -wMstrict -le "print 'perl version: ', $]; ;; my $string = '123! 987!'; (my $new_string = $string) =~ s/!/bang/g; print qq{original '$string' new string '$new_string'}; " perl version: 5.008009 original '123! 987!' new string '123bang 987bang'


    Give a man a fish:  <%-(-(-(-<