diagnostics(3perl)
π diagnostics(3perl) β Perl Programmers Reference Guide π
okay bestie, gather round, because this is the diagnostics page (that's diagnostics(3perl) if you're being formal, which we are not) and it is genuinely one of the slay-est little corners of perl. it's giving "your warnings but make them make sense". let's go π«Ά
β¨ NAME β¨
diagnostics, splain β produce verbose warning diagnostics
π SYNOPSIS π
Using the diagnostics pragma:
use diagnostics;
use diagnostics -verbose;
diagnostics->enable;
diagnostics->disable;
Using the splain standalone filter program:
perl program 2>diag.out
splain [-v] [-p] diag.out
Using diagnostics to get stack traces from a misbehaving script:
perl -Mdiagnostics=-traceonly my_script.pl
π¦ DESCRIPTION π¦
The diagnostics Pragma π
this module takes the terse little diagnostics that perl normally hands you β both from the perl compiler and from the perl interpreter (i.e. when you run perl with a -w switch, or do use warnings) β and glow-ups them with the way more explicative (and honestly endearing) descriptions living over in perldiag. and like all the other pragmata, it hits the compilation phase of your program, not just the execution phase. no cap.
to use it as a pragma in your program, literally just invoke
use diagnostics;
at the start (or near the start) of your program. heads up tho: (Note that this does enable perl's -w flag.) your whole compilation then gets subject(ed :-) to the enhanced diagnostics. and they still go out STDERR, because of course they do.
now, because of the messy interaction between runtime and compiletime issues β and because it's probably not a very good idea anyway β you may not use no diagnostics to turn them off at compiletime. sorry bestie. however, you can control their behaviour at runtime using the disable() and enable() methods, to turn them off and on respectively. iconique.
the -verbose flag first prints out the perldiag introduction before any other diagnostics. and the $diagnostics::PRETTY variable can generate nicer escape sequences for pagers. β¨
warnings dispatched from perl itself (or, more accurately, those that match descriptions found in perldiag) are only displayed once β no duplicate descriptions, we don't do repeats here. but user code generated warnings a la warn() are totally unaffected, which allows duplicate user messages to be displayed. the people's warning stays.
this module also adds a stack trace to the error message when perl dies. super useful for pinpointing what caused the death. the -traceonly (or just -t) flag turns off the explanations of warning messages, leaving just the stack traces. so if your script is dying, run it again with
perl -Mdiagnostics=-traceonly my_bad_script
to see the call stack at the time of death. π₯ and by supplying the -warntrace (or just -w) flag, any warnings emitted will also come with a stack trace.
The splain Program π
another program, splain, is actually nothing more than a link to the (executable) diagnostics.pm module, as well as a link to the diagnostics.pod documentation. wild, i know.
the -v flag is like the use diagnostics -verbose directive. the -p flag is like the $diagnostics::PRETTY variable. and since you're post-processing with splain, there's no sense in being able to enable() or disable() processing. it's simply not that girl.
output from splain is directed to STDOUT, unlike the pragma.
π EXAMPLES π
the following file is certain to trigger a few errors at both runtime and compiletime:
use diagnostics;
print NOWHERE "nothing\n";
print STDERR "\n\tThis message should be unadorned.\n";
warn "\tThis is a user warning";
print "\nDIAGNOSTIC TESTER: Please enter a <CR> here: ";
my $a, $b = scalar <STDIN>;
print "\n";
print $x/$y;
if you'd rather run your program first and then look at its problems afterwards, do this:
perl -w test.pl 2>test.out
./splain < test.out
note that this is not in general possible in shells of more dubious heritage, as the theoretical
(perl -w test.pl >/dev/tty) >& test.out
./splain < test.out
because you just moved the existing stdout to somewhere else. π
if you don't want to modify your source code, but still want on-the-fly warnings, do this:
exec 3>&1; perl -w test.pl 2>&1 1>&3 3>&- | splain 1>&2 3>&-
nifty, eh? πΈ
if you want to control warnings on the fly, do something like this. and make sure you do the use first, or you won't be able to get at the enable() or disable() methods.
use diagnostics; # checks entire compilation phase
print "\ntime for 1st bogus diags: SQUAWKINGS\n";
print BOGUS1 'nada';
print "done with 1st bogus\n";
diagnostics->disable; # only turns off runtime warnings
print "\ntime for 2nd bogus: (squelched)\n";
print BOGUS2 'nada';
print "done with 2nd bogus\n";
diagnostics->enable; # turns back on runtime warnings
print "\ntime for 3rd bogus: SQUAWKINGS\n";
print BOGUS3 'nada';
print "done with 3rd bogus\n";
diagnostics->disable;
print "\ntime for 4th bogus: (squelched)\n";
print BOGUS4 'nada';
print "done with 4th bogus\n";
π INTERNALS π
diagnostic messages derive from the perldiag.pod file when it's available at runtime. otherwise, they may be embedded in the file itself when the splain package is built. see the Makefile for details. π§
if an extant $SIG{__WARN__} handler is discovered, it will continue to be honored β but only after the diagnostics::splainthis() function (that's the module's $SIG{__WARN__} interceptor) has had its way with your warnings.
there's a $diagnostics::DEBUG variable you may set if you're desperately curious what sorts of things are being intercepted. π
BEGIN { $diagnostics::DEBUG = 1 }
π BUGS π
not being able to say no diagnostics is annoying, but may not be insurmountable.
the -pretty directive is called too late to affect matters. you have to do this instead, and before you load the module:
BEGIN { $diagnostics::PRETTY = 1 }
i could start up faster by delaying compilation until it should be needed, but this gets a panic: top_level when using the pragma form in Perl 5.001e. π«
while it's true that this documentation is somewhat subserious, if you use a program named splain, you should expect a bit of whimsy. and honestly? that's the vibe.
π AUTHOR π
Tom Christiansen <tchrist@mox.perl.com>, 25 June 1995.
and that's the whole page, babe. this came to you courtesy of perl v5.44.0, dated 2026-10-17 β go forth and let your warnings be verbose, your stack traces be tracey, and your splain be as whimsical as advertised. mwah π
β¨
π¦ 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