http://www.perlmonks.org?node_id=475118


in reply to Replacing a specfied instance of a pattern in a string

my $i= 0; $str =~ s/(...)/ 3 == ++$i ? 'fish' : $1 /ge;
or
my $i = 3; 0 while $str =~ /fsih/gi && --$i; substr( $str, $-[0], $+[0]-$-[0], 'fish' ) if ! $i;

- tye