Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Get the current function used to handle control+z

by exodist (Monk)
on Dec 14, 2008 at 04:47 UTC ( #730247=perlquestion: print w/replies, xml ) Need Help??

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

Hello monks, I am writing a module that gets input in raw mode and interprets the special chartacters such as escape, insert, F1-F12, and control keys such as control+a-z.

Currently I have actions occur when certain keys are pressed. I want these to be overridable. For control+c I bound it like so:
'<CONTROL+C>' => sub { exit(0) };
I need to do something similar for control+z, to make it background the process. At first I tried this:
'<CONTROL+Z>' => $SIG{INT};
But it seems this is empty unless set to something new.

I need to know of a way to do this if it is possible.

--------------------------------------

I would rather take 30 minutes to re-invent the wheel then take 30 days to learn how to use someone else's. Before pestering me about my re-invention prepare to defend yourself with a way of learning how to use the wheel in less time than it takes for me to make one, one that I might add is specialized to my specific task!

  • Comment on Get the current function used to handle control+z

Replies are listed 'Best First'.
Re: Get the current function used to handle control+z
by diotalevi (Canon) on Dec 14, 2008 at 05:51 UTC

    The key press Control-z is typically mapped by your termcap or terminfo database to the capability susp. Your terminal interprets that and sends a SIGSTOP signal to your foreground process. SIGSTOP is 19 on the CentOS box I looked at and 17 on the Mac OS X machine I also looked at. The kill program on your *NIX takes a signal name. kill() in Perl doesn't though the Config module stores your local system's mapping as well.

    system "kill -STOP $$"; # stop self

    ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

      It's a little clunky, but you can do the kill from within perl by counting SIGSTOP's position in $Config{sig_name} (which, unfortunately, is a space-separated list rather than an array.)

      perl -MConfig -le 'for( split /\s/, $Config{sig_name} ) { last if /^STOP$/; $n++ } kill $n, $$'
      This just counts signal names until it gets to STOP, and $n will contain the right number.

        There's no need to search signal number in $Config
        kill STOP => $$;
        will work just fine.
Re: Get the current function used to handle control+z
by ikegami (Pope) on Dec 14, 2008 at 04:53 UTC

    Signals are sent using kill.

    '<CONTROL+Z>' => sub { kill INT => $$ }

    PS — Save yourself and ourselves trouble. Use <c>...</c> around code and data blocks.

      Thanks, however that seems to terminate the process, I want it to background the process. Maybe INT was not what I was looking for?

      --------------------------------------

      I would rather take 30 minutes to re-invent the wheel then take 30 days to learn how to use someone else's. Before pestering me about my re-invention prepare to defend yourself with a way of learning how to use the wheel in less time than it takes for me to make one, one that I might add is specialized to my specific task!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://730247]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2020-10-20 00:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (208 votes). Check out past polls.

    Notices?