blib/lib/ifdef.pm

filter: Statementstmt 67.8% 59 / 87 Branchbran 55.1% 32 / 58 Conditioncond 54.5% 6 / 11 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 80.0% 8 / 10 Podpod 100% 3 / 3 totaltotal 62.8% 110 / 175 CC 37 SCAR 35.4
CC
37
Cov
62%
__ANON__
110.0
import
24.5
oneline
15.0
CRAP 34.4
↑ Summary
1 package ifdef;
2  
3 3 $VERSION= '0.10';
4  
5 # be strict from now on
6 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # take all =begin CAPITALS pod sections
9 3 my $ALL;
10  
11 # output all source to be output as diff to STDERR
12 BEGIN {
13 3 ▶ my $diff= $ENV{'IFDEF_DIFF'} || 0;
Subroutine BEGIN: called CC=2 SCAR=7.2
Condition50%1/2
$ENV{'IFDEF_DIFF'} || 0
l!l
03
MC/DC0%0/1
$ENV{'IFDEF_DIFF'} || 0
$ENV{'IFDEF_DIFF'}
Truth table50%1/2
A
A= $ENV{'IFDEF_DIFF'}
Aresult
11
00
14 3 eval "sub DIFF () { $diff }";
15 } #BEGIN
16  
17 # get the necessary modules
18 3 ▶use IO::File ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 # set up source filter for the initial script
21 3 ▶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 3 my $ACTIVATING; # whether we're inside a =begin section being activated
26 3 my $INPOD; # whether we're inside any =pod section
27 3 my $DEPTH; # depth of conditionals
28 3 my @STATE; # state of each level
29 3 my %IFDEF; # filename conversion hash
30  
31 # install an @INC handler
32 unshift( @INC, sub {
33 0 ▶ my ( $ref, $filename, $path )= @_;
Subroutine __ANON__: not called CC=10 SCAR=47.0
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 3 } );
78  
79 # satisfy require
80 3 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 sub process {
91  
92 # process all lines
93 6 ▶ my @line= split m#(?<=$/)#, $_[0];
Subroutine process: called CC=4 SCAR=14.5
Pod documented
94 6 &reset;
95 6 local $_= \my $foo;
96 6 &oneline foreach @line;
97  
98 # close of activating section (e.g. when called by "load")
99 6 ▶ push @line,"}$/" if $ACTIVATING;
Branch: 1 branch
BranchTrueFalseTotal
if $ACTIVATING 1 5 6
100  
101 # return if not in void context
102 6 ▶ return join( '', @line ) if defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if defined wantarray 6 0 6
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 17 ▶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 sub oneline {
123  
124 # let the world know if we should
125 146 ▶ print STDERR "<$_" if DIFF;
Subroutine oneline: called CC=15 SCAR=27.1
Pod documented
126  
127 # it's a pod marker
128 146 ▶ if ( m#^=(\w+)# ){
Branch: 3 branches
BranchTrueFalseTotal
if (/^=(\w+)/) { } 30 116 146
elsif ($INPOD) { } 51 65 116
elsif (/^#\s+([A-Z_0-9]+)\b/) { } 6 59 65
129  
130 # going back to source
131 30 ▶ if ( $1 eq 'cut' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($1 eq 'cut') { } 7 23 30
elsif ($1 eq 'begin') { } 13 10 23
elsif ($1 eq 'end') { } 5 5 10
132 7 ▶ $_= $ACTIVATING ? "}$/" : $/;
Branch: 1 branch
BranchTrueFalseTotal
$ACTIVATING ? : 1 6 7
133 7 &reset;
134 }
135  
136 # beginning potentially special pod section
137 elsif ( $1 eq 'begin' ) {
138 13 ▶ if ( m#^=begin\s+([A-Z_0-9]+)\b# ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^=begin\s+([A-Z_0-9]+)\b/) { } 12 1 13
139  
140 # activating
141 12 ▶ if ( $ALL or $ENV{$1} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ALL or $ENV{$1}) { } 7 5 12
Condition100%3/3
$ALL or $ENV{$1}
l!l&&r!l&&!r
255
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 7 ▶ $_= $ACTIVATING ? "}{$/" : "{;$/";
Branch: 1 branch
BranchTrueFalseTotal
$ACTIVATING ? : 1 6 7
143 7 $ACTIVATING= 1;
144 7 $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 1 $_= $/;
158 1 $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 51 $_= $/;
179 }
180  
181 # looks like comment, make it normal line if so indicated
182 elsif ( m/^#\s+([A-Z_0-9]+)\b/ ) {
183 6 ▶ s/^#\s+(?:[A-Z_0-9]+)\b// if $ENV{$1};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{$1} 3 3 6
184 }
185  
186 # let the world know if we should
187 146 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 sub import {
199  
200 # being called from source (unless it's from the test-suite)
201 4 ▶ warn "The '".
Subroutine import: called CC=9 SCAR=32.0
Branch: 1 branch
BranchTrueFalseTotal
if (caller)[2] and ($_[0] ne "_testing_" and not shift()) 0 4 4
Condition33%2/6
(caller)[2] and ($_[0] ne "_testing_" and not shift())
!ll&&!rl&&r
040
$_[0] ne "_testing_" and not shift()
!ll&&!rl&&r
040
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 4 shift;
208  
209 # check all parameters
210 4 my @ignored;
211 4 foreach (@_) {
212 # it's all
213 1 ▶ if ( m#^:?all$# ) {
Branch: 3 branches
BranchTrueFalseTotal
if (/^:?all$/) { } 1 0 1
elsif (/^:?selected$/) { } 0 0 0
elsif (/^[A-Z_0-9]+$/) { } 0 0 0
214 1 $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 4 ▶ warn "Ignored parameters: @ignored\n" if @ignored;
Branch: 1 branch
BranchTrueFalseTotal
if @ignored 0 4 4
235  
236 # make sure we start with a clean slate
237 4 &reset;
238  
239 # set up source filter
240 return Filter::Util::Call::filter_add( sub {
241 0 ▶ if ( ( $STATUS= Filter::Util::Call::filter_read() ) > 0 ) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if (($STATUS = &Filter::Util::Call::filter_read()) > 0) 0 0 0
242 0 &oneline;
243 }
244 0 $STATUS;
245 4 } );
246 } #import
247  
248 #---------------------------------------------------------------------------
249  
250 __END__
↑ Summary