my $max_recurs = 10; $max_recurs-- while $string =~ ... && $max_recurs; warn "Too many recursive substitutions on '$string'" if !$max_recurs && $^W;