in reply to Perl OO, input alarm timeout

I don't know why it's happening, but localizing the alarm handler in Enginesub.pm seems to solve it over here.

sub go { my $self = shift @_; local $SIG{ALRM} = \&ti; eval { ...

Output:

5 seconds main input: no input running.... name name1 (2 seconds) sub input: no input running defaults..... name name2 (2 seconds) sub input: no input running defaults..... 5 seconds main input: no input running.... name name1 (2 seconds) sub input: no input running defaults..... name name2 (2 seconds) sub input: no input running defaults.....

Update: Aha, of course... Since you don't localize the handler in Enginesub, it will replace the one you had for your main loop. This means that the second time the 5 second timeout triggers, it will call the ti sub instead of the timed_out sub. This in turn means that your if ($@ =~ /BLAH/) is false, since ti sets it to TIMEOUT instead.

Changing your main while loop to:

while() { eval { alarm(5); print "\n\n5 seconds main input: "; chomp(my $input = <STDIN>); alarm(0); }; if ($@ =~ /BLAH/) { print "\n\nno input running....\n\n"; $engine->run(); } if ($@ =~ /TIMEOUT/) { print "\n\nOops, still using ti handler...\n\n"; } sub timed_out { die "BLAH"; } }

The output is:

5 seconds main input: no input running.... name name1 (2 seconds) sub input: no input running defaults..... name name2 (2 seconds) sub input: no input running defaults..... Oops, still using ti handler... 5 seconds main input: Oops, still using ti handler... 5 seconds main input: Oops, still using ti handler...

(Removed some line breaks to make things easier to read)

Replies are listed 'Best First'.
Re^2: Perl OO, input alarm timeout
by yoda54 (Monk) on Jul 23, 2006 at 08:33 UTC
    Thanks!