1N/Apackage diagnostics;
1N/A
1N/A=head1 NAME
1N/A
1N/Adiagnostics, splain - produce verbose warning diagnostics
1N/A
1N/A=head1 SYNOPSIS
1N/A
1N/AUsing the C<diagnostics> pragma:
1N/A
1N/A use diagnostics;
1N/A use diagnostics -verbose;
1N/A
1N/A enable diagnostics;
1N/A disable diagnostics;
1N/A
1N/AUsing the C<splain> standalone filter program:
1N/A
1N/A perl program 2>diag.out
1N/A splain [-v] [-p] diag.out
1N/A
1N/A=head1 DESCRIPTION
1N/A
1N/A=head2 The C<diagnostics> Pragma
1N/A
1N/AThis module extends the terse diagnostics normally emitted by both the
1N/Aperl compiler and the perl interpreter, augmenting them with the more
1N/Aexplicative and endearing descriptions found in L<perldiag>. Like the
1N/Aother pragmata, it affects the compilation phase of your program rather
1N/Athan merely the execution phase.
1N/A
1N/ATo use in your program as a pragma, merely invoke
1N/A
1N/A use diagnostics;
1N/A
1N/Aat the start (or near the start) of your program. (Note
1N/Athat this I<does> enable perl's B<-w> flag.) Your whole
1N/Acompilation will then be subject(ed :-) to the enhanced diagnostics.
1N/AThese still go out B<STDERR>.
1N/A
1N/ADue to the interaction between runtime and compiletime issues,
1N/Aand because it's probably not a very good idea anyway,
1N/Ayou may not use C<no diagnostics> to turn them off at compiletime.
1N/AHowever, you may control their behaviour at runtime using the
1N/Adisable() and enable() methods to turn them off and on respectively.
1N/A
1N/AThe B<-verbose> flag first prints out the L<perldiag> introduction before
1N/Aany other diagnostics. The $diagnostics::PRETTY variable can generate nicer
1N/Aescape sequences for pagers.
1N/A
1N/AWarnings dispatched from perl itself (or more accurately, those that match
1N/Adescriptions found in L<perldiag>) are only displayed once (no duplicate
1N/Adescriptions). User code generated warnings a la warn() are unaffected,
1N/Aallowing duplicate user messages to be displayed.
1N/A
1N/A=head2 The I<splain> Program
1N/A
1N/AWhile apparently a whole nuther program, I<splain> is actually nothing
1N/Amore than a link to the (executable) F<diagnostics.pm> module, as well as
1N/Aa link to the F<diagnostics.pod> documentation. The B<-v> flag is like
1N/Athe C<use diagnostics -verbose> directive.
1N/AThe B<-p> flag is like the
1N/A$diagnostics::PRETTY variable. Since you're post-processing with
1N/AI<splain>, there's no sense in being able to enable() or disable() processing.
1N/A
1N/AOutput from I<splain> is directed to B<STDOUT>, unlike the pragma.
1N/A
1N/A=head1 EXAMPLES
1N/A
1N/AThe following file is certain to trigger a few errors at both
1N/Aruntime and compiletime:
1N/A
1N/A use diagnostics;
1N/A print NOWHERE "nothing\n";
1N/A print STDERR "\n\tThis message should be unadorned.\n";
1N/A warn "\tThis is a user warning";
1N/A print "\nDIAGNOSTIC TESTER: Please enter a <CR> here: ";
1N/A my $a, $b = scalar <STDIN>;
1N/A print "\n";
1N/A print $x/$y;
1N/A
1N/AIf you prefer to run your program first and look at its problem
1N/Aafterwards, do this:
1N/A
1N/A perl -w test.pl 2>test.out
1N/A ./splain < test.out
1N/A
1N/ANote that this is not in general possible in shells of more dubious heritage,
1N/Aas the theoretical
1N/A
1N/A (perl -w test.pl >/dev/tty) >& test.out
1N/A ./splain < test.out
1N/A
1N/ABecause you just moved the existing B<stdout> to somewhere else.
1N/A
1N/AIf you don't want to modify your source code, but still have on-the-fly
1N/Awarnings, do this:
1N/A
1N/A exec 3>&1; perl -w test.pl 2>&1 1>&3 3>&- | splain 1>&2 3>&-
1N/A
1N/ANifty, eh?
1N/A
1N/AIf you want to control warnings on the fly, do something like this.
1N/AMake sure you do the C<use> first, or you won't be able to get
1N/Aat the enable() or disable() methods.
1N/A
1N/A use diagnostics; # checks entire compilation phase
1N/A print "\ntime for 1st bogus diags: SQUAWKINGS\n";
1N/A print BOGUS1 'nada';
1N/A print "done with 1st bogus\n";
1N/A
1N/A disable diagnostics; # only turns off runtime warnings
1N/A print "\ntime for 2nd bogus: (squelched)\n";
1N/A print BOGUS2 'nada';
1N/A print "done with 2nd bogus\n";
1N/A
1N/A enable diagnostics; # turns back on runtime warnings
1N/A print "\ntime for 3rd bogus: SQUAWKINGS\n";
1N/A print BOGUS3 'nada';
1N/A print "done with 3rd bogus\n";
1N/A
1N/A disable diagnostics;
1N/A print "\ntime for 4th bogus: (squelched)\n";
1N/A print BOGUS4 'nada';
1N/A print "done with 4th bogus\n";
1N/A
1N/A=head1 INTERNALS
1N/A
1N/ADiagnostic messages derive from the F<perldiag.pod> file when available at
1N/Aruntime. Otherwise, they may be embedded in the file itself when the
1N/Asplain package is built. See the F<Makefile> for details.
1N/A
1N/AIf an extant $SIG{__WARN__} handler is discovered, it will continue
1N/Ato be honored, but only after the diagnostics::splainthis() function
1N/A(the module's $SIG{__WARN__} interceptor) has had its way with your
1N/Awarnings.
1N/A
1N/AThere is a $diagnostics::DEBUG variable you may set if you're desperately
1N/Acurious what sorts of things are being intercepted.
1N/A
1N/A BEGIN { $diagnostics::DEBUG = 1 }
1N/A
1N/A
1N/A=head1 BUGS
1N/A
1N/ANot being able to say "no diagnostics" is annoying, but may not be
1N/Ainsurmountable.
1N/A
1N/AThe C<-pretty> directive is called too late to affect matters.
1N/AYou have to do this instead, and I<before> you load the module.
1N/A
1N/A BEGIN { $diagnostics::PRETTY = 1 }
1N/A
1N/AI could start up faster by delaying compilation until it should be
1N/Aneeded, but this gets a "panic: top_level" when using the pragma form
1N/Ain Perl 5.001e.
1N/A
1N/AWhile it's true that this documentation is somewhat subserious, if you use
1N/Aa program named I<splain>, you should expect a bit of whimsy.
1N/A
1N/A=head1 AUTHOR
1N/A
1N/ATom Christiansen <F<tchrist@mox.perl.com>>, 25 June 1995.
1N/A
1N/A=cut
1N/A
1N/Ause strict;
1N/Ause 5.006;
1N/Ause Carp;
1N/A
1N/Aour $VERSION = 1.12;
1N/Aour $DEBUG;
1N/Aour $VERBOSE;
1N/Aour $PRETTY;
1N/A
1N/Ause Config;
1N/Amy($privlib, $archlib) = @Config{qw(privlibexp archlibexp)};
1N/Aif ($^O eq 'VMS') {
1N/A require VMS::Filespec;
1N/A $privlib = VMS::Filespec::unixify($privlib);
1N/A $archlib = VMS::Filespec::unixify($archlib);
1N/A}
1N/Amy @trypod = (
1N/A "$archlib/pod/perldiag.pod",
1N/A "$privlib/pod/perldiag-$Config{version}.pod",
1N/A "$privlib/pod/perldiag.pod",
1N/A "$archlib/pods/perldiag.pod",
1N/A "$privlib/pods/perldiag-$Config{version}.pod",
1N/A "$privlib/pods/perldiag.pod",
1N/A );
1N/A# handy for development testing of new warnings etc
1N/Aunshift @trypod, "./pod/perldiag.pod" if -e "pod/perldiag.pod";
1N/A(my $PODFILE) = ((grep { -e } @trypod), $trypod[$#trypod])[0];
1N/A
1N/Aif ($^O eq 'MacOS') {
1N/A # just updir one from each lib dir, we'll find it ...
1N/A ($PODFILE) = grep { -e } map { "$_:pod:perldiag.pod" } @INC;
1N/A}
1N/A
1N/A
1N/A$DEBUG ||= 0;
1N/Amy $WHOAMI = ref bless []; # nobody's business, prolly not even mine
1N/A
1N/Alocal $| = 1;
1N/Alocal $_;
1N/A
1N/Amy $standalone;
1N/Amy(%HTML_2_Troff, %HTML_2_Latin_1, %HTML_2_ASCII_7);
1N/A
1N/ACONFIG: {
1N/A our $opt_p = our $opt_d = our $opt_v = our $opt_f = '';
1N/A
1N/A unless (caller) {
1N/A $standalone++;
1N/A require Getopt::Std;
1N/A Getopt::Std::getopts('pdvf:')
1N/A or die "Usage: $0 [-v] [-p] [-f splainpod]";
1N/A $PODFILE = $opt_f if $opt_f;
1N/A $DEBUG = 2 if $opt_d;
1N/A $VERBOSE = $opt_v;
1N/A $PRETTY = $opt_p;
1N/A }
1N/A
1N/A if (open(POD_DIAG, $PODFILE)) {
1N/A warn "Happy happy podfile from real $PODFILE\n" if $DEBUG;
1N/A last CONFIG;
1N/A }
1N/A
1N/A if (caller) {
1N/A INCPATH: {
1N/A for my $file ( (map { "$_/$WHOAMI.pm" } @INC), $0) {
1N/A warn "Checking $file\n" if $DEBUG;
1N/A if (open(POD_DIAG, $file)) {
1N/A while (<POD_DIAG>) {
1N/A next unless
1N/A /^__END__\s*# wish diag dbase were more accessible/;
1N/A print STDERR "podfile is $file\n" if $DEBUG;
1N/A last INCPATH;
1N/A }
1N/A }
1N/A }
1N/A }
1N/A } else {
1N/A print STDERR "podfile is <DATA>\n" if $DEBUG;
1N/A *POD_DIAG = *main::DATA;
1N/A }
1N/A}
1N/Aif (eof(POD_DIAG)) {
1N/A die "couldn't find diagnostic data in $PODFILE @INC $0";
1N/A}
1N/A
1N/A
1N/A%HTML_2_Troff = (
1N/A 'amp' => '&', # ampersand
1N/A 'lt' => '<', # left chevron, less-than
1N/A 'gt' => '>', # right chevron, greater-than
1N/A 'quot' => '"', # double quote
1N/A
1N/A "Aacute" => "A\\*'", # capital A, acute accent
1N/A # etc
1N/A
1N/A);
1N/A
1N/A%HTML_2_Latin_1 = (
1N/A 'amp' => '&', # ampersand
1N/A 'lt' => '<', # left chevron, less-than
1N/A 'gt' => '>', # right chevron, greater-than
1N/A 'quot' => '"', # double quote
1N/A
1N/A "Aacute" => "\xC1" # capital A, acute accent
1N/A
1N/A # etc
1N/A);
1N/A
1N/A%HTML_2_ASCII_7 = (
1N/A 'amp' => '&', # ampersand
1N/A 'lt' => '<', # left chevron, less-than
1N/A 'gt' => '>', # right chevron, greater-than
1N/A 'quot' => '"', # double quote
1N/A
1N/A "Aacute" => "A" # capital A, acute accent
1N/A # etc
1N/A);
1N/A
1N/Aour %HTML_Escapes;
1N/A*HTML_Escapes = do {
1N/A if ($standalone) {
1N/A $PRETTY ? \%HTML_2_Latin_1 : \%HTML_2_ASCII_7;
1N/A } else {
1N/A \%HTML_2_Latin_1;
1N/A }
1N/A};
1N/A
1N/A*THITHER = $standalone ? *STDOUT : *STDERR;
1N/A
1N/Amy %transfmt = ();
1N/Amy $transmo = <<EOFUNC;
1N/Asub transmo {
1N/A #local \$^W = 0; # recursive warnings we do NOT need!
1N/A study;
1N/AEOFUNC
1N/A
1N/Amy %msg;
1N/A{
1N/A print STDERR "FINISHING COMPILATION for $_\n" if $DEBUG;
1N/A local $/ = '';
1N/A local $_;
1N/A my $header;
1N/A my $for_item;
1N/A while (<POD_DIAG>) {
1N/A
1N/A unescape();
1N/A if ($PRETTY) {
1N/A sub noop { return $_[0] } # spensive for a noop
1N/A sub bold { my $str =$_[0]; $str =~ s/(.)/$1\b$1/g; return $str; }
1N/A sub italic { my $str = $_[0]; $str =~ s/(.)/_\b$1/g; return $str; }
1N/A s/[BC]<(.*?)>/bold($1)/ges;
1N/A s/[LIF]<(.*?)>/italic($1)/ges;
1N/A } else {
1N/A s/[BC]<(.*?)>/$1/gs;
1N/A s/[LIF]<(.*?)>/$1/gs;
1N/A }
1N/A unless (/^=/) {
1N/A if (defined $header) {
1N/A if ( $header eq 'DESCRIPTION' &&
1N/A ( /Optional warnings are enabled/
1N/A || /Some of these messages are generic./
1N/A ) )
1N/A {
1N/A next;
1N/A }
1N/A s/^/ /gm;
1N/A $msg{$header} .= $_;
1N/A undef $for_item;
1N/A }
1N/A next;
1N/A }
1N/A unless ( s/=item (.*?)\s*\z//) {
1N/A
1N/A if ( s/=head1\sDESCRIPTION//) {
1N/A $msg{$header = 'DESCRIPTION'} = '';
1N/A undef $for_item;
1N/A }
1N/A elsif( s/^=for\s+diagnostics\s*\n(.*?)\s*\z// ) {
1N/A $for_item = $1;
1N/A }
1N/A next;
1N/A }
1N/A
1N/A if( $for_item ) { $header = $for_item; undef $for_item }
1N/A else {
1N/A $header = $1;
1N/A while( $header =~ /[;,]\z/ ) {
1N/A <POD_DIAG> =~ /^\s*(.*?)\s*\z/;
1N/A $header .= ' '.$1;
1N/A }
1N/A }
1N/A
1N/A # strip formatting directives from =item line
1N/A $header =~ s/[A-Z]<(.*?)>/$1/g;
1N/A
1N/A my @toks = split( /(%l?[dx]|%c|%(?:\.\d+)?s)/, $header );
1N/A if (@toks > 1) {
1N/A my $conlen = 0;
1N/A for my $i (0..$#toks){
1N/A if( $i % 2 ){
1N/A if( $toks[$i] eq '%c' ){
1N/A $toks[$i] = '.';
1N/A } elsif( $toks[$i] eq '%d' ){
1N/A $toks[$i] = '\d+';
1N/A } elsif( $toks[$i] eq '%s' ){
1N/A $toks[$i] = $i == $#toks ? '.*' : '.*?';
1N/A } elsif( $toks[$i] =~ '%.(\d+)s' ){
1N/A $toks[$i] = ".{$1}";
1N/A } elsif( $toks[$i] =~ '^%l*x$' ){
1N/A $toks[$i] = '[\da-f]+';
1N/A }
1N/A } elsif( length( $toks[$i] ) ){
1N/A $toks[$i] =~ s/^.*$/\Q$&\E/;
1N/A $conlen += length( $toks[$i] );
1N/A }
1N/A }
1N/A my $lhs = join( '', @toks );
1N/A $transfmt{$header}{pat} =
1N/A " s{^$lhs}\n {\Q$header\E}s\n\t&& return 1;\n";
1N/A $transfmt{$header}{len} = $conlen;
1N/A } else {
1N/A $transfmt{$header}{pat} =
1N/A " m{^\Q$header\E} && return 1;\n";
1N/A $transfmt{$header}{len} = length( $header );
1N/A }
1N/A
1N/A print STDERR "$WHOAMI: Duplicate entry: \"$header\"\n"
1N/A if $msg{$header};
1N/A
1N/A $msg{$header} = '';
1N/A }
1N/A
1N/A
1N/A close POD_DIAG unless *main::DATA eq *POD_DIAG;
1N/A
1N/A die "No diagnostics?" unless %msg;
1N/A
1N/A # Apply patterns in order of decreasing sum of lengths of fixed parts
1N/A # Seems the best way of hitting the right one.
1N/A for my $hdr ( sort { $transfmt{$b}{len} <=> $transfmt{$a}{len} }
1N/A keys %transfmt ){
1N/A $transmo .= $transfmt{$hdr}{pat};
1N/A }
1N/A $transmo .= " return 0;\n}\n";
1N/A print STDERR $transmo if $DEBUG;
1N/A eval $transmo;
1N/A die $@ if $@;
1N/A}
1N/A
1N/Aif ($standalone) {
1N/A if (!@ARGV and -t STDIN) { print STDERR "$0: Reading from STDIN\n" }
1N/A while (defined (my $error = <>)) {
1N/A splainthis($error) || print THITHER $error;
1N/A }
1N/A exit;
1N/A}
1N/A
1N/Amy $olddie;
1N/Amy $oldwarn;
1N/A
1N/Asub import {
1N/A shift;
1N/A $^W = 1; # yup, clobbered the global variable;
1N/A # tough, if you want diags, you want diags.
1N/A return if defined $SIG{__WARN__} && ($SIG{__WARN__} eq \&warn_trap);
1N/A
1N/A for (@_) {
1N/A
1N/A /^-d(ebug)?$/ && do {
1N/A $DEBUG++;
1N/A next;
1N/A };
1N/A
1N/A /^-v(erbose)?$/ && do {
1N/A $VERBOSE++;
1N/A next;
1N/A };
1N/A
1N/A /^-p(retty)?$/ && do {
1N/A print STDERR "$0: I'm afraid it's too late for prettiness.\n";
1N/A $PRETTY++;
1N/A next;
1N/A };
1N/A
1N/A warn "Unknown flag: $_";
1N/A }
1N/A
1N/A $oldwarn = $SIG{__WARN__};
1N/A $olddie = $SIG{__DIE__};
1N/A $SIG{__WARN__} = \&warn_trap;
1N/A $SIG{__DIE__} = \&death_trap;
1N/A}
1N/A
1N/Asub enable { &import }
1N/A
1N/Asub disable {
1N/A shift;
1N/A return unless $SIG{__WARN__} eq \&warn_trap;
1N/A $SIG{__WARN__} = $oldwarn || '';
1N/A $SIG{__DIE__} = $olddie || '';
1N/A}
1N/A
1N/Asub warn_trap {
1N/A my $warning = $_[0];
1N/A if (caller eq $WHOAMI or !splainthis($warning)) {
1N/A print STDERR $warning;
1N/A }
1N/A &$oldwarn if defined $oldwarn and $oldwarn and $oldwarn ne \&warn_trap;
1N/A};
1N/A
1N/Asub death_trap {
1N/A my $exception = $_[0];
1N/A
1N/A # See if we are coming from anywhere within an eval. If so we don't
1N/A # want to explain the exception because it's going to get caught.
1N/A my $in_eval = 0;
1N/A my $i = 0;
1N/A while (1) {
1N/A my $caller = (caller($i++))[3] or last;
1N/A if ($caller eq '(eval)') {
1N/A $in_eval = 1;
1N/A last;
1N/A }
1N/A }
1N/A
1N/A splainthis($exception) unless $in_eval;
1N/A if (caller eq $WHOAMI) { print STDERR "INTERNAL EXCEPTION: $exception"; }
1N/A &$olddie if defined $olddie and $olddie and $olddie ne \&death_trap;
1N/A
1N/A return if $in_eval;
1N/A
1N/A # We don't want to unset these if we're coming from an eval because
1N/A # then we've turned off diagnostics.
1N/A
1N/A # Switch off our die/warn handlers so we don't wind up in our own
1N/A # traps.
1N/A $SIG{__DIE__} = $SIG{__WARN__} = '';
1N/A
1N/A # Have carp skip over death_trap() when showing the stack trace.
1N/A local($Carp::CarpLevel) = 1;
1N/A
1N/A confess "Uncaught exception from user code:\n\t$exception";
1N/A # up we go; where we stop, nobody knows, but i think we die now
1N/A # but i'm deeply afraid of the &$olddie guy reraising and us getting
1N/A # into an indirect recursion loop
1N/A};
1N/A
1N/Amy %exact_duplicate;
1N/Amy %old_diag;
1N/Amy $count;
1N/Amy $wantspace;
1N/Asub splainthis {
1N/A local $_ = shift;
1N/A local $\;
1N/A ### &finish_compilation unless %msg;
1N/A s/\.?\n+$//;
1N/A my $orig = $_;
1N/A # return unless defined;
1N/A
1N/A # get rid of the where-are-we-in-input part
1N/A s/, <.*?> (?:line|chunk).*$//;
1N/A
1N/A # Discard 1st " at <file> line <no>" and all text beyond
1N/A # but be aware of messsages containing " at this-or-that"
1N/A my $real = 0;
1N/A my @secs = split( / at / );
1N/A $_ = $secs[0];
1N/A for my $i ( 1..$#secs ){
1N/A if( $secs[$i] =~ /.+? (?:line|chunk) \d+/ ){
1N/A $real = 1;
1N/A last;
1N/A } else {
1N/A $_ .= ' at ' . $secs[$i];
1N/A }
1N/A }
1N/A
1N/A # remove parenthesis occurring at the end of some messages
1N/A s/^\((.*)\)$/$1/;
1N/A
1N/A if ($exact_duplicate{$orig}++) {
1N/A return &transmo;
1N/A } else {
1N/A return 0 unless &transmo;
1N/A }
1N/A
1N/A $orig = shorten($orig);
1N/A if ($old_diag{$_}) {
1N/A autodescribe();
1N/A print THITHER "$orig (#$old_diag{$_})\n";
1N/A $wantspace = 1;
1N/A } else {
1N/A autodescribe();
1N/A $old_diag{$_} = ++$count;
1N/A print THITHER "\n" if $wantspace;
1N/A $wantspace = 0;
1N/A print THITHER "$orig (#$old_diag{$_})\n";
1N/A if ($msg{$_}) {
1N/A print THITHER $msg{$_};
1N/A } else {
1N/A if (0 and $standalone) {
1N/A print THITHER " **** Error #$old_diag{$_} ",
1N/A ($real ? "is" : "appears to be"),
1N/A " an unknown diagnostic message.\n\n";
1N/A }
1N/A return 0;
1N/A }
1N/A }
1N/A return 1;
1N/A}
1N/A
1N/Asub autodescribe {
1N/A if ($VERBOSE and not $count) {
1N/A print THITHER &{$PRETTY ? \&bold : \&noop}("DESCRIPTION OF DIAGNOSTICS"),
1N/A "\n$msg{DESCRIPTION}\n";
1N/A }
1N/A}
1N/A
1N/Asub unescape {
1N/A s {
1N/A E<
1N/A ( [A-Za-z]+ )
1N/A >
1N/A } {
1N/A do {
1N/A exists $HTML_Escapes{$1}
1N/A ? do { $HTML_Escapes{$1} }
1N/A : do {
1N/A warn "Unknown escape: E<$1> in $_";
1N/A "E<$1>";
1N/A }
1N/A }
1N/A }egx;
1N/A}
1N/A
1N/Asub shorten {
1N/A my $line = $_[0];
1N/A if (length($line) > 79 and index($line, "\n") == -1) {
1N/A my $space_place = rindex($line, ' ', 79);
1N/A if ($space_place != -1) {
1N/A substr($line, $space_place, 1) = "\n\t";
1N/A }
1N/A }
1N/A return $line;
1N/A}
1N/A
1N/A
1N/A1 unless $standalone; # or it'll complain about itself
1N/A__END__ # wish diag dbase were more accessible