blib/lib/Filter/HereDocIndent.pm

filter: Statementstmt 88.1% 67 / 76 Branchbran 67.8% 19 / 28 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 91.6% 11 / 12 Podpod 0.0% 0 / 4 totaltotal 80.8% 97 / 120 CC 18 SCAR 21.1
CC
18
Cov
83%
process_line
14.7
filter_block
6.0
import
3.2
CRAP 8.3
↑ Summary
1 package Filter::HereDocIndent;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Filter::Util::Call;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 # use Debug::ShowStuff ':all';
5 1 ▶use re 'taint';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use vars qw($VERSION $debug);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # documentation at end of file
9  
10  
11 # version
12 1 $VERSION = '1.01';
13  
14 # constants
15 1 ▶use constant REG => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use constant HEREDOC => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use constant POD => 3; # reserved for later improvement
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19  
20 #------------------------------------------------------------------
21 # new
22 #
23 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
24 1 my ($class, %opts) = @_;
25 1 my $self = bless({}, $class);
26
27 # default INDENT_CONTENT
28 1 ▶ defined($opts{'INDENT_CONTENT'}) or $opts{'INDENT_CONTENT'} = 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{"INDENT_CONTENT"} or $opts{"INDENT_CONTENT"} = 1 0 1 1
29 1 $self->{'INDENT_CONTENT'} = $opts{'INDENT_CONTENT'};
30
31 # NWS: strip {nws} out of heredocs
32 1 $self->{'NWS'} = $opts{'NWS'};
33
34 # default state
35 1 $self->{'state'} = REG;
36
37 # return object
38 1 return $self;
39 }
40 #
41 # new
42 #------------------------------------------------------------------
43  
44  
45 #------------------------------------------------------------------
46 # import routine: creates filter object and adds it
47 # to the filters array
48 #
49 1 ▶sub import {
Subroutine import: called CC=3 SCAR=11.7
50 1 my ($class, %opts) = @_;
51
52 # add filter if set to do so
53 1 ▶ if ( defined($opts{'filter_add'}) ? $opts{'filter_add'} : 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $opts{"filter_add"} ? $opts{"filter_add"} : 1) 1 0 1
defined $opts{'filter_add'} ? : 0 1 1
54 1 filter_add($class->new(%opts));
55 }
56 }
57 #
58 # import routine
59 #------------------------------------------------------------------
60  
61  
62 #------------------------------------------------------------------
63 # filter: this sub is run for every line in the calling script
64 #
65 62 ▶sub filter {
Subroutine filter: called CC=1 SCAR=0.0
Pod undocumented
66 62 my $self = shift;
67 62 my $status = filter_read() ;
68 62 my $line = $_;
69
70 62 ($status, $line) = $self->process_line($status, $line);
71
72 # set line and return value
73 62 $_= $line;
74 62 $status;
75 }
76 #
77 # filter
78 #------------------------------------------------------------------
79  
80  
81  
82 #------------------------------------------------------------------
83 # filter_block
84 #
85 0 ▶sub filter_block {
Subroutine filter_block: not called CC=2 SCAR=17.9
Pod undocumented
86 0 my ($self, $block) = @_;
87 0 my (@lines, $status, $rv);
88
89 # parse block into lines
90 0 @lines = split("\n", $block);
91
92 # loop through lines
93 LINE_LOOP:
94 0 foreach my $line (@lines) {
95 0 ($status, $line) = $self->process_line(1, "$line\n");
96 }
97
98 # get return string
99 0 $rv = join('', @lines);
100
101 # return
102 0 return $rv;
103 }
104 #
105 # filter_block
106 #------------------------------------------------------------------
107  
108  
109  
110 #------------------------------------------------------------------
111 # process_line
112 #
113 62 ▶sub process_line {
Subroutine process_line: called CC=14 SCAR=26.9
Pod undocumented
114 62 my ($self, $status, $line) = @_;
115
116 # if we're at the end of the file
117 62 ▶ if (! $status) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $status) { } 1 61 62
elsif ($self->{'state'} == 2) { } 8 53 61
118 # for debugging this module
119 1 ▶ if ($debug)
Branch: 1 branch
BranchTrueFalseTotal
if ($Filter::HereDocIndent::debug) 0 1 1
120 0 {print STDERR "\n--------------------------\n"}
121 }
122
123 # if in here doc
124 elsif ($self->{'state'} == HEREDOC) {
125 # if this is the end of the heredoc
126 8 ▶ if ($line =~ m|^(\s*)$self->{'del_regex'}\s*$|) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^(\s*)$self->{'del_regex'}\s*$/) { } 2 6 8
127 2 my $len = length($1);
128
129 2 ▶ if ($self->{'INDENT_CONTENT'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"INDENT_CONTENT"}) 2 0 2
130 2 foreach my $el (@{$self->{'lines'}}) {
131 6 ▶ $el =~ s|^\s{$len}|| or $el =~ s|^\s+||;
Branch: 1 branch
BranchTrueFalseTotal
$el =~ s/^\s{$len}// or $el =~ s/^\s+// 5 1 6
132 6 ▶ $el eq '' and $el = "\n";
Branch: 1 branch
BranchTrueFalseTotal
$el eq "" and $el = "\n" 0 6 6
133 }
134 }
135
136 2 $line = join('', @{$self->{'lines'}}, $self->{'del'}, "\n");
137
138 # add empty lines so that line numbers match up with original code
139 # NOTE: The following line still doesn't address the issue of
140 # {nws} removals
141 # $line .= "\n" x scalar(@{$self->{'lines'}});
142
143 2 foreach (@{$self->{'lines'}})
144 6 { $line .= "\n" }
145
146 # remove whitespace
147 2 ▶ if ($self->{'NWS'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"NWS"}) 0 2 2
148 0 $line =~ s|\s*\{nws\}\s*||gs;
149 }
150
151 # set state to regular code
152 2 $self->{'state'} = REG;
153 }
154
155 # else add to lines array
156 else {
157 6 push @{$self->{'lines'}}, $line;
158 6 $line = '';
159 }
160 }
161
162 # else in regular code
163 else {
164 # if this line starts a heredoc
165 53 ▶ if ($line =~ m/
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ / ^ # start of line [^#]* # anything except a comment marker << \s* ( '[^']+' | "[^"]+" | \w+ ) [^'"]* ; \s* /sx) 2 51 53
166 ^ # start of line
167 [^#]* # anything except a comment marker
168 <<
169 \s*
170
171 (
172 '[^']+'
173 |
174 "[^"]+"
175 |
176 \w+
177 )
178
179 [^'"]*
180 ;
181 \s*
182
183 /sx
184 ) {
185
186 2 $self->{'del'} = $1;
187 2 ▶ $self->{'del'} =~ s|^'(.*)'$|$1| or $self->{'del'} =~ s|^"(.*)"$|$1|;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"del"} =~ s/^'(.*)'$/$1/ or $self->{"del"} =~ s/^"(.*)"$/$1/ 2 0 2
188 2 $self->{'del_regex'} = quotemeta($self->{'del'});
189
190 2 $self->{'lines'} = [];
191 2 $self->{'state'} = HEREDOC;
192 }
193 }
194
195 # for debugging this module
196 62 ▶ print STDERR $line if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Filter::HereDocIndent::debug 0 62 62
197
198 62 return ($status, $line);
199 }
200 #
201 # process_line
202 #------------------------------------------------------------------
203  
204  
205  
206 # return true
207 1 1;
208  
209 __END__
↑ Summary