in reply to simple timeout

(posted this in the wrong place so reposting here)

This works for me but I'm on a linux box...
only difference bet your code and this is I commented out the 'die' and put 'next' in there.
It's a little noisy so I dropped the '-w' from the perl line...
#!/usr/bin/perl use strict; my $ndx = 0; my @servers = (1,2,3,4); my $globalServerNum; $SIG{'ALRM'} = \&time_out; print "\n\n"; foreach $ndx (@servers) { $globalServerNum = $ndx; eval { alarm(5); print "server $ndx please respond: "; my $answer = <STDIN>; my $timeleft = alarm(0); print "($timeleft) sec left\n"; } } print "\nbye bye end-of-workday\n"; exit; sub time_out { print "\nserver $globalServerNum timed out, go next\n"; next; #die "FED UP WAITING"; }

Replies are listed 'Best First'.
Re^2: simple timeout
by disciple01 (Novice) on Nov 08, 2004 at 16:54 UTC
    That code just sists there for me doing nothing.

    It displays

    server 1 please respond:

    indefinetely. =(

    Incidently the die is required (for the purposes of the script). The eval and die actually work hand in hand, when an eval is called the program takes note the exit bracket in the eval statement. When if a die is called within an eval the program will not fall over, instead execution will continue from the final eval bracket previously remembered. Hence the alarm should trigger a die which causes immediate exit from the eval.