Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re: quiting program

by maverick (Curate)
on May 31, 2002 at 19:40 UTC ( #170814=note: print w/replies, xml ) Need Help??

in reply to quiting program

You'll need to setup a signal handler to catch the ctrl-c.
my $num_ctrlc = 0; $SIG{'INT'} = sub { $num_ctrlc++; print "Got $num_ctrlc ctrl-c\n"; exit if $num_ctrlc > 2; }; # rest of program
The $SIG{'INT'} part creates a signal handler for 'interrupt' (control-c) with your code. In this case, a little sub that increments a global counter, prints a message, and exists if the counter is above a certain number

BTW, your while loop is only going to run once, since @a will contain ALL of STDIN on the first pass... Perhaps you meant something more like:

while ($a = <STDIN>) { print $a; }

OmG! They killed tilly! You *bleep*!!

Replies are listed 'Best First'.
Re^2: quiting program
by particle (Vicar) on May 31, 2002 at 20:43 UTC
    your code is probably better implemented as a closure. then there's no special variable in the same scope as your code.

    { my $num_ctrlc = 0; $SIG{'INT'} = sub { print "Got " . ++$num_ctrlc . " ctrl-c\n"; exit if $num_ctrlc > 2; } }

    ~Particle *accelerates*

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://170814]
[robby_dobby]: Likewise! I haven't seen a lot of people with HAs back where I come from, so I figured it might be the same around here too...
[robby_dobby]: Oh, shit -- I was replying to someone, but it wound up here. -_-
robby_dobby is still not used to PM's interface. /o\
[oiskuu]: War. War never changes.
[robby_dobby]: "My cat is a very good mouser" -- that's a great way to identify a Brit among a crowd :-)
thezip looks around to see no one else dancing to Piazzola...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (11)
As of 2017-11-17 19:42 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (272 votes). Check out past polls.