in reply to Re: How to timeout a while loop?
in thread How to timeout a while loop?

Hey Suaveant,

Great! Thanks a lot; in fact i needed to change some details for it to work, like putting the sub that listens the $@ outside the while, but now it is just perfect.

Here´s the final code, for the monk's record:

my $number = 0; $SIG{ALRM} = sub {die 'timeout'}; while ( $number < 10 ) { eval { $number++; alarm(3); if ( $number == 3 ) { sleep 4; } # This is to simulate the delay + the routines may generate... print $number; print "\n"; }; alarm(0); } if($@ eq 'timeout') { next; } else { die $@; }
Thanks a lot, my friend

Andre_br

Replies are listed 'Best First'.
Re^3: How to timeout a while loop?
by suaveant (Parson) on Apr 25, 2005 at 20:47 UTC
    Umm.. if you leave the if at the bottom outside the while the next is useless, and any $@ values will cause the loop to continue, including non-timeout values. You could possibly also miss valid die messages. To be really useful you should probably check the $@ at the end of the eval after the alarm(0);

                    - Ant
                    - Some of my best work - (1 2 3)