use strict ; use warnings ; while () { my ($pat, $repl) = (split '/') [1, 2]; # Escape $ and @ preceded by an even number of '\', except for $1 etc. $repl =~ s/(? " ; eval $e ; die $@ if $@ ; print "'$s'\n" ; } __DATA__ s/foo/bar/ s/(fo)o/bar$1/ s/oo/\${system "echo hello sailor !"}/ s/oo/\\${system "echo hello sailor !"}/ s/oo/\\\${system "echo hello sailor !"}/ s/oo/\\\\${system "echo hello sailor !"}/ s/oo/\\\\\${system "echo hello sailor !"}/ s/oo/@{system "echo hello sailor !"}/ s/oo/\@{system "echo hello sailor !"}/ s/oo/\\@{system "echo hello sailor !"}/