while ($string =~ s/(abc)/ sprintf("%s def(%s)", $1, $x) /eig) { die "oops" if $x++ > 10; }