in reply to How to timeout a while loop?

You want a die 'timeout'; in your $SIG{ALRM} that will break you out at the end of your eval (put a ; at the end of that eval) then put your code catch code... like so

$SIG{ALRM} = sub {die 'timeout'}; while ( $number < 10 ) { eval { alarm(3); print $number; if ( $number == 3 ) { sleep 4; } # This is to simulate the delay + the routines may generate... $number++; print "\n"; }; alarm(0); if($@ eq 'timeout') { next; } else { die $@; } }
Not tested, but should get you pointed in the right direction. Alarm 0 is important to unset the alarm.

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

Replies are listed 'Best First'.
Re^2: How to timeout a while loop?
by Andre_br (Pilgrim) on Apr 25, 2005 at 20:36 UTC
    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

      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)