icon Top 9 categories map      RocketAware > Perl >

$SIG{expr}

Tips: Browse or Search all pages for efficient awareness of Perl functions, operators, and FAQs.



Home

Search Perl pages


Subjects

By activity
Professions, Sciences, Humanities, Business, ...

User Interface
Text-based, GUI, Audio, Video, Keyboards, Mouse, Images,...

Text Strings
Conversions, tests, processing, manipulation,...

Math
Integer, Floating point, Matrix, Statistics, Boolean, ...

Processing
Algorithms, Memory, Process control, Debugging, ...

Stored Data
Data storage, Integrity, Encryption, Compression, ...

Communications
Networks, protocols, Interprocess, Remote, Client Server, ...

Hard World
Timing, Calendar and Clock, Audio, Video, Printer, Controls...

File System
Management, Filtering, File & Directory access, Viewers, ...

    
$SIG{expr}
The hash %SIG is used to set signal handlers for various signals. Example:

    sub handler {       # 1st argument is signal name
        local($sig) = @_;
        print "Caught a SIG$sig--shutting down\n";
        close(LOG);
        exit(0);
    }

    $SIG{'INT'} = 'handler';
    $SIG{'QUIT'} = 'handler';
    ...
    $SIG{'INT'} = 'DEFAULT';    # restore default action
    $SIG{'QUIT'} = 'IGNORE';    # ignore SIGQUIT

The %SIG array contains values for only the signals actually set within the Perl script. Here are some other examples:

    $SIG{PIPE} = Plumber;       # SCARY!!
    $SIG{"PIPE"} = "Plumber";   # just fine, assumes main::Plumber
    $SIG{"PIPE"} = \&Plumber;   # just fine; assume current Plumber
    $SIG{"PIPE"} = Plumber();   # oops, what did Plumber() return??

The one marked scary is problematic because it's a bareword, which means sometimes it's a string representing the function, and sometimes it's going to call the subroutine call right then and there! Best to be sure and quote it or take a reference to it. *Plumber works too. See the perlsub manpage.

If your system has the sigaction() function then signal handlers are installed using it. This means you get reliable signal handling. If your system has the SA_RESTART flag it is used when signals handlers are installed. This means that system calls for which it is supported continue rather than returning when a signal arrives. If you want your system calls to be interrupted by signal delivery then do something like this:

    use POSIX ':signal_h';

    my $alarm = 0;
    sigaction SIGALRM, new POSIX::SigAction sub { $alarm = 1 }
        or die "Error setting SIGALRM handler: $!\n";

See POSIX.

Certain internal hooks can be also set using the %SIG hash. The routine indicated by $SIG{__WARN__} is called when a warning message is about to be printed. The warning message is passed as the first argument. The presence of a __WARN__ hook causes the ordinary printing of warnings to STDERR to be suppressed. You can use this to save warnings in a variable, or turn warnings into fatal errors, like this:

    local $SIG{__WARN__} = sub { die $_[0] };
    eval $proggie;

The routine indicated by $SIG{__DIE__} is called when a fatal exception is about to be thrown. The error message is passed as the first argument. When a __DIE__ hook routine returns, the exception processing continues as it would have in the absence of the hook, unless the hook routine itself exits via a goto, a loop exit, or a die(). The __DIE__ handler is explicitly disabled during the call, so that you can die from a __DIE__ handler. Similarly for __WARN__. See die, warn and eval.

Source: Perl predefined variables
Copyright: Larry Wall, et al.
Next: $^M

Previous: $ENV{expr}



(Corrections, notes, and links courtesy of RocketAware.com)


[Overview Topics]

Up to: Process Signals and Events




Rapid-Links: Search | About | Comments | Submit Path: RocketAware > Perl > perlvar/_SIG_expr_.htm
RocketAware.com is a service of Mib Software
Copyright 2000, Forrest J. Cavalier III. All Rights Reserved.
We welcome submissions and comments