If you're looping with a sleep (instead of actually hanging on a blocking function to return) then I think you just want to just do away with the alarm all together and keep a loop counter.
$loop_count = 10; while( ! $done and $loop_count-- ) { $done = is_it_done_yet(); sleep 1; }
The alarm is more useful if you have to break out of a blocking function, in which case you probably wouldn't be using sleep anyway.
Update...
On re-read, it sounds like you don't want to get caught hanging on is_it_done_yet(). In which case I think you want to alarm this function separately in an eval.
while( ! $done ) { eval { local $SIG{ALRM} = sub { die "alarm\n" }; alarm 10; $done = is_it_done_yet(); alarm 0; }; last if @$ and $@ eq "alarm\n"; # hit alarm timeout sleep 1; }
In reply to Re: Alternatives to Mixing Alarm and Sleep
by ruzam
in thread Alternatives to Mixing Alarm and Sleep
by pileofrogs
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |