Compile.pm

filter: Statementstmt 92.5% 262 / 283 Branchbran 68.7% 66 / 96 Conditioncond 71.7% 33 / 46 MC/DCmcdc 48.1% 13 / 27 Subroutinesub 87.5% 35 / 40 Podpod 0.0% 0 / 28 totaltotal 78.6% 409 / 520 CC 67 SCAR 18.4
CC
67
Cov
85%
pmc_output
13.6
pmc_parse_blocks
13.6
pmc_reduce
11.0
CRAP 6.3
↑ Summary Compile/Opt.pm »
1 # To Do:
2 #
3 # - Make preface part of parsed code, since it might contain `package`
4 # statements or other scoping stuff.
5 # - Build code into an AST.
6 9 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
7 package Module::Compile;
8 9 our $VERSION = '0.38';
9  
10 9 ▶use Digest::SHA1();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # A lexical hash to keep track of which files have already been filtered
13 9 my $filtered = {};
14  
15 # A map of digests to code blocks
16 9 my $digest_map = {};
17  
18 # All subroutines are prefixed with pmc_ so subclasses don't
19 # accidentally override things they didn't intend to.
20  
21 # Determine which stack frame points to the code we are filtering.
22 # This is a method in case it needs to be overridden.
23 3 ▶sub pmc_caller_stack_frame { 0 };
Subroutine pmc_caller_stack_frame: called CC=1 SCAR=0.0
Pod undocumented
24  
25 # This is called while parsing source code to determine if the
26 # module/class in a use/no line is part of the Module::Compile game.
27 #
28 # Return true if this class supports PMC compilation.
29 #
30 # The hope is that this will allow interoperability with modules that
31 # do not inherit from Module::Compile but still want to do this sort
32 # of thing.
33 12 ▶sub pmc_is_compiler_module { 1 };
Subroutine pmc_is_compiler_module: called CC=1 SCAR=0.0
Pod undocumented
34  
35 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
36 0 return bless {}, shift;
37 }
38  
39 # This is called to determine whether the meaning of use/no is reversed.
40 20 ▶sub pmc_use_means_no { 0 }
Subroutine pmc_use_means_no: called CC=1 SCAR=0.0
Pod undocumented
41  
42 # This is called to determine whether the use line means a one line section.
43 7 ▶sub pmc_use_means_now { 0 }
Subroutine pmc_use_means_now: called CC=1 SCAR=0.0
Pod undocumented
44  
45 # All Module::Compile based modules inherit this import routine.
46 8 ▶sub import {
Subroutine import: called CC=2 SCAR=7.0
47 8 my ($class) = @_;
48 8 ▶ return if $class->pmc_use_means_no;
Branch: 1 branch
BranchTrueFalseTotal
if $class->pmc_use_means_no 0 8 8
49 8 goto &{$class->can('pmc_import')};
50 }
51  
52 # Treat unimport like import if use means no
53 0 ▶sub unimport {
Subroutine unimport: not called CC=2 SCAR=17.9
54 0 my ($class) = @_;
55 0 ▶ return unless $class->pmc_use_means_no;
Branch: 1 branch
BranchTrueFalseTotal
unless $class->pmc_use_means_no 0 0 0
56 0 goto &{$class->can('pmc_import')};
57 }
58  
59 8 ▶sub pmc_import {
Subroutine pmc_import: called CC=3 SCAR=11.0
Pod undocumented
60 8 my ($class, @args) = @_;
61  
62 # Handler modules can do `use Module::Compile -base;`. Make them ISA
63 # Module::Compile and get the hell out of Dodge.
64 8 ▶ $class->pmc_set_base(@args) and return;
Branch: 1 branch
BranchTrueFalseTotal
$class->pmc_set_base(@args) and return 5 3 8
65  
66 3 my ($module, $line) = (caller($class->pmc_caller_stack_frame))[1, 2];
67  
68 3 ▶ return if $filtered->{$module}++;
Branch: 1 branch
BranchTrueFalseTotal
if $filtered->{$module}++ 0 3 3
69  
70 3 ▶ my $callback = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
71 3 my ($class, $content, $data) = @_;
72 3 my $output = $class->pmc_template($module, $content, $data);
73 3 $class->pmc_output($module, $output);
74 3 };
75  
76 3 $class->pmc_check_compiled_file($module);
77  
78 3 $class->pmc_filter($module, $line, $callback);
79  
80 # Is there a meaningful return value here?
81 3 return;
82 }
83  
84 # File might not be a module (.pm) and might be compiled already.
85 # If so, run the compiled file.
86 3 ▶sub pmc_check_compiled_file {
Subroutine pmc_check_compiled_file: called CC=5 SCAR=17.2
Pod undocumented
87 3 my ($class, $file) = @_;
88  
89 3 ▶ if (defined $file and $file !~ /\.pm$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file and not $file =~ /\.pm$/i) 1 2 3
Condition66%2/3
defined $file and not $file =~ /\.pm$/i
!ll&&!rl&&r
021
MC/DC50%1/2
defined $file and not $file =~ /\.pm$/i
defined $file not $file =~ /\.pm$/i
Truth table66%2/3
A and not(B)
A= defined $fileB= not $file =~ /\.pm$/i
ABresult
0X0
100
111
90 # Do the freshness check ourselves
91 1 my $pmc = $file.'c';
92 1 ▶ $class->pmc_run_compiled_file($pmc), die
Branch: 1 branch
BranchTrueFalseTotal
if -s $pmc and -M $pmc <= -M $file 0 1 1
Condition33%1/3
-s $pmc and -M $pmc <= -M $file
!ll&&!rl&&r
100
MC/DC0%0/2
-s $pmc and -M $pmc <= -M $file
-s $pmc -M $pmc <= -M $file
Truth table33%1/3
A and B
A= -s $pmcB= -M $pmc <= -M $file
ABresult
0X0
100
111
93 if -s $pmc and (-M $pmc <= -M $file);
94 }
95 }
96  
97 0 ▶sub pmc_run_compiled_file {
Subroutine pmc_run_compiled_file: not called CC=2 SCAR=17.9
Pod undocumented
98 0 my ($class, $pmc) = @_;
99 0 my ($package) = caller($class->pmc_file_caller_frame());
100 0 eval "package $package; do \$pmc";
101 0 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
102 0 exit 0;
103 }
104  
105 0 ▶sub pmc_file_caller_frame { 2 }
Subroutine pmc_file_caller_frame: not called CC=1 SCAR=6.9
Pod undocumented
106  
107 # Set up inheritance
108 8 ▶sub pmc_set_base {
Subroutine pmc_set_base: called CC=4 SCAR=13.9
Pod undocumented
109 8 my ($class, $flag) = @_;
110  
111 # Handle the `use Module::Compile -base;` command.
112 8 ▶ if ($class->isa(__PACKAGE__) and defined $flag and $flag eq '-base') {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->isa(__PACKAGE__) and defined $flag and $flag eq "-base") 5 3 8
Condition66%4/6
$class->isa(__PACKAGE__) and defined $flag and $flag eq "-base"
!ll&&!rl&&r
305
$class->isa(__PACKAGE__) and defined $flag
!ll&&!rl&&r
035
MC/DC33%1/3
$class->isa(__PACKAGE__) and defined $flag and $flag eq "-base"
$class->isa(__PACKAGE__) defined $flag $flag eq "-base"
Truth table50%2/4
A and B and C
A= $class->isa(__PACKAGE__)B= defined $flagC= $flag eq "-base"
ABCresult
0XX0
10X0
1100
1111
113 5 my $descendant = (caller 1)[0];;
114 9 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
115 5 push @{$descendant . '::ISA'}, $class;
116 5 return 1;
117 }
118  
119 3 return 0;
120 }
121  
122 # Generate the actual code that will go into the .pmc file.
123 3 ▶sub pmc_template {
Subroutine pmc_template: called CC=2 SCAR=6.9
Pod undocumented
124 3 my ($class, $module, $content, $data) = @_;
125 3 my $base = __PACKAGE__;
126 3 my $check = $class->freshness_check($module);
127 3 ▶ my $version = $class->VERSION || '0';
Condition100%2/2
$class->VERSION || '0'
l!l
21
MC/DC100%1/1
$class->VERSION || '0'
$class->VERSION
Truth table100%2/2
A
A= $class->VERSION
Aresult
11
00
128 3 return join "\n",
129 "# Generated by $class $version ($base $VERSION) - do not edit!",
130 "$check$content$data";
131 }
132  
133 # This returns a piece of Perl code to do a runtime check to see if the
134 # .pmc file is fresh. By default we use a 32-bit running checksum.
135 3 ▶sub freshness_check {
Subroutine freshness_check: called CC=2 SCAR=7.0
Pod undocumented
136 3 my ($class, $module) = @_;
137 3 my $sum = sprintf('%08X', do {
138 3 local $/;
139 3 ▶ open my $fh, "<", $module
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $module or die "Cannot open ${module}: $!" 3 0 3
140 or die "Cannot open $module: $!";
141 3 binmode($fh, ':crlf'); # normalize CRLF for consistent checksum
142 3 unpack('%32N*', <$fh>);
143 });
144 3 return << "...";
145 ################((( 32-bit Checksum Validator III )))################
146 #line 1
147 BEGIN { use 5.006; local (*F, \$/); (\$F = __FILE__) =~ s!c\$!!; open(F)
148 or die "Cannot open \$F: \$!"; binmode(F, ':crlf'); if (unpack('%32N*',
149 \$F=readline(*F)) != 0x$sum) { use Filter::Util::Call; my \$f = \$F;
150 filter_add(sub { filter_del(); 1 while &filter_read; \$_ = \$f; 1; })}}
151 #line 1
152 ...
153 }
154  
155 # Write the output to the .pmc file
156 3 ▶sub pmc_output {
Subroutine pmc_output: called CC=8 SCAR=26.1
Pod undocumented
157 3 my ($class, $module, $output) = @_;
158 3 ▶ $class->pmc_can_output($module)
Branch: 1 branch
BranchTrueFalseTotal
$class->pmc_can_output($module) or return 0 3 0 3
159 or return 0;
160 3 my $pmc = $module . 'c';
161  
162 # If we can't open the file, just return. The filtering will not be cached,
163 # but that might be ok.
164 3 ▶ open my $fh, ">", $pmc
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $pmc or return 0 3 0 3
165 or return 0;
166  
167 # Protect against disk full or whatever else.
168 3 local $@;
169 3 eval {
170 3 ▶ print $fh $output
Branch: 1 branch
BranchTrueFalseTotal
print $fh $output or die 3 0 3
171 or die;
172 3 ▶ close $fh
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die 3 0 3
173 or die;
174 };
175 3 ▶ if ( my $e = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $@) 0 3 3
176 # close $fh? die if unlink?
177 0 ▶ if ( -e $pmc ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $pmc) 0 0 0
178 0 ▶ unlink $pmc
Branch: 1 branch
BranchTrueFalseTotal
unlink $pmc or die "Can't delete errant ${pmc}: $!" 0 0 0
179 or die "Can't delete errant $pmc: $!";
180 }
181 0 return 0;
182 }
183  
184 3 return 1;
185 }
186  
187 # Check whether output can be written.
188 3 ▶sub pmc_can_output {
Subroutine pmc_can_output: called CC=1 SCAR=0.0
Pod undocumented
189 3 my ($class, $file_path) = @_;
190 3 return 1;
191 # return $file_path =~ /\.pm$/;
192 }
193  
194 # We use a source filter to get all the code for compiling.
195 3 ▶sub pmc_filter {
Subroutine pmc_filter: called CC=3 SCAR=11.0
Pod undocumented
196 3 my ($class, $module, $line_number, $post_process) = @_;
197  
198 # Read original module source code instead of taking from filter,
199 # because we need all the lines including the ones before the `use`
200 # statement, so we can parse Perl into packages and such.
201 3 ▶ open my $fh, $module
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, $module or die "Can't open $module for input:\n$!" 3 0 3
202 or die "Can't open $module for input:\n$!";
203 3 my $module_content = do { local $/; <$fh> };
204 3 close $fh;
205  
206 # Find the real __DATA__ or __END__ line. (Not one hidden in a Pod
207 # section or heredoc).
208 3 my $folded_content = $class->pmc_fold_blocks($module_content);
209 3 my $folded_data = '';
210 3 ▶ if ($folded_content =~ s/^((?:__(?:DATA|END)__$).*)//ms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($folded_content =~ s/^((?:__(?:DATA|END)__$).*)//ms) 2 1 3
211 2 $folded_data = $1;
212 }
213 3 my $real_content = $class->pmc_unfold_blocks($folded_content);
214 3 my $real_data = $class->pmc_unfold_blocks($folded_data);
215  
216 # Calculate the number of lines to skip in the source filter, since
217 # we already have them in $real_content.
218 3 my @lines = ($real_content =~ /(.*\n)/g);
219 3 my $lines_to_skip = @lines;
220 3 $lines_to_skip -= $line_number;
221  
222 # Use filter to skip past that many lines
223 # Leave __DATA__ section intact
224 3 my $done = 0;
225 3 require Filter::Util::Call;
226 4 ▶ Filter::Util::Call::filter_add(sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
227 4 ▶ return 0 if $done;
Branch: 1 branch
BranchTrueFalseTotal
if $done 1 3 4
228 3 my $data_line = '';
229 3 while (1) {
230 38 my $status = Filter::Util::Call::filter_read();
231 38 ▶ last unless $status;
Branch: 1 branch
BranchTrueFalseTotal
unless $status 1 37 38
232 37 ▶ return $status if $status < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $status < 0 0 37 37
233 # Skip lines up to the DATA section.
234 37 ▶ next if $lines_to_skip-- > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $lines_to_skip-- > 0 35 2 37
235 2 ▶ if (/^__(?:END|DATA)__$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^__(?:END|DATA)__$/) 2 0 2
236 # Don't filter the DATA section, or else the DATA file
237 # handle becomes invalid.
238  
239 # XXX - Maybe there is a way to simply recreate the DATA
240 # file handle, or at least seek back to the start of it.
241 # Needs investigation.
242  
243 # For now this means that we only allow compilation on
244 # the module content; not the DATA section. Because we
245 # want to make sure that the program runs the same way
246 # as both a .pm and a .pmc.
247  
248 2 $data_line = $_;
249 2 last;
250 }
251 }
252 continue {
253 35 $_ = '';
254 }
255  
256 3 $real_content =~ s/\r//g;
257 3 my $filtered_content = $class->pmc_process($real_content);
258 3 $class->$post_process($filtered_content, $real_data);
259  
260 3 $filtered_content =~ s/(.*\n){$line_number}//;
261  
262 3 $_ = $filtered_content . $data_line;
263  
264 3 $done = 1;
265 3 });
266 }
267  
268 9 ▶use constant TEXT => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
269 9 ▶use constant CONTEXT => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
270 9 ▶use constant CLASSES => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
271 # Break the code into blocks. Compile the blocks.
272 # Fold out heredocs etc
273 # Parse the code into packages, blocks and subs
274 # Parse the code by `use/no *::Compiler`
275 # Build an AST
276 # Reduce the AST until fully reduced
277 # Return the result
278 5 ▶sub pmc_process {
Subroutine pmc_process: called CC=5 SCAR=16.6
Pod undocumented
279 5 my $class = shift;
280 5 my $data = shift;
281 5 my @blocks = $class->pmc_parse_blocks($data);
282 5 ▶ while (@blocks = $class->pmc_reduce(@blocks)) {
Branch: 1 branch
BranchTrueFalseTotal
if (@blocks = $class->pmc_reduce(@blocks)) 19 0 19
283 19 ▶ if (@blocks == 1 and @{$blocks[0][CLASSES]} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@blocks == 1 and @{$blocks[0][2];} == 0) 5 14 19
Condition66%2/3
@blocks == 1 and @{$blocks[0][2];} == 0
!ll&&!rl&&r
1405
MC/DC50%1/2
@blocks == 1 and @{$blocks[0][2];} == 0
@blocks == 1 @{$blocks[0][2];} == 0
Truth table66%2/3
A and B
A= @blocks == 1B= @{$blocks[0][2];} == 0
ABresult
0X0
100
111
284 5 my $content = $blocks[0][TEXT];
285 5 ▶ $content .= "\n" unless $content =~ /\n\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $content =~ /\n\z/ 0 5 5
286 5 return $content;
287 }
288 }
289 0 die "How did I get here?!?";
290 }
291  
292 # Analyze the remaining blocks and determine which compilers to call to reduce
293 # the problem.
294 #
295 # XXX This routine must do some kind of reduction each pass, or infinite loop
296 # will ensue. It is not yet certain if this is the case.
297 19 ▶sub pmc_reduce {
Subroutine pmc_reduce: called CC=11 SCAR=24.0
Pod undocumented
298 19 my $class = shift;
299 19 my @blocks;
300 my $prev;
301 19 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 44 19 63
302 44 my $block = shift;
303 44 my $next = $_[TEXT];
304 44 ▶ if ($next and "@{$block->[CLASSES]}" eq "@{$next->[CLASSES]}") {
Branch: 2 branches
BranchTrueFalseTotal
if ($next and join($", @{$block->[2];}) eq join($", @{$next->[2];})) { } 13 31 44
elsif (!$prev || @{$prev->[2];} < @{$block->[2];} and !$next || @{$next->[2];} < @{$block->[2];}) { } 8 33 41
Condition100%3/3
$next and join($", @{$block->[2];}) eq join($", @{$next->[2];})
!ll&&!rl&&r
121913
MC/DC100%2/2
$next and join($", @{$block->[2];}) eq join($", @{$next->[2];})
$next join($", @{$block->[2];}) eq join($", @{$next->[2];})
Truth table100%3/3
A and B
A= $nextB= join($", @{$block->[2];}) eq join($", @{$next->[2];})
ABresult
0X0
100
111
305 13 shift;
306 13 $block->[TEXT] .= $next->[TEXT];
307 }
308 ▶ elsif (
Condition100%6/6
!$prev || @{$prev->[2];} < @{$block->[2];} and !$next || @{$next->[2];} < @{$block->[2];}
!ll&&!rl&&r
10138
not $prev or @{$prev->[2];} < @{$block->[2];}
l!l&&r!l&&!r
111010
MC/DC66%2/3
!$prev || @{$prev->[2];} < @{$block->[2];} and !$next || @{$next->[2];} < @{$block->[2];}
not $prev @{$prev->[2];} < @{$block->[2];} !$next || @{$next->[2];} < @{$block->[2];}
Truth table80%4/5
not(A) or B and C
A= not $prevB= @{$prev->[2];} < @{$block->[2];}C= !$next || @{$next->[2];} < @{$block->[2];}
ABCresult
00X0
1X00
0100
1X11
0111
309 20 ▶ (not $prev or @{$prev->[CLASSES]} < @{$block->[CLASSES]}) and
Condition100%3/3
not $next or @{$next->[2];} < @{$block->[2];}
l!l&&r!l&&!r
3513
MC/DC100%2/2
not $next or @{$next->[2];} < @{$block->[2];}
not $next @{$next->[2];} < @{$block->[2];}
Truth table100%3/3
not(A) or B
A= not $nextB= @{$next->[2];} < @{$block->[2];}
ABresult
1X1
011
000
310 18 (not $next or @{$next->[CLASSES]} < @{$block->[CLASSES]})
311 ) {
312 8 ▶ my $prev_len = $prev ? @{$prev->[CLASSES]} : 0;
Branch: 1 branch
BranchTrueFalseTotal
$prev ? : 8 0 8
313 8 ▶ my $next_len = $next ? @{$next->[CLASSES]} : 0;
Branch: 1 branch
BranchTrueFalseTotal
$next ? : 5 3 8
314 8 ▶ my $offset = ($prev_len > $next_len) ? $prev_len : $next_len;
Branch: 1 branch
BranchTrueFalseTotal
$prev_len > $next_len ? : 0 8 8
315 8 my $length = @{$block->[CLASSES]} - $offset;
316 8 $class->pmc_call($block, $offset, $length);
317 }
318 44 push @blocks, $block;
319 44 $prev = $block;
320 }
321 19 return @blocks;
322 }
323  
324 # Call a set of compilers on a piece of source code.
325 8 ▶sub pmc_call {
Subroutine pmc_call: called CC=5 SCAR=16.1
Pod undocumented
326 8 my $class = shift;
327 8 my $block = shift;
328 8 my $offset = shift;
329 8 my $length = shift;
330  
331 8 my $text = $block->[TEXT];
332 8 my $context = $block->[CONTEXT];
333 8 my @classes = splice(@{$block->[CLASSES]}, $offset, $length);
334 8 for my $klass (@classes) {
335 8 local $_ = $text;
336 8 ▶ my $return = $klass->pmc_compile($text, ($context->{$klass} || {}));
Condition50%1/2
$context->{$klass} || {}
l!l
80
MC/DC0%0/1
$context->{$klass} || {}
$context->{$klass}
Truth table50%1/2
A
A= $context->{$klass}
Aresult
11
00
337 8 ▶ $text = (defined $return and $return !~ /^\d+\z/)
Branch: 1 branch
BranchTrueFalseTotal
defined $return && !($return =~ /^\d+\z/) ? : 5 3 8
Condition66%2/3
defined $return and not $return =~ /^\d+\z/
!ll&&!rl&&r
035
MC/DC50%1/2
defined $return and not $return =~ /^\d+\z/
defined $return not $return =~ /^\d+\z/
Truth table66%2/3
A and not(B)
A= defined $returnB= not $return =~ /^\d+\z/
ABresult
0X0
100
111
338 ? $return
339 : $_;
340 }
341 8 $block->[TEXT] = $text;
342 }
343  
344 # Divide a Perl module into blocks. This code divides a module based on
345 # lines that use/no a Module::Compile subclass.
346 6 ▶sub pmc_parse_blocks {
Subroutine pmc_parse_blocks: called CC=13 SCAR=26.1
Pod undocumented
347 6 my $class = shift;
348 6 my $data = shift;
349 6 my @blocks = ();
350 6 my @classes = ();
351 6 my $context = {};
352 6 my $text = '';
353 6 my @parts = split /^([^\S\n]*(?:use|no)[^\S\n]+[\w\:\']+[^\n]*\n)/m, $data;
354 6 for my $part (@parts) {
355 40 ▶ if ($part =~ /^[^\S\n]*(use|no)[^\S\n]+([\w\:\']+)[^\n]*\n/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($part =~ /^[^\S\n]*(use|no)[^\S\n]+([\w\:\']+)[^\n]*\n/) { } 17 23 40
356 17 my ($use, $klass, $file) = ($1, $2, $2);
357 17 $file =~ s{(?:::|')}{/}g;
358 17 ▶ if ($klass =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($klass =~ /^\d+$/) 0 17 17
359 0 $text .= $part;
360 0 next;
361 }
362 {
363 17 local $@;
364 17 eval { require "$file.pm" };
365 17 ▶ die $@ if $@ and "$@" !~ /^Can't locate /;
Branch: 1 branch
BranchTrueFalseTotal
if $@ and not "$@" =~ /^Can't locate / 0 17 17
Condition66%2/3
$@ and not "$@" =~ /^Can't locate /
!ll&&!rl&&r
1250
MC/DC0%0/2
$@ and not "$@" =~ /^Can't locate /
$@ not "$@" =~ /^Can't locate /
Truth table66%2/3
A and not(B)
A= $@B= not "$@" =~ /^Can't locate /
ABresult
0X0
100
111
366 }
367 17 ▶ if ($klass->can('pmc_is_compiler_module') and
Branch: 1 branch
BranchTrueFalseTotal
if ($klass->can('pmc_is_compiler_module') and $klass->pmc_is_compiler_module) { } 12 5 17
Condition66%2/3
$klass->can("pmc_is_compiler_module") and $klass->pmc_is_compiler_module
!ll&&!rl&&r
5012
MC/DC50%1/2
$klass->can("pmc_is_compiler_module") and $klass->pmc_is_compiler_module
$klass->can("pmc_is_compiler_module") $klass->pmc_is_compiler_module
Truth table66%2/3
A and B
A= $klass->can("pmc_is_compiler_module")B= $klass->pmc_is_compiler_module
ABresult
0X0
100
111
368 $klass->pmc_is_compiler_module) {
369 12 push @blocks, [$text, {%$context}, [@classes]];
370 12 $text = '';
371 12 @classes = grep {$_ ne $klass} @classes;
372 12 ▶ if (($use eq 'use') xor $klass->pmc_use_means_no) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use eq 'use' xor $klass->pmc_use_means_no) { } 9 3 12
Condition50%2/4
$use eq "use" xor $klass->pmc_use_means_no
l&&rl&&!r!l&&r!l&&!r
0903
MC/DC50%1/2
$use eq "use" xor $klass->pmc_use_means_no
$use eq "use" $klass->pmc_use_means_no
Truth table50%2/4
A xor B
A= $use eq "use"B= $klass->pmc_use_means_no
ABresult
110
101
011
000
373 9 push @classes, $klass;
374 9 ▶ $context->{$klass} = {%{$context->{$klass} || {}}};
Condition50%1/2
$context->{$klass} || {}
l!l
09
MC/DC0%0/1
$context->{$klass} || {}
$context->{$klass}
Truth table50%1/2
A
A= $context->{$klass}
Aresult
11
00
375 9 $context->{$klass}{use} = $part;
376 9 ▶ if ($klass->pmc_use_means_now) {
Branch: 1 branch
BranchTrueFalseTotal
if ($klass->pmc_use_means_now) 2 7 9
377 2 push @blocks, ['', {%$context}, [@classes]];
378 2 @classes = grep {$_ ne $klass} @classes;
379 2 delete $context->{$klass};
380 }
381 }
382 else {
383 3 delete $context->{$klass};
384 }
385 }
386 else {
387 5 $text .= $part;
388 }
389 }
390 else {
391 23 $text .= $part;
392 }
393 }
394 6 ▶ push @blocks, [$text, {%$context}, [@classes]]
Branch: 1 branch
BranchTrueFalseTotal
if length $text 6 0 6
395 if length $text;
396 6 return @blocks;
397 }
398  
399 # Compile/Filter some source code into something else. This is almost
400 # always overridden in a subclass.
401 2 ▶sub pmc_compile {
Subroutine pmc_compile: called CC=1 SCAR=0.0
Pod undocumented
402 2 my ($class, $source_code_string, $context_hashref) = @_;
403 2 return $source_code_string;
404 }
405  
406 # Regexp fragments for matching heredoc, pod section, comment block and
407 # data section.
408 9 my $re_here = qr/
409 (?: # Heredoc starting line
410 ^ # Start of some line
411 ((?-s:.*?)) # $2 - text before heredoc marker
412 <<(?!=) # heredoc marker
413 [\t\x20]* # whitespace between marker and quote
414 ((?>['"]?)) # $3 - possible left quote
415 ([\w\-\.]*) # $4 - heredoc terminator
416 (\3 # $5 - possible right quote
417 (?-s:.*\n)) # and rest of the line
418 (.*?\n) # $6 - Heredoc content
419 (?<!\n[0-9a-fA-F]{40}\n) # Not another digest
420 (\4\n) # $7 - Heredoc terminating line
421 )
422 /xsm;
423  
424 9 my $re_pod = qr/
425 (?:
426 (?-s:^=(?!cut\b)\w+.*\n) # Pod starter line
427 .*? # Pod lines
428 (?:(?-s:^=cut\b.*\n)|\z) # Pod terminator
429 )
430 /xsm;
431  
432 9 my $re_comment = qr/
433 (?:
434 (?m-s:^[^\S\n]*\#.*\n)+ # one or more comment lines
435 )
436 /xsm;
437  
438 9 my $re_data = qr/
439 (?:
440 ^(?:__END__|__DATA__)\n # DATA starter line
441 .* # Rest of lines
442 )
443 /xsm;
444  
445 # Fold each heredoc, pod section, comment block and data section, each
446 # into a single line containing a digest of the original content.
447 #
448 # This makes further dividing of Perl code less troublesome.
449 12 ▶sub pmc_fold_blocks {
Subroutine pmc_fold_blocks: called CC=6 SCAR=17.9
Pod undocumented
450 12 my ($class, $source) = @_;
451  
452 12 $source =~ s/(~{3,})/$1~/g;
453 12 $source =~ s/(^'{3,})/$1'/gm;
454 12 $source =~ s/(^`{3,})/$1`/gm;
455 12 $source =~ s/(^={3,})/$1=/gm;
456  
457 12 while (1) {
458 9 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
459 32 ▶ $source =~ s/
Branch: 1 branch
BranchTrueFalseTotal
$source =~ s[ ( $re_pod | $re_comment | $re_here | $re_data ) ][my $result = $1; $result =~ /\A($re_data)/ ? $class->pmc_fold_data : ($result =~ /\A($re_pod)/ ? $class->pmc_fold_pod : ($result =~ /\A($re_comment)/ ? $class->pmc_fold_comment : ($result =~ /\A($re_here)/ ? $class->pmc_fold_here : die("'${result}' didn't match '${re_comment}'"))));]ex or last 20 12 32
460 (
461 $re_pod |
462 $re_comment |
463 $re_here |
464 $re_data
465 )
466 /
467 20 my $result = $1;
468 20 ▶ $result =~ m{\A($re_data)} ? $class->pmc_fold_data() :
Branch: 4 branches
BranchTrueFalseTotal
$result =~ /\A($re_data)/ ? : 3 17 20
$result =~ /\A($re_pod)/ ? : 3 14 17
$result =~ /\A($re_comment)/ ? : 4 10 14
$result =~ /\A($re_here)/ ? : 10 0 10
469 $result =~ m{\A($re_pod)} ? $class->pmc_fold_pod() :
470 $result =~ m{\A($re_comment)} ? $class->pmc_fold_comment() :
471 $result =~ m{\A($re_here)} ? $class->pmc_fold_here() :
472 die "'$result' didn't match '$re_comment'";
473 /ex or last;
474 }
475  
476 12 $source =~ s/(?<!~)~~~(?!~)/<</g;
477 12 $source =~ s/^'''(?!') /__DATA__\n/gm;
478 12 $source =~ s/^```(?!`)/#/gm;
479 12 $source =~ s/^===(?!=)/=/gm;
480  
481 12 $source =~ s/^(={3,})=/$1/gm;
482 12 $source =~ s/^('{3,})'/$1/gm;
483 12 $source =~ s/^(`{3,})`/$1/gm;
484 12 $source =~ s/(~{3,})~/$1/g;
485  
486 12 return $source;
487 }
488  
489 6 ▶sub pmc_unfold_blocks {
Subroutine pmc_unfold_blocks: called CC=2 SCAR=7.0
Pod undocumented
490 6 my ($class, $source) = @_;
491  
492 6 $source =~ s/
493 (
494 ^__DATA__\n[0-9a-fA-F]{40}\n
495 |
496 ^=pod\s[0-9a-fA-F]{40}\n=cut\n
497 )
498 /
499 3 my $match = $1;
500 3 ▶ $match =~ s!.*?([0-9a-fA-F]{40}).*!$1!s or die;
Branch: 1 branch
BranchTrueFalseTotal
$match =~ s/.*?([0-9a-fA-F]{40}).*/$1/s or die 3 0 3
501 3 $digest_map->{$match}
502 /xmeg;
503  
504 6 return $source;
505 }
506  
507 # Fold a heredoc's content but don't fold other heredocs from the
508 # same line.
509 10 ▶sub pmc_fold_here {
Subroutine pmc_fold_here: called CC=3 SCAR=11.1
Pod undocumented
510 10 my $class = shift;
511 10 my $result = "$2~~~$3$4$5";
512 10 my $preface = '';
513 10 my $text = $6;
514 10 my $stop = $7;
515 10 while (1) {
516 12 ▶ if ($text =~ s!^(([0-9a-fA-F]{40})\n.*\n)!!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ s/^(([0-9a-fA-F]{40})\n.*\n)//) 2 10 12
517 2 ▶ if (defined $digest_map->{$2}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $digest_map->{$2}) { } 2 0 2
518 2 $preface .= $1;
519 2 next;
520 }
521 else {
522 0 $text = $1 . $text;
523 0 last;
524 }
525 }
526 10 last;
527 }
528 10 my $digest = $class->pmc_fold($text);
529 10 $result = "$result$preface$digest\n$stop";
530 10 $result;
531 }
532  
533 3 ▶sub pmc_fold_pod {
Subroutine pmc_fold_pod: called CC=1 SCAR=0.0
Pod undocumented
534 3 my $class = shift;
535 3 my $text = $1;
536 3 my $digest = $class->pmc_fold($text);
537 3 return qq{===pod $digest\n===cut\n};
538 }
539  
540 4 ▶sub pmc_fold_comment {
Subroutine pmc_fold_comment: called CC=1 SCAR=0.0
Pod undocumented
541 4 my $class = shift;
542 4 my $text = $1;
543 4 my $digest = $class->pmc_fold($text);
544 4 return qq{``` $digest\n};
545 }
546  
547 3 ▶sub pmc_fold_data {
Subroutine pmc_fold_data: called CC=1 SCAR=0.0
Pod undocumented
548 3 my $class = shift;
549 3 my $text = $1;
550 3 my $digest = $class->pmc_fold($text);
551 3 return qq{''' $digest\n};
552 }
553  
554 # Fold a piece of code into a unique string.
555 20 ▶sub pmc_fold {
Subroutine pmc_fold: called CC=1 SCAR=0.0
Pod undocumented
556 20 require Digest::SHA1;
557 20 my ($class, $text) = @_;
558 20 my $digest = Digest::SHA1::sha1_hex($text);
559 20 $digest_map->{$digest} = $text;
560 20 return $digest;
561 }
562  
563 # Expand folded code into original content.
564 0 ▶sub pmc_unfold {
Subroutine pmc_unfold: not called CC=1 SCAR=6.9
Pod undocumented
565 0 my ($class, $digest) = @_;
566 0 return $digest_map->{$digest};
567 }
568  
569 9 1;
↑ Summary Compile/Opt.pm »