Dave Gray <[EMAIL PROTECTED]> wrote: On 1/8/07, hOURS wrote: > Hi everyone, > Jay offered me the following code to help with something. I don't undertand > it, but tried to use it anyway to see if it would work. The computer told me > there was a syntax error in the area I highlighted in color. I can't find it > - maybe that's because I don't understand the code fully, but if anyone can > show me where it is I'd be grateful. > > Jay Savage wrote:my $timeout = 3600; # 1 hour > eval { > local $SIG{ALRM} = sub { die "longRunningModule timed out\n" }; > alarm $timeout; > require longRunningModule; > alarm 0; > }
in case this isn't fixed yet, eval blocks need to be ended with a semicolon, like eval { }; Awesome. That semi-colon did the trick. I now have syntax-error free code. But it doesn't work like I'd expect. I've tried to de-bug, but not having written this myself - well I just can't find the problem. Can anyone help? Some modifications I made: I changed the names of the programs being tested to fit what I had. I reduced the alarm time from one hour down to a few seconds. I included statements to print out error messages. (The original if and else blocks just had comments.) And lastly, for my test, since I'll be checking a large number of programs for infinite loops, I encased this code in a for loop. As you can see below, it executes the code 4 times, testing the programs, infinite1.pl, infinite2.pl, infinite3.pl, and infinite4.pl. Those are all very simple programs. 1, 2, and 4 are identical: $x = 3 + 4; print $x; Running them gives the expected 7 on the screen. For infinite3.pl I encased that in an endless loop: while (1) { $x = 3 + 4; print $x; } Running this gives the expected never-ending stream of 7's. Here's the finished code for error checking: for($number = 1; $number < 5; $number +=1) { $ProgramName = "infinite" . $number . ".pl"; eval { local $SIG{ALRM} = sub { die "$ProgramName timed out\n" }; alarm 9; require $ProgramName; alarm 0; }; if($@) { if($@ =~ /timed out/) { print "timeout\n"; # timeout } else {print "some other error\n"; # some other error } } } So when I run this code and check those four little programs for errors I get the following: some other error some other error some other error some other error I don't know why I get 4 errors and not one, and why they're of the wrong type. Any help would be appreciated. Thank you, Fred Kittelmann --------------------------------- Have a burning question? Go to Yahoo! Answers and get answers from real people who know.