<?xml version="1.0" encoding="windows-1252"?>
<node id="1137" title="perlman:lib:diagnostics" created="1999-12-22 19:49:31" updated="2005-08-12 21:28:14">
<type id="119">
perlfunc</type>
<author id="113">
root</author>
<data>
<field name="doctext">
</field>
<field name="name">

&lt;P&gt;
diagnostics - Perl compiler pragma to force verbose warning diagnostics

&lt;P&gt;
splain - standalone program to do the same thing

&lt;P&gt;
&lt;HR&gt;
</field>
<field name="synopsis">

&lt;P&gt;
As a pragma:

&lt;P&gt;
&lt;PRE&gt;    use diagnostics;
    use diagnostics -verbose;
&lt;/PRE&gt;
&lt;P&gt;
&lt;PRE&gt;    enable  diagnostics;
    disable diagnostics;
&lt;/PRE&gt;
&lt;P&gt;
Aa a program:

&lt;P&gt;
&lt;PRE&gt;    perl program 2&amp;gt;diag.out
    splain &amp;#091;-v&amp;#093; &amp;#091;-p&amp;#093; dia</field>
<field name="description">

&lt;P&gt;
&lt;HR&gt;
&lt;H2&gt;&lt;A NAME="The_C_diagnostics_Pragma"&gt;The &lt;CODE&gt;diagnostics&lt;/CODE&gt; Pragma&lt;/A&gt;&lt;/H2&gt;
&lt;P&gt;
This module extends the terse diagnostics normally emitted by both the perl
compiler and the perl interpeter, augmenting them with the more explicative
and endearing descriptions found in [perlman:perldiag|perldiag]. Like the other pragmata, it affects the compilation phase of your program
rather than merely the execution phase.

&lt;P&gt;
To use in your program as a pragma, merely invoke

&lt;P&gt;
&lt;PRE&gt;    use diagnostics;
&lt;/PRE&gt;
&lt;P&gt;
at the start (or near the start) of your program. (Note that this &lt;EM&gt;does&lt;/EM&gt; enable perl's &lt;STRONG&gt;-w&lt;/STRONG&gt; flag.) Your whole compilation will then be 
&lt;CODE&gt;subject(ed&lt;/CODE&gt; :-) to the
enhanced diagnostics. These still go out &lt;STRONG&gt;STDERR&lt;/STRONG&gt;.

&lt;P&gt;
Due to the interaction between runtime and compiletime issues, and because
it's probably not a very good idea anyway, you may not use &lt;CODE&gt;no diagnostics&lt;/CODE&gt; to turn them off at compiletime. However, you may control there behaviour at runtime using the 
&lt;CODE&gt;disable()&lt;/CODE&gt; and 
&lt;CODE&gt;enable()&lt;/CODE&gt; methods to turn them off and on respectively.

&lt;P&gt;
The &lt;STRONG&gt;-verbose&lt;/STRONG&gt; flag first prints out the [perlman:perldiag|perldiag] introduction before any other diagnostics. The $diagnostics::PRETTY
variable can generate nicer escape sequences for pagers.

&lt;P&gt;
&lt;HR&gt;
&lt;H2&gt;&lt;A NAME="The_I_splain_Program"&gt;The &lt;EM&gt;splain&lt;/EM&gt; Program&lt;/A&gt;&lt;/H2&gt;
&lt;P&gt;
While apparently a whole nuther program, &lt;EM&gt;splain&lt;/EM&gt; is actually nothing more than a link to the (executable) &lt;EM&gt;diagnostics.pm&lt;/EM&gt; module, as well as a link to the &lt;EM&gt;diagnostics.pod&lt;/EM&gt; documentation. The &lt;STRONG&gt;-v&lt;/STRONG&gt; flag is like the &lt;CODE&gt;use diagnostics -verbose&lt;/CODE&gt; directive. The &lt;STRONG&gt;-p&lt;/STRONG&gt; flag is like the $diagnostics::PRETTY variable. Since you're
post-processing with 
&lt;EM&gt;splain&lt;/EM&gt;, there's no sense in being able to 
&lt;CODE&gt;enable()&lt;/CODE&gt; or 
&lt;CODE&gt;disable()&lt;/CODE&gt; processing.

&lt;P&gt;
Output from &lt;EM&gt;splain&lt;/EM&gt; is directed to &lt;STRONG&gt;STDOUT&lt;/STRONG&gt;, unlike the pragma.

&lt;P&gt;
&lt;HR&gt;
&lt;H1&gt;&lt;A NAME="EXAMPLES"&gt;EXAMPLES&lt;/A&gt;&lt;/H1&gt;
&lt;P&gt;
The following file is certain to trigger a few errors at both runtime and
compiletime:

&lt;P&gt;
&lt;PRE&gt;    use diagnostics;
    print NOWHERE &amp;quot;nothing\n&amp;quot;;
    print STDERR &amp;quot;\n\tThis message should be unadorned.\n&amp;quot;;
    warn &amp;quot;\tThis is a user warning&amp;quot;;
    print &amp;quot;\nDIAGNOSTIC TESTER: Please enter a &amp;lt;CR&amp;gt; here: &amp;quot;;
    my $a, $b = scalar &amp;lt;STDIN&amp;gt;;
    print &amp;quot;\n&amp;quot;;
    print $x/$y;
&lt;/PRE&gt;
&lt;P&gt;
If you prefer to run your program first and look at its problem afterwards,
do this:

&lt;P&gt;
&lt;PRE&gt;    perl -w test.pl 2&amp;gt;test.out
    ./splain &amp;lt; test.out
&lt;/PRE&gt;
&lt;P&gt;
Note that this is not in general possible in shells of more dubious
heritage, as the theoretical 

&lt;P&gt;
&lt;PRE&gt;    (perl -w test.pl &amp;gt;/dev/tty) &amp;gt;&amp;amp; test.out
    ./splain &amp;lt; test.out
&lt;/PRE&gt;
&lt;P&gt;
Because you just moved the existing &lt;STRONG&gt;stdout&lt;/STRONG&gt; to somewhere else.

&lt;P&gt;
If you don't want to modify your source code, but still have on-the-fly
warnings, do this:

&lt;P&gt;
&lt;PRE&gt;    exec 3&amp;gt;&amp;amp;1; perl -w test.pl 2&amp;gt;&amp;amp;1 1&amp;gt;&amp;amp;3 3&amp;gt;&amp;amp;- | splain 1&amp;gt;&amp;amp;2 3&amp;gt;&amp;amp;- 
&lt;/PRE&gt;
&lt;P&gt;
Nifty, eh?

&lt;P&gt;
If you want to control warnings on the fly, do something like this. Make
sure you do the [perlfunc:use|use] first, or you won't be able to get at the 
&lt;CODE&gt;enable()&lt;/CODE&gt; or 
&lt;CODE&gt;disable()&lt;/CODE&gt; methods.

&lt;P&gt;
&lt;PRE&gt;    use diagnostics; # checks entire compilation phase 
        print &amp;quot;\ntime for 1st bogus diags: SQUAWKINGS\n&amp;quot;;
        print BOGUS1 'nada';
        print &amp;quot;done with 1st bogus\n&amp;quot;;
&lt;/PRE&gt;
&lt;P&gt;
&lt;PRE&gt;    disable diagnostics; # only turns off runtime warnings
        print &amp;quot;\ntime for 2nd bogus: (squelched)\n&amp;quot;;
        print BOGUS2 'nada';
        print &amp;quot;done with 2nd bogus\n&amp;quot;;
&lt;/PRE&gt;
&lt;P&gt;
&lt;PRE&gt;    enable diagnostics; # turns back on runtime warnings
        print &amp;quot;\ntime for 3rd bogus: SQUAWKINGS\n&amp;quot;;
        print BOGUS3 'nada';
        print &amp;quot;done with 3rd bogus\n&amp;quot;;
&lt;/PRE&gt;
&lt;P&gt;
&lt;PRE&gt;    disable diagnostics;
        print &amp;quot;\ntime for 4th bogus: (squelched)\n&amp;quot;;
        print BOGUS4 'nada';
        print &amp;quot;done with 4th bogus\n&amp;quot;;
&lt;/PRE&gt;
&lt;P&gt;
&lt;HR&gt;
&lt;H1&gt;&lt;A NAME="INTERNALS"&gt;INTERNALS&lt;/A&gt;&lt;/H1&gt;
&lt;P&gt;
Diagnostic messages derive from the &lt;EM&gt;perldiag.pod&lt;/EM&gt; file when available at runtime. Otherwise, they may be embedded in the file
itself when the splain package is built. See the &lt;EM&gt;Makefile&lt;/EM&gt; for details.

&lt;P&gt;
If an extant 
&lt;FONT SIZE=-1&gt;$SIG{__WARN__}&lt;/FONT&gt; handler is discovered, it will continue to be honored, but only after the diagnostics::splainthis() function (the module's 
&lt;FONT SIZE=-1&gt;$SIG{__WARN__}&lt;/FONT&gt; interceptor) has had its way with your warnings.

&lt;P&gt;
There is a $diagnostics::DEBUG variable you may set if you're desperately
curious what sorts of things are being intercepted.

&lt;P&gt;
&lt;PRE&gt;    BEGIN { $diagnostics::DEBUG = 1 } 
&lt;/PRE&gt;
&lt;P&gt;
&lt;HR&gt;
&lt;H1&gt;&lt;A NAME="BUGS"&gt;BUGS&lt;/A&gt;&lt;/H1&gt;
&lt;P&gt;
Not being able to say ``no diagnostics'' is annoying, but may not be
insurmountable.

&lt;P&gt;
The &lt;CODE&gt;-pretty&lt;/CODE&gt; directive is called too late to affect matters. You have to do this
instead, and &lt;EM&gt;before&lt;/EM&gt; you load the module.

&lt;P&gt;
&lt;PRE&gt;    BEGIN { $diagnostics::PRETTY = 1 } 
&lt;/PRE&gt;
&lt;P&gt;

&lt;FONT SIZE=-1&gt;I&lt;/FONT&gt; 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.

&lt;P&gt;
While it's true that this documentation is somewhat subserious, if you use
a program named &lt;EM&gt;splain&lt;/EM&gt;, you should expect a bit of whimsy.

&lt;P&gt;
&lt;HR&gt;
&lt;H1&gt;&lt;A NAME="AUTHOR"&gt;AUTHOR&lt;/A&gt;&lt;/H1&gt;
&lt;P&gt;
Tom Christiansen &amp;lt;&lt;EM&gt;tchrist@mox.perl.com&lt;/EM&gt;&amp;gt;, 25 June 1995.

&lt;HR&gt;
</field>
</data>
</node>
