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

How do you trap for control Z in unix...
sub blingbling{ local($sig) = @_; print "\n\nBling Bling\n\n"; } $SIG{'INT'} = 'handler'; #Catch ^C

It works for control C, but i cant seem to figure out Z

Replies are listed 'Best First'.
Re: Trapping for Control^Z
by pg (Canon) on Nov 18, 2002 at 20:40 UTC
    According to POSIX standard, a CTRL-Z should tigger a SIGTSTP being sent out. Try it.
Re: Trapping for Control^Z
by dakkar (Hermit) on Nov 18, 2002 at 22:12 UTC

    Keep in mind that you are not trapping ^C or ^Z: you are trapping the signal that the terminal is sending to your process.

    If you really want to get the keys, try:

    #!/usr/bin/perl -w use Term::ReadKey; ReadMode 4; while (($key=ReadKey(0)) ne 'Q') { print "Got key ",ord($key),"\n"; } ReadMode 0;
Re: Trapping for Control^Z
by nothingmuch (Priest) on Nov 18, 2002 at 19:53 UTC
    while (defined (my $key = each %SIG)){ my $foo = $key; # for closures $SIG{$foo} = sub { print "I am SIG$foo\n" }; }
    See which signal is sent when you to ^Z, if at all. Perhaps sleep x, or <STDIN> for blocking would be useful.

    Only tested with SIGALRM as I have no kill or /bin/kill... *sob*

    Update: Fixed HTML entities for STDIN

    Update 2: Appearantly it's $SIG{TSTP}

    -nuffin
    zz zZ Z Z #!perl
Re: Trapping for Control^Z
by broquaint (Abbot) on Nov 19, 2002 at 12:15 UTC
    What a coinky-dink - JSchmitz asked this in the CB not more than a day ago. If you're looking to trap the Ctrl-z which suspends a process then you need to trap the TSTP signal e.g
    shell> perl -e '$SIG{TSTP} = sub { warn "suspension trapped\n" }; \ sleep 50; print "on with the show ...\n"' __output__ suspension trapped on with the show ...
    That of course assumes Ctrl-z was pressed within those 50 sleeping seconds. Also note that this interrupts the current operation, in this case the sleep (at least on my linux system). Check out man 7 signal for more info on signals.
    HTH

    _________
    broquaint

Re: Trapping for Control^Z
by Kri (Acolyte) on Nov 18, 2002 at 22:05 UTC
    It works!!! thanks ..::KARMA POINTS::..
Re: Trapping for Control^Z
by Anonymous Monk on Nov 19, 2002 at 15:33 UTC
    $SIG{'TSTP'} = 'blingbling'; #catches terminal stop signals