blib/lib/Filter/Simple.pm

filter: Statementstmt 81.0% 150 / 185 Branchbran 60.8% 45 / 74 Conditioncond 27.2% 9 / 33 MC/DCmcdc 11.7% 2 / 17 Subroutinesub 76.9% 20 / 26 Podpod 0.0% 0 / 6 totaltotal 66.2% 226 / 341 SCAR 48.1
CC
51
Cov
70%
__ANON__
42.0
__ANON__
20.3
__ANON__
11.0
CRAP 122.2
↑ Summary
1 package Filter::Simple;
2  
3 8 ▶use Text::Balanced ':ALL';
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 8 ▶use vars qw{ $VERSION @EXPORT };
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 8 $VERSION = '0.92_01';
8  
9 8 ▶use Filter::Util::Call;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 8 @EXPORT = qw( FILTER FILTER_ONLY );
13  
14  
15 sub import {
16 7 ▶ if (@_>1) { shift; goto &FILTER }
Subroutine import: called CC=3 SCAR=13.1
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) { } 0 7 7
17 7 else { *{caller()."::$_"} = \&$_ foreach @EXPORT }
18 }
19  
20 sub fail {
21 0 ▶ croak "FILTER_ONLY: ", @_;
Subroutine fail: not called CC=1 SCAR=6.9
Pod undocumented
22 }
23  
24 my $exql = sub {
25 49 ▶ my @bits = extract_quotelike $_[0], qr//;
Subroutine __ANON__: called CC=2 SCAR=6.9
26 49 ▶ return unless $bits[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $bits[0] 34 15 49
27 15 return \@bits;
28 8 };
29  
30 8 my $ncws = qr/\s+/;
31 8 my $nl = qr/(?:\r\n?|\n)/;
32 8 my $comment = qr/(?<![\$\@%])#.*/;
33 8 my $ws = qr/(?:$ncws|$comment)+/;
34 8 my $id = qr/\b(?!([ysm]|q[rqxw]?|tr)\b)\w+/;
35 8 my $EOP = qr/\n\n|\Z/;
36 8 my $CUT = qr/\n=cut.*$EOP/;
37 8 my $pod_or_DATA = qr/
38 ^=(?:head[1-4]|item) .*? $CUT
39 | ^=pod .*? $CUT
40 | ^=for .*? $CUT
41 | ^=begin .*? $CUT
42 | ^__(DATA|END)__\r?\n.*
43 /smx;
44 8 my $variable = qr{
45 [\$*\@%]\s*
46 \{\s*(?!::)(?:\d+|[][&`'#+*./|,";%=~:?!\@<>()-]|\^[A-Z]?)\}
47 | (?:\$#?|[*\@\%]|\\&)\$*\s*
48 (?: \{\s*(?:\^(?=[A-Z_]))?(?:\w|::|'\w)*\s*\}
49 | (?:\^(?=[A-Z_]))?(?:\w|::|'\w)*
50 | (?=\{) # ${ block }
51 )
52 )
53 | \$\s*(?!::)(?:\d+|[][&`'#+*./|,";%=~:?!\@<>()-]|\^[A-Z]?)
54 }x;
55  
56 8 my %extractor_for = (
57 quotelike => [ $ws, $variable, $id, { MATCH => \&extract_quotelike } ], # here we need to extract here documents better!
58 regex => [ $ws, $pod_or_DATA, $id, $exql ],
59 string => [ $ws, $pod_or_DATA, $id, $exql ],
60 code => [ $ws, { DONT_MATCH => $pod_or_DATA }, $variable,
61 $id, { DONT_MATCH => \&extract_quotelike } ], # here we need to extract here documents better!
62 code_no_comments
63 => [ { DONT_MATCH => $comment },
64 $ncws, { DONT_MATCH => $pod_or_DATA }, $variable,
65 $id, { DONT_MATCH => \&extract_quotelike } ], # here we need to extract here documents better!
66 executable => [ $ws, { DONT_MATCH => $pod_or_DATA } ],
67 executable_no_comments
68 => [ { DONT_MATCH => $comment },
69 $ncws, { DONT_MATCH => $pod_or_DATA } ],
70 all => [ { MATCH => qr/(?s:.*)/ } ],
71 );
72  
73 my %selector_for = (
74 0 ▶ all => sub { my ($t)=@_; sub{ $_=$$_; $t->(@_); $_} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
75 0 ▶ executable=> sub { my ($t)=@_; sub{ref() ? $_=$$_ : $t->(@_); $_} },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 0 0
76 0 ▶ quotelike => sub { my ($t)=@_; sub{ref() && do{$_=$$_; $t->(@_)};
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
ref $_ and do { $_ = $$_; &$t(@_) } 0 0 0
77 # Here, transform here documents back
78 # depending on whether they had content
79 # coming after them or not
80 # This might break other stuff
81 # that relies on that transform
82 0 warn "Returning [[$_]]";
83 0 $_} },
84 0 ▶ regex => sub { my ($t)=@_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
85 0 ▶ sub{ref() or return $_;
Subroutine __ANON__: not called CC=6 SCAR=37.4
Branch: 1 branch
BranchTrueFalseTotal
ref $_ or return $_ 0 0 0
86 0 my ($ql,$rest_of_line,$pre,$op,$ld,$pat) = @$_;
87 0 ▶ return $_->[0] unless $op =~ /^(qr|m|s)/
Branch: 1 branch
BranchTrueFalseTotal
unless $op =~ /^(qr|m|s)/ or not $op and $ld eq "/" || $ld eq "?" 0 0 0
Condition0%0/9
$op =~ /^(qr|m|s)/ or not $op and $ld eq "/" || $ld eq "?"
l!l&&r!l&&!r
000
not $op and $ld eq "/" || $ld eq "?"
!ll&&!rl&&r
000
$ld eq "/" or $ld eq "?"
l!l&&r!l&&!r
000
MC/DC0%0/4
$op =~ /^(qr|m|s)/ or not $op and $ld eq "/" || $ld eq "?"
$op =~ /^(qr|m|s)/ not $op $ld eq "/" $ld eq "?"
Truth table0%0/5
A or not(B) and C or D
A= $op =~ /^(qr|m|s)/B= not $opC= $ld eq "/"D= $ld eq "?"
ABCDresult
1XXX1
011X1
01011
00XX0
01000
88 || !$op && ($ld eq '/' || $ld eq '?');
89 0 $_ = $pat;
90 0 $t->(@_);
91 0 $ql =~ s/^(\s*\Q$op\E\s*\Q$ld\E)\Q$pat\E/$1$_/;
92 0 return "$pre$ql";
93 0 };
94 },
95 2 ▶ string => sub { my ($t)=@_;
Subroutine __ANON__: called CC=1 SCAR=0.6
96 32 ▶ sub{ref() or return $_;
Subroutine __ANON__: called CC=11 SCAR=30.1
Branch: 1 branch
BranchTrueFalseTotal
ref $_ or return $_ 17 15 32
97 15 local *args = \@_;
98 15 my ($pre,$op,$ld1,$str1,$rd1,$ld2,$str2,$rd2,$flg) = @{$_}[2..10];
99 15 ▶ return $_->[0] if $op =~ /^(qr|m)/
Branch: 1 branch
BranchTrueFalseTotal
if $op =~ /^(qr|m)/ or not $op and $ld1 eq "/" || $ld1 eq "?" 0 15 15
Condition33%3/9
$op =~ /^(qr|m)/ or not $op and $ld1 eq "/" || $ld1 eq "?"
l!l&&r!l&&!r
0015
not $op and $ld1 eq "/" || $ld1 eq "?"
!ll&&!rl&&r
0150
$ld1 eq "/" or $ld1 eq "?"
l!l&&r!l&&!r
0015
MC/DC0%0/4
$op =~ /^(qr|m)/ or not $op and $ld1 eq "/" || $ld1 eq "?"
$op =~ /^(qr|m)/ not $op $ld1 eq "/" $ld1 eq "?"
Truth table20%1/5
A or not(B) and C or D
A= $op =~ /^(qr|m)/B= not $opC= $ld1 eq "/"D= $ld1 eq "?"
ABCDresult
1XXX1
011X1
01011
00XX0
01000
100 || !$op && ($ld1 eq '/' || $ld1 eq '?');
101 15 ▶ if (!$op || $op eq 'tr' || $op eq 'y') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $op or $op eq "tr" or $op eq "y") 0 0 0
Condition33%2/6
not $op or $op eq "tr" or $op eq "y"
l!l&&r!l&&!r
1500
not $op or $op eq "tr"
l!l&&r!l&&!r
1500
MC/DC0%0/3
not $op or $op eq "tr" or $op eq "y"
not $op $op eq "tr" $op eq "y"
Truth table25%1/4
not(A) or B or C
A= not $opB= $op eq "tr"C= $op eq "y"
ABCresult
1XX1
01X1
0011
0000
102 15 local *_ = \$str1;
103 15 $t->(@args);
104 }
105 15 ▶ if ($op =~ /^(tr|y|s)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op =~ /^(tr|y|s)/) 0 15 15
106 0 local *_ = \$str2;
107 0 $t->(@args);
108 }
109 15 my $result = "$pre$op$ld1$str1$rd1";
110 15 ▶ $result .= $ld2 if $ld1 =~ m/[[({<]/; #])}>
Branch: 1 branch
BranchTrueFalseTotal
if $ld1 =~ /[[({<]/ 0 15 15
111 15 $result .= "$str2$rd2$flg";
112 15 return $result;
113 2 };
114 },
115 8 );
116  
117 sub gen_std_filter_for {
118 7 ▶ my ($type, $transform) = @_;
Subroutine gen_std_filter_for: called CC=1 SCAR=0.0
Pod undocumented
119 return sub {
120 24 ▶ my $instr;
Subroutine __ANON__: called CC=11 SCAR=24.0
121 24 local @components; # See documentation, this is a global variable
122 24 for (extract_multiple($_,$extractor_for{$type})) {
123 710 ▶ if (ref()) { push @components, $_; $instr=0 }
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_) { } 79 631 710
elsif ($instr) { } 530 101 631
124 530 elsif ($instr) { $components[-1] .= $_; }
125 101 else { push @components, $_; $instr=1 }
126 }
127  
128 24 ▶ if ($type =~ /^code/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /^code/) { } 22 2 24
129 22 my $count = 0;
130 22 local $placeholder = qr/\Q$;\E(.{4})\Q$;\E/s; # see documentation
131 22 my $extractor = $placeholder;
132 22 my $extractor_nc = qr/\Q$;\E(?:.{4})\Q$;\E/s;
133 $_ = join "",
134 22 ▶ map { ref $_ ? $;.pack('N',$count++).$; : $_ }
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 64 84 148
135 @components;
136 22 @components = grep { ref $_ } @components;
137 22 $transform->(@_);
138  
139 22 my @res;
140  
141 # The approach is as follows:
142 # Since we have eliminated all (multiline) strings and comments
143 # and converted them to strings that match /$;....$;/,
144 # we can do our replacement line-by-line, appending here documents
145 # below the line by pushing their output onto @res as well
146  
147 # We split up each line into non-string and string parts and then
148 # rebuild the target string anew, replacing all placeholders
149 # within it. If we encounter a here document, finish the current
150 # line and append the here document immediately as the next line.
151 # If we encounter any other quotelike, replace it inline and then
152 # stash away anything after the newline for later reassembly.
153 # This handles these cases:
154 # foo(<<EOM,q[
155 # EOM
156 # single-quoted]);
157 # and
158 # foo(q[
159 # single-quoted],<<EOM);
160 # EOM
161 my @following_lines;
162 22 my $whole_line = qr/((?:[^$;\r\n]+|$;....$;)*$nl)/s;
163 @res = map {
164 22 ▶ if( ! defined ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $_) { } 0 147 147
elsif (/$placeholder/s) { } 43 104 147
165 0 ''
166 } elsif( /$placeholder/ ) {
167 43 s{$placeholder}{
168 62 my $str = ${$components[unpack('N',$1)]};
169 62 ▶ if( $str =~ s/^(<<[^\r\n]+)$nl// ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($str =~ s/^(<<[^\r\n]+)$nl//) { } 13 49 62
elsif (@following_lines and $str =~ s/^([^\n\r]*$nl)//) { } 2 94 96
170 # Split the here document into the header
171 # and the body
172 13 push @following_lines, $str;
173 13 $str = $1;
174  
175 ▶ } elsif( @following_lines and $str =~ s/^([^\n\r]*$nl)// ) {
Condition66%2/3
@following_lines and $str =~ s/^([^\n\r]*$nl)//
!ll&&!rl&&r
4702
MC/DC50%1/2
@following_lines and $str =~ s/^([^\n\r]*$nl)//
@following_lines $str =~ s/^([^\n\r]*$nl)//
Truth table66%2/3
A and B
A= @following_linesB= $str =~ s/^([^\n\r]*$nl)//
ABresult
0X0
100
111
176 # Here we have a multiline string being
177 # replaced for the placeholder, so we need to
178 # rebuild it respecting potential preceding
179 # here documents
180 2 push @following_lines, $str;
181 2 $str = $1;
182 # Just purge onto the end:
183 2 $str = $1 . join "", (splice( @following_lines, 0 ));
184 }
185 $str
186 62 }ge;
187 # must have been a newline, so purge the
188 # accumulated output:
189 43 ($_,splice(@following_lines));
190 } else {
191 # Plain string replacement
192 104 $_
193 };
194 } /$whole_line/g;
195  
196  
197 22 $_ = join "", @res;
198 }
199 else {
200 2 my $selector = $selector_for{$type}->($transform);
201 2 $_ = join "", map $selector->(@_), @components;
202 }
203 }
204 7 };
205  
206 sub FILTER (&;$) {
207 3 ▶ my $caller = caller;
Subroutine FILTER: called CC=1 SCAR=0.0
Pod undocumented
208 3 my ($filter, $terminator) = @_;
209 8 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
210 3 *{"${caller}::import"} = gen_filter_import($caller,$filter,$terminator);
211 3 *{"${caller}::unimport"} = gen_filter_unimport($caller);
212 }
213  
214 sub FILTER_ONLY {
215 4 ▶ my $caller = caller;
Subroutine FILTER_ONLY: called CC=4 SCAR=13.9
Pod undocumented
216 4 ▶ while (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 6 4 10
217 6 my ($what, $how) = splice(@_, 0, 2);
218 fail "Unknown selector: $what"
219 6 ▶ unless exists $extractor_for{$what};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $extractor_for{$what} 0 6 6
220 6 ▶ fail "Filter for $what is not a subroutine reference"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $how eq "CODE" 0 6 6
221 unless ref $how eq 'CODE';
222 6 push @transforms, gen_std_filter_for($what,$how);
223 }
224 4 my $terminator = shift;
225  
226 my $multitransform = sub {
227 4 ▶ foreach my $transform ( @transforms ) {
Subroutine __ANON__: called CC=2 SCAR=6.9
228 6 $transform->(@_);
229 }
230 4 };
231 8 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
232 4 *{"${caller}::import"} =
233 gen_filter_import($caller,$multitransform,$terminator);
234 4 *{"${caller}::unimport"} = gen_filter_unimport($caller);
235 }
236  
237 8 my $ows = qr/(?:[ \t]+|#[^\n]*)*/;
238  
239 sub gen_filter_import {
240 7 ▶ my ($class, $filter, $terminator) = @_;
Subroutine gen_filter_import: called CC=1 SCAR=0.2
Pod undocumented
241 7 my %terminator;
242 7 my $prev_import = *{$class."::import"}{CODE};
243 return sub {
244 7 ▶ my ($imported_class, @args) = @_;
Subroutine __ANON__: called CC=8 SCAR=22.7
245 7 my $def_terminator =
246 qr/^(?:\s*no\s+$imported_class\s*;$ows|__(?:END|DATA)__)\r?$/;
247 7 ▶ if (!defined $terminator) {
Branch: 4 branches
BranchTrueFalseTotal
if (not defined $terminator) { } 7 0 7
elsif (not ref $terminator or ref $terminator eq 'Regexp') { } 0 0 0
elsif (ref $terminator ne 'HASH') { } 0 0 0
elsif (not exists $terminator->{'terminator'}) { } 0 0 0
248 7 $terminator{terminator} = $def_terminator;
249 }
250 ▶ elsif (!ref $terminator || ref $terminator eq 'Regexp') {
Condition0%0/3
not ref $terminator or ref $terminator eq "Regexp"
l!l&&r!l&&!r
000
MC/DC0%0/2
not ref $terminator or ref $terminator eq "Regexp"
not ref $terminator ref $terminator eq "Regexp"
Truth table0%0/3
not(A) or B
A= not ref $terminatorB= ref $terminator eq "Regexp"
ABresult
1X1
011
000
251 0 $terminator{terminator} = $terminator;
252 }
253 elsif (ref $terminator ne 'HASH') {
254 0 croak "Terminator must be specified as scalar or hash ref"
255 }
256 elsif (!exists $terminator->{terminator}) {
257 0 $terminator{terminator} = $def_terminator;
258 }
259 filter_add(
260 sub {
261 11 ▶ my ($status, $lastline);
Subroutine __ANON__: called CC=10 SCAR=23.4
262 11 my $count = 0;
263 11 my $data = "";
264 11 ▶ while ($status = filter_read()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status = &filter_read()) 103 8 111
265 103 ▶ return $status if $status < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $status < 0 0 103 103
266 103 ▶ if ($terminator{terminator} &&
Branch: 1 branch
BranchTrueFalseTotal
if ($terminator{"terminator"} and /$terminator{"terminator"}/) 3 100 103
Condition66%2/3
$terminator{"terminator"} and /$terminator{"terminator"}/
!ll&&!rl&&r
01003
MC/DC50%1/2
$terminator{"terminator"} and /$terminator{"terminator"}/
$terminator{"terminator"} /$terminator{"terminator"}/
Truth table66%2/3
A and B
A= $terminator{"terminator"}B= /$terminator{"terminator"}/
ABresult
0X0
100
111
267 m/$terminator{terminator}/) {
268 3 $lastline = $_;
269 3 last;
270 }
271 100 $data .= $_;
272 100 $count++;
273 100 $_ = "";
274 }
275 11 ▶ return $count if not $count;
Branch: 1 branch
BranchTrueFalseTotal
unless $count 4 7 11
276 7 $_ = $data;
277 7 ▶ $filter->($imported_class, @args) unless $status < 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $status < 0 7 0 7
278 7 ▶ if (defined $lastline) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $lastline) 3 4 7
279 3 ▶ if (defined $terminator{becomes}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $terminator{'becomes'}) { } 0 3 3
elsif ($lastline =~ /$def_terminator/) { } 3 0 3
280 0 $_ .= $terminator{becomes};
281 }
282 elsif ($lastline =~ $def_terminator) {
283 3 $_ .= $lastline;
284 }
285 }
286 7 return $count;
287 }
288 7 );
289 7 ▶ if ($prev_import) {
Branch: 2 branches
BranchTrueFalseTotal
if ($prev_import) { } 1 6 7
elsif ($class->isa('Exporter')) { } 1 5 6
290 1 goto &$prev_import;
291 }
292 elsif ($class->isa('Exporter')) {
293 1 $class->export_to_level(1,@_);
294 }
295 }
296 7 }
297  
298 sub gen_filter_unimport {
299 7 ▶ my ($class) = @_;
Subroutine gen_filter_unimport: called CC=1 SCAR=0.0
Pod undocumented
300 return sub {
301 2 ▶ filter_del();
Subroutine __ANON__: called CC=2 SCAR=7.2
302 2 ▶ goto &$prev_unimport if $prev_unimport;
Branch: 1 branch
BranchTrueFalseTotal
if $prev_unimport 0 2 2
303 }
304 7 }
305  
306 8 1;
307  
308 __END__
↑ Summary