in reply to Waiting for Alarm
Here is the code
printsuse strict; sub Call_with_Timeout{ my ($TIMEOUT, $CODEREF) = @_; eval { local $SIG{ALRM} = sub { die "alarm time out" }; alarm $TIMEOUT; $CODEREF->(); # Whatever needs to be done alarm 0; 1; # return value from eval on normalcy } or return -1; # Timeout happened return 1; # OK } my $t=Call_with_Timeout(3, sub{ sleep 3, print "$_.\n" for 1..2}); print "Timed out=$t;\n";
"As you get older three things happen. The first is your memory goes, and I can't remember the other two... " - Sir Norman Wisdom
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Waiting for Alarm
by n8ur (Acolyte) on Feb 16, 2008 at 18:21 UTC | |
by n8ur (Acolyte) on Feb 16, 2008 at 18:23 UTC |