blib/lib/IfLoop.pm

filter: Statementstmt 48.5% 49 / 101 Branchbran 8.3% 3 / 36 Conditioncond 12.5% 1 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 71.4% 10 / 14 Podpod 0.0% 0 / 5 totaltotal 37.2% 63 / 169 CC 22 SCAR 36.2
CC
22
Cov
37%
while_until_key
110.0
handle_filehandles
20.0
filter_blocks
13.7
CRAP 37.3
↑ Summary
1 package IfLoop;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Filter::Util::Call;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Text::Balanced;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 #require Exporter;
11 #our @ISA = qw(Exporter);
12 # Items to export into callers namespace by default. Note: do not export
13 # names by default without a very good reason. Use EXPORT_OK instead.
14 # Do not simply export all your public functions/methods/constants.
15  
16 # This allows declaration use IfLoop ':all';
17 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
18 # will save memory.
19 #our %EXPORT_TAGS = ( 'all' => [ qw( ) ] );
20 #our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
21 #our @EXPORT = qw();
22  
23 1 our $VERSION = '0.03';
24 1 our $DEBUG = 0;
25  
26 # Helps tell us about where in the file we are.
27 1 my $offset;
28  
29 #---------------------------------------------------------------------------
30 #---------------------------------------------------------------------------
31 sub line
32 0 ▶{
Subroutine line: not called CC=2 SCAR=17.9
Pod undocumented
33 0 my ($pretext,$offset) = @_;
34 0 ▶ ($pretext=~tr/\n/\n/)+($offset||0);
Condition0%0/2
$offset || 0
l!l
00
MC/DC0%0/1
$offset || 0
$offset
Truth table0%0/2
A
A= $offset
Aresult
11
00
35 }
36  
37 #---------------------------------------------------------------------------
38 #---------------------------------------------------------------------------
39 sub import
40 1 ▶{
Subroutine import: called CC=2 SCAR=7.0
41 1 my($type, @arguments) = @_ ;
42  
43 1 ▶ if(scalar(@arguments) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @arguments == 0) 1 0 1
44 {
45 1 @arguments = qw(while until);
46 }
47
48 1 my $tmp = join ':1:', @arguments,':1';
49 1 @arguments = split ':', $tmp;
50  
51 1 $offset = (caller)[2]+1;
52 1 filter_add({@arguments}) ;
53 }
54  
55 #---------------------------------------------------------------------------
56 #---------------------------------------------------------------------------
57 sub unimport
58 0 ▶{
Subroutine unimport: not called CC=1 SCAR=6.9
59 0 filter_del();
60 }
61  
62 #---------------------------------------------------------------------------
63 #---------------------------------------------------------------------------
64 sub handle_filehandles
65 0 ▶{
Subroutine handle_filehandles: not called CC=4 SCAR=30.0
Pod undocumented
66 0 my $bool_condition = shift;
67 0 my $r_source = shift;
68 0 my $line = shift;
69  
70 0 my @pos = Text::Balanced::_match_codeblock(\$bool_condition,
71 qr/\s*/,
72 qr/\(/,qr/\)/,
73 qr/[(<]/,qr/[>)]/,
74 undef);
75 0 ▶ if(@pos)
Branch: 1 branch
BranchTrueFalseTotal
if (@pos) 0 0 0
76 {
77 0 my $tmp = substr($bool_condition,$pos[0],$pos[4]-$pos[0]);
78 0 ▶ if($tmp =~ m/(<.*>)/)
Branch: 1 branch
BranchTrueFalseTotal
if ($tmp =~ /(<.*>)/) 0 0 0
79 {
80 0 my $file_access = $`.$1;
81 0 ▶ if($file_access !~ m/\$\_\s*=\s*<.*>/o)
Branch: 1 branch
BranchTrueFalseTotal
if (not $file_access =~ /\$\_\s*=\s*<.*>/o) 0 0 0
82 {
83 0 die "Filehandles \"<FILE>\" must be used like \"\$_ = <FILE>\"\n".
84 "Like the normal \"if-elsif-else\" syntax. \$_ is not set automagically!\n".
85 "Check bool statement: $bool_condition part of chain near line# ".
86 &line(substr($$r_source,0,pos $$r_source),$line)."\n";
87 }
88 }
89 }
90 }
91  
92 #---------------------------------------------------------------------------
93 #---------------------------------------------------------------------------
94 sub filter
95 1 ▶{
Subroutine filter: called CC=2 SCAR=7.2
Pod undocumented
96 1 my $self = shift ;
97 1 my $status;
98
99 1 $status = filter_read(100_000);
100 1 ▶ return $status if($status < 0);
Branch: 1 branch
BranchTrueFalseTotal
if $status < 0 0 1 1
101  
102 1 $_ = &filter_blocks($self,$_,$offset);
103  
104 0 $status ;
105 }
106  
107 #---------------------------------------------------------------------------
108 #---------------------------------------------------------------------------
109 sub filter_blocks
110 1 ▶{
Subroutine filter_blocks: called CC=7 SCAR=26.2
Pod undocumented
111 #Many a regex shamelessly stolen from Damian's Switch module.
112  
113 1 my $self = shift;
114 1 my $source = shift;
115 1 my $line = shift;
116  
117 1 my $keyword = '';
118  
119 1 ▶ while($source =~ m/(\n*)(\s*)((elsif|if)until)\b(?=\s*[(])(?{$keyword = $3})/gc ||
Branch: 1 branch
BranchTrueFalseTotal
if ($source =~ /(\n*)(\s*)((elsif|if)until)\b(?=\s*[(])(?{ $keyword = $3; })/cg or $source =~ /(\n*)(\s*)((elsif|if)while)\b(?=\s*[(])(?{ $keyword = $3; })/cg) 1 0 1
Condition33%1/3
$source =~ /(\n*)(\s*)((elsif|if)until)\b(?=\s*[(])(?{ $keyword = $3; })/cg or $source =~ /(\n*)(\s*)((elsif|if)while)\b(?=\s*[(])(?{ $keyword = $3; })/cg
l!l&&r!l&&!r
100
MC/DC0%0/2
$source =~ /(\n*)(\s*)((elsif|if)until)\b(?=\s*[(])(?{ $keyword = $3; })/cg or $source =~ /(\n*)(\s*)((elsif|if)while)\b(?=\s*[(])(?{ $keyword = $3; })/cg
$source =~ /(\n*)(\s*)((elsif|if)until)\b(?=\s*[(])(?{ $keyword = $3; })/cg $source =~ /(\n*)(\s*)((elsif|if)while)\b(?=\s*[(])(?{ $keyword = $3; })/cg
Truth table33%1/3
A or B
A= $source =~ /(\n*)(\s*)((elsif|if)until)\b(?=\s*[(])(?{ $keyword = $3; })/cgB= $source =~ /(\n*)(\s*)((elsif|if)while)\b(?=\s*[(])(?{ $keyword = $3; })/cg
ABresult
1X1
011
000
120 $source =~ m/(\n*)(\s*)((elsif|if)while)\b(?=\s*[(])(?{$keyword = $3})/gc )
121
122 {
123 1 my $r_fctn;
124 1 my %args = (self => $self,
125 r_source => \$source,
126 line => $line,
127 keyword => $keyword);
128  
129 1 $keyword =~ m/(?:if|elsif)(.*)/;
130 {
131 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
132 1 my $base_keyword = $1;
133  
134 1 ▶ next if(!$self->{$base_keyword} || !$base_keyword);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{$base_keyword} and $base_keyword 0 0 0
Condition0%0/3
$self->{$base_keyword} and $base_keyword
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{$base_keyword} and $base_keyword
$self->{$base_keyword} $base_keyword
Truth table0%0/3
A and B
A= $self->{$base_keyword}B= $base_keyword
ABresult
0X0
100
111
135 0 $r_fctn = \&{${base_keyword}.'_key'};
136 }
137  
138 0 ▶ $r_fctn->(\%args) if(ref($r_fctn) eq 'CODE');
Branch: 1 branch
BranchTrueFalseTotal
if ref $r_fctn eq "CODE" 0 0 0
139 }
140 0 ▶ print STDERR $source if($DEBUG);
Branch: 1 branch
BranchTrueFalseTotal
if $IfLoop::DEBUG 0 0 0
141 0 return $source;
142 }
143  
144 #---------------------------------------------------------------------------
145 #---------------------------------------------------------------------------
146 1 ▶{ no warnings; *while_key = *until_key = \&while_until_key; }
Subroutine BEGIN: called CC=1 SCAR=0.0
147  
148 sub while_until_key
149 0 ▶{
Subroutine while_until_key: not called CC=10 SCAR=47.0
Pod undocumented
150 0 my $r_args = shift;
151 0 my $self = $r_args->{self};
152 0 my $r_source = $r_args->{r_source};
153 0 my $line = $r_args->{line};
154 0 my $keyword = $r_args->{keyword};
155
156 0 pos $$r_source = pos($$r_source);
157
158 0 my @pos = Text::Balanced::_match_codeblock($r_source,
159 qr/\s*/,
160 qr/\(/,qr/\)/,
161 qr/[{(]/,qr/[)}]/,
162 undef);
163
164 #Capture \G so that if we encounter comments
165 # in the chain we can reset and go back for another pass.
166 0 my $pos_G = pos $$r_source;
167  
168 0 ▶ print STDERR "|@pos|\n" if($DEBUG);
Branch: 1 branch
BranchTrueFalseTotal
if $IfLoop::DEBUG 0 0 0
169 0 ▶ print STDERR substr($$r_source,$pos[0]-10,$pos[4]-$pos[0]+10),"\n" if($DEBUG);
Branch: 1 branch
BranchTrueFalseTotal
if $IfLoop::DEBUG 0 0 0
170
171 #substr($source,$pos[0]-10,$pos[4]-$pos[0]+10) #grabs elsewhile(...);
172 #substr($source,$pos[0],$pos[4]-$pos[0]) #grabs (...);
173
174 0 my $bool_condition = substr($$r_source,$pos[0],$pos[4]-$pos[0]);
175 0 my @replace=($pos[0]-7); #default replace starting place for an "if"
176 0 my $text = 'if'; #default replacement for an "if"
177
178 #change the @replace array and the $text if the statement is not an "if"
179 0 ▶ if($keyword =~ m/elsif.*/)
Branch: 1 branch
BranchTrueFalseTotal
if ($keyword =~ /elsif.*/) 0 0 0
180 {
181 0 $text = "elsif";
182 0 $replace[0] = $pos[0]-10; #It just so happens that until and while
183 #both have five letters in them.
184 }
185
186 #Filehandles that set $_ are speeeeecial Mmm-Kay
187 # lets die and warn the user with some position information.
188 0 &handle_filehandles($bool_condition,$r_source,$line);
189
190 #Adjust the syntax of the if to account for until. HA!
191 0 ▶ if($keyword =~ m/.*until/){$text .= "(!$bool_condition)\{do";}
Branch: 1 branch
BranchTrueFalseTotal
if ($keyword =~ /.*until/) { } 0 0 0
192 0 else {$text .= "$bool_condition\{do"; }
193
194 0 @pos = Text::Balanced::_match_codeblock($r_source,
195 qr/\s*/,
196 qr/\{/,qr/\}/,
197 qr/\{/,qr/\}/,
198 undef);
199 0 ▶ print STDERR "|@pos|\n" if($DEBUG);
Branch: 1 branch
BranchTrueFalseTotal
if $IfLoop::DEBUG 0 0 0
200 0 ▶ print STDERR substr($$r_source,$pos[0],$pos[4]-$pos[0]),"\n" if($DEBUG);
Branch: 1 branch
BranchTrueFalseTotal
if $IfLoop::DEBUG 0 0 0
201
202 #If no positions are present then we must be doing the comment thing...
203 0 ▶ if(scalar @pos)
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @pos) { } 0 0 0
204 {
205 0 my $inner = substr($$r_source,$pos[0],$pos[4]-$pos[0]);
206
207 0 push @replace, ($pos[4]-$pos[0])+$pos[0];
208
209 #Allow N number of nests for the syntax.
210 0 $inner = &filter_blocks($self,$inner,$line);
211
212 #Adjust the syntax of the if to account for until. HA!
213 0 ▶ if($keyword =~ m/.*until/)
Branch: 1 branch
BranchTrueFalseTotal
if ($keyword =~ /.*until/) { } 0 0 0
214 {
215 0 $text .= $inner."until$bool_condition}";
216 }
217 else
218 {
219 0 $text .= $inner."while$bool_condition}";
220 }
221
222 0 ▶ print STDERR "|@replace|" if($DEBUG);
Branch: 1 branch
BranchTrueFalseTotal
if $IfLoop::DEBUG 0 0 0
223
224 0 substr($$r_source,$replace[0],$replace[1]-$replace[0],$text);
225 }
226 else
227 {
228 0 pos $$r_source = $pos_G;
229 }
230  
231 }# End fctn while_until_key;
232  
233  
234 1 1;
235 __END__
↑ Summary