File Coverage

blib/lib/Perl/Tidy/Debugger.pm
Criterion Covered Total %
statement 59 64 92.1
branch 9 16 56.2
condition 3 9 33.3
subroutine 9 9 100.0
pod 0 4 0.0
total 80 102 78.4


line stmt bran cond sub pod time code
1             package Perl::Tidy::Debugger;
2              
3             #####################################################################
4             #
5             # The Perl::Tidy::Debugger class shows line tokenization
6             #
7             #####################################################################
8              
9 44     44   243 use strict;
  44         77  
  44         1285  
10 44     44   149 use warnings;
  44         62  
  44         1764  
11 44     44   157 use English qw( -no_match_vars );
  44         61  
  44         178  
12             our $VERSION = '20260705';
13              
14 44     44   12922 use constant EMPTY_STRING => q{};
  44         84  
  44         4306  
15 44     44   212 use constant SPACE => q{ };
  44         62  
  44         24336  
16              
17             sub new {
18              
19 2     2 0 5 my ( $class, $filename, $is_encoded_data ) = @_;
20              
21 2         11 return bless {
22             _debug_file => $filename,
23             _debug_file_opened => 0,
24             _fh => undef,
25             _is_encoded_data => $is_encoded_data,
26             }, $class;
27             } ## end sub new
28              
29             sub really_open_debug_file {
30              
31 2     2 0 2 my $self = shift;
32 2         4 my $debug_file = $self->{_debug_file};
33 2         3 my $is_encoded_data = $self->{_is_encoded_data};
34 2         22 my $fh = Perl::Tidy::streamhandle( $debug_file, 'w', $is_encoded_data );
35 2 50       11 if ( !$fh ) {
36 0         0 Perl::Tidy::Warn("can't open debug file '$debug_file'\n");
37             }
38 2         3 $self->{_debug_file_opened} = 1;
39 2         9 $self->{_fh} = $fh;
40 2         7 $fh->print(
41             "Use -dump-token-types (-dtt) to get a list of token type codes\n");
42 2         3 return;
43             } ## end sub really_open_debug_file
44              
45             sub close_debug_file {
46              
47 2     2 0 4 my $self = shift;
48 2 50       4 if ( $self->{_debug_file_opened} ) {
49 2         4 my $fh = $self->{_fh};
50 2         3 my $debug_file = $self->{_debug_file};
51 2 0 33     25 if ( $fh
      33        
      33        
52             && $fh->can('close')
53             && $debug_file ne '-'
54             && !ref($debug_file) )
55             {
56 0 0       0 $fh->close()
57             or Perl::Tidy::Warn(
58             "can't close DEBUG file '$debug_file': $OS_ERROR\n");
59             }
60             }
61 2         4 return;
62             } ## end sub close_debug_file
63              
64             sub write_debug_entry {
65              
66             # This is a debug dump routine which may be modified as necessary
67             # to dump tokens on a line-by-line basis. The output will be written
68             # to the .DEBUG file when the -D flag is entered.
69 7     7 0 12 my ( $self, $line_of_tokens ) = @_;
70              
71 7         11 my $rtoken_type = $line_of_tokens->{_rtoken_type};
72 7         14 my $rtokens = $line_of_tokens->{_rtokens};
73 7         8 my $input_line_number = $line_of_tokens->{_line_number};
74              
75             ## uncomment if needed:
76             ## my $input_line = $line_of_tokens->{_line_text};
77             ## my $rlevels = $line_of_tokens->{_rlevels};
78             ## my $line_type = $line_of_tokens->{_line_type};
79              
80 7         13 my $token_str = "$input_line_number: ";
81 7         12 my $reconstructed_original = "$input_line_number: ";
82              
83 7         12 my $pattern = EMPTY_STRING;
84 7         11 my @next_char = ( '"', '"' );
85 7         8 my $i_next = 0;
86 7 100       19 if ( !$self->{_debug_file_opened} ) {
87 2         7 $self->really_open_debug_file();
88             }
89 7         9 my $fh = $self->{_fh};
90              
91 7         7 foreach my $j ( 0 .. @{$rtoken_type} - 1 ) {
  7         34  
92              
93             # testing patterns
94 20 100       34 if ( $rtoken_type->[$j] eq 'k' ) {
95 2         4 $pattern .= $rtokens->[$j];
96             }
97             else {
98 18         21 $pattern .= $rtoken_type->[$j];
99             }
100 20         23 $reconstructed_original .= $rtokens->[$j];
101 20         20 my $num = length( $rtokens->[$j] );
102 20         21 my $type_str = $rtoken_type->[$j];
103              
104             # be sure there are no blank tokens (shouldn't happen)
105             # This can only happen if a programming error has been made
106             # because all valid tokens are non-blank
107 20 50       27 if ( $type_str eq SPACE ) {
108 0         0 $fh->print("BLANK TOKEN on the next line\n");
109 0         0 $type_str = $next_char[$i_next];
110 0         0 $i_next = 1 - $i_next;
111             }
112              
113 20 100       29 if ( length($type_str) == 1 ) {
114 19         25 $type_str = $type_str x $num;
115             }
116 20         23 $token_str .= $type_str;
117             }
118              
119             # Write what you want here ...
120             # $fh->print "$input_line\n";
121             # $fh->print "$pattern\n";
122 7         25 $fh->print("$reconstructed_original\n");
123 7         17 $fh->print("$token_str\n");
124              
125 7         17 return;
126             } ## end sub write_debug_entry
127             1;