File Coverage

blib/lib/Perl/Tidy/Diagnostics.pm
Criterion Covered Total %
statement 12 38 31.5
branch 0 10 0.0
condition n/a
subroutine 4 9 44.4
pod 0 3 0.0
total 16 60 26.6


line stmt bran cond sub pod time code
1             package Perl::Tidy::Diagnostics;
2              
3             #####################################################################
4             #
5             # The Perl::Tidy::Diagnostics class writes the DIAGNOSTICS file, which is
6             # useful for program development.
7             #
8             # Only one such file is created regardless of the number of input
9             # files processed. This allows the results of processing many files
10             # to be summarized in a single file.
11              
12             # Output messages go to a file named DIAGNOSTICS, where
13             # they are labeled by file and line. This allows many files to be
14             # scanned at once for some particular condition of interest. It was
15             # particularly useful for developing guessing strategies.
16             #
17             #####################################################################
18              
19 44     44   342 use strict;
  44         111  
  44         1546  
20 44     44   208 use warnings;
  44         67  
  44         1944  
21 44     44   163 use English qw( -no_match_vars );
  44         65  
  44         177  
22             our $VERSION = '20260705';
23              
24 44     44   13622 use constant EMPTY_STRING => q{};
  44         73  
  44         18900  
25              
26             sub AUTOLOAD {
27              
28             # Catch any undefined sub calls so that we are sure to get
29             # some diagnostic information. This sub should never be called
30             # except for a programming error.
31 0     0     our $AUTOLOAD;
32 0 0         return if ( $AUTOLOAD =~ /\bDESTROY$/ );
33 0           my ( $pkg, $fname, $lno ) = caller();
34 0           my $my_package = __PACKAGE__;
35 0           print {*STDERR} <<EOM;
  0            
36             ======================================================================
37             Error detected in package '$my_package', version $VERSION
38             Received unexpected AUTOLOAD call for sub '$AUTOLOAD'
39             Called from package: '$pkg'
40             Called from File '$fname' at line '$lno'
41             This error is probably due to a recent programming change
42             ======================================================================
43             EOM
44 0           exit 1;
45             } ## end sub AUTOLOAD
46              
47       0     sub DESTROY {
48              
49             # required to avoid call to AUTOLOAD in some versions of perl
50             }
51              
52             sub new {
53              
54 0     0 0   my $class = shift;
55 0           return bless {
56             _write_diagnostics_count => 0,
57             _last_diagnostic_file => EMPTY_STRING,
58             _input_file => EMPTY_STRING,
59             _fh => undef,
60             }, $class;
61             } ## end sub new
62              
63             sub set_input_file {
64 0     0 0   my ( $self, $input_file ) = @_;
65 0           $self->{_input_file} = $input_file;
66 0           return;
67             }
68              
69             sub write_diagnostics {
70 0     0 0   my ( $self, $msg, $line_number ) = @_;
71              
72             # Write a message to the diagnostics file
73             # Input parameters:
74             # $msg = string describing the event
75             # $line_number = optional line number
76              
77 0 0         if ( !$self->{_write_diagnostics_count} ) {
78 0 0         open( $self->{_fh}, ">", "DIAGNOSTICS" )
79             or Perl::Tidy::Die("couldn't open DIAGNOSTICS: $OS_ERROR\n");
80             }
81              
82 0 0         if ( defined($line_number) ) {
83 0           $msg = "$line_number:\t$msg";
84             }
85              
86 0           my $fh = $self->{_fh};
87 0           my $last_diagnostic_file = $self->{_last_diagnostic_file};
88 0           my $input_file = $self->{_input_file};
89 0 0         if ( $last_diagnostic_file ne $input_file ) {
90 0           $fh->print("\nFILE:$input_file\n");
91             }
92 0           $self->{_last_diagnostic_file} = $input_file;
93 0           $fh->print($msg);
94 0           $self->{_write_diagnostics_count}++;
95 0           return;
96             } ## end sub write_diagnostics
97              
98             1;