Skip to content

Perl Signal Handling Guide — %SIG, Signal Traps, and Process Communication

DodaTech Updated 2026-06-28 2 min read

In this tutorial, you will learn about Perl Signal Handling Guide. We cover key concepts, practical examples, and best practices to help you master this topic.

Perl signal handling assigns coderefs to keys in %SIG for signal trapping -- enabling graceful cleanup on INT/TERM, custom handlers for USR1/USR2, and safe deferred signals using POSIX::SigAction to avoid unsafe operations inside handlers.

Basic Signal Handling

# Trap Ctrl+C (INT)
$SIG{INT} = sub {
    print "Caught interrupt, cleaning up...\n";
    # cleanup code here
    exit(1);
};

# Trap terminate (TERM)
$SIG{TERM} = sub {
    print "Shutting down gracefully\n";
    exit(0);
};

Signal Safety

# Avoid unsafe operations in signal handlers
# Safe: setting a flag variable

my $should_exit = 0;
$SIG{INT} = sub { $should_exit = 1 };

while (1) {
    last if $should_exit;
    print "Working...\n";
    sleep(1);
}
print "Exited cleanly\n";

Ignoring Signals

# Ignore SIGPIPE (broken pipe)
$SIG{PIPE} = 'IGNORE';

# Default behavior
$SIG{INT} = 'DEFAULT';

# Custom cleanup with __DIE__
$SIG{__DIE__} = sub {
    print "Program died: @_\n";
    exit(1);
};

Profiling with Signals

# USR1 handler for runtime debugging
$SIG{USR1} = sub {
    print "Memory: " . `ps -o rss= -p $$` . " KB\n";
    print "Working directory: " . `pwd`;
};

# USR2 for toggling debug
my $debug = 0;
$SIG{USR2} = sub { $debug = !$debug };

# Run: kill -USR1 <pid>

Common Mistakes

1. Unsafe operations in handlers

Don't call die, IO operations, or complex logic inside signal handlers. Set a flag and handle it in the main loop.

2. Signal handlers in libraries

Setting $SIG in a module affects the entire program. Document signal changes or localize them.

3. Not restoring default handler

Temporarily set a handler, then restore: local $SIG{INT} = sub { ... } for scope-limited changes.

Practice Questions

1. How do you trap Ctrl+C in Perl? Assign a coderef to $SIG{INT}: $SIG{INT} = sub { print "Caught\n"; exit }.

2. Why should signal handlers be simple? Signals can arrive at any time. Complex operations (I/O, allocation) may cause deadlocks or crashes.

3. How do you restore default signal behavior? Set $SIG{INT} = 'DEFAULT'. For ignore: $SIG{PIPE} = 'IGNORE'.

FAQ

{{< faq question="What is deferred signal handling?" >}} Using POSIX::SigAction with safe_flags => 1 to defer signals to a safe point. Avoids race conditions in handlers. {{< /faq >}}

{{< faq question="Can I send signals from Perl?" >}} Yes: kill('INT', $pid) sends a signal. kill('USR1', $$) sends to self. kill(0, $pid) checks if Process exists. {{< /faq >}}

{{< faq question="What signals cannot be trapped?" >}} SIGKILL (9) and SIGSTOP (17, 19 on some systems) cannot be caught, blocked, or ignored. {{< /faq >}}

What's Next

Now learn about process management in Perl.

Topic Description Link
Processes Process management {{< ref "28-processes" >}}
Memory Memory management {{< ref "30-memory" >}}

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro