Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Alternatives to Mixing Alarm and Sleep

by ruzam (Curate)
on Feb 20, 2010 at 01:39 UTC ( [id://824306]=note: print w/replies, xml ) Need Help??


in reply to Alternatives to Mixing Alarm and Sleep

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; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://824306]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-24 02:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found