sigtrap(3perl)
π sigtrap β the Perl pragma from the Perl Programmers Reference Guide π
okay bestie, so this is sigtrap(3perl), and no cap, it's genuinely the easiest way to do signal handling in Perl. we're talking simple. we're talking cute little pragma energy. let's get into it π«Ά
β¨ NAME β¨
sigtrap β a Perl pragma that turns on simple signal handling, like, without you having to hand-write a single %SIG assignment.
πΈ SYNOPSIS πΈ
use sigtrap;
use sigtrap qw(stack-trace old-interface-signals); # equivalent
use sigtrap qw(BUS SEGV PIPE ABRT);
use sigtrap qw(die INT QUIT);
use sigtrap qw(die normal-signals);
use sigtrap qw(die untrapped normal-signals);
use sigtrap qw(die untrapped normal-signals
stack-trace any error-signals);
use sigtrap 'handler' => \&my_handler, 'normal-signals';
use sigtrap qw(handler my_handler normal-signals
stack-trace error-signals);
look at that, use sigtrap; on its own and you're already cookin' π
π DESCRIPTION π
so the sigtrap pragma is basically a lil baby interface for installing signal handlers. you've got options, bestie:
- let it install one of the two handlers it ships with itself β one that gives you a Perl stack trace π§, and one that just plain
die()s π - OR supply your own handler and have sigtrap install that instead
and here's the fun part: you can tell it to install a handler only for signals that are either untrapped or ignored. like, "don't touch what's already handled, i'm not trying to be messy π"
it has three lists of signals ready to go, plus you can absolutely pass your own list. main character energy, truly.
the arguments you pass to the use statement that invokes sigtrap are processed in order. so:
- when a signal name or the name of one of sigtrap's signal lists comes up β a handler gets installed immediately β‘
- when an option comes up β it affects all the handlers installed after it
order matters, periodt.
π OPTIONS π
π SIGNAL HANDLERS
these options decide which handler gets used for the signals installed after them.
stack-traceβ the handler used for subsequently installed signals writes a Perl stack trace toSTDERRand then tries to dump core. this is the default signal handler, iykyk.dieβ the handler used for subsequently installed signals callsdie(well, actuallycroak) with a message telling you which signal got caught. dramatic AND informative.handler your-handlerβyour-handlerbecomes the handler for subsequently installed signals.your-handlercan be any value that's valid as an assignment to an element of%SIG. wanna see examples of handler functions? go look at perlvar, bestie.
π¦ SIGNAL LISTS
sigtrap comes with a few built-in lists of signals to trap. here they are:
normal-signalsβ the signals a program might normally expect to run into, and which by default cause it to terminate. they areHUP,INT,PIPEandTERM. very "this is just life" of them πerror-signalsβ these ones usually mean something has gone seriously wrong with the Perl interpreter or with your script. they areABRT,BUS,EMT,FPE,ILL,QUIT,SEGV,SYSandTRAP. πold-interface-signalsβ the signals that the old sigtrap interface trapped by default. they areABRT,BUS,EMT,FPE,ILL,PIPE,QUIT,SEGV,SYS,TERM, andTRAP. and here's the tea: if you pass sigtrap no signals and no signal lists at all, this is the list it uses. no notes.
β οΈ for each of these three lists, the collection of signals set to be trapped is checked before trapping. so if your architecture doesn't implement a particular signal, it will not be trapped β it'll just be silently ignored. no error, no drama, it just doesn't happen π
β OTHER
untrappedβ this token tells sigtrap to install handlers only for the signals listed after it which aren't already trapped or ignored. respect the boundaries πanyβ this token tells sigtrap to install handlers for all signals listed after it. this is the default behavior.signalβ any argument that looks like a signal name (that is, matching the regex/^[A-Z][A-Z0-9]*$/) means sigtrap should install a handler for that name. yes bestie, that's the whole check, look at the regex.numberβ requires that at least version number of sigtrap is being used. vibe check on the version fr fr.
π₯ EXAMPLES π₯
give a stack trace for the old-interface-signals:
use sigtrap;
ditto (same thing, just spelled out):
use sigtrap qw(stack-trace old-interface-signals);
give a stack trace on the 4 listed signals only:
use sigtrap qw(BUS SEGV PIPE ABRT);
die on INT or QUIT:
use sigtrap qw(die INT QUIT);
die on HUP, INT, PIPE or TERM:
use sigtrap qw(die normal-signals);
die on HUP, INT, PIPE or TERM β except don't change the behavior for signals that are already trapped or ignored:
use sigtrap qw(die untrapped normal-signals);
die on receipt of any of the normal-signals that's currently untrapped, and provide a stack trace on receipt of any of the error-signals:
use sigtrap qw(die untrapped normal-signals
stack-trace any error-signals);
install my_handler() as the handler for the normal-signals:
use sigtrap 'handler', \&my_handler, 'normal-signals';
install my_handler() as the handler for the normal-signals, and provide a Perl stack trace on receipt of one of the error-signals:
use sigtrap qw(handler my_handler normal-signals
stack-trace error-signals);
π SEE ALSO π
okay so, technically this page didn't hand me a SEE ALSO section, so i'm not gonna invent one β but the DESCRIPTION does point you at perlvar for examples of handler functions, so go peek at that if you want the full handler inspo π
and that's the whole thing, bestie! you ate that π
this was sigtrap(3perl) from perl v5.44.0, dated 2026-10-17. iconic behavior, honestly πβ¨π
π¦ where this page comes from
- arch rolling: perl 5.44.0-1 Β· Artistic-1.0-Perl AND GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms