Chunks.pm

filter: Statementstmt 6.1% 8 / 130 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 8.7% 2 / 23 Podpod 47.6% 10 / 21 totaltotal 7.6% 20 / 260 CC 40 SCAR 36.2
CC
40
Cov
4%
parse
110.0
readSimpleFile
72.0
getDataValue
30.0
CRAP 37.2
« Chunks/Super.pm ↑ Summary Chunks/Local.pm »
1 package HTML::Chunks;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 our $VERSION = '1.55';
6  
7 1 ▶use constant DATA_REGEX => qr/##[\w\.]+##/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 sub new
10 0 ▶{
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
11 0 my $self = bless {}, shift;
12  
13 0 $self->init(@_);
14 0 return $self;
15 }
16  
17 sub init
18 0 ▶{
Subroutine init: not called CC=2 SCAR=17.9
Pod undocumented
19 0 my $self = shift;
20  
21 0 $self->{crush} = 1;
22 0 $self->{cascade} = 1;
23 0 $self->{dataStack} = [];
24 0 $self->{chunk} = {};
25 0 $self->{default} = undef;
26
27 0 ▶ $self->read(@_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
28 }
29  
30 sub readChunkFile
31 0 ▶{
Subroutine readChunkFile: not called CC=4 SCAR=30.0
Pod documented
32 0 my $self = shift;
33  
34 0 foreach my $file (@_) {
35 0 ▶ if (my $fh = $self->getFilehandle($file)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fh = $self->getFilehandle($file)) { } 0 0 0
36 0 my @chunkStack;
37
38 0 ▶ while (my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 0 0 0
39 0 $self->parse(\@chunkStack, \$line);
40 }
41 0 close $fh;
42 }
43 else {
44 0 warn "Can't open file [$file]\n";
45 }
46 }
47 }
48  
49 sub readSimpleFile
50 0 ▶{
Subroutine readSimpleFile: not called CC=8 SCAR=42.8
Pod documented
51 0 my $self = shift;
52  
53 0 foreach my $file (@_) {
54 0 ▶ if (my $fh = $self->getFilehandle($file)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fh = $self->getFilehandle($file)) 0 0 0
55 0 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
56 0 ▶ next if /^#/ or !/\S/;
Branch: 1 branch
BranchTrueFalseTotal
if /^#/ or not /\S/ 0 0 0
Condition0%0/3
/^#/ or not /\S/
l!l&&r!l&&!r
000
MC/DC0%0/2
/^#/ or not /\S/
/^#/ not /\S/
Truth table0%0/3
A or not(B)
A= /^#/B= not /\S/
ABresult
1X1
011
000
57
58 0 s/[\r\n]//g;
59 0 s/^\s+|\s+$//g;
60
61 0 my ($name, $chunk) = split(/\s+/, $_, 2);
62 0 ▶ if ($name && $chunk) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name and $chunk) 0 0 0
Condition0%0/3
$name and $chunk
!ll&&!rl&&r
000
MC/DC0%0/2
$name and $chunk
$name $chunk
Truth table0%0/3
A and B
A= $nameB= $chunk
ABresult
0X0
100
111
63 0 $self->addNamedChunk($name, $chunk);
64 }
65 }
66 0 close $fh;
67 }
68 }
69 }
70  
71 sub getFilehandle
72 0 ▶{
Subroutine getFilehandle: not called CC=3 SCAR=24.8
Pod undocumented
73 0 my $self = shift;
74 0 my ($file) = @_;
75
76 0 ▶ return $file if ref $file; # already a filehandle
Branch: 1 branch
BranchTrueFalseTotal
if ref $file 0 0 0
77 0 ▶ return open (CHUNKIN, $file) ? \*CHUNKIN : undef;
Branch: 1 branch
BranchTrueFalseTotal
open(CHUNKIN, $file) ? : 0 0 0
78 }
79
80 sub addChunk
81 0 ▶{
Subroutine addChunk: not called CC=4 SCAR=30.0
Pod documented
82 0 my $self = shift;
83  
84 0 foreach my $chunk (@_) {
85 0 ▶ next unless $chunk;
Branch: 1 branch
BranchTrueFalseTotal
unless $chunk 0 0 0
86
87 # allow a ref to be passed for efficiency
88 0 ▶ my $ref = ref $chunk ? $chunk : \$chunk;
Branch: 1 branch
BranchTrueFalseTotal
ref $chunk ? : 0 0 0
89 0 $self->parse([], $ref);
90 }
91 }
92  
93 sub addNamedChunk
94 0 ▶{
Subroutine addNamedChunk: not called CC=4 SCAR=30.0
Pod documented
95 0 my $self = shift;
96 0 my ($name, $chunk) = @_;
97
98 0 ▶ return unless ($name && $chunk);
Branch: 1 branch
BranchTrueFalseTotal
unless $name and $chunk 0 0 0
Condition0%0/3
$name and $chunk
!ll&&!rl&&r
000
MC/DC0%0/2
$name and $chunk
$name $chunk
Truth table0%0/3
A and B
A= $nameB= $chunk
ABresult
0X0
100
111
99
100 # allow a ref to be passed for efficiency
101 0 ▶ my $ref = ref $chunk ? $chunk : \$chunk;
Branch: 1 branch
BranchTrueFalseTotal
ref $chunk ? : 0 0 0
102 0 $self->{chunk}{$name} = undef;
103 0 $self->parse([ \$self->{chunk}{$name} ], $ref);
104 }
105  
106 sub parse
107 0 ▶{
Subroutine parse: not called CC=10 SCAR=47.0
Pod undocumented
108 0 my $self = shift;
109 0 my ($chunkStack, $data) = @_;
110 0 my $chunk = $chunkStack->[-1];
111
112 # allow a ref to be passed for efficiency
113 0 ▶ my $dataRef = ref $data ? $data : \$data;
Branch: 1 branch
BranchTrueFalseTotal
ref $data ? : 0 0 0
114  
115 0 foreach (split(/(<!--.*?-->)/, $$dataRef)) {
116 0 ▶ if (/<!--\s*BEGIN\s+([\w\.]+)\s+-->/i) {
Branch: 3 branches
BranchTrueFalseTotal
if (/<!--\s*BEGIN\s+([\w\.]+)\s+-->/i) { } 0 0 0
elsif ($chunk and /<!--\s*END\s+.*-->/i) { } 0 0 0
elsif ($chunk and $_ || !$self->{'crush'}) { } 0 0 0
117 0 $self->{chunk}{$1} = undef;
118 0 push @{$chunkStack}, $chunk = \$self->{chunk}{$1};
119 }
120 ▶ elsif ($chunk && /<!--\s*END\s+.*-->/i) {
Condition0%0/3
$chunk and /<!--\s*END\s+.*-->/i
!ll&&!rl&&r
000
MC/DC0%0/2
$chunk and /<!--\s*END\s+.*-->/i
$chunk /<!--\s*END\s+.*-->/i
Truth table0%0/3
A and B
A= $chunkB= /<!--\s*END\s+.*-->/i
ABresult
0X0
100
111
121 0 ▶ $$chunk =~ s/\s{2,}/\n/sg if $self->{crush}; # crush whitespace
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"crush"} 0 0 0
122 0 pop @{$chunkStack};
123 0 $chunk = $chunkStack->[-1];
124 }
125 ▶ elsif ($chunk && ($_ || !$self->{crush})) {
Condition0%0/6
$chunk and $_ || !$self->{"crush"}
!ll&&!rl&&r
000
$_ or not $self->{"crush"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$chunk and $_ || !$self->{"crush"}
$chunk $_ not $self->{"crush"}
Truth table0%0/4
A and B or not(C)
A= $chunkB= $_C= not $self->{"crush"}
ABCresult
0XX0
1000
11X1
1011
126 0 $$chunk .= $_;
127 }
128 }
129 }
130  
131 sub output
132 0 ▶{
Subroutine output: not called CC=1 SCAR=6.9
Pod documented
133 0 my $self = shift;
134 0 my $name = shift;
135  
136 0 $self->outputAsChunk(\$self->{chunk}{$name}, @_);
137 }
138  
139 sub outputAsChunk
140 0 ▶{
Subroutine outputAsChunk: not called CC=4 SCAR=30.0
Pod undocumented
141 0 my $self = shift;
142 0 my $chunk = shift;
143 0 my $data = shift;
144  
145 0 ▶ if ($chunk) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chunk) 0 0 0
146 0 ▶ my $chunkRef = ref $chunk ? $chunk : \$chunk;
Branch: 1 branch
BranchTrueFalseTotal
ref $chunk ? : 0 0 0
147 0 ▶ $data ||= {};
Condition0%0/2
$data ||= {}
l!l
00
MC/DC0%0/1
$data ||= {}
$data
Truth table0%0/2
A
A= $data
Aresult
11
00
148
149 0 push @{$self->{dataStack}}, $data;
150 0 $self->outputBasicChunk($chunkRef, @_);
151 0 pop @{$self->{dataStack}};
152 }
153 }
154  
155 # basic chunk output including data substitution, using data already
156 # on the data stack.
157 sub outputBasicChunk
158 0 ▶{
Subroutine outputBasicChunk: not called CC=4 SCAR=30.0
Pod undocumented
159 0 my $self = shift;
160 0 my $chunk = shift;
161 0 ▶ my $chunkRef = ref $chunk ? $chunk : \$chunk;
Branch: 1 branch
BranchTrueFalseTotal
ref $chunk ? : 0 0 0
162 0 my $data_regex = $self->DATA_REGEX;
163  
164 0 foreach my $piece (split(/(<!--\s*$data_regex\s*-->|$data_regex)/, $$chunkRef)) {
165 0 ▶ if ($piece =~ /($data_regex)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($piece =~ /($data_regex)/) { } 0 0 0
166 0 $self->outputData(substr($1, 2, -2), @_);
167 }
168 else {
169 0 print $piece;
170 }
171 }
172 }
173  
174 sub outputData
175 0 ▶{
Subroutine outputData: not called CC=2 SCAR=17.9
Pod undocumented
176 0 my $self = shift;
177 0 my $name = shift;
178 0 my $value = $self->getDataValue($name);
179
180 0 ▶ if (ref $value eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq 'CODE') { } 0 0 0
181 0 &{$value}($self, $name, @_);
182 }
183 else {
184 0 print $value;
185 }
186 }
187  
188 sub getDataValue
189 0 ▶{
Subroutine getDataValue: not called CC=5 SCAR=34.0
Pod undocumented
190 0 my ($self, $name) = (shift, shift);
191 0 my $value;
192 0 ▶ my $last = $self->{cascade} ? 0 : $#{$self->{dataStack}};
Branch: 1 branch
BranchTrueFalseTotal
$self->{'cascade'} ? : 0 0 0
193
194 0 ▶ for (my $ndx = $#{$self->{dataStack}}; $ndx >= $last; $ndx--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ndx >= $last) 0 0 0
195 0 ▶ if (exists($self->{dataStack}[$ndx]{$name})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"dataStack"}[$ndx]{$name}) 0 0 0
196 0 $value = $self->{dataStack}[$ndx]{$name};
197 0 last;
198 }
199 }
200
201 0 ▶ if ($value eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($value eq '') { } 0 0 0
202 0 return $self->{default};
203 }
204 else {
205 0 return $value;
206 }
207 }
208  
209 sub getChunkNames
210 0 ▶{
Subroutine getChunkNames: not called CC=1 SCAR=6.9
Pod documented
211 0 my $self = shift;
212 0 return keys %{$self->{chunk}};
213 }
214  
215 sub getChunk
216 0 ▶{
Subroutine getChunk: not called CC=1 SCAR=6.9
Pod documented
217 0 my $self = shift;
218 0 my ($name) = @_;
219
220 0 return $self->{chunk}{$name};
221 }
222  
223 sub getChunkHash
224 0 ▶{
Subroutine getChunkHash: not called CC=1 SCAR=6.9
Pod undocumented
225 0 my $self = shift;
226 0 return { %{$self->{chunk}} };
227 }
228  
229 sub setCrush
230 0 ▶{
Subroutine setCrush: not called CC=1 SCAR=6.9
Pod documented
231 0 my $self = shift;
232 0 my $old = $self->{crush};
233 0 ($self->{crush}) = (@_);
234 0 return $old;
235 }
236  
237 sub setCascade
238 0 ▶{
Subroutine setCascade: not called CC=1 SCAR=6.9
Pod documented
239 0 my $self = shift;
240 0 my $old = $self->{cascade};
241 0 ($self->{cascade}) = (@_);
242 0 return $old;
243 }
244  
245 sub setDefaultDataValue
246 0 ▶{
Subroutine setDefaultDataValue: not called CC=1 SCAR=6.9
Pod undocumented
247 0 my $self = shift;
248 0 ($self->{default}) = (@_);
249 }
250  
251 # legacy wrappers for backward compatibility
252  
253 sub crush
254 0 ▶{
Subroutine crush: not called CC=1 SCAR=6.9
Pod undocumented
255 0 my $self = shift;
256 0 return $self->setCrush(@_);
257 }
258  
259 sub read
260 0 ▶{
Subroutine read: not called CC=1 SCAR=6.9
Pod undocumented
261 0 my $self = shift;
262 0 $self->readChunkFile(@_);
263 }
264  
265 1 1;
266  
267 __END__
« Chunks/Super.pm ↑ Summary Chunks/Local.pm »