1diagnostics(3pm) Perl Programmers Reference Guide diagnostics(3pm)
2
3
4
6 diagnostics, splain - produce verbose warning diagnostics
7
9 Using the "diagnostics" pragma:
10
11 use diagnostics;
12 use diagnostics -verbose;
13
14 enable diagnostics;
15 disable diagnostics;
16
17 Using the "splain" standalone filter program:
18
19 perl program 2>diag.out
20 splain [-v] [-p] diag.out
21
22 Using diagnostics to get stack traces from a misbehaving script:
23
24 perl -Mdiagnostics=-traceonly my_script.pl
25
27 The "diagnostics" Pragma
28 This module extends the terse diagnostics normally emitted by both the
29 perl compiler and the perl interpreter (from running perl with a -w
30 switch or "use warnings"), augmenting them with the more explicative
31 and endearing descriptions found in perldiag. Like the other pragmata,
32 it affects the compilation phase of your program rather than merely the
33 execution phase.
34
35 To use in your program as a pragma, merely invoke
36
37 use diagnostics;
38
39 at the start (or near the start) of your program. (Note that this does
40 enable perl's -w flag.) Your whole compilation will then be subject(ed
41 :-) to the enhanced diagnostics. These still go out STDERR.
42
43 Due to the interaction between runtime and compiletime issues, and
44 because it's probably not a very good idea anyway, you may not use "no
45 diagnostics" to turn them off at compiletime. However, you may control
46 their behaviour at runtime using the disable() and enable() methods to
47 turn them off and on respectively.
48
49 The -verbose flag first prints out the perldiag introduction before any
50 other diagnostics. The $diagnostics::PRETTY variable can generate
51 nicer escape sequences for pagers.
52
53 Warnings dispatched from perl itself (or more accurately, those that
54 match descriptions found in perldiag) are only displayed once (no
55 duplicate descriptions). User code generated warnings a la warn() are
56 unaffected, allowing duplicate user messages to be displayed.
57
58 This module also adds a stack trace to the error message when perl
59 dies. This is useful for pinpointing what caused the death. The
60 -traceonly (or just -t) flag turns off the explanations of warning
61 messages leaving just the stack traces. So if your script is dieing,
62 run it again with
63
64 perl -Mdiagnostics=-traceonly my_bad_script
65
66 to see the call stack at the time of death. By supplying the -warntrace
67 (or just -w) flag, any warnings emitted will also come with a stack
68 trace.
69
70 The splain Program
71 While apparently a whole nuther program, splain is actually nothing
72 more than a link to the (executable) diagnostics.pm module, as well as
73 a link to the diagnostics.pod documentation. The -v flag is like the
74 "use diagnostics -verbose" directive. The -p flag is like the
75 $diagnostics::PRETTY variable. Since you're post-processing with
76 splain, there's no sense in being able to enable() or disable()
77 processing.
78
79 Output from splain is directed to STDOUT, unlike the pragma.
80
82 The following file is certain to trigger a few errors at both runtime
83 and compiletime:
84
85 use diagnostics;
86 print NOWHERE "nothing\n";
87 print STDERR "\n\tThis message should be unadorned.\n";
88 warn "\tThis is a user warning";
89 print "\nDIAGNOSTIC TESTER: Please enter a <CR> here: ";
90 my $a, $b = scalar <STDIN>;
91 print "\n";
92 print $x/$y;
93
94 If you prefer to run your program first and look at its problem
95 afterwards, do this:
96
97 perl -w test.pl 2>test.out
98 ./splain < test.out
99
100 Note that this is not in general possible in shells of more dubious
101 heritage, as the theoretical
102
103 (perl -w test.pl >/dev/tty) >& test.out
104 ./splain < test.out
105
106 Because you just moved the existing stdout to somewhere else.
107
108 If you don't want to modify your source code, but still have on-the-fly
109 warnings, do this:
110
111 exec 3>&1; perl -w test.pl 2>&1 1>&3 3>&- | splain 1>&2 3>&-
112
113 Nifty, eh?
114
115 If you want to control warnings on the fly, do something like this.
116 Make sure you do the "use" first, or you won't be able to get at the
117 enable() or disable() methods.
118
119 use diagnostics; # checks entire compilation phase
120 print "\ntime for 1st bogus diags: SQUAWKINGS\n";
121 print BOGUS1 'nada';
122 print "done with 1st bogus\n";
123
124 disable diagnostics; # only turns off runtime warnings
125 print "\ntime for 2nd bogus: (squelched)\n";
126 print BOGUS2 'nada';
127 print "done with 2nd bogus\n";
128
129 enable diagnostics; # turns back on runtime warnings
130 print "\ntime for 3rd bogus: SQUAWKINGS\n";
131 print BOGUS3 'nada';
132 print "done with 3rd bogus\n";
133
134 disable diagnostics;
135 print "\ntime for 4th bogus: (squelched)\n";
136 print BOGUS4 'nada';
137 print "done with 4th bogus\n";
138
140 Diagnostic messages derive from the perldiag.pod file when available at
141 runtime. Otherwise, they may be embedded in the file itself when the
142 splain package is built. See the Makefile for details.
143
144 If an extant $SIG{__WARN__} handler is discovered, it will continue to
145 be honored, but only after the diagnostics::splainthis() function (the
146 module's $SIG{__WARN__} interceptor) has had its way with your
147 warnings.
148
149 There is a $diagnostics::DEBUG variable you may set if you're
150 desperately curious what sorts of things are being intercepted.
151
152 BEGIN { $diagnostics::DEBUG = 1 }
153
155 Not being able to say "no diagnostics" is annoying, but may not be
156 insurmountable.
157
158 The "-pretty" directive is called too late to affect matters. You have
159 to do this instead, and before you load the module.
160
161 BEGIN { $diagnostics::PRETTY = 1 }
162
163 I could start up faster by delaying compilation until it should be
164 needed, but this gets a "panic: top_level" when using the pragma form
165 in Perl 5.001e.
166
167 While it's true that this documentation is somewhat subserious, if you
168 use a program named splain, you should expect a bit of whimsy.
169
171 Tom Christiansen <tchrist@mox.perl.com>, 25 June 1995.
172
173
174
175perl v5.12.4 2011-06-07 diagnostics(3pm)