sub adv_tr { my ($str, $x, $from, $to) = @_; my $pos = 0; # find index of Xth "$from" ($pos = index($str, $from, $pos)) == -1 and return while $x--; eval "substr(\$str, $pos) =~ tr/\Q$from\E/\Q$to\E/"; }