my $limit = time + 3_600; my $cnt; while (1) { ++$cnt; last if $cnt > 100 || time > $limit; # rest of your loop }