I'm having trouble with replacer strings that contain special characters with using s///
:
> cat replace.pl
use strict;
use warnings;
my $orig_string = 'abc${abc}def';
my $replacer = '${abc}';
my $replacement = 'TEXT';
print "\nBefore replacement: $orig_string";
$orig_string =~ s/$replacer/$replacement/g;
print "\nAfter replacement: $orig_string";
print "\n";
exit;
Erroneous output:
> /usr/bin/perl replace.pl
Before replacement: abc${abc}def
After replacement: abc${abc}def
This works if I manually escape the $
in $replacer
as my $replacer = '\${abc}';
, but the replacer will have an unknown number and set of special characters in production.
Perl version:
> /usr/bin/perl -v
This is perl 5, version 16, subversion 3 (v5.16.3)