$ perl -wle'use strict; my $str2 = "@cce$$ag@!n"; print $str2;' Global symbol "@cce" requires explicit package name at -e line 1. Global symbol "$ag" requires explicit package name at -e line 1. Execution of -e aborted due to compilation errors. $ perl -wle'use strict; my $str2 = "\@cce\$\$ag\@!n"; print $str2;' @cce$$ag@!n #### my $str1_pat = quotemeta($str1); $str1 =~ s/$str1_pat/$str2/; - or - $str1 =~ s/\Q$str1\E/$str2/; # Same thing.