emptynet has asked for the wisdom of the Perl Monks concerning the following question:

Hello to all! I have studied posts here for a while now, trying to improve my meagre Perl coding skills, but seemingly to little avail so far! I've been toying with alarms, in order to build a slideshow script. This thing will display random images, images in sequence forward or backwards or pause indefinitely on an image. If the user does not press a key within the timmeout period, the script repeats the last action. Sadly for reasons I don't understand, it works 99% of the time, and occasionally craps out with the following error message:
panic: leave_scope inconsistency at slideshow.pl line xx. panic: leave_scope inconsistency.
This corresponds to the "eval {" line in the code fragment below. Can anybody help explain what I've clearly failed to grasp?
do { $SIG{CHLD} = 'IGNORE'; undef $pid; die "fork: $!" unless defined ($pid = fork); if ($pid) { eval { local $SIG{ALRM} = sub { die "alarm\n" }; alarm $timeout; do { # Wait for a keypress # Take appropriate action SWITCHKEY: { if ($key eq "q") { $continue = 0; last SWITCHKEY; }; if ($key eq "n") { $action = $key; $index++ ; last SWITCHKEY; }; if ($key eq "b") { $action = $key; $index-- ; last SWITCHKEY; }; if ($key eq ".") { alarm 0 ; last SWITCHKEY; }; $action = "r"; $index = int rand @files; } } until $key ne "."; alarm 0; # Close the current image window (by killing the child) kill TERM => $pid; }; if ($@) { # Timed out die unless $@ eq "alarm\n"; kill TERM => $pid; SWITCHACTION: { # n -> next image if ($action eq "n") { $index++; last SWITCHACTION; }; # b -> previous image if ($action eq "b") { $index--; last SWITCHACTION; }; # r -> random image if ($action eq "r") { $index = int rand @files; last SWITCHACTION; }; } } } else { # This is the child process # exec an image viewer over the child process... exec("/usr/bin/ee '$file'"); } } until not $continue;
Minds are like parachutes; they only work when open. (Sir James Dewar)

Replies are listed 'Best First'.
Re (tilly) 1: Why does my parent panic when comitting serial infanticide?
by tilly (Archbishop) on Nov 28, 2000 at 07:59 UTC
    Your Perl is fine. Perl's signal handling is just flaky. File a bug report using perlbug, let p5p know (yet again) that people Really Do Care About This.

    In the meantime you will likely want to rethink your approach to not need signals. For one idea you might try POE which has come up here before. Or poll for keyboard input.

    Sorry I don't have a nicer answer for you.

      Thanks tilly - I feel better knowing I wasn't insane after all. I'm using Term::Readkey to test keys for input, I just skipped that code in my post. I'll take a closer look at POE - but this may be overkill for my purposes. Cheers! Mike.