sub foo { my $bar = shift; if ( $CONDITION ) { #modify $bar if you need to } return $bar; } my $text =~ s{()}{foo($1)}sige;