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

Hello, I have written an application in perl that connects to stream of data via a socket. The application constantly reads the binary records coming down the socket, and writes the data to a human-readable tab-delimited file. (This application must run on either a Unix or Windows machine)

My cunumdrum was letting the application know that it was time to disconnect from the socket. I managed to do that using a signal handler to capture the ctrl-c keystroke. The signal handler would close the socket connection.

$SIG{INT} = \&UserInterrupt; sub UserInterrupt{close(INPUT_HANDLE);}
This works OK, but I would like to use a different keystroke to signal the closing of the socket connection. What other signals can be produced by user keystrokes?

Replies are listed 'Best First'.
Re: Question about signals
by Tanktalus (Canon) on Jan 26, 2005 at 20:54 UTC

    Windows - I'm not sure there are any others.

    Unix - ctrl-z produces something - SIGSTOP, I think. And "kill -1 $perl_pid" (from another shell) produces SIGHUP (a popular way of alerting daemons to re-read their config file), "kill -2 $perl_pid" (from another shell) produces SIGINT (same as ctrl-c), "kill -3 $perl_pid" ... etc. Anything else requires you to poll the keyboard for special keys, such as using Term::ReadKey:

    use Term::ReadKey; ReadMode 4; if (my $key = ReadKey(-1) and $key = ' ') { # space was pressed close(INPUT_HANDLE); }
    Just put something like that at various places in your loop, although if you're currently blocked on something else, this won't really help.

      Thanks for the info. I thought about using the Term::Readkey module. I think it will work for my purposes, but I have some other questions about it which I will post in another thread.
Re: Question about signals
by ambrus (Abbot) on Jan 26, 2005 at 20:55 UTC

    You can change the interrupt character with tc[gs]etattr, see "Terminal Modes" in glibc info; and the POSIX::Termios structure in perldoc POSIX.

      You can change the character that triggers an interrupt but the signal remains the same (apologies to Zeppelin, Led). And if you don't want to muck with the POSIX calls yourself you can use stty intr from the shell.

Re: Question about signals
by zentara (Cardinal) on Jan 27, 2005 at 16:00 UTC
    You might want to read "man skill" , and see what skill does, (send a signal), that may help you with your experimenting with trapping different signals. You could use it in conjunction with Term::ReadKey and system, to send whatever signals you want, to whatever pid you want.

    I'm not really a human, but I play one on earth. flash japh