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