in reply to Not getting the expected result when using eval/alarm
I modified your example so it would work standalone (since you didn't provide your openRemoteSql routine, or example values for @hostlist, I improvised):
use strict; use warnings; my @hostlist = (1 .. 10); HOST: foreach my $host (@hostlist) { my $timeout = 2; eval { local $SIG{ALRM} = sub { die "alarm\n" }; alarm $timeout; openRemoteSql(); alarm 0; }; if ($@) { print "Timed out\n"; next HOST; } else { print "Didn't time out\n"; } } sub openRemoteSql { if (int rand 2) { print "Blocking\n"; <STDIN>; } }
I have it randomly block -- simulating a long-running process -- and in those cases, the timeout appears to work as expected. This leads me to believe the code you pasted is fine, and your problem might lie elsewhere. If nothing else, this might help you narrow down the problem further.
Update: added strict and warnings, modified code to pass
|
|---|