Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Any reference about %SIG

by exilepanda (Friar)
on Jan 28, 2023 at 08:56 UTC ( [id://11149972]=perlquestion: print w/replies, xml ) Need Help??

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

I want to study more on SIGNAL. But after half hour digging, there's nothing useful. I am looking some full documentation that explains

$SIG{__DIE__} will fire when die() happen
$SIG{INT} will fire when Ctrl-C pressed
...

Is that anything like this exists?

Replies are listed 'Best First'.
Re: Any reference about %SIG
by haukex (Archbishop) on Jan 28, 2023 at 08:58 UTC

    Perl's special variables are documented in perlvar: %SIG

      Thank you but this is only give few examples. I am looking for a document explains all the signals' meaning and trigger.
        I am looking for a document explains all the signals' meaning and trigger.

        The signals special to Perl, like __WARN__ and __DIE__, are documented in %SIG, the other signals are generally POSIX signals, which you can read about for example in signal(7) like here and here, or in Signal (IPC). Note that a lot of signals may be OS-specific. If you could tell us what you want to use them for (and on which platforms), perhaps we can give more specific advice.

Re: Any reference about %SIG -- windows
by Discipulus (Canon) on Jan 28, 2023 at 20:03 UTC
    Hello exilepanda,

    signals on windows are well summariazed here.

    As far as I remember and also glancing the above module comment INT,QUIT,BREAK are handled in windows so you can use BREAK to do something else than die, I must have written some program using that signal to partially report a long running job status.

    See also Catch-and-Handle-Signals-in-Perl by David Farrel (not windows specific tho).

    Then Signals vs. Windows SIGHUP delivered on Windows Strawberry Perl and alarm() on Windows and sending signal to self with kill on windows. Recently I used $SIG{INT} and also some link in my Bibliotheca

    A simple example to start: I run this infinite loop and pressed 3 times CTRL-BREAK and then CTRL-C to terminate it.

    use strict; use warnings; my $growing_number; # Handle Ctrl-C $SIG{INT} = sub{ print "INT: last value of \$growing_number is: $growing_number\nTe +rminating..\n"; exit; }; # Handle Ctrl-Break $SIG{BREAK} = sub{ print "BREAK: Currently \$growing_number is: $growing_number\n"; }; while (1){ $growing_number += time; sleep 1; } __END__ BREAK: Currently $growing_number is: 10049615223 BREAK: Currently $growing_number is: 18424294603 BREAK: Currently $growing_number is: 23449102243 INT: last value of $growing_number is: 28473909892 Terminating..

    L*

    PS note also that if you put sleep 60 then the OS in question catch the signal every 60 seconds.. not so good. You can workaround it using sleep 1 for 1..60

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: Any reference about %SIG
by eyepopslikeamosquito (Archbishop) on Jan 28, 2023 at 23:39 UTC

    Since you seem to be asking a fairly general question about signals, I would advise you to avoid them as much as you possibly can, especially on Windows.

    As for why, this long discussion thread includes a lot of historical detail on the personalities and politics behind early Windows development, especially signals and the Windows POSIX subsystem.

    Update: to further clarify, signals should be avoided on Windows, where they are not a native IPC mechanism. Though there is no avoiding them on Unix (where they are native) you must take great care when writing C signal handlers to ensure they are async-signal-safe because they tend to crash and malfunction in strange ways if not (as noted here recently :). Disclaimer: though I've used signals in Unix C code, I've hardly used them in Perl. You might like to search for "signal" in this long technical node from the inimitable BrowserUk describing his experiences with signals on Windows, where he noted "their use in multi-threaded Perl code is very confused".

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11149972]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-19 22:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found