hOURS am Donnerstag, 30. November 2006 21:09:
> Jen Spinney <[EMAIL PROTECTED]> wrote:  On 11/20/06, hOURS  wrote:
> >  Recently I posed a question on here regarding a program I have that 
> > runs other programs (through require statements and what not). My 
> > problem was that the programs getting run might have syntax errors and  I
> > wanted to skip over each of those and go onto the next one. We  figured
> > out a way to handle that. It turns out however, that these  programs
> > sometimes have an even more troublesome problem: infinite  loops. I knew
> > about this possibility, but figured I would just use the  time function,
> > and if a program was taking to long, skip over it. Yeah,  that wasn't so
> > smart. I can't have the main program check the elapsed  time while the
> > required program is running its infinite loop. Or can I  somehow? Any
> > ideas anybody? Thank you.
> >         Fred Kittelmann
>
> Fred,
> Have you checked out the alarm function?  I'm a beginner myself and I
> had a similar problem earlier today.  alarm seemed to do it for me.
> Good luck!
>
> - Jen
>
>   Thanks Jen,
>   I've checked out alarm as much as I can.  My PERL textbook  scarcely
> mentions it.  Trying "perldoc -f alarm" was a little more  informative, but
> I still don't understand how to use this.  Can  anyone explain it to me?
> Fred

Does the following modified code example from 'perldoc -f alarm' helps?

Dani

#!/usr/bin/perl
use strict;
use warnings;

my $timeout=5; # secs

eval {
  # Assign a signal handler subroutine which is invoked in case
  # the alarm signal is sent
  #
  local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required

  # "send an alarm signal after $timeout seconds!"
  #
  alarm $timeout;

  # to test a non-timeout die, uncomment following line:
  #die;

  # the problem:
  #
  endless_loop();

  # reset alarm timer: "Don't send alarm signal any more"
  #
  alarm 0;
};

# Check if the code within eval died because of an alarm signal
# or something else. We check the die message for that.
#
if ($@) {
  if ($@ eq "alarm\n") {
    warn "endless_loop() interrupted after timeout\n";
  }
  else {
    warn "code in eval died!\n";
    die;
  }
}

warn "program continues...\n";

sub endless_loop { {} while 1 }

__END__

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to