Here is a way you could encapsulate the chaining in a subroutine
use strict;
my $str = "the boy walked the dog";
$str=ChainSubs($str,
walked=>'fed',
boy=>'girl',
dog=>'Audrey II'
);
print $str;
sub ChainSubs{
my ($string,%subs)=@_;
foreach my $original (keys %subs) {
$string=~s/$original/$subs{$original}/;
}
$string;
}
This of course assumes that you don't care what order the substitutions are done in. If you did care, you could walk through @_ two elements at a time like this:
sub ChainSubs{
my $string = shift;
for (my $i=0;$i<@_;$i+=2) {
$string=~s/$_[$i]/$_[$i+1]/g;
}
return $string;
}