my $n_secs = 30; #seconds after which we die. my $start = time; ## each iteration of this loop is short, but the whole ## thing is long while ( $some_condition ) { die 'We ran too long!' if time-$start >= $n_secs; ## do stuff ## }