Say I have the following in perl:
my $string;
$string =~ s/ /\\\\ /g;
$string =~ s/\'/\\\\\'/g;
$string =~ s/`/\\\\`/g;
Can the above substitut
Although it's arguably easier to read the way you have it now, you can perform these substitutions at once by using a loop, or combining them in one expression:
# loop
$string =~ s/$_/\\$_/g foreach (' ', "'", '`');
# combined
$string =~ s/([ '`])/\\$1/g;
By the way, you can make your substitutions a little easier to read by avoiding "leaning toothpick syndrome", as the various regex operators allow you to use a variety of delimiters:
$string =~ s{ }{\\ }g;
$string =~ s{'}{\\'}g;
$string =~ s{`}{\\`}g;