in reply to manipulating capture groups in s///

If the replacement rules can be formulated in a look up table just use a hash:

my %lookup = ( bar => 'thing1', foo => 'thing2'); $_ = "this has bar and foo\n"; s/(bar|foo)/$lookup{$1}/g; print; #=> this has thing1 and thing2

If the replacement requires computation, then use the /e modifier which evaluates the replacement side of the s/// operator as Perl code:

sub foo { return reverse uc shift } $_ = "this has bar and foo\n"; s/(bar|foo)/foo($1)/eg; print; #=> this has RAB and OOF