At 12:01 AM 7/16/02 +0100, Daniel Gardner wrote:
>Max Clark wrote:
>
> > I'm looking for a way to run an operation (like a while (<>) loop for
> > example) until the user presses "ctrl-c". Except instead of ending the
> > perl script I want to trap the "ctrl-c" and present the user a menu of
> > options.
>
>This is very minimal:
>
>,----
>| $SIG{INT} = sub { warn "ctl-c pressed!\n" };
>|
>| while (1) {
>| print "hi\n";
>| sleep 1;
>| }
>`----
Please don't do I/O from a signal handler, it sets a bad example.
my $interrupted;
local $SIG{INT} = sub { $interupted = 1 };
while (1) {
print "hi\n";
sleep 1;
if ($interrupted) { warn "ctrl-c pressed!\n" and $interrupted = 0 }
}
>If you run that it'll print "hi" forever, and a message when you press
>ctl-c. You'll have to kill it to stop it (something to think about)
--
Peter Scott
Pacific Systems Design Technologies
http://www.perldebugged.com/
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]