blib/lib/ifdef.pm

filter: Statementstmt 62.5% 60 / 96 Branchbran 53.1% 34 / 64 Conditioncond 54.5% 6 / 11 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 72.7% 8 / 11 Podpod 100% 4 / 4 totaltotal 59.3% 114 / 192 CC 65 SCAR 33.1
CC
65
Cov
58%
__ANON__
110.0
import
24.7
import
24.7
CRAP 27.4
↑ Summary
1 package ifdef;
2  
3 4 $VERSION= '0.12';
4  
5 # be strict from now on
6 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # take all =begin CAPITALS pod sections
9 4 my $ALL;
10  
11 # output all source to be output as diff to STDERR
12 4 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.2
13 4 ▶ my $diff= $ENV{'IFDEF_DIFF'} || 0;
Condition50%1/2
$ENV{'IFDEF_DIFF'} || 0
l!l
04
MC/DC0%0/1
$ENV{'IFDEF_DIFF'} || 0
$ENV{'IFDEF_DIFF'}
Truth table50%1/2
A
A= $ENV{'IFDEF_DIFF'}
Aresult
11
00
14 4 eval "sub DIFF () { $diff }";
15 } #BEGIN
16  
17 # get the necessary modules
18 4 ▶use IO::File ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 # set up source filter for the initial script
21 4 ▶use Filter::Util::Call ();
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 # initializations
24 my $STATUS; # status as returned by source filter
25 4 my $ACTIVATING; # whether we're inside a =begin section being activated
26 4 my $INPOD; # whether we're inside any =pod section
27 4 my $DEPTH; # depth of conditionals
28 4 my @STATE; # state of each level
29 4 my %IFDEF; # filename conversion hash
30  
31 # install an @INC handler
32 0 ▶unshift( @INC, sub {
Subroutine __ANON__: not called CC=10 SCAR=47.0
33 0 my ( $ref, $filename, $path )= @_;
34  
35 # return what we know of this module
36 0 ▶ if ( !ref $ref ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $ref) 0 0 0
37 0 $ref =~ s#/#::#;
38 0 return $IFDEF{$ref};
39 }
40  
41 # check all directories and handlers
42 0 foreach (@INC) {
43  
44 # let that INC handle do it if it is an INC handle and it's not us
45 0 ▶ if (ref) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_) { } 0 0 0
elsif (-f ($path = "$_/$filename")) { } 0 0 0
46 0 ▶ goto &$_ unless $_ eq $ref;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ eq $ref 0 0 0
47 }
48  
49 # we found the file
50 elsif ( -f ( $path= "$_/$filename" ) ) {
51  
52 # create temp file
53 0 ▶ open( my $in, $path ) or next;
Branch: 1 branch
BranchTrueFalseTotal
open my $in, $path or next 0 0 0
54 0 ▶ my $out= IO::File->new_tmpfile
Branch: 1 branch
BranchTrueFalseTotal
my $out = "IO::File"->new_tmpfile or die "Failed to create temporary file for '${path}': $!\n" 0 0 0
55 or die "Failed to create temporary file for '$path': $!\n";
56 0 $filename =~ s#/#::#;
57 0 $IFDEF{$filename}= $path;
58  
59 # process all lines
60 0 local $_= \my $foo; # current state of localizing $_ ?
61 0 ▶ while ( readline $in ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $in)) 0 0 0
62 0 &oneline;
63 0 print $out $_;
64 }
65 0 close $in;
66 0 &reset;
67  
68 # make sure we start reading from start again
69 0 ▶ $out->seek( 0, 0 ) or die "Failed to seek: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
$out->seek(0, 0) or die "Failed to seek: $!\n" 0 0 0
70  
71 0 return $out;
72 }
73 }
74  
75 # indicate that the rest should be searched (which will fail)
76 0 return;
77 4 } );
78  
79 # satisfy require
80 4 1;
81  
82 #---------------------------------------------------------------------------
83 # process
84 #
85 # Process a string (consisting of many lines)
86 #
87 # IN: 1 string to process
88 # OUT: 1 processed string (in place change if called in void context)
89  
90 7 ▶sub process {
Subroutine process: called CC=4 SCAR=20.7
Pod documented
91  
92 # process all lines
93 0 ▶ my @line= split m#(?<=$/)#, $_[0];
Subroutine process: not called CC=4 SCAR=20.7
Pod documented
94 0 &reset;
95 0 local $_= \my $foo;
96 0 &oneline foreach @line;
97  
98 # close of activating section (e.g. when called by "load")
99 0 ▶ push @line,"}$/" if $ACTIVATING;
Branch: 2 branches
BranchTrueFalseTotal
if $ACTIVATING 2 5 7
if $ACTIVATING 0 0 0
100  
101 # return if not in void context
102 0 ▶ return join( '', @line ) if defined wantarray;
Branch: 2 branches
BranchTrueFalseTotal
if defined wantarray 7 0 7
if defined wantarray 0 0 0
103  
104 # change in place
105 0 $_[0]= join( '',@line );
106  
107 0 return undef;
108 } #process
109  
110 #---------------------------------------------------------------------------
111 # reset
112 #
113 # Reset all internal variables to a known state
114  
115 22 ▶sub reset { $ACTIVATING= $INPOD= $DEPTH= 0 } #reset
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
116  
117 #---------------------------------------------------------------------------
118 # oneline
119 #
120 # Process one line in $_ in place
121  
122 157 ▶sub oneline {
Subroutine oneline: called CC=15 SCAR=27.1
Pod documented
123  
124 # let the world know if we should
125 157 print STDERR "<$_" if DIFF;
126  
127 # it's a pod marker
128 157 ▶ if ( m#^=(\w+)# ){
Branch: 3 branches
BranchTrueFalseTotal
if (/^=(\w+)/) { } 35 122 157
elsif ($INPOD) { } 52 70 122
elsif (/^#\s+([A-Z_0-9]+)\b/) { } 7 63 70
129  
130 # going back to source
131 35 ▶ if ( $1 eq 'cut' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($1 eq 'cut') { } 9 26 35
elsif ($1 eq 'begin') { } 16 10 26
elsif ($1 eq 'end') { } 5 5 10
132 9 ▶ $_= $ACTIVATING ? "}$/" : $/;
Branch: 1 branch
BranchTrueFalseTotal
$ACTIVATING ? : 2 7 9
133 9 &reset;
134 }
135  
136 # beginning potentially special pod section
137 elsif ( $1 eq 'begin' ) {
138 16 ▶ if ( m#^=begin\s+([A-Z_0-9]+)\b# ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^=begin\s+([A-Z_0-9]+)\b/) { } 14 2 16
139  
140 # activating
141 14 ▶ if ( $ALL or $ENV{$1} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ALL or $ENV{$1}) { } 9 5 14
Condition100%3/3
$ALL or $ENV{$1}
l!l&&r!l&&!r
455
MC/DC100%2/2
$ALL or $ENV{$1}
$ALL $ENV{$1}
Truth table100%3/3
A or B
A= $ALLB= $ENV{$1}
ABresult
1X1
011
000
142 9 ▶ $_= $ACTIVATING ? "}{$/" : "{;$/";
Branch: 1 branch
BranchTrueFalseTotal
$ACTIVATING ? : 1 8 9
143 9 $ACTIVATING= 1;
144 9 $INPOD= 0;
145 }
146  
147 # not activating now
148 else {
149 5 ▶ $_= $ACTIVATING ? "}$/" : $/;
Branch: 1 branch
BranchTrueFalseTotal
$ACTIVATING ? : 2 3 5
150 5 $ACTIVATING= 0;
151 5 $INPOD= 1;
152 }
153 }
154  
155 # normal begin of pod
156 else {
157 2 $_= $/;
158 2 $INPOD= 1;
159 }
160 }
161  
162 # at the end of a possibly activated section
163 elsif ( $1 eq 'end' ) {
164 5 ▶ $_ = $ACTIVATING ? "}$/" : $/;
Branch: 1 branch
BranchTrueFalseTotal
$ACTIVATING ? : 2 3 5
165 5 $ACTIVATING= 0;
166 5 $INPOD= 1;
167 }
168  
169 # it's another pod directive
170 else {
171 5 $_= $/;
172 5 $INPOD= 1;
173 }
174 }
175  
176 # already inside pod
177 elsif ($INPOD) {
178 52 $_= $/;
179 }
180  
181 # looks like comment, make it normal line if so indicated
182 elsif ( m/^#\s+([A-Z_0-9]+)\b/ ) {
183 7 ▶ s/^#\s+(?:[A-Z_0-9]+)\b// if $ENV{$1};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{$1} 3 4 7
184 }
185  
186 # let the world know if we should
187 157 print STDERR ">$_" if DIFF;
188 } #oneline
189  
190 #---------------------------------------------------------------------------
191  
192 # Perl specific subroutines
193  
194 #---------------------------------------------------------------------------
195 # IN: 1 class (ignored)
196 # 2..N keys to watch for
197  
198 6 ▶sub import {
Subroutine import: called CC=10 SCAR=32.1
199  
200 # being called from source (unless it's from the test-suite)
201 6 ▶ warn "The '".
Branch: 1 branch
BranchTrueFalseTotal
if (caller)[2] and ($_[0] ne "_testing_" and not shift()) 0 6 6
Condition33%2/6
(caller)[2] and ($_[0] ne "_testing_" and not shift())
!ll&&!rl&&r
060
$_[0] ne "_testing_" and not shift()
!ll&&!rl&&r
060
MC/DC0%0/3
(caller)[2] and ($_[0] ne "_testing_" and not shift())
(caller)[2] $_[0] ne "_testing_" not shift()
Truth table25%1/4
A and B and not(C)
A= (caller)[2]B= $_[0] ne "_testing_"C= not shift()
ABCresult
0XX0
10X0
1100
1111
202 __PACKAGE__.
203 "' pragma is not supposed to be called from source\n"
204 if ( (caller)[2] ) and ( $_[0] ne '_testing_' and !shift );
205  
206 # lose the class
207 6 shift;
208  
209 # check all parameters
210 6 my @ignored;
211 6 foreach (@_) {
212 # it's all
213 2 ▶ if ( m#^:?all$# ) {
Branch: 3 branches
BranchTrueFalseTotal
if (/^:?all$/) { } 2 0 2
elsif (/^:?selected$/) { } 0 0 0
elsif (/^[A-Z_0-9]+$/) { } 0 0 0
214 2 $ALL= 1;
215 }
216  
217 # not all
218 elsif ( m#^:?selected$# ) {
219 0 $ALL= 0;
220 }
221  
222 # looks like an environment var reference
223 elsif ( m#^[A-Z_0-9]+$# ) {
224 0 $ENV{$_}= 1;
225 }
226  
227 # huh?
228 else {
229 0 push @ignored, $_;
230 }
231 }
232  
233 # huh?
234 6 ▶ warn "Ignored parameters: @ignored\n" if @ignored;
Branch: 1 branch
BranchTrueFalseTotal
if @ignored 0 6 6
235  
236 # make sure we start with a clean slate
237 6 &reset;
238  
239 # https://rt.cpan.org/Ticket/Display.html?id=95772
240 # https://github.com/Perl/perl5/issues/24975
241 # Source filters can only be added during the interpreter's
242 # compilation phase; don't try adding one at runtime.
243 6 ▶ return if defined $^S;
Branch: 1 branch
BranchTrueFalseTotal
if defined $^S 2 4 6
244  
245 # set up source filter
246 0 ▶ return Filter::Util::Call::filter_add( sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
247 0 ▶ if ( ( $STATUS= Filter::Util::Call::filter_read() ) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($STATUS = &Filter::Util::Call::filter_read()) > 0) 0 0 0
248 0 &oneline;
249 }
250 0 $STATUS;
251 4 } );
252 } #import
253  
254 #---------------------------------------------------------------------------
255  
256 __END__
↑ Summary