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

My query has to do with how one would stop perl from accepting 3 carraige returns when one enters Ctrl-C and decides to resume the programme..

# ./prompt2
Testing One Two Three
^CPress -=ENTER=- to Resume or 'q' to quit
<ENTER * 3 times>
Got to the end of the programme

Here is the code
#!/usr/bin/perl $SIG{'INT'} = sub { print "Press -=ENTER=- to Resume or 'q' to quit\n"; chomp (my $quit=<STDIN>); die "\nTerminating at users request \n" if $quit =~ /^q$/i; }; print "Testing One Two Three\n"; chomp (my $continue=<STDIN>); print "Got to the end of the programme \n";

janitored by ybiC: Balanced <code> tags around codeblock, other minor format tweaks for legibility

Replies are listed 'Best First'.
Re: Resuming from a SIG{INT}
by tinita (Parson) on Mar 16, 2004 at 17:26 UTC
    i would suggest that another CTRL-C would terminate the program as i think that's more common practice, but anyway -
    if you want to decide if the user entered 3 empty lines (or CRs) or a single character 'q' you'd have to use Term::ReadKey and maybe Term::ReadLine.
Re: Resuming from a SIG{INT}
by NetWallah (Canon) on Mar 16, 2004 at 17:13 UTC
    Your code does not take "3 carriage returns".

    A single CR after CTRL-C DOES quit the program with the correct message (as coded) "Got to the end of the programme".

    Offense, like beauty, is in the eye of the beholder, and a fantasy.
    By guaranteeing freedom of expression, the First Amendment also guarntees offense.

Re: Resuming from a SIG{INT}
by Limbic~Region (Chancellor) on Mar 16, 2004 at 19:26 UTC
    jmccaf01,
    I assure you that your code is working as you have coded it. I just think you didn't code it the way you wanted it. Try the following instead:
    #!/usr/bin/perl use strict; use warnings; $SIG{INT} = sub { print "Program interuption detected!\n"; print "Press ENTER to continue or any other key to quit\n"; my $foo = <STDIN>; chomp $foo; die "\nTerminating at users request\n" if $foo; print "Resuming our regularly scheduled programming\n"; }; print "This would be your program starting up\n"; for ( 1 .. 10 ) { print "$_\n" for 1 .. 10; sleep 1; } print "This would be your program finishing cleanly\n";
    Cheers - L~R
      You cheat :)

      Your example shows that if a signal handler that uses diamond operator is lucky enough not to interrupt another diamond operator it will probably usually most of the time work as expected.

        kappa,
        We completely have different interpretations of why the OP chose to read from STDIN during the main execution of the program. I was assuming it was to get the program to pause long enough to test and it was the extra carriage return(s) that were not desired.

        Cheers - L~R

Re: Resuming from a SIG{INT}
by kappa (Chaplain) on Mar 16, 2004 at 20:34 UTC
    Please read perldoc -q signal. You should not do anything serious from a signal handler, especially use buffered IO.
Re: Resuming from a SIG{INT}
by chip (Curate) on Mar 16, 2004 at 16:30 UTC
    Please explain more about the behavior you see vs. the behavior you want.

        -- Chip Salzenberg, Free-Floating Agent of Chaos

      If one enters Ctrl-C and decides to resume the programme a single Carraige Return should be ok.
      At present it does not behave the way I expected it to.

        The first Enter is actually making the program continue. But after returning, Perl seems to require two Enters for the <STDIN> in the main program to return.

        I have no idea why this is so. I suspect it's a bug.

            -- Chip Salzenberg, Free-Floating Agent of Chaos