FAQ 8.13 How do I trap control characters/signals?

Discussion in 'Perl Misc' started by PerlFAQ Server, Apr 8, 2011.

  1. This is an excerpt from the latest version perlfaq8.pod, which
    comes with the standard Perl distribution. These postings aim to
    reduce the number of repeated questions as well as allow the community
    to review and update the answers. The latest version of the complete
    perlfaq is at http://faq.perl.org .

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

    8.13: How do I trap control characters/signals?

    You don't actually "trap" a control character. Instead, that character
    generates a signal which is sent to your terminal's currently
    foregrounded process group, which you then trap in your process. Signals
    are documented in "Signals" in perlipc and the section on "Signals" in
    the Camel.

    You can set the values of the %SIG hash to be the functions you want to
    handle the signal. After perl catches the signal, it looks in %SIG for a
    key with the same name as the signal, then calls the subroutine value
    for that key.

    # as an anonymous subroutine

    $SIG{INT} = sub { syswrite(STDERR, "ouch\n", 5 ) };

    # or a reference to a function

    $SIG{INT} = \&ouch;

    # or the name of the function as a string

    $SIG{INT} = "ouch";

    Perl versions before 5.8 had in its C source code signal handlers which
    would catch the signal and possibly run a Perl function that you had set
    in %SIG. This violated the rules of signal handling at that level
    causing perl to dump core. Since version 5.8.0, perl looks at %SIG after
    the signal has been caught, rather than while it is being caught.
    Previous versions of this answer were incorrect.



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

    The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
    are not necessarily experts in every domain where Perl might show up,
    so please include as much information as possible and relevant in any
    corrections. The perlfaq-workers also don't have access to every
    operating system or platform, so please include relevant details for
    corrections to examples that do not work on particular platforms.
    Working code is greatly appreciated.

    If you'd like to help maintain the perlfaq, see the details in
    perlfaq.pod.
     
    PerlFAQ Server, Apr 8, 2011
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. marc-andr?

    Trap DataGrid in a each control loop

    marc-andr?, Nov 17, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    576
    =?Utf-8?B?TWlrZUw=?=
    Nov 19, 2004
  2. Mikey
    Replies:
    0
    Views:
    460
    Mikey
    May 11, 2006
  3. RHNewBie

    How to trap signals in a shared library

    RHNewBie, Jul 10, 2003, in forum: C Programming
    Replies:
    1
    Views:
    334
    Mark McIntyre
    Jul 10, 2003
  4. TCook
    Replies:
    0
    Views:
    366
    TCook
    Aug 13, 2006
  5. TCook
    Replies:
    0
    Views:
    167
    TCook
    Aug 13, 2006
Loading...

Share This Page