{
    "mode": "perldoc",
    "parameter": "diagnostics",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/diagnostics/json",
    "generated": "2026-06-14T01:13:49Z",
    "synopsis": "Using the \"diagnostics\" pragma:\nuse diagnostics;\nuse diagnostics -verbose;\nenable  diagnostics;\ndisable diagnostics;\nUsing the \"splain\" standalone filter program:\nperl program 2>diag.out\nsplain [-v] [-p] diag.out\nUsing diagnostics to get stack traces from a misbehaving script:\nperl -Mdiagnostics=-traceonly myscript.pl",
    "sections": {
        "NAME": {
            "content": "diagnostics, splain - produce verbose warning diagnostics\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "Using the \"diagnostics\" pragma:\n\nuse diagnostics;\nuse diagnostics -verbose;\n\nenable  diagnostics;\ndisable diagnostics;\n\nUsing the \"splain\" standalone filter program:\n\nperl program 2>diag.out\nsplain [-v] [-p] diag.out\n\nUsing diagnostics to get stack traces from a misbehaving script:\n\nperl -Mdiagnostics=-traceonly myscript.pl\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "The \"diagnostics\" Pragma\nThis module extends the terse diagnostics normally emitted by both the perl compiler and the\nperl interpreter (from running perl with a -w switch or \"use warnings\"), augmenting them with\nthe more explicative and endearing descriptions found in perldiag. Like the other pragmata, it\naffects the compilation phase of your program rather than merely the execution phase.\n\nTo use in your program as a pragma, merely invoke\n\nuse diagnostics;\n\nat the start (or near the start) of your program. (Note that this *does* enable perl's -w flag.)\nYour whole compilation will then be subject(ed :-) to the enhanced diagnostics. These still go\nout STDERR.\n\nDue to the interaction between runtime and compiletime issues, and because it's probably not a\nvery good idea anyway, you may not use \"no diagnostics\" to turn them off at compiletime.\nHowever, you may control their behaviour at runtime using the disable() and enable() methods to\nturn them off and on respectively.\n\nThe -verbose flag first prints out the perldiag introduction before any other diagnostics. The\n$diagnostics::PRETTY variable can generate nicer escape sequences for pagers.\n\nWarnings dispatched from perl itself (or more accurately, those that match descriptions found in\nperldiag) are only displayed once (no duplicate descriptions). User code generated warnings a la",
            "subsections": [
                {
                    "name": "warn",
                    "content": "This module also adds a stack trace to the error message when perl dies. This is useful for\npinpointing what caused the death. The -traceonly (or just -t) flag turns off the explanations\nof warning messages leaving just the stack traces. So if your script is dieing, run it again\nwith\n\nperl -Mdiagnostics=-traceonly mybadscript\n\nto see the call stack at the time of death. By supplying the -warntrace (or just -w) flag, any\nwarnings emitted will also come with a stack trace.\n\nThe *splain* Program\nWhile apparently a whole nuther program, *splain* is actually nothing more than a link to the\n(executable) diagnostics.pm module, as well as a link to the diagnostics.pod documentation. The\n-v flag is like the \"use diagnostics -verbose\" directive. The -p flag is like the\n$diagnostics::PRETTY variable. Since you're post-processing with *splain*, there's no sense in\nbeing able to enable() or disable() processing.\n\nOutput from *splain* is directed to STDOUT, unlike the pragma.\n"
                }
            ]
        },
        "EXAMPLES": {
            "content": "The following file is certain to trigger a few errors at both runtime and compiletime:\n\nuse diagnostics;\nprint NOWHERE \"nothing\\n\";\nprint STDERR \"\\n\\tThis message should be unadorned.\\n\";\nwarn \"\\tThis is a user warning\";\nprint \"\\nDIAGNOSTIC TESTER: Please enter a <CR> here: \";\nmy $a, $b = scalar <STDIN>;\nprint \"\\n\";\nprint $x/$y;\n\nIf you prefer to run your program first and look at its problem afterwards, do this:\n\nperl -w test.pl 2>test.out\n./splain < test.out\n\nNote that this is not in general possible in shells of more dubious heritage, as the theoretical\n\n(perl -w test.pl >/dev/tty) >& test.out\n./splain < test.out\n\nBecause you just moved the existing stdout to somewhere else.\n\nIf you don't want to modify your source code, but still have on-the-fly warnings, do this:\n\nexec 3>&1; perl -w test.pl 2>&1 1>&3 3>&- | splain 1>&2 3>&-\n\nNifty, eh?\n\nIf you want to control warnings on the fly, do something like this. Make sure you do the \"use\"\nfirst, or you won't be able to get at the enable() or disable() methods.\n\nuse diagnostics; # checks entire compilation phase\nprint \"\\ntime for 1st bogus diags: SQUAWKINGS\\n\";\nprint BOGUS1 'nada';\nprint \"done with 1st bogus\\n\";\n\ndisable diagnostics; # only turns off runtime warnings\nprint \"\\ntime for 2nd bogus: (squelched)\\n\";\nprint BOGUS2 'nada';\nprint \"done with 2nd bogus\\n\";\n\nenable diagnostics; # turns back on runtime warnings\nprint \"\\ntime for 3rd bogus: SQUAWKINGS\\n\";\nprint BOGUS3 'nada';\nprint \"done with 3rd bogus\\n\";\n\ndisable diagnostics;\nprint \"\\ntime for 4th bogus: (squelched)\\n\";\nprint BOGUS4 'nada';\nprint \"done with 4th bogus\\n\";\n",
            "subsections": []
        },
        "INTERNALS": {
            "content": "Diagnostic messages derive from the perldiag.pod file when available at runtime. Otherwise, they\nmay be embedded in the file itself when the splain package is built. See the Makefile for\ndetails.\n\nIf an extant $SIG{WARN} handler is discovered, it will continue to be honored, but only\nafter the diagnostics::splainthis() function (the module's $SIG{WARN} interceptor) has had\nits way with your warnings.\n\nThere is a $diagnostics::DEBUG variable you may set if you're desperately curious what sorts of\nthings are being intercepted.\n\nBEGIN { $diagnostics::DEBUG = 1 }\n",
            "subsections": []
        },
        "BUGS": {
            "content": "Not being able to say \"no diagnostics\" is annoying, but may not be insurmountable.\n\nThe \"-pretty\" directive is called too late to affect matters. You have to do this instead, and\n*before* you load the module.\n\nBEGIN { $diagnostics::PRETTY = 1 }\n\nI could start up faster by delaying compilation until it should be needed, but this gets a\n\"panic: toplevel\" when using the pragma form in Perl 5.001e.\n\nWhile it's true that this documentation is somewhat subserious, if you use a program named\n*splain*, you should expect a bit of whimsy.\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "Tom Christiansen <tchrist@mox.perl.com>, 25 June 1995.\n",
            "subsections": []
        }
    },
    "summary": "diagnostics, splain - produce verbose warning diagnostics",
    "flags": [],
    "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 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.",
        "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\";",
        "disable diagnostics; # only turns off runtime warnings",
        "print \"\\ntime for 2nd bogus: (squelched)\\n\";",
        "print BOGUS2 'nada';",
        "print \"done with 2nd bogus\\n\";",
        "enable diagnostics; # turns back on runtime warnings",
        "print \"\\ntime for 3rd bogus: SQUAWKINGS\\n\";",
        "print BOGUS3 'nada';",
        "print \"done with 3rd bogus\\n\";",
        "disable diagnostics;",
        "print \"\\ntime for 4th bogus: (squelched)\\n\";",
        "print BOGUS4 'nada';",
        "print \"done with 4th bogus\\n\";"
    ],
    "see_also": []
}