#!/usr/bin/env perl use strict; use warnings; my @iterations = map { 10 ** $_ } 0 .. 10; my $timeout = 2; for my $iterations_this_loop (@iterations) { eval { local $SIG{ALRM} = sub { die "TIMEOUT: $iterations_this_loop\n" }; alarm $timeout; for (0 .. $iterations_this_loop) { # Processing here } alarm 0; }; if ($@) { die $@ unless $@ =~ /^TIMEOUT: \d+/; # propagate unexpected errors print $@; next; } else { print "ENOUGH TIME: $iterations_this_loop\n"; } }