For this literal question which involves only two substrings to be replaced, you should use two separate s///
as @Patrick85 recommends. The approach below begins to make sense only if you have 3 or more such replacements. In that case, tucking the mapping away in a hash will make your code clearer.
Use a hash to look up replacements:
#!/usr/bin/env perl
use strict;
use warnings;
my %replacement = (
'\left' => '(',
'\right' => ')'
);
my $s = '\left \right \center \right \left \middle';
my $t = $s =~ s/( \\ (?: left | right ))/$replacement{$1}/rgx;
print "$s\n$t\n";
There are many problems with trying to cram everything into a single line:
$brackets=~s{\\(left|right)}{$&=~s/\\left/\(/r && $&=~s/\\right/\)/gr; }ge;
It is hard to read. Also, it is impossible to generalize. What happens if you need to map N strings to their replacements? Let's try to write this a little more clearly by adding some whitespace:
$brackets =~ s{
\\
(left | right)
}{
$& =~ s/\\left/\(/r &&
$& =~ s/\\right/\)/gr;
}gex;
Even if this did what you wanted, you've now replaced a single s///
coupled with a simple hash lookup with something that does two additional s///
operations for each match in your source string.
For the example string you showed, this one would do eight additional s///
operations.
That is not good.