5 diagnostics - Perl compiler pragma to force verbose warning diagnostics
7 splain - standalone program to do the same thing
14 use diagnostics -verbose;
21 perl program 2>diag.out
22 splain [-v] [-p] diag.out
27 =head2 The C<diagnostics> Pragma
29 This module extends the terse diagnostics normally emitted by both the
30 perl compiler and the perl interpreter, augmenting them with the more
31 explicative and endearing descriptions found in L<perldiag>. Like the
32 other pragmata, it affects the compilation phase of your program rather
33 than merely the execution phase.
35 To use in your program as a pragma, merely invoke
39 at the start (or near the start) of your program. (Note
40 that this I<does> enable perl's B<-w> flag.) Your whole
41 compilation will then be subject(ed :-) to the enhanced diagnostics.
42 These still go out B<STDERR>.
44 Due to the interaction between runtime and compiletime issues,
45 and because it's probably not a very good idea anyway,
46 you may not use C<no diagnostics> to turn them off at compiletime.
47 However, you may control their behaviour at runtime using the
48 disable() and enable() methods to turn them off and on respectively.
50 The B<-verbose> flag first prints out the L<perldiag> introduction before
51 any other diagnostics. The $diagnostics::PRETTY variable can generate nicer
52 escape sequences for pagers.
54 Warnings dispatched from perl itself (or more accurately, those that match
55 descriptions found in L<perldiag>) are only displayed once (no duplicate
56 descriptions). User code generated warnings ala warn() are unaffected,
57 allowing duplicate user messages to be displayed.
59 =head2 The I<splain> Program
61 While apparently a whole nuther program, I<splain> is actually nothing
62 more than a link to the (executable) F<diagnostics.pm> module, as well as
63 a link to the F<diagnostics.pod> documentation. The B<-v> flag is like
64 the C<use diagnostics -verbose> directive.
65 The B<-p> flag is like the
66 $diagnostics::PRETTY variable. Since you're post-processing with
67 I<splain>, there's no sense in being able to enable() or disable() processing.
69 Output from I<splain> is directed to B<STDOUT>, unlike the pragma.
73 The following file is certain to trigger a few errors at both
74 runtime and compiletime:
77 print NOWHERE "nothing\n";
78 print STDERR "\n\tThis message should be unadorned.\n";
79 warn "\tThis is a user warning";
80 print "\nDIAGNOSTIC TESTER: Please enter a <CR> here: ";
81 my $a, $b = scalar <STDIN>;
85 If you prefer to run your program first and look at its problem
88 perl -w test.pl 2>test.out
91 Note that this is not in general possible in shells of more dubious heritage,
94 (perl -w test.pl >/dev/tty) >& test.out
97 Because you just moved the existing B<stdout> to somewhere else.
99 If you don't want to modify your source code, but still have on-the-fly
102 exec 3>&1; perl -w test.pl 2>&1 1>&3 3>&- | splain 1>&2 3>&-
106 If you want to control warnings on the fly, do something like this.
107 Make sure you do the C<use> first, or you won't be able to get
108 at the enable() or disable() methods.
110 use diagnostics; # checks entire compilation phase
111 print "\ntime for 1st bogus diags: SQUAWKINGS\n";
113 print "done with 1st bogus\n";
115 disable diagnostics; # only turns off runtime warnings
116 print "\ntime for 2nd bogus: (squelched)\n";
118 print "done with 2nd bogus\n";
120 enable diagnostics; # turns back on runtime warnings
121 print "\ntime for 3rd bogus: SQUAWKINGS\n";
123 print "done with 3rd bogus\n";
126 print "\ntime for 4th bogus: (squelched)\n";
128 print "done with 4th bogus\n";
132 Diagnostic messages derive from the F<perldiag.pod> file when available at
133 runtime. Otherwise, they may be embedded in the file itself when the
134 splain package is built. See the F<Makefile> for details.
136 If an extant $SIG{__WARN__} handler is discovered, it will continue
137 to be honored, but only after the diagnostics::splainthis() function
138 (the module's $SIG{__WARN__} interceptor) has had its way with your
141 There is a $diagnostics::DEBUG variable you may set if you're desperately
142 curious what sorts of things are being intercepted.
144 BEGIN { $diagnostics::DEBUG = 1 }
149 Not being able to say "no diagnostics" is annoying, but may not be
152 The C<-pretty> directive is called too late to affect matters.
153 You have to do this instead, and I<before> you load the module.
155 BEGIN { $diagnostics::PRETTY = 1 }
157 I could start up faster by delaying compilation until it should be
158 needed, but this gets a "panic: top_level" when using the pragma form
161 While it's true that this documentation is somewhat subserious, if you use
162 a program named I<splain>, you should expect a bit of whimsy.
166 Tom Christiansen <F<tchrist@mox.perl.com>>, 25 June 1995.
180 my($privlib, $archlib) = @Config{qw(privlibexp archlibexp)};
182 require VMS::Filespec;
183 $privlib = VMS::Filespec::unixify($privlib);
184 $archlib = VMS::Filespec::unixify($archlib);
187 "$archlib/pod/perldiag.pod",
188 "$privlib/pod/perldiag-$Config{version}.pod",
189 "$privlib/pod/perldiag.pod",
190 "$archlib/pods/perldiag.pod",
191 "$privlib/pods/perldiag-$Config{version}.pod",
192 "$privlib/pods/perldiag.pod",
194 # handy for development testing of new warnings etc
195 unshift @trypod, "./pod/perldiag.pod" if -e "pod/perldiag.pod";
196 (my $PODFILE) = ((grep { -e } @trypod), $trypod[$#trypod])[0];
199 my $WHOAMI = ref bless []; # nobody's business, prolly not even mine
205 my(%HTML_2_Troff, %HTML_2_Latin_1, %HTML_2_ASCII_7);
208 our $opt_p = our $opt_d = our $opt_v = our $opt_f = '';
213 Getopt::Std::getopts('pdvf:')
214 or die "Usage: $0 [-v] [-p] [-f splainpod]";
215 $PODFILE = $opt_f if $opt_f;
216 $DEBUG = 2 if $opt_d;
221 if (open(POD_DIAG, $PODFILE)) {
222 warn "Happy happy podfile from real $PODFILE\n" if $DEBUG;
228 for my $file ( (map { "$_/$WHOAMI.pm" } @INC), $0) {
229 warn "Checking $file\n" if $DEBUG;
230 if (open(POD_DIAG, $file)) {
233 /^__END__\s*# wish diag dbase were more accessible/;
234 print STDERR "podfile is $file\n" if $DEBUG;
241 print STDERR "podfile is <DATA>\n" if $DEBUG;
242 *POD_DIAG = *main::DATA;
246 die "couldn't find diagnostic data in $PODFILE @INC $0";
251 'amp' => '&', # ampersand
252 'lt' => '<', # left chevron, less-than
253 'gt' => '>', # right chevron, greater-than
254 'quot' => '"', # double quote
256 "Aacute" => "A\\*'", # capital A, acute accent
262 'amp' => '&', # ampersand
263 'lt' => '<', # left chevron, less-than
264 'gt' => '>', # right chevron, greater-than
265 'quot' => '"', # double quote
267 "Aacute" => "\xC1" # capital A, acute accent
273 'amp' => '&', # ampersand
274 'lt' => '<', # left chevron, less-than
275 'gt' => '>', # right chevron, greater-than
276 'quot' => '"', # double quote
278 "Aacute" => "A" # capital A, acute accent
285 $PRETTY ? \%HTML_2_Latin_1 : \%HTML_2_ASCII_7;
291 *THITHER = $standalone ? *STDOUT : *STDERR;
293 my $transmo = <<EOFUNC;
295 #local \$^W = 0; # recursive warnings we do NOT need!
301 print STDERR "FINISHING COMPILATION for $_\n" if $DEBUG;
310 sub noop { return $_[0] } # spensive for a noop
311 sub bold { my $str =$_[0]; $str =~ s/(.)/$1\b$1/g; return $str; }
312 sub italic { my $str = $_[0]; $str =~ s/(.)/_\b$1/g; return $str; }
313 s/[BC]<(.*?)>/bold($1)/ges;
314 s/[LIF]<(.*?)>/italic($1)/ges;
317 s/[LIF]<(.*?)>/$1/gs;
320 if (defined $header) {
321 if ( $header eq 'DESCRIPTION' &&
322 ( /Optional warnings are enabled/
323 || /Some of these messages are generic./
334 unless ( s/=item (.*?)\s*\z//) {
336 if ( s/=head1\sDESCRIPTION//) {
337 $msg{$header = 'DESCRIPTION'} = '';
340 elsif( s/^=for\s+diagnostics\s*\n(.*?)\s*\z// ) {
346 # strip formatting directives in =item line
347 $header = $for_item || $1;
349 $header =~ s/[A-Z]<(.*?)>/$1/g;
351 if ($header =~ /%[csd]/) {
352 my $rhs = my $lhs = $header;
353 if ($lhs =~ s/(.*?)%d(?!%d)(.*)/\Q$1\E-?\\d+\Q$2\E/g) {
354 $lhs =~ s/\\%s/.*?/g;
356 # if i had lookbehind negations,
357 # i wouldn't have to do this \377 noise
358 $lhs =~ s/(.*?)%s/\Q$1\E.*?\377/g;
359 $lhs =~ s/\377([^\377]*)$/\Q$1\E/;
361 $lhs =~ s/\.\*\?$/.*/; # Allow %s at the end to eat it all
364 $transmo .= " s{^$lhs}\n {\Q$rhs\E}s\n\t&& return 1;\n";
366 $transmo .= " m{^\Q$header\E} && return 1;\n";
369 print STDERR "$WHOAMI: Duplicate entry: \"$header\"\n"
376 close POD_DIAG unless *main::DATA eq *POD_DIAG;
378 die "No diagnostics?" unless %msg;
380 $transmo .= " return 0;\n}\n";
381 print STDERR $transmo if $DEBUG;
387 if (!@ARGV and -t STDIN) { print STDERR "$0: Reading from STDIN\n" }
388 while (defined (my $error = <>)) {
389 splainthis($error) || print THITHER $error;
399 $^W = 1; # yup, clobbered the global variable;
400 # tough, if you want diags, you want diags.
401 return if $SIG{__WARN__} eq \&warn_trap;
405 /^-d(ebug)?$/ && do {
410 /^-v(erbose)?$/ && do {
415 /^-p(retty)?$/ && do {
416 print STDERR "$0: I'm afraid it's too late for prettiness.\n";
421 warn "Unknown flag: $_";
424 $oldwarn = $SIG{__WARN__};
425 $olddie = $SIG{__DIE__};
426 $SIG{__WARN__} = \&warn_trap;
427 $SIG{__DIE__} = \&death_trap;
430 sub enable { &import }
434 return unless $SIG{__WARN__} eq \&warn_trap;
435 $SIG{__WARN__} = $oldwarn || '';
436 $SIG{__DIE__} = $olddie || '';
441 if (caller eq $WHOAMI or !splainthis($warning)) {
442 print STDERR $warning;
444 &$oldwarn if defined $oldwarn and $oldwarn and $oldwarn ne \&warn_trap;
448 my $exception = $_[0];
450 # See if we are coming from anywhere within an eval. If so we don't
451 # want to explain the exception because it's going to get caught.
455 my $caller = (caller($i++))[3] or last;
456 if ($caller eq '(eval)') {
462 splainthis($exception) unless $in_eval;
463 if (caller eq $WHOAMI) { print STDERR "INTERNAL EXCEPTION: $exception"; }
464 &$olddie if defined $olddie and $olddie and $olddie ne \&death_trap;
466 # We don't want to unset these if we're coming from an eval because
467 # then we've turned off diagnostics. (Actually what does this next
469 $SIG{__DIE__} = $SIG{__WARN__} = '' unless $in_eval;
470 local($Carp::CarpLevel) = 1;
471 confess "Uncaught exception from user code:\n\t$exception";
472 # up we go; where we stop, nobody knows, but i think we die now
473 # but i'm deeply afraid of the &$olddie guy reraising and us getting
474 # into an indirect recursion loop
484 ### &finish_compilation unless %msg;
487 # return unless defined;
488 s/, <.*?> (?:line|chunk).*$//;
489 my $real = s/(.*?) at .*? (?:line|chunk) \d+.*/$1/;
491 if ($exact_duplicate{$orig}++) {
495 return 0 unless &transmo;
497 $orig = shorten($orig);
500 print THITHER "$orig (#$old_diag{$_})\n";
504 $old_diag{$_} = ++$count;
505 print THITHER "\n" if $wantspace;
507 print THITHER "$orig (#$old_diag{$_})\n";
509 print THITHER $msg{$_};
511 if (0 and $standalone) {
512 print THITHER " **** Error #$old_diag{$_} ",
513 ($real ? "is" : "appears to be"),
514 " an unknown diagnostic message.\n\n";
523 if ($VERBOSE and not $count) {
524 print THITHER &{$PRETTY ? \&bold : \&noop}("DESCRIPTION OF DIAGNOSTICS"),
525 "\n$msg{DESCRIPTION}\n";
536 exists $HTML_Escapes{$1}
537 ? do { $HTML_Escapes{$1} }
539 warn "Unknown escape: E<$1> in $_";
548 if (length($line) > 79 and index($line, "\n") == -1) {
549 my $space_place = rindex($line, ' ', 79);
550 if ($space_place != -1) {
551 substr($line, $space_place, 1) = "\n\t";
558 1 unless $standalone; # or it'll complain about itself
559 __END__ # wish diag dbase were more accessible