Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Control C in PERL?

by brassmon_k (Sexton)
on Apr 18, 2001 at 01:35 UTC ( #73355=perlquestion: print w/replies, xml ) Need Help??
brassmon_k has asked for the wisdom of the Perl Monks concerning the following question:

Is there anyway in a PERL script to do this:

I have an infinite loop statement but I want the script to not be terminated if someone does a control C ^C. Is there actually a way to tell PERL to ignore a ^C and continue with the loop. I tried telling it yeah, and I tried the slashes the quotes and everything but it still doesn't want to recognize the ^C

I tried it many ways but the below is just an example:
if ($input = '^C') { keep running loop }

Replies are listed 'Best First'.
(jeffa) Re: Control C in PERL?
by jeffa (Bishop) on Apr 18, 2001 at 01:40 UTC
    You need to deal with signals:
    $SIG{INT} = 'IGNORE';
    is all you need to ingore CNTRL-C. You can assign a reference to a subroutine if you need fine control:
    $SIG{INT} = \&interrupt; sub interrupt { print STDERR "Caught a control c!\n"; exit; # or just about anything else you'd want to do }


      Cool the reason I wanted to know that is because I'm going to play a little trick on my buddy
      He thought it was funny to alter my autoexec.bat on my PC at work so it would go into a booting loop
      You know the old trick...Type autoexec.bat and the bottom of the autoexec.bat and it will go into a boot loop
      Anyway I'm getting him back. I'm going to do it on his UNIX machine (Note this is just a user terminal not something
      important because I'm not a jerk) Anyway It's going to be kind of like the infamous "cookie" script except it will make
      him guess something he can never guess. It will keep asking for different things and the answer will never relate to the
      question being asked and I needed the CTRL-C deal so I could stop him from terminating the script. I already took care of it
      if he tries to open up another shell and terminate the shell process.

      Or I could just do this one....I like this one...Make an alias for the cd command that does a kill -9 on the openwindows process
      so whenver he does a cd (It takes a few tries to figure out what's killing open windows)you get booted out of openwindows. It's really fun to watch.
      Hey were having fun and being inventive at the same time.

      The BrassMonk
Re: Control C in PERL?
by jwest (Friar) on Apr 18, 2001 at 01:43 UTC
    A control-C is actually an interrupt signal, by the time your terminal driver is done mangling it. Install a signal handler for INT that ignores the signal. Such as:
    $SIG{INT} = 'IGNORE';
    Hope this helps!

    -><- -><- -><- -><- -><-
    All things are Perfect
        To every last Flaw
        And bound in accord
             With Eris's Law
     - HBT; The Book of Advice, 1:7
Re: Control C in PERL?
by buckaduck (Chaplain) on Apr 18, 2001 at 01:44 UTC
    Capturing signals is discussed in great detail in perlipc.

    local $SIG{INT} = 'IGNORE';


Re: Control C in PERL?
by AgentM (Curate) on Apr 18, 2001 at 01:52 UTC
    Control-C is a special keyboard character caught by the terminal drivers. It passes the SIGINT to the foreground process. While it's easy to ignore this single character sequence as shown above, it may be better to use Curses to ensure that you ignore ALL input while your loop is running. Also, Curses can help you to "beautify" your output. Also, you could assign ^C a subroutine or some instructions to notify the user that the key is being ignored. The C docs will help you more than the perldocs, so look at man curs_inopts(3X). Good luck!
    AgentM Systems nor Nasca Enterprises nor Bone::Easy nor Macperl is responsible for the comments made by AgentM. Remember, you can build any logical system with NOR.
      You can also use Term::ReadKey to help control the signals. This works better on Win32 systems for some reason.

      Apply yourself to new problems without preparation, develop confidence in your ability to to meet situations as they arrise.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://73355]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2018-06-24 12:31 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.