SPLAIN(1perl)

πŸ“¦
version 5.44.0 Β· 2 versions
, in arch rolling

✨ NAME ✨

okay bestie, so this is SPLAIN(1perl), and it lives in the Perl Programmers Reference Guide πŸ’– it's all about diagnostics and splain β€” two icons whose whole job is producing verbose warning diagnostics πŸŽ€


🌸 SYNOPSIS 🌸

First up, using the diagnostics pragma:

use diagnostics;
use diagnostics -verbose;

diagnostics->enable;
diagnostics->disable;

Then using the splain standalone filter program:

perl program 2>diag.out
splain [-v] [-p] diag.out

And using diagnostics to get stack traces from a misbehaving script πŸ‘€

perl -Mdiagnostics=-traceonly my_script.pl

πŸ’… DESCRIPTION πŸ’…

πŸ¦‹ The diagnostics Pragma πŸ¦‹

So this module takes the terse diagnostics that the perl compiler and the perl interpreter normally emit (like when you run perl with a -w switch or say use warnings) and it glows them up ✨ β€” augmenting them with the more explicative and endearing descriptions found in perldiag. And just like the other pragmata, it hits the compilation phase of your program, not just execution. main character behavior fr fr πŸ‘‘

To use it in your program as a pragma, literally just invoke:

use diagnostics;

at the start (or near the start) of your program. Heads up tho β€” this does enable perl's -w flag. Your whole compilation will then be subject(ed :-) to the enhanced diagnostics. And these still go out STDERR πŸ’–

Now, because of the 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 πŸ™ˆ However, you can control their behaviour at runtime using the disable() and enable() methods to turn them off and on respectively. slay.

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 reruns here πŸ’… User code generated warnings a la warn() are unaffected, so duplicate user messages can still show up.

This module also adds a stack trace to the error message when perl dies 😭 which is lowkey so 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 dieing, 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. iconic.

🌈 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 πŸ‘‘ 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 β€” not applicable bestie.

Oh and btw: 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 (we love a chaotic test file):

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 prefer to run your program first and look at its problem 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 πŸ™ˆ oops.

If you don't want to modify your source code, but still have 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. 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 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 (the module's $SIG{__WARN__} interceptor) has had its way with your warnings πŸ’–

There's also 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 🌸


πŸ‘‘ AUTHOR πŸ‘‘

Tom Christiansen <tchrist@mox.perl.com>, 25 June 1995.


and that's the whole page bestie, no cap 🫢 this was perl v5.44.0, dated 2026-10-17, straight out of the Perl Programmers Reference Guide β€” mwah, periodt πŸ’…βœ¨

πŸ“¦ 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