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

I am trying to handle a ctrl-c signal ( $SIG{'INT'} ), but my program is not capturing it. I am pretty sure this is due to the program currently waiting on input from STDIN via IO::Prompt. When I hit ctrl-c, the program stops and my handling doesn't happen. The handler does work if I insert a sleep and interrupt the sleep.

My question, is how can I capture the signal while using IO::Prompt, or do I have to prompt without using that module?
#!/bin/perl -w use strict; use IO::Prompt; $SIG{'INT'} = 'INT_handler'; my $PROMPT="Yes or no? [YES,no] :"; while (prompt $PROMPT, -until => qr/YES|n|N/) { print "\nExcuse me? Say again?\n\n"; } my $YES_NO=$_; sub INT_handler { print_info("Caught signal Interupt. Cleaning up..."); exit(0); }

Replies are listed 'Best First'.
Re: Not Catching Signals While Program is Prompting with IO::Prompt
by BrowserUk (Patriarch) on Jun 08, 2012 at 21:02 UTC

    Probably because like some many overspecified CPAN modules, IO::Prompt throws everything including the kitchen sink at solving what are essentially simple problems, without considering the knock-on affects upon the callers code.

    In this case, the kitchen sink is Term::Readkey and ReadMode 'raw', $IN;.

    It compounds that by taking the unilateral decision to terminate the calling process if the user type ^C:

    local $SIG{INT} = sub { ReadMode 'restore', $IN; exit };

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

      That sucks. Thanks for the response. I didn't like it that much anyway. I think I will write my own sub to prompt.
      found this bug... https://rt.cpan.org/Ticket/Display.html?id=38014

        2008. It doesn't look like anythings going to change any time soon.

        Frankly, the module is overkill for what it does. A simple loop that prompts and re-prompts until the input matches a regex is pretty trivial to write.

        sub promptTill{{ local $\; print "$_[0]: "; chomp( my $input = <STDIN> ); redo unless $input =~ $_[1]; return $input }} print promptTill( 'Yes/No', qr[^(?:y(?:e(?:s)?)?|n(?:o)?)$]i );;

        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        The start of some sanity?