my $newstring = munge_string( 'one_two_three', '312' );
sub munge_string
{
my ( $string, $patternkey ) = @_;
# patterns can also be 'text$1$2$3', etc
my %patterns = ( 123 => '$1$2$3',
312 => '$3$1$2' );
# in reality the regex is also dynamic (based on $patternkey)
$string =~ m/(\w+)_(\w+)_(\w+)/;
return $patterns{$patternkey}; # want 'threeonetwo', got '$3$1$2'
}
####
$string =~ s/(\w+)_(\w+)_(\w+)/$patterns{$patternkey}/e;
print $string; # '$3$1$2'
##
##
$string =~ s/(\w+)_(\w+)_(\w+)/$3$1$2/;
print $string; # 'threeonetwo'
##
##
$string =~ s/(\w+)_(\w+)_(\w+)/join( '', $3, $1, $2 )/e;
print $string; # 'threeonetwo'