Ben Boulanger wrote:
Code is below:


make this local $SIG{ALRM} = sub { print "$test\n"; die 'died in eval\n'; };
make this local and put it inside the eval to avoid conflict with signal handlers already in place (if the timeout occurs within a system call)
see: perldoc -f alarm for more details


eval {
local $SIG{ALRM = sub { $timeout++; die; }
it is not safe to make any system calls within a signal handler
imagine what might happen if your code was in the middle of executing a print statement when the signal occurred...
it is best to just set a flag then check the flag after the eval

while (<CMD>) { $test .= $_; #$x++; #if ($x = $total) { # close(CMD); #} }

       print "$test";
       alarm(10);
};
if ($timeout) {
    print "eval timed out\n";
}

Here ia a hacked verion of the code you posted:

#!/usr/bin/perl -w
use strict;
use IO::Socket;


STDOUT->autoflush();


my ($test, $x, $timeout);


print scalar(localtime()), "\n";


eval { local $SIG{ALRM} = sub { $timeout++; die;}; open(CMD, "<blah") or die "could not open blah $!"; $test = "test."; alarm(10); while (<CMD>) { chomp; $test .= ' ' . $_; $x++; for (my $i = 0; $i < 1_000_000; $i++) { ; } }


print "$test $x\n";


};


if ($timeout) { print "timeout error on eval x => $x ($@)"; print scalar(localtime()), "\n"; }


[EMAIL PROTECTED] hacking]$ ./pmalarm.pl Mon Sep 20 15:52:42 2004 timeout error on eval x => 42 (Died at ./pmalarm.pl line 12, <CMD> line 42. )Mon Sep 20 15:52:52 2004 [EMAIL PROTECTED] hacking]$

Hope this helps,

Mike

_______________________________________________
Boston-pm mailing list
[EMAIL PROTECTED]
http://mail.pm.org/mailman/listinfo/boston-pm

Reply via email to