#!/usr/bin/perl -w use strict; my $time_to_die=0; my $timeout=5; #in seconds $SIG{ALRM} = sub { $time_to_die=1; }; alarm($timeout); while(!$time_to_die){ #tasks here print time,"\n"; } #will allow while loop to complete #after $SIG{ALRM} is received