$foo = 'aabbcc'; $foo =~ s/(.*)$_/$1X/ for qw(a b c); print $foo; STDOUT: aXbXcX