#!/usr/bin/env perl use strict; use warnings; my $restartcount = 0; my $done = 1; for(my $i = 0; $i < 100; $i ++) { # Do your complex stuff here print "$i\n"; # Simulate some kind of random error # that requires a restart of the loop if(int(rand(100)+1) % 23 == 0) { print "Yada-Yada error, restarting loop\n"; $i = 0; $restartcount++; } # Simulate a condition that requires an abort if(int(rand(1000)+1) == 42) { print "Fatal error, aborting after $restartcount tries\n"; $done = 0; last; } } if($done) { print "Loop done\n"; print "Had to restart $restartcount times\n"; }