How to print stack trace on error in Perl?
Question
How to print stack trace on error in Perl?
How to Print a Stack Trace on Error in Perl
When debugging Perl code, seeing where an error occurred and the sequence of calls that led to it can be invaluable. A stack trace shows the call stack—subroutines called leading up to the error—helping you locate the root cause more quickly than a simple error message.
Perl provides several ways to capture and print stack traces upon errors. The core Carp module is the most common approach, offering functions like carp, croak, and notably confess, which prints a detailed stack trace when reporting errors.
Using Carp::confess to Print Stack Traces
confess is similar to die but automatically includes the call stack in the error output. This is extremely helpful during debugging as it gives you context on how the program reached the error state.
use strict;
use warnings;
use Carp;
sub level3 {
confess "Something went wrong at level3!";
}
sub level2 {
level3();
}
sub level1 {
level2();
}
eval {
level1();
};
if (my $err = $@) {
print "Caught error:\n$err";
}
In this example:
level1callslevel2callslevel3.level3callsconfess, which prints the error plus the full call stack.- An
evalblock catches the error so the program can print it nicely instead of exiting abruptly.
Running this will produce output something like:
Something went wrong at level3! at ... (stack trace showing calls)
This includes file names, line numbers, and the call hierarchy, making debugging much easier.
Notes on Perl Context and Carp
The stack trace output comes from Carp using Perl's internal facilities to walk the call stack. Since Perl uses different sigils ($, @, %) to denote scalar, array, and hash variables, the printed trace also helps identify where variables were used or passed within the subroutines.
Perl's philosophy of "There’s more than one way to do it" (TMTOWTDI) means there are alternative ways to get a stack trace, such as using modules like Devel::StackTrace or Devel::Confess (which enhances Carp by replacing die with stack traces), but Carp is typically available in core and sufficient for many debugging tasks.
Common Gotchas
- eval and $@ handling: If you use
evalto catch errors, remember to check and localize$@properly. Intervening code might overwrite it. - Output buffering: When printing stack traces, if using buffered output, the error text may not appear immediately. Use
$| = 1;to autoflush STDOUT if needed. - Minimal stack traces:
confessshows the full call stack including internal Perl calls, which can be noisy. Consider filtering or using other modules for customized traces. - Performance: Printing stack traces on every error can incur overhead. Use them mainly in development or debugging mode.
Summary
To print a stack trace on error in Perl, import Carp and replace die with confess. This will show a detailed call stack at the point of failure, greatly aiding debugging. Wrap calls in an eval block if you want to catch and handle these errors gracefully.
Verified Code
Executed in a sandbox to capture real output. • v5.34.1 • 26ms
Caught error:
Something went wrong at level3! at - line 6.
main::level3() called at - line 10
main::level2() called at - line 14
main::level1() called at - line 18
eval {...} called at - line 17
(empty)