blib/lib/HTML/Tabulate.pm

filter: Statementstmt 87.2% 680 / 779 Branchbran 70.7% 365 / 516 Conditioncond 60.4% 191 / 316 MC/DCmcdc 34.9% 65 / 186 Subroutinesub 91.1% 62 / 68 Podpod 17.8% 10 / 56 totaltotal 71.4% 1373 / 1921 CC 399 SCAR 43.0
CC
399
Cov
77%
derive_fields
569.0
get_dataset
152.0
check_valid
104.7
CRAP 73.4
↑ Summary
1 package HTML::Tabulate;
2  
3 26 ▶use 5.005;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 26 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 26 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 26 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 26 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 26 ▶use vars qw($VERSION @ISA @EXPORT @EXPORT_OK $TITLE_HEADING_LEVEL);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 26 require Exporter;
11 26 @ISA = qw(Exporter);
12 26 @EXPORT = qw();
13 26 @EXPORT_OK = qw(&render);
14  
15 26 $VERSION = '0.44';
16 26 my $DEFAULT_TEXT_FORMAT = "<p>%s</p>\n";
17 26 my %DEFAULT_DEFN = (
18 style => 'down',
19 table => {},
20 title => { format => "<h2>%s</h2>\n" },
21 text => { format => $DEFAULT_TEXT_FORMAT },
22 caption => { type => 'caption', format => $DEFAULT_TEXT_FORMAT },
23 field_attr => { -defaults => {}, },
24 );
25 26 my %VALID_ARG = (
26 table => 'HASH/SCALAR',
27 thead => 'HASH/SCALAR',
28 tbody => 'HASH/SCALAR',
29 tfoot => 'HASH/SCALAR',
30 tr => 'HASH/CODE',
31 thtr => 'HASH',
32 th => 'HASH',
33 td => 'HASH',
34 fields => 'ARRAY',
35 fields_add => 'HASH',
36 fields_omit => 'ARRAY',
37 in_fields => 'ARRAY',
38 labels => 'SCALAR/HASH',
39 label_links => 'HASH',
40 stripe => 'ARRAY/SCALAR/HASH',
41 null => 'SCALAR',
42 trim => 'SCALAR',
43 style => 'SCALAR',
44 # limit => 'SCALAR',
45 # output => 'SCALAR',
46 # first => 'SCALAR',
47 # last => 'SCALAR',
48 field_attr => 'HASH',
49 # xhtml: boolean indicating whether to use xhtml-style tagging
50 xhtml => 'SCALAR',
51 # title: title/heading to be rendered above table
52 title => 'SCALAR/HASH/CODE',
53 # text: text to be rendered above table, after title
54 text => 'SCALAR/HASH/CODE',
55 # caption: text to be rendered below table
56 caption => 'SCALAR/HASH/CODE',
57 # data_prepend: data rows to be inserted before main dataset
58 data_prepend => 'ARRAY',
59 # data_append: data rows to be appended to main dataset
60 data_append => 'ARRAY',
61 # colgroups: array of hashrefs to be inserted as individual colgroups
62 colgroups => 'ARRAY',
63 # labelgroups: named groupings of labels used to create two-tier headers
64 labelgroups => 'HASH',
65 # derived: fields not present in the underlying data, to skip unnecessary lookups
66 derived => 'ARRAY',
67 );
68 26 my %VALID_FIELDS = (
69 -defaults => 'HASH',
70 );
71 26 my %FIELD_ATTR = (
72 escape => 'SCALAR',
73 value => 'SCALAR/CODE',
74 format => 'SCALAR/CODE',
75 link => 'SCALAR/CODE',
76 label => 'SCALAR/CODE',
77 label_format => 'SCALAR/CODE',
78 label_link => 'SCALAR/CODE',
79 label_escape => 'SCALAR',
80 default => 'SCALAR',
81 composite => 'ARRAY',
82 composite_join => 'SCALAR/CODE',
83 derived => 'SCALAR',
84 );
85 26 my %MINIMISED_ATTR = map { $_ => 1 } qw(
86 checked compact declare defer disabled ismap multiple
87 nohref noresize noshade nowrap readonly selected
88 );
89 26 my $URI_ESCAPE_CHARS = "^A-Za-z0-9\-_.!~*'()?&;:/=";
90 26 $TITLE_HEADING_LEVEL = 'h2'; # TODO: deprecated
91  
92 # -------------------------------------------------------------------------
93 # Provided for subclassing
94 sub get_valid_arg
95 {
96 123 ▶ return wantarray ? %VALID_ARG : \%VALID_ARG;
Subroutine get_valid_arg: called CC=2 SCAR=7.6
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 123 0 123
97 }
98  
99 # Provided for subclassing
100 sub get_valid_fields
101 {
102 123 ▶ return wantarray ? %VALID_FIELDS : \%VALID_FIELDS;
Subroutine get_valid_fields: called CC=2 SCAR=7.6
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 123 0 123
103 }
104  
105 # Provided for subclassing
106 sub get_field_attributes
107 {
108 31 ▶ return wantarray ? %FIELD_ATTR : \%FIELD_ATTR;
Subroutine get_field_attributes: called CC=2 SCAR=7.6
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 31 31
109 }
110  
111 #
112 # Check $self->{defn} for invalid arguments or types
113 #
114 sub check_valid
115 {
116 123 ▶ my ($self, $defn) = @_;
Subroutine check_valid: called CC=35 SCAR=46.5
Pod undocumented
117  
118 # Check top-level args
119 123 my %valid = $self->get_valid_arg();
120 123 my (@invalid, @badtype);
121 123 for (sort keys %$defn) {
122 241 ▶ if (! exists $valid{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $valid{$_}) 0 241 241
123 0 push @invalid, $_;
124 0 next;
125 }
126 241 my $type = ref $defn->{$_};
127 push @badtype, $_
128 241 ▶ if $type && $type ne 'SCALAR' && $valid{$_} !~ m/$type/;
Branch: 1 branch
BranchTrueFalseTotal
if $type and $type ne "SCALAR" and not $valid{$_} =~ /$type/ 0 241 241
Condition66%4/6
$type and $type ne "SCALAR" and not $valid{$_} =~ /$type/
!ll&&!rl&&r
741670
$type and $type ne "SCALAR"
!ll&&!rl&&r
740167
MC/DC0%0/3
$type and $type ne "SCALAR" and not $valid{$_} =~ /$type/
$type $type ne "SCALAR" not $valid{$_} =~ /$type/
Truth table50%2/4
A and B and not(C)
A= $typeB= $type ne "SCALAR"C= not $valid{$_} =~ /$type/
ABCresult
0XX0
10X0
1100
1111
129 push @badtype, $_
130 241 ▶ if ! $type && $valid{$_} !~ m/SCALAR/;
Branch: 1 branch
BranchTrueFalseTotal
unless $type or $valid{$_} =~ /SCALAR/ 0 241 241
Condition66%2/3
$type or $valid{$_} =~ /SCALAR/
l!l&&r!l&&!r
167740
MC/DC0%0/2
$type or $valid{$_} =~ /SCALAR/
$type $valid{$_} =~ /SCALAR/
Truth table66%2/3
A or B
A= $typeB= $valid{$_} =~ /SCALAR/
ABresult
1X1
011
000
131 }
132 123 ▶ croak "[check_valid] invalid argument found: " . join(',',@invalid)
Branch: 1 branch
BranchTrueFalseTotal
if @invalid 0 123 123
133 if @invalid;
134 123 ▶ croak "[check_valid] invalid types for argument: " . join(',',@badtype)
Branch: 1 branch
BranchTrueFalseTotal
if @badtype 0 123 123
135 if @badtype;
136  
137 # Check special fields
138 123 %valid = $self->get_valid_fields();
139 123 @invalid = ();
140 123 @badtype = ();
141 123 for (sort grep(/^-/, keys(%{$defn->{field_attr}})) ) {
142 6 ▶ if (! exists $valid{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $valid{$_}) 0 6 6
143 0 push @invalid, $_;
144 0 next;
145 }
146 6 my $type = ref $defn->{field_attr}->{$_};
147 push @badtype, $_
148 6 ▶ if $type && $type ne 'SCALAR' && $valid{$_} !~ m/$type/;
Branch: 1 branch
BranchTrueFalseTotal
if $type and $type ne "SCALAR" and not $valid{$_} =~ /$type/ 0 6 6
Condition33%2/6
$type and $type ne "SCALAR" and not $valid{$_} =~ /$type/
!ll&&!rl&&r
060
$type and $type ne "SCALAR"
!ll&&!rl&&r
006
MC/DC0%0/3
$type and $type ne "SCALAR" and not $valid{$_} =~ /$type/
$type $type ne "SCALAR" not $valid{$_} =~ /$type/
Truth table25%1/4
A and B and not(C)
A= $typeB= $type ne "SCALAR"C= not $valid{$_} =~ /$type/
ABCresult
0XX0
10X0
1100
1111
149 push @badtype, $_
150 6 ▶ if ! $type && $valid{$_} !~ m/SCALAR/;
Branch: 1 branch
BranchTrueFalseTotal
unless $type or $valid{$_} =~ /SCALAR/ 0 6 6
Condition33%1/3
$type or $valid{$_} =~ /SCALAR/
l!l&&r!l&&!r
600
MC/DC0%0/2
$type or $valid{$_} =~ /SCALAR/
$type $valid{$_} =~ /SCALAR/
Truth table33%1/3
A or B
A= $typeB= $valid{$_} =~ /SCALAR/
ABresult
1X1
011
000
151 }
152 123 ▶ croak "[check_valid] invalid field argument found: " . join(',',@invalid)
Branch: 1 branch
BranchTrueFalseTotal
if @invalid 0 123 123
153 if @invalid;
154 123 ▶ croak "[check_valid] invalid types for field argument: " . join(',',@badtype)
Branch: 1 branch
BranchTrueFalseTotal
if @badtype 0 123 123
155 if @badtype;
156  
157 # Check field attributes
158 123 ▶ $self->{field_attr} ||= $self->get_field_attributes();
Condition66%2/3
$self->{"field_attr"} ||= $self->get_field_attributes
l!l&&r!l&&!r
92310
MC/DC0%0/2
$self->{"field_attr"} ||= $self->get_field_attributes
$self->{"field_attr"} $self->get_field_attributes
Truth table66%2/3
A ||= B
A= $self->{"field_attr"}B= $self->get_field_attributes
ABresult
1X1
011
000
159 123 %valid = %{$self->{field_attr}};
160 123 @badtype = ();
161 123 for my $field (keys %{$defn->{field_attr}}) {
162 croak "[check_valid] invalid field argument entry '$field': " .
163 $defn->{field_attr}->{$field}
164 55 ▶ if ref $defn->{field_attr}->{$field} ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $defn->{"field_attr"}{$field} ne "HASH" 0 55 55
165 55 for (sort keys %{$defn->{field_attr}->{$field}}) {
166 91 ▶ next if ! exists $valid{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $valid{$_} 45 46 91
167 46 ▶ next if ! $valid{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless $valid{$_} 0 46 46
168 46 my $type = ref $defn->{field_attr}->{$field}->{$_};
169 46 ▶ if (! ref $valid{$_}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $valid{$_}) { } 46 0 46
elsif (ref $valid{$_} eq 'ARRAY') { } 0 0 0
170 push @badtype, $_
171 46 ▶ if $type && $type ne 'SCALAR' && $valid{$_} !~ m/$type/;
Branch: 1 branch
BranchTrueFalseTotal
if $type and $type ne "SCALAR" and not $valid{$_} =~ /$type/ 0 46 46
Condition66%4/6
$type and $type ne "SCALAR" and not $valid{$_} =~ /$type/
!ll&&!rl&&r
20260
$type and $type ne "SCALAR"
!ll&&!rl&&r
20026
MC/DC0%0/3
$type and $type ne "SCALAR" and not $valid{$_} =~ /$type/
$type $type ne "SCALAR" not $valid{$_} =~ /$type/
Truth table50%2/4
A and B and not(C)
A= $typeB= $type ne "SCALAR"C= not $valid{$_} =~ /$type/
ABCresult
0XX0
10X0
1100
1111
172 push @badtype, $_
173 46 ▶ if ! $type && $valid{$_} !~ m/SCALAR/;
Branch: 1 branch
BranchTrueFalseTotal
unless $type or $valid{$_} =~ /SCALAR/ 0 46 46
Condition66%2/3
$type or $valid{$_} =~ /SCALAR/
l!l&&r!l&&!r
26200
MC/DC0%0/2
$type or $valid{$_} =~ /SCALAR/
$type $valid{$_} =~ /SCALAR/
Truth table66%2/3
A or B
A= $typeB= $valid{$_} =~ /SCALAR/
ABresult
1X1
011
000
174 }
175 elsif (ref $valid{$_} eq 'ARRAY') {
176 0 ▶ if ($type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type) { } 0 0 0
177 0 push @badtype, $_;
178 }
179 else {
180 0 my $val = $defn->{field_attr}->{$field}->{$_};
181 0 ▶ push @badtype, "$_ ($val)" if ! grep /^$val$/, @{$valid{$_}};
Branch: 1 branch
BranchTrueFalseTotal
unless grep /^$val$/, @{$valid{$_};} 0 0 0
182 }
183 }
184 else {
185 croak "[check_valid] invalid field attribute entry for '$_': " .
186 0 ref $valid{$_};
187 }
188 }
189 55 ▶ croak "[check_valid] invalid type for '$field' field attribute: " .
Branch: 1 branch
BranchTrueFalseTotal
if @badtype 0 55 55
190 join(',',@badtype) if @badtype;
191 }
192 }
193  
194 #
195 # Merge $hash1 and $hash2 together, returning the result (or, in void
196 # context, merging into $self->{defn}). Performs a shallow (one-level deep)
197 # hash merge unless the field is defined in the @recurse_keys array, in
198 # which case we do a full recursive merge.
199 #
200 sub merge
201 {
202 246 ▶ my $self = shift;
Subroutine merge: called CC=14 SCAR=26.5
Pod documented
203 246 ▶ my $hash1 = shift || {};
Condition50%1/2
shift() || {}
l!l
2460
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
204 246 my $hash2 = shift;
205 246 my $arg = shift;
206  
207 246 ▶ croak "[merge] invalid hash1 '$hash1'" if ref $hash1 ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $hash1 ne "HASH" 0 246 246
208 246 ▶ croak "[merge] invalid hash2 '$hash2'" if $hash2 && ref $hash2 ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if $hash2 and ref $hash2 ne "HASH" 0 246 246
Condition66%2/3
$hash2 and ref $hash2 ne "HASH"
!ll&&!rl&&r
22440
MC/DC0%0/2
$hash2 and ref $hash2 ne "HASH"
$hash2 ref $hash2 ne "HASH"
Truth table66%2/3
A and B
A= $hash2B= ref $hash2 ne "HASH"
ABresult
0X0
100
111
209  
210 246 my $single_arg = ! $hash2;
211  
212 # Use $self->{defn} as $hash1 if only one argument
213 246 ▶ if ($single_arg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($single_arg) 2 244 246
214 2 $hash2 = $hash1;
215 2 $hash1 = $self->{defn};
216 }
217  
218 # Check hash2 for valid args (except when recursive)
219 246 ▶ my $sub = (caller(1))[3] || '';
Condition100%2/2
(caller 1)[3] || ''
l!l
2442
MC/DC100%1/1
(caller 1)[3] || ''
(caller 1)[3]
Truth table100%2/2
A
A= (caller 1)[3]
Aresult
11
00
220 246 ▶ $self->check_valid($hash2) unless substr($sub, -7) eq '::merge';
Branch: 1 branch
BranchTrueFalseTotal
unless substr($sub, -7) eq "::merge" 123 123 246
221  
222 246 my $merge = $self->deepcopy($hash1);
223  
224 # Add hash2 to $merge
225 246 my @recurse_keys = qw(field_attr);
226 246 for my $key (keys %$hash2) {
227 # If this value is a hashref on both sides, do a shallow hash merge
228 # unless we need to do a proper recursive merge
229 395 ▶ if (ref $hash2->{$key} eq 'HASH' && ref $merge->{$key} eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $hash2->{$key} eq 'HASH' and ref $merge->{$key} eq 'HASH') { } 153 242 395
Condition100%3/3
ref $hash2->{$key} eq "HASH" and ref $merge->{$key} eq "HASH"
!ll&&!rl&&r
139103153
MC/DC100%2/2
ref $hash2->{$key} eq "HASH" and ref $merge->{$key} eq "HASH"
ref $hash2->{$key} eq "HASH" ref $merge->{$key} eq "HASH"
Truth table100%3/3
A and B
A= ref $hash2->{$key} eq "HASH"B= ref $merge->{$key} eq "HASH"
ABresult
0X0
100
111
230 # Recursive merge
231 153 ▶ if (grep /^$key$/, @recurse_keys) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep /^$key$/, @recurse_keys) { } 123 30 153
232 123 $merge->{$key} = $self->merge($hash1->{$key}, $hash2->{$key});
233 }
234 # Shallow hash merge
235 else {
236 30 @{$merge->{$key}}{ keys %{$hash1->{$key}}, keys %{$hash2->{$key}} } = (values %{$hash1->{$key}}, values %{$hash2->{$key}});
237 }
238 }
239 # Otherwise (scalars, arrayrefs etc) just copy the value
240 else {
241 242 $merge->{$key} = $hash2->{$key};
242 }
243 }
244  
245 # In void context update $self->{defn}
246 246 ▶ if (! defined wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined wantarray) { } 2 244 246
247 2 $self->{defn} = $merge;
248 # Must invalidate transient $self->{defn_t} when $self->{defn} changes
249 2 ▶ delete $self->{defn_t} if exists $self->{defn_t};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"defn_t"} 0 2 2
250 }
251 else {
252 244 return $merge;
253 }
254 }
255  
256 sub defn
257 {
258 8 ▶ my $self = shift;
Subroutine defn: called CC=1 SCAR=0.0
Pod undocumented
259 8 return $self->{defn};
260 }
261  
262 # Initialisation
263 sub init
264 {
265 31 ▶ my $self = shift;
Subroutine init: called CC=6 SCAR=19.1
Pod undocumented
266 31 ▶ my $defn = shift || {};
Condition100%2/2
shift() || {}
l!l
292
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
267 31 ▶ croak "[init] invalid defn '$defn'" if $defn && ref $defn ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if $defn and ref $defn ne "HASH" 0 31 31
Condition33%1/3
$defn and ref $defn ne "HASH"
!ll&&!rl&&r
0310
MC/DC0%0/2
$defn and ref $defn ne "HASH"
$defn ref $defn ne "HASH"
Truth table33%1/3
A and B
A= $defnB= ref $defn ne "HASH"
ABresult
0X0
100
111
268  
269 # Map $defn table => 1 to table => {} for cleaner merging
270 31 ▶ $defn->{table} = {} if $defn->{table} && ! ref $defn->{table};
Branch: 1 branch
BranchTrueFalseTotal
if $defn->{"table"} and not ref $defn->{"table"} 0 31 31
Condition66%2/3
$defn->{"table"} and not ref $defn->{"table"}
!ll&&!rl&&r
2740
MC/DC0%0/2
$defn->{"table"} and not ref $defn->{"table"}
$defn->{"table"} not ref $defn->{"table"}
Truth table66%2/3
A and not(B)
A= $defn->{"table"}B= not ref $defn->{"table"}
ABresult
0X0
100
111
271  
272 # Initialise $self->{defn} by merging defaults and $defn
273 31 $self->{defn} = $self->merge(\%DEFAULT_DEFN, $defn);
274  
275 31 return $self;
276 }
277  
278 sub new
279 {
280 31 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
281 31 my $self = {};
282 31 bless $self, $class;
283 31 $self->init(@_);
284 }
285  
286 # -------------------------------------------------------------------------
287 #
288 # If deriving field names, also derive labels (if not already defined)
289 #
290 sub derive_label
291 {
292 356 ▶ my ($self, $field) = @_;
Subroutine derive_label: called CC=1 SCAR=0.0
Pod undocumented
293 356 $field =~ s/_+/ /g;
294 356 $field = join ' ', map { ucfirst($_) } split(/\s+/, $field);
295 356 $field =~ s/(Id)$/\U$1/;
296 356 return $field;
297 }
298  
299 #
300 # Try and derive a reasonable field list from $self->{defn_t} using the set data.
301 # Croaks on failure.
302 #
303 sub derive_fields
304 {
305 7 ▶ my ($self, $set) = @_;
Subroutine derive_fields: called CC=39 SCAR=63.4
Pod undocumented
306 7 my $defn = $self->{defn_t};
307  
308 # For iterators, prefetch the first row and use its keys
309 7 ▶ croak "invalid Tabulate data type '$set'" unless ref $set;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $set 0 7 7
310 7 ▶ if (ref $set eq 'CODE') {
Branch: 6 branches
BranchTrueFalseTotal
if (ref $set eq 'CODE') { } 0 7 7
elsif (builtin::blessed($set) and $set->can('Next')) { } 0 7 7
elsif (builtin::blessed($set) and $set->can('next')) { } 0 7 7
elsif (ref $set eq 'ARRAY') { } 6 1 7
elsif (ref $set and ref $set ne 'HASH' and $set->isa('Class::DBI')) { } 0 1 1
elsif (defined eval { do { keys %$set } }) { } 1 0 1
311 0 my $row = $set->();
312 0 $self->{prefetch} = $row;
313 0 ▶ $defn->{fields} = [ sort keys %$row ] if eval { keys %$row };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { keys %$row } } 0 0 0
314 }
315 ▶ elsif (blessed $set and $set->can('Next')) {
Condition33%1/3
builtin::blessed($set) and $set->can("Next")
!ll&&!rl&&r
700
MC/DC0%0/2
builtin::blessed($set) and $set->can("Next")
builtin::blessed($set) $set->can("Next")
Truth table33%1/3
A and B
A= builtin::blessed($set)B= $set->can("Next")
ABresult
0X0
100
111
316 0 ▶ my $row = $set->can('First') ? $set->First : $set->Next;
Branch: 1 branch
BranchTrueFalseTotal
$set->can('First') ? : 0 0 0
317 0 $self->{prefetch} = $row;
318 0 ▶ $defn->{fields} = [ sort keys %$row ] if eval { keys %$row };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { keys %$row } } 0 0 0
319 }
320 ▶ elsif (blessed $set and $set->can('next')) {
Condition33%1/3
builtin::blessed($set) and $set->can("next")
!ll&&!rl&&r
700
MC/DC0%0/2
builtin::blessed($set) and $set->can("next")
builtin::blessed($set) $set->can("next")
Truth table33%1/3
A and B
A= builtin::blessed($set)B= $set->can("next")
ABresult
0X0
100
111
321 0 ▶ my $row = $set->can('first') ? $set->first : $set->next;
Branch: 1 branch
BranchTrueFalseTotal
$set->can('first') ? : 0 0 0
322 0 $self->{prefetch} = $row;
323 0 ▶ $defn->{fields} = [ sort keys %$row ] if eval { keys %$row };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { keys %$row } } 0 0 0
324 }
325 # For arrays
326 elsif (ref $set eq 'ARRAY') {
327 6 ▶ if (! @$set) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$set) 0 6 6
328 0 $defn->{fields} = [];
329 0 return;
330 }
331 6 my $obj = $set->[0];
332 # Arrayref of hashrefs
333 6 ▶ if (ref $obj eq 'HASH') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $obj eq 'HASH') { } 3 3 6
elsif (ref $obj eq 'ARRAY') { } 3 0 3
elsif ($obj->isa('Class::DBI')) { } 0 0 0
334 3 $defn->{fields} = [ sort keys %$obj ];
335 }
336 # Arrayref of arrayrefs - access via subscripts unless labels are defined
337 elsif (ref $obj eq 'ARRAY') {
338 3 ▶ if ($defn->{labels}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($defn->{'labels'}) { } 0 3 3
339 0 croak "[derive_fields] no fields found and cannot derive fields from data arrayrefs";
340 }
341 # Arrayref of arrayrefs, labels off
342 else {
343 3 $defn->{fields} = [ 0 .. $#$obj ];
344 }
345 }
346 # For Class::DBI objects, derive via columns groups
347 elsif ($obj->isa('Class::DBI')) {
348 0 my @col = $obj->columns('Tabulate');
349 0 ▶ @col = ( $obj->columns('Essential'), $obj->columns('Others') )
Branch: 1 branch
BranchTrueFalseTotal
if not @col and $obj->columns("Essential") 0 0 0
Condition0%0/3
not @col and $obj->columns("Essential")
!ll&&!rl&&r
000
MC/DC0%0/2
not @col and $obj->columns("Essential")
not @col $obj->columns("Essential")
Truth table0%0/3
not(A) and B
A= not @colB= $obj->columns("Essential")
ABresult
0X0
100
111
350 if ! @col && $obj->columns('Essential');
351 0 ▶ @col = $obj->columns('All') if ! @col;
Branch: 1 branch
BranchTrueFalseTotal
unless @col 0 0 0
352 0 ▶ $defn->{fields} = [ @col ] if @col;
Branch: 1 branch
BranchTrueFalseTotal
if @col 0 0 0
353 }
354 # If all else fails, try treating as a hash
355 6 ▶ unless (ref $defn->{fields} && @{$defn->{fields}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $defn->{"fields"} and @{$defn->{"fields"};}) 0 6 6
Condition33%1/3
ref $defn->{"fields"} and @{$defn->{"fields"};}
!ll&&!rl&&r
006
MC/DC0%0/2
ref $defn->{"fields"} and @{$defn->{"fields"};}
ref $defn->{"fields"} @{$defn->{"fields"};}
Truth table33%1/3
A and B
A= ref $defn->{"fields"}B= @{$defn->{"fields"};}
ABresult
0X0
100
111
356 0 ▶ if (! defined eval { $defn->{fields} = [ sort keys %$obj ] }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined eval { do { $defn->{"fields"} = [sort(keys %$obj)] } }) 0 0 0
357 0 croak "[derive_fields] no fields found and initial object '$obj' is strange type";
358 }
359 }
360 }
361 # Else looks like a single object - check for Class::DBI
362 ▶ elsif (ref $set && ref $set ne 'HASH' && $set->isa('Class::DBI')) {
Condition33%2/6
ref $set and ref $set ne "HASH" and $set->isa("Class::DBI")
!ll&&!rl&&r
100
ref $set and ref $set ne "HASH"
!ll&&!rl&&r
010
MC/DC0%0/3
ref $set and ref $set ne "HASH" and $set->isa("Class::DBI")
ref $set ref $set ne "HASH" $set->isa("Class::DBI")
Truth table25%1/4
A and B and C
A= ref $setB= ref $set ne "HASH"C= $set->isa("Class::DBI")
ABCresult
0XX0
10X0
1100
1111
363 0 my @col = $set->columns('Tabulate');
364 0 ▶ @col = ( $set->columns('Essential'), $set->columns('Others') )
Branch: 1 branch
BranchTrueFalseTotal
if not @col and $set->columns("Essential") 0 0 0
Condition0%0/3
not @col and $set->columns("Essential")
!ll&&!rl&&r
000
MC/DC0%0/2
not @col and $set->columns("Essential")
not @col $set->columns("Essential")
Truth table0%0/3
not(A) and B
A= not @colB= $set->columns("Essential")
ABresult
0X0
100
111
365 if ! @col && $set->columns('Essential');
366 0 ▶ @col = $set->columns('All') if ! @col;
Branch: 1 branch
BranchTrueFalseTotal
unless @col 0 0 0
367 0 ▶ $defn->{fields} = [ @col ] if @col;
Branch: 1 branch
BranchTrueFalseTotal
if @col 0 0 0
368 }
369 # Otherwise try treating as a hash
370 1 elsif (defined eval { keys %$set }) {
371 1 my $first = (sort keys %$set)[0];
372 1 ▶ my $ref = ref $set->{$first} if defined $first;
Branch: 1 branch
BranchTrueFalseTotal
if defined $first 1 0 1
373 # Check whether first value is reference
374 1 ▶ if ($ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref) { } 0 1 1
375 # Hashref of hashrefs
376 0 ▶ if ($ref eq 'HASH') {
Branch: 3 branches
BranchTrueFalseTotal
if ($ref eq 'HASH') { } 0 0 0
elsif (ref $set->[0] ne 'ARRAY') { } 0 0 0
elsif ($defn->{'labels'}) { } 0 0 0
377 0 $defn->{fields} = [ sort keys %{$set->{$first}} ];
378 }
379 elsif (ref $set->[0] ne 'ARRAY') {
380 0 croak "[derive_fields] no fields found and first row '" . $set->[0] . "' is strange type";
381 }
382 # Hashref of arrayrefs - fatal only if labels => 1
383 elsif ($defn->{labels}) {
384 0 croak "[derive_fields] no fields found and cannot derive fields from data arrayrefs";
385 }
386 # Hashref of arrayrefs, labels off
387 else {
388 0 $defn->{fields} = [ 0 .. $#{$set->[$first]} ];
389 }
390 }
391 else {
392 1 $defn->{fields} = [ sort keys %$set ];
393 }
394 }
395 else {
396 0 croak "[derive_fields] no fields found and set '$set' is strange type: $@";
397 }
398
399 croak sprintf "[derive_fields] field derivation failed (fields: %s)",
400 $defn->{fields}
401 7 ▶ unless ref $defn->{fields} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $defn->{"fields"} eq "ARRAY" 0 7 7
402 }
403  
404 # Derive a fields list if none is defined
405 sub check_fields
406 {
407 107 ▶ my $self = shift;
Subroutine check_fields: called CC=4 SCAR=14.0
Pod undocumented
408 107 my ($set) = @_;
409 $self->derive_fields($set)
410 if ! $self->{defn_t}->{fields} ||
411 ref $self->{defn_t}->{fields} ne 'ARRAY' ||
412 107 ▶ ! @{$self->{defn_t}->{fields}};
Branch: 1 branch
BranchTrueFalseTotal
if not $self->{"defn_t"}{"fields"} or ref $self->{"defn_t"}{"fields"} ne "ARRAY" or not @{$self->{"defn_t"}{"fields"};} 7 100 107
Condition66%4/6
not $self->{"defn_t"}{"fields"} or ref $self->{"defn_t"}{"fields"} ne "ARRAY" or not @{$self->{"defn_t"}{"fields"};}
l!l&&r!l&&!r
70100
not $self->{"defn_t"}{"fields"} or ref $self->{"defn_t"}{"fields"} ne "ARRAY"
l!l&&r!l&&!r
70100
MC/DC33%1/3
not $self->{"defn_t"}{"fields"} or ref $self->{"defn_t"}{"fields"} ne "ARRAY" or not @{$self->{"defn_t"}{"fields"};}
not $self->{"defn_t"}{"fields"} ref $self->{"defn_t"}{"fields"} ne "ARRAY" not @{$self->{"defn_t"}{"fields"};}
Truth table50%2/4
not(A) or B or not(C)
A= not $self->{"defn_t"}{"fields"}B= ref $self->{"defn_t"}{"fields"} ne "ARRAY"C= not @{$self->{"defn_t"}{"fields"};}
ABCresult
1XX1
01X1
0011
0000
413 }
414  
415 # Splice additional fields into the fields array
416 sub splice_fields
417 {
418 1 ▶ my $self = shift;
Subroutine splice_fields: called CC=6 SCAR=18.0
Pod undocumented
419 1 my $defn = $self->{defn_t};
420 1 my $add = $defn->{fields_add};
421 1 ▶ return unless ref $defn->{fields} eq 'ARRAY' && ref $add eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $defn->{"fields"} eq "ARRAY" and ref $add eq "HASH" 0 1 1
Condition33%1/3
ref $defn->{"fields"} eq "ARRAY" and ref $add eq "HASH"
!ll&&!rl&&r
001
MC/DC0%0/2
ref $defn->{"fields"} eq "ARRAY" and ref $add eq "HASH"
ref $defn->{"fields"} eq "ARRAY" ref $add eq "HASH"
Truth table33%1/3
A and B
A= ref $defn->{"fields"} eq "ARRAY"B= ref $add eq "HASH"
ABresult
0X0
100
111
422  
423 1 ▶ for (my $i = $#{$defn->{fields}}; $i >= 0; $i--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 6 1 7
424 6 my $f = $defn->{fields}->[$i];
425 6 ▶ next unless $add->{$f};
Branch: 1 branch
BranchTrueFalseTotal
unless $add->{$f} 4 2 6
426 2 ▶ if (ref $add->{$f} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $add->{$f} eq 'ARRAY') { } 1 1 2
427 1 splice @{$defn->{fields}}, $i+1, 0, @{$add->{$f}};
428 }
429 else {
430 1 splice @{$defn->{fields}}, $i+1, 0, $add->{$f};
431 }
432 }
433 }
434  
435 # Omit/remove fields from the fields array
436 sub omit_fields
437 {
438 26 ▶ my $self = shift;
Subroutine omit_fields: called CC=1 SCAR=0.0
Pod undocumented
439 26 my $defn = $self->{defn_t};
440 26 my %omit = map { $_ => 1 } @{$defn->{fields_omit}};
441 26 $defn->{fields} = [ grep { ! exists $omit{$_} } @{$defn->{fields}} ];
442 }
443  
444 #
445 # Deep copy routine, originally swiped from a Randal Schwartz column
446 #
447 sub deepcopy
448 {
449 5304 ▶ my ($self, $this) = @_;
Subroutine deepcopy: called CC=6 SCAR=19.0
Pod undocumented
450 5304 ▶ if (! ref $this) {
Branch: 5 branches
BranchTrueFalseTotal
if (not ref $this) { } 2488 2816 5304
elsif (ref $this eq 'ARRAY') { } 249 2567 2816
elsif (ref $this eq 'HASH') { } 2458 109 2567
elsif (ref $this eq 'CODE') { } 109 0 109
elsif (sprintf $this) { } 0 0 0
451 2488 return $this;
452 } elsif (ref $this eq "ARRAY") {
453 249 return [map $self->deepcopy($_), @$this];
454 } elsif (ref $this eq "HASH") {
455 2458 return {map { $_ => $self->deepcopy($this->{$_}) } keys %$this};
456 } elsif (ref $this eq "CODE") {
457 109 return $this;
458 } elsif (sprintf $this) {
459 # Object! As a last resort, try copying the stringification value
460 0 return sprintf $this;
461 } else {
462 0 die "what type is $_? (" . ref($this) . ")";
463 }
464 }
465  
466 #
467 # Create a transient presentation definition (defn_t) by doing a set of one-off
468 # or dataset-specific mappings on the current table definition e.g. deriving
469 # a field list if none is set, setting up a field map for arrayref-of-
470 # arrayref sets, and mapping top-level shortcuts into their field
471 # attribute equivalents.
472 #
473 sub prerender_munge
474 {
475 107 ▶ my $self = shift;
Subroutine prerender_munge: called CC=35 SCAR=35.7
Pod undocumented
476 107 my ($set, $defn) = @_;
477  
478 # Use $self->{defn} if $defn not passed
479 107 ▶ $defn ||= $self->{defn};
Condition66%2/3
$defn ||= $self->{"defn"}
l!l&&r!l&&!r
90170
MC/DC0%0/2
$defn ||= $self->{"defn"}
$defn $self->{"defn"}
Truth table66%2/3
A ||= B
A= $defnB= $self->{"defn"}
ABresult
1X1
011
000
480  
481 # If already done, return unless we require any dataset-specific mappings
482 # if ($self->{defn_t}) {
483 # return unless
484 # ref $defn->{fields} ne 'ARRAY' ||
485 # ! @{$defn->{fields}} ||
486 # (ref $set eq 'ARRAY' && @$set && ref $set->[0] eq 'ARRAY');
487 # }
488  
489 # Copy $defn to $self->{defn_t}
490 107 $self->{defn_t} = $self->deepcopy($defn);
491  
492 # Try to derive field list if not set
493 107 $self->check_fields($set);
494  
495 # Set up a field map in case we have arrayref based data
496 107 my $defn_t = $self->{defn_t};
497 107 my $pos = 0;
498 107 ▶ my $fields = ref $defn_t->{in_fields} eq 'ARRAY' ? $defn_t->{in_fields} : $defn_t->{fields};
Branch: 1 branch
BranchTrueFalseTotal
ref $defn_t->{'in_fields'} eq 'ARRAY' ? : 1 106 107
499 107 $defn_t->{field_map} = { map { $_ => $pos++; } @$fields };
500  
501 # Splice any additional fields into the fields array
502 107 ▶ $self->splice_fields if $defn_t->{fields_add};
Branch: 1 branch
BranchTrueFalseTotal
if $defn_t->{"fields_add"} 1 106 107
503 107 ▶ $self->omit_fields if $defn_t->{fields_omit};
Branch: 1 branch
BranchTrueFalseTotal
if $defn_t->{"fields_omit"} 26 81 107
504  
505 # Map top-level 'labels' and 'label_links' hashrefs into fields
506 107 ▶ if (ref $defn_t->{labels} eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $defn_t->{"labels"} eq "HASH") 10 97 107
507 10 for (keys %{$defn_t->{labels}}) {
508 23 ▶ $defn_t->{field_attr}->{$_} ||= {};
Condition100%2/2
$defn_t->{"field_attr"}{$_} ||= {}
l!l
518
MC/DC100%1/1
$defn_t->{"field_attr"}{$_} ||= {}
$defn_t->{"field_attr"}{$_}
Truth table100%2/2
A
A= $defn_t->{"field_attr"}{$_}
Aresult
11
00
509 23 $defn_t->{field_attr}->{$_}->{label} = $defn_t->{labels}->{$_};
510 }
511 }
512 107 ▶ if (ref $defn_t->{label_links} eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $defn_t->{"label_links"} eq "HASH") 1 106 107
513 1 for (keys %{$defn_t->{label_links}}) {
514 1 ▶ $defn_t->{field_attr}->{$_} ||= {};
Condition50%1/2
$defn_t->{"field_attr"}{$_} ||= {}
l!l
01
MC/DC0%0/1
$defn_t->{"field_attr"}{$_} ||= {}
$defn_t->{"field_attr"}{$_}
Truth table50%1/2
A
A= $defn_t->{"field_attr"}{$_}
Aresult
11
00
515 1 $defn_t->{field_attr}->{$_}->{label_link} = $defn_t->{label_links}->{$_};
516 }
517 }
518  
519 # Map top-level 'derived' field list into fields
520 107 ▶ if ($defn_t->{derived}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($defn_t->{"derived"}) 0 107 107
521 0 for (@{ $defn_t->{derived} }) {
522 0 $defn_t->{field_attr}->{$_}->{derived} = 1;
523 }
524 }
525  
526 # If style across, map top-level 'thtr' hashref into -defaults label_ attributes
527 107 ▶ if ($self->{defn_t}->{style} eq 'across' && ref $defn_t->{thtr} eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"defn_t"}{"style"} eq "across" and ref $defn_t->{"thtr"} eq "HASH") 1 106 107
Condition100%3/3
$self->{"defn_t"}{"style"} eq "across" and ref $defn_t->{"thtr"} eq "HASH"
!ll&&!rl&&r
10511
MC/DC100%2/2
$self->{"defn_t"}{"style"} eq "across" and ref $defn_t->{"thtr"} eq "HASH"
$self->{"defn_t"}{"style"} eq "across" ref $defn_t->{"thtr"} eq "HASH"
Truth table100%3/3
A and B
A= $self->{"defn_t"}{"style"} eq "across"B= ref $defn_t->{"thtr"} eq "HASH"
ABresult
0X0
100
111
528 1 for (keys %{$defn_t->{thtr}}) {
529 $defn_t->{field_attr}->{-defaults}->{"label_$_"} = $defn_t->{thtr}->{$_}
530 1 ▶ if ! exists $defn_t->{field_attr}->{-defaults}->{"label_$_"};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $defn_t->{"field_attr"}{"-defaults"}{"label_$_"} 1 0 1
531 }
532 }
533 # Map top-level 'th' hashref into -defaults label_ attributes
534 107 ▶ if (ref $defn_t->{th} eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $defn_t->{"th"} eq "HASH") 8 99 107
535 8 for (keys %{$defn_t->{th}}) {
536 $defn_t->{field_attr}->{-defaults}->{"label_$_"} = $defn_t->{th}->{$_}
537 8 ▶ if ! exists $defn_t->{field_attr}->{-defaults}->{"label_$_"};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $defn_t->{"field_attr"}{"-defaults"}{"label_$_"} 8 0 8
538 }
539 }
540 # Map top-level 'td' hashref into -defaults
541 107 ▶ if (ref $defn_t->{td} eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $defn_t->{"td"} eq "HASH") 4 103 107
542 4 $defn_t->{field_attr}->{-defaults} = { %{$defn_t->{td}}, %{$defn_t->{field_attr}->{-defaults}} };
543 }
544  
545 # Move regex field_attr definitions into a -regex hash
546 107 $defn_t->{field_attr}->{-regex} = {};
547 107 for (keys %{$defn_t->{field_attr}}) {
548 # The following test is an ugly hack, but the regex is stringified now
549 289 ▶ next unless m/^\(\?.*\)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless /^\(\?.*\)$/ 285 4 289
550 4 $defn_t->{field_attr}->{-regex}->{$_} = $defn_t->{field_attr}->{$_};
551 4 delete $defn_t->{field_attr}->{$_};
552 }
553  
554 # Force a non-array stripe to be a binary array
555 107 ▶ if ($defn_t->{stripe} && ref $defn_t->{stripe} ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($defn_t->{"stripe"} and ref $defn_t->{"stripe"} ne "ARRAY") 6 101 107
Condition100%3/3
$defn_t->{"stripe"} and ref $defn_t->{"stripe"} ne "ARRAY"
!ll&&!rl&&r
9836
MC/DC100%2/2
$defn_t->{"stripe"} and ref $defn_t->{"stripe"} ne "ARRAY"
$defn_t->{"stripe"} ref $defn_t->{"stripe"} ne "ARRAY"
Truth table100%3/3
A and B
A= $defn_t->{"stripe"}B= ref $defn_t->{"stripe"} ne "ARRAY"
ABresult
0X0
100
111
556 6 $defn_t->{stripe} = [ undef, $defn_t->{stripe} ];
557 }
558  
559 # thead and tfoot imply tbody
560 107 ▶ if ($defn_t->{thead}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($defn_t->{"thead"}) 7 100 107
561 7 ▶ $defn_t->{tbody} ||= 1;
Condition100%2/2
$defn_t->{"tbody"} ||= 1
l!l
16
MC/DC100%1/1
$defn_t->{"tbody"} ||= 1
$defn_t->{"tbody"}
Truth table100%2/2
A
A= $defn_t->{"tbody"}
Aresult
11
00
562 7 ▶ $defn_t->{thead} = {} if ! ref $defn_t->{thead};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $defn_t->{"thead"} 4 3 7
563 }
564 107 ▶ if ($defn_t->{tfoot}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($defn_t->{"tfoot"}) 3 104 107
565 3 ▶ $defn_t->{tbody} ||= 1;
Condition100%2/2
$defn_t->{"tbody"} ||= 1
l!l
12
MC/DC100%1/1
$defn_t->{"tbody"} ||= 1
$defn_t->{"tbody"}
Truth table100%2/2
A
A= $defn_t->{"tbody"}
Aresult
11
00
566 3 ▶ $defn_t->{tfoot} = {} if ! ref $defn_t->{tfoot};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $defn_t->{"tfoot"} 2 1 3
567 }
568  
569 # Setup tbody attributes hash for hashref tbodies
570 107 ▶ if ($defn_t->{tbody}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($defn_t->{"tbody"}) 23 84 107
571 23 ▶ if (ref $defn_t->{tbody}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $defn_t->{'tbody'}) { } 13 10 23
572 13 $defn_t->{tbody_attr} = $self->deepcopy($defn_t->{tbody});
573 13 for (keys %{$defn_t->{tbody_attr}}) {
574 17 ▶ delete $defn_t->{tbody_attr}->{$_} if m/^-/;
Branch: 1 branch
BranchTrueFalseTotal
if /^-/ 12 5 17
575 }
576 }
577 else {
578 10 $defn_t->{tbody_attr} = {};
579 }
580 }
581  
582 }
583  
584 # Split fields up according to labelgroups into two field lists
585 # labelgroup entries look like LabelGroup => [ qw(field1 field2 field3) ]
586 sub labelgroup_fields
587 {
588 1 ▶ my $self = shift;
Subroutine labelgroup_fields: called CC=10 SCAR=23.2
Pod undocumented
589  
590 1 my @fields = @{$self->{defn_t}->{fields}};
591 1 my $labelgroups = $self->{defn_t}->{labelgroups};
592  
593 # Map first field of each labelgroup into a hash
594 1 my %grouped_fields;
595 1 for my $label (keys %$labelgroups) {
596 1 my $field1 = $labelgroups->{$label}->[0];
597 1 $grouped_fields{ $field1 } = $label;
598 }
599  
600 # Process all fields looking for label groups, and splitting out if found
601 1 my (@fields1, @fields2);
602 1 ▶ while (my $f = shift @fields) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $f = shift @fields) 3 1 4
603 3 ▶ if (my $label = $grouped_fields{ $f }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $label = $grouped_fields{$f}) { } 1 2 3
604 # Found a grouped label - splice labelled fields into fields2
605 1 my @gfields = @{ $labelgroups->{$label} };
606 1 shift @gfields; # discard $f
607  
608 # Check all fields match
609 1 my @next_group;
610 1 ▶ while (my $g = shift @gfields) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $g = shift @gfields) 1 1 2
611 1 my $fn = shift @fields;
612 1 ▶ push @next_group, $fn if $fn eq $g;
Branch: 1 branch
BranchTrueFalseTotal
if $fn eq $g 1 0 1
613 }
614  
615 # If we have as many as we're expecting, we're good
616 1 ▶ if (@next_group == @{ $labelgroups->{$label} } - 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@next_group == @{$labelgroups->{$label};} - 1) { } 1 0 1
617 1 push @fields2, $f, @next_group;
618 1 push @fields1, $label;
619 }
620 # Otherwise our field list doesn't exactly match the label group - omit
621 else {
622 0 push @fields1, $f;
623 # Push @next_group back into @fields for reprocessing
624 0 unshift @fields, @next_group;
625 }
626 }
627  
628 # Not a labelgroup
629 else {
630 2 push @fields1, $f;
631 }
632 }
633  
634 # Setup $field1_tx_attr map if we have any @fields2 fields
635 1 my $field1_tx_attr = {};
636 1 ▶ if (@fields2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fields2) 1 0 1
637 1 for my $f (@fields1) {
638 3 ▶ if (my $grouped_fields = $labelgroups->{$f}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $grouped_fields = $labelgroups->{$f}) { } 1 2 3
639 1 $field1_tx_attr->{$f} = { colspan => scalar(@$grouped_fields) };
640 }
641 else {
642 2 $field1_tx_attr->{$f} = { rowspan => 2 };
643 }
644 }
645 }
646  
647 1 return (\@fields1, \@fields2, $field1_tx_attr);
648 }
649  
650 # -------------------------------------------------------------------------
651 #
652 # Return the given HTML $tag with attributes from the $attr hashref.
653 # An attribute with a non-empty value (i.e. not '' or undef) is rendered
654 # attr="value"; one with a value of '' is rendered as a 'bare' attribute
655 # (i.e. no '=') in non-xhtml mode; one with undef is simply ignored
656 # (e.g. allowing unset CGI parameters to be ignored).
657 #
658 sub start_tag
659 {
660 2279 ▶ my ($self, $tag, $attr, $close) = @_;
Subroutine start_tag: called CC=9 SCAR=22.0
Pod undocumented
661 2279 my $xhtml = $self->{defn_t}->{xhtml};
662 2279 my $str = "<$tag";
663 2279 ▶ if (ref $attr eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $attr eq "HASH") 2245 34 2279
664 2245 for my $a (sort keys %$attr) {
665 364 ▶ next if ! defined $attr->{$a};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $attr->{$a} 24 340 364
666 340 ▶ if ($attr->{$a} ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr->{$a} ne '') { } 316 24 340
667 316 $str .= qq( $a="$attr->{$a}");
668 }
669 else {
670 24 ▶ if ($MINIMISED_ATTR{$a}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($MINIMISED_ATTR{$a}) { } 12 12 24
671 12 ▶ $str .= $xhtml ? qq( $a="$a") : qq( $a);
Branch: 1 branch
BranchTrueFalseTotal
$xhtml ? : 6 6 12
672 }
673 else {
674 12 $str .= qq( $a="");
675 }
676 }
677 }
678 }
679 2279 ▶ $str .= ' /' if $close && $xhtml;
Branch: 1 branch
BranchTrueFalseTotal
if $close and $xhtml 6 2273 2279
Condition100%3/3
$close and $xhtml
!ll&&!rl&&r
227036
MC/DC100%2/2
$close and $xhtml
$close $xhtml
Truth table100%3/3
A and B
A= $closeB= $xhtml
ABresult
0X0
100
111
680 2279 $str .= ">";
681 2279 return $str;
682 }
683  
684 sub end_tag
685 {
686 2270 ▶ my ($self, $tag) = @_;
Subroutine end_tag: called CC=1 SCAR=0.0
Pod undocumented
687 2270 return "</$tag>";
688 }
689  
690 # ------------------------------------------------------------------------
691 # Pre- and post-table content
692  
693 # Title, text, and caption elements may be:
694 # - hashref, containing 'value' (scalar) and 'format' (scalar or subref)
695 # elements that are rendered like table cells
696 # - scalar, that is treated as a scalar 'value' as above with a default
697 # 'format'
698 # - subref, that is executed and the results used verbatim (i.e. no default
699 # 'format' applies
700 sub text_element
701 {
702 321 ▶ my $self = shift;
Subroutine text_element: called CC=21 SCAR=30.8
Pod undocumented
703 321 my ($type, $dataset) = @_;
704 321 ▶ return '' unless grep /^$type$/, qw(title text caption);
Branch: 1 branch
BranchTrueFalseTotal
unless grep /^$type$/, ("title", "text", "caption") 0 321 321
705  
706 321 my $elt = $self->{defn_t}->{$type};
707  
708 # Subref - execute and return results
709 321 ▶ if (ref $elt eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $elt eq 'CODE') { } 2 319 321
elsif (not ref $elt) { } 16 303 319
710 2 return $elt->($dataset, $type);
711 }
712  
713 # Scalar - convert to hashref
714 elsif (! ref $elt) {
715 16 my $value = $elt;
716 16 $elt = {};
717 # If there's a DEFAULT_DEFN $elt entry, use that as defaults
718 16 ▶ if ($DEFAULT_DEFN{$type} && ref $DEFAULT_DEFN{$type} eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($DEFAULT_DEFN{$type} and ref $DEFAULT_DEFN{$type} eq "HASH") 16 0 16
Condition33%1/3
$DEFAULT_DEFN{$type} and ref $DEFAULT_DEFN{$type} eq "HASH"
!ll&&!rl&&r
0016
MC/DC0%0/2
$DEFAULT_DEFN{$type} and ref $DEFAULT_DEFN{$type} eq "HASH"
$DEFAULT_DEFN{$type} ref $DEFAULT_DEFN{$type} eq "HASH"
Truth table33%1/3
A and B
A= $DEFAULT_DEFN{$type}B= ref $DEFAULT_DEFN{$type} eq "HASH"
ABresult
0X0
100
111
719 16 $elt = { %{$DEFAULT_DEFN{$type}} };
720 }
721 16 $elt->{value} = $value;
722 }
723  
724 # Hashref - render and return
725 319 ▶ if (ref $elt eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $elt eq "HASH") 319 0 319
726 319 ▶ return '' unless defined $elt->{value} or defined $elt->{title};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $elt->{"value"} or defined $elt->{"title"} 294 25 319
Condition100%3/3
defined $elt->{"value"} or defined $elt->{"title"}
l!l&&r!l&&!r
232294
MC/DC100%2/2
defined $elt->{"value"} or defined $elt->{"title"}
defined $elt->{"value"} defined $elt->{"title"}
Truth table100%3/3
A or B
A= defined $elt->{"value"}B= defined $elt->{"title"}
ABresult
1X1
011
000
727  
728 # Omit formatting if tag-wrapped
729 return $elt->{value}
730 25 ▶ if defined $elt->{value} && $elt->{value} =~ m/^\s*\<.*\>\s*$/s;
Branch: 1 branch
BranchTrueFalseTotal
if defined $elt->{"value"} and $elt->{"value"} =~ /^\s*\<.*\>\s*$/s 6 19 25
Condition100%3/3
defined $elt->{"value"} and $elt->{"value"} =~ /^\s*\<.*\>\s*$/s
!ll&&!rl&&r
2176
MC/DC100%2/2
defined $elt->{"value"} and $elt->{"value"} =~ /^\s*\<.*\>\s*$/s
defined $elt->{"value"} $elt->{"value"} =~ /^\s*\<.*\>\s*$/s
Truth table100%3/3
A and B
A= defined $elt->{"value"}B= $elt->{"value"} =~ /^\s*\<.*\>\s*$/s
ABresult
0X0
100
111
731 return $elt->{title}
732 19 ▶ if defined $elt->{title} && $elt->{title} =~ m/^\s*\<.*\>\s*$/s;
Branch: 1 branch
BranchTrueFalseTotal
if defined $elt->{"title"} and $elt->{"title"} =~ /^\s*\<.*\>\s*$/s 0 19 19
Condition66%2/3
defined $elt->{"title"} and $elt->{"title"} =~ /^\s*\<.*\>\s*$/s
!ll&&!rl&&r
1720
MC/DC0%0/2
defined $elt->{"title"} and $elt->{"title"} =~ /^\s*\<.*\>\s*$/s
defined $elt->{"title"} $elt->{"title"} =~ /^\s*\<.*\>\s*$/s
Truth table66%2/3
A and B
A= defined $elt->{"title"}B= $elt->{"title"} =~ /^\s*\<.*\>\s*$/s
ABresult
0X0
100
111
733  
734 # sprintf format pattern
735 return sprintf $elt->{format}, $elt->{value}
736 if defined $elt->{value} && defined $elt->{format} &&
737 19 ▶ ! ref $elt->{format};
Branch: 1 branch
BranchTrueFalseTotal
if defined $elt->{"value"} and defined $elt->{"format"} and not ref $elt->{"format"} 13 6 19
Condition100%6/6
defined $elt->{"value"} and defined $elt->{"format"} and not ref $elt->{"format"}
!ll&&!rl&&r
3313
defined $elt->{"value"} and defined $elt->{"format"}
!ll&&!rl&&r
2116
MC/DC100%3/3
defined $elt->{"value"} and defined $elt->{"format"} and not ref $elt->{"format"}
defined $elt->{"value"} defined $elt->{"format"} not ref $elt->{"format"}
Truth table100%4/4
A and B and not(C)
A= defined $elt->{"value"}B= defined $elt->{"format"}C= not ref $elt->{"format"}
ABCresult
0XX0
10X0
1100
1111
738  
739 # subref format pattern
740 return $elt->{format}->($elt->{value}, $dataset, $type)
741 if defined $elt->{value} && defined $elt->{format} &&
742 6 ▶ ref $elt->{format} eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if defined $elt->{"value"} and defined $elt->{"format"} and ref $elt->{"format"} eq "CODE" 3 3 6
Condition83%5/6
defined $elt->{"value"} and defined $elt->{"format"} and ref $elt->{"format"} eq "CODE"
!ll&&!rl&&r
303
defined $elt->{"value"} and defined $elt->{"format"}
!ll&&!rl&&r
213
MC/DC66%2/3
defined $elt->{"value"} and defined $elt->{"format"} and ref $elt->{"format"} eq "CODE"
defined $elt->{"value"} defined $elt->{"format"} ref $elt->{"format"} eq "CODE"
Truth table75%3/4
A and B and C
A= defined $elt->{"value"}B= defined $elt->{"format"}C= ref $elt->{"format"} eq "CODE"
ABCresult
0XX0
10X0
1100
1111
743
744 # Deprecated formatting style
745 3 ▶ if ($elt->{title}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elt->{"title"}) 2 1 3
746 2 my $title = $elt->{title};
747 2 ▶ my $tag = $elt->{tag} || 'h2';
Condition100%2/2
$elt->{'tag'} || 'h2'
l!l
11
MC/DC100%1/1
$elt->{'tag'} || 'h2'
$elt->{'tag'}
Truth table100%2/2
A
A= $elt->{'tag'}
Aresult
11
00
748 2 delete $elt->{title};
749 2 delete $elt->{tag};
750 2 delete $elt->{format};
751 2 return $self->start_tag($tag, $elt) . $title .
752 $self->end_tag($tag, $elt) . "\n";
753 }
754  
755 # fallthru: return 'value'
756 1 return $elt->{value};
757 }
758  
759 0 return '';
760 }
761  
762 # unchomp: ensure (non-empty) elements end with a newline
763 sub unchomp
764 {
765 321 ▶ my $self = shift;
Subroutine unchomp: called CC=4 SCAR=13.9
Pod undocumented
766 321 my $data = shift;
767 321 ▶ $data .= "\n" if defined $data && $data ne '' && substr($data,-1) ne "\n";
Branch: 1 branch
BranchTrueFalseTotal
if defined $data and $data ne "" and substr($data, -1) ne "\n" 7 314 321
Condition83%5/6
defined $data and $data ne "" and substr($data, -1) ne "\n"
!ll&&!rl&&r
294207
defined $data and $data ne ""
!ll&&!rl&&r
029427
MC/DC66%2/3
defined $data and $data ne "" and substr($data, -1) ne "\n"
defined $data $data ne "" substr($data, -1) ne "\n"
Truth table75%3/4
A and B and C
A= defined $dataB= $data ne ""C= substr($data, -1) ne "\n"
ABCresult
0XX0
10X0
1100
1111
768 321 $data
769 }
770  
771 # title: title/heading preceding the table
772 107 ▶sub title { my $self = shift; $self->unchomp($self->text_element('title', @_)) }
Subroutine title: called CC=1 SCAR=0.0
Pod documented
773 # text: text preceding begin table tag (after title, if any)
774 107 ▶sub text { my $self = shift; $self->unchomp($self->text_element('text', @_)) }
Subroutine text: called CC=1 SCAR=0.0
Pod documented
775  
776 # caption: either new-style <caption> text, or legacy text after end table tag
777 sub caption {
778 214 ▶ my $self = shift;
Subroutine caption: called CC=11 SCAR=24.1
Pod documented
779 214 my ($set, $post_table) = @_;
780 214 my $defn_t = $self->{defn_t};
781  
782 # Legacy text must have a 'format' element
783 214 ▶ if ($post_table &&
Branch: 2 branches
BranchTrueFalseTotal
if ($post_table and ref $defn_t->{'caption'} ne 'HASH' || !$defn_t->{'caption'}{'type'} || $defn_t->{'caption'}{'type'} ne 'caption_caption') { } 105 109 214
elsif (not $post_table and (ref $defn_t->{'caption'} eq 'HASH' and $defn_t->{'caption'}{'type'} and $defn_t->{'caption'}{'type'} eq 'caption_caption')) { } 2 116 118
Condition88%8/9
$post_table and ref $defn_t->{"caption"} ne "HASH" || !$defn_t->{"caption"}{"type"} || $defn_t->{"caption"}{"type"} ne "caption_caption"
!ll&&!rl&&r
1072105
ref $defn_t->{"caption"} ne "HASH" or not $defn_t->{"caption"}{"type"} or $defn_t->{"caption"}{"type"} ne "caption_caption"
l!l&&r!l&&!r
7982
ref $defn_t->{"caption"} ne "HASH" or not $defn_t->{"caption"}{"type"}
l!l&&r!l&&!r
70100
MC/DC75%3/4
$post_table and ref $defn_t->{"caption"} ne "HASH" || !$defn_t->{"caption"}{"type"} || $defn_t->{"caption"}{"type"} ne "caption_caption"
$post_table ref $defn_t->{"caption"} ne "HASH" not $defn_t->{"caption"}{"type"} $defn_t->{"caption"}{"type"} ne "caption_caption"
Truth table80%4/5
A and B or not(C) or D
A= $post_tableB= ref $defn_t->{"caption"} ne "HASH"C= not $defn_t->{"caption"}{"type"}D= $defn_t->{"caption"}{"type"} ne "caption_caption"
ABCDresult
0XXX0
10000
11XX1
101X1
10011
784 (ref $defn_t->{caption} ne 'HASH' ||
785 ! $defn_t->{caption}->{type} ||
786 $defn_t->{caption}->{type} ne 'caption_caption')) {
787 105 $self->unchomp($self->text_element('caption', $set));
788 }
789 ▶ elsif (! $post_table &&
Condition88%8/9
not $post_table and (ref $defn_t->{"caption"} eq "HASH" and $defn_t->{"caption"}{"type"} and $defn_t->{"caption"}{"type"} eq "caption_caption")
!ll&&!rl&&r
21052
ref $defn_t->{"caption"} eq "HASH" and $defn_t->{"caption"}{"type"} and $defn_t->{"caption"}{"type"} eq "caption_caption"
!ll&&!rl&&r
7982
ref $defn_t->{"caption"} eq "HASH" and $defn_t->{"caption"}{"type"}
!ll&&!rl&&r
70100
MC/DC75%3/4
not $post_table and (ref $defn_t->{"caption"} eq "HASH" and $defn_t->{"caption"}{"type"} and $defn_t->{"caption"}{"type"} eq "caption_caption")
not $post_table ref $defn_t->{"caption"} eq "HASH" $defn_t->{"caption"}{"type"} $defn_t->{"caption"}{"type"} eq "caption_caption"
Truth table80%4/5
not(A) and B and C and D
A= not $post_tableB= ref $defn_t->{"caption"} eq "HASH"C= $defn_t->{"caption"}{"type"}D= $defn_t->{"caption"}{"type"} eq "caption_caption"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
790 (ref $defn_t->{caption} eq 'HASH' &&
791 $defn_t->{caption}->{type} &&
792 $defn_t->{caption}->{type} eq 'caption_caption')) {
793 delete $defn_t->{caption}->{format}
794 2 ▶ if ($defn_t->{caption}->{format} || '') eq $DEFAULT_TEXT_FORMAT;
Branch: 1 branch
BranchTrueFalseTotal
if ($defn_t->{"caption"}{"format"} || "") eq $DEFAULT_TEXT_FORMAT 1 1 2
Condition50%1/2
$defn_t->{'caption'}{'format'} || ''
l!l
20
MC/DC0%0/1
$defn_t->{'caption'}{'format'} || ''
$defn_t->{'caption'}{'format'}
Truth table50%1/2
A
A= $defn_t->{'caption'}{'format'}
Aresult
11
00
795 2 $self->unchomp(
796 $self->start_tag('caption') .
797 $self->text_element('caption', $set) .
798 $self->end_tag('caption')
799 )
800 }
801 }
802  
803 sub colgroups {
804 107 ▶ my $self = shift;
Subroutine colgroups: called CC=7 SCAR=19.5
Pod documented
805 107 my ($set) = @_;
806 107 my $defn_t = $self->{defn_t};
807  
808 107 ▶ return '' unless $self->{defn_t}->{colgroups};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"defn_t"}{"colgroups"} 104 3 107
809  
810 3 my $content = '';
811 3 for my $cg (@{$self->{defn_t}->{colgroups}}) {
812 8 ▶ if ($cg->{cols} && ref $cg->{cols} && ref $cg->{cols} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($cg->{'cols'} and ref $cg->{'cols'} and ref $cg->{'cols'} eq 'ARRAY') { } 1 7 8
Condition66%4/6
$cg->{"cols"} and ref $cg->{"cols"} and ref $cg->{"cols"} eq "ARRAY"
!ll&&!rl&&r
701
$cg->{"cols"} and ref $cg->{"cols"}
!ll&&!rl&&r
701
MC/DC33%1/3
$cg->{"cols"} and ref $cg->{"cols"} and ref $cg->{"cols"} eq "ARRAY"
$cg->{"cols"} ref $cg->{"cols"} ref $cg->{"cols"} eq "ARRAY"
Truth table50%2/4
A and B and C
A= $cg->{"cols"}B= ref $cg->{"cols"}C= ref $cg->{"cols"} eq "ARRAY"
ABCresult
0XX0
10X0
1100
1111
813 1 my $cols = delete $cg->{cols};
814 1 $content .= $self->start_tag('colgroup', $cg, 0) . "\n";
815 1 for my $col (@$cols) {
816 2 $content .= $self->start_tag('col', $col, 1) . "\n";
817 }
818 1 $content .= $self->end_tag('colgroup') . "\n";
819 }
820 else {
821 7 $content .= $self->start_tag('colgroup', $cg, 1) . "\n";
822 }
823 }
824 3 return $content;
825 }
826  
827 # ------------------------------------------------------------------------
828 # Content before begin table tag
829 sub pre_table
830 {
831 107 ▶ my $self = shift;
Subroutine pre_table: called CC=3 SCAR=11.2
Pod undocumented
832 107 my ($set) = @_;
833 107 my $content = '';
834 107 ▶ $content .= $self->title($set) if $self->{defn_t}->{title};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"defn_t"}{"title"} 107 0 107
835 107 ▶ $content .= $self->text($set) if $self->{defn_t}->{text};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"defn_t"}{"text"} 107 0 107
836 107 return $content;
837 }
838  
839 # Provided for subclassing
840 sub start_table
841 {
842 107 ▶ my $self = shift;
Subroutine start_table: called CC=3 SCAR=11.0
Pod undocumented
843 107 ▶ return '' if exists $self->{defn_t}->{table} && ! $self->{defn_t}->{table};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"defn_t"}{"table"} and not $self->{"defn_t"}{"table"} 1 106 107
Condition66%2/3
exists $self->{"defn_t"}{"table"} and not $self->{"defn_t"}{"table"}
!ll&&!rl&&r
01061
MC/DC50%1/2
exists $self->{"defn_t"}{"table"} and not $self->{"defn_t"}{"table"}
exists $self->{"defn_t"}{"table"} not $self->{"defn_t"}{"table"}
Truth table66%2/3
A and not(B)
A= exists $self->{"defn_t"}{"table"}B= not $self->{"defn_t"}{"table"}
ABresult
0X0
100
111
844 106 return $self->start_tag('table',$self->{defn_t}->{table}) . "\n";
845 }
846  
847 # Provided for subclassing
848 sub end_table
849 {
850 107 ▶ my $self = shift;
Subroutine end_table: called CC=3 SCAR=11.0
Pod undocumented
851 107 ▶ return '' if exists $self->{defn_t}->{table} && ! $self->{defn_t}->{table};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"defn_t"}{"table"} and not $self->{"defn_t"}{"table"} 1 106 107
Condition66%2/3
exists $self->{"defn_t"}{"table"} and not $self->{"defn_t"}{"table"}
!ll&&!rl&&r
01061
MC/DC50%1/2
exists $self->{"defn_t"}{"table"} and not $self->{"defn_t"}{"table"}
exists $self->{"defn_t"}{"table"} not $self->{"defn_t"}{"table"}
Truth table66%2/3
A and not(B)
A= exists $self->{"defn_t"}{"table"}B= not $self->{"defn_t"}{"table"}
ABresult
0X0
100
111
852 106 return $self->end_tag('table') . "\n";
853 }
854  
855 # Content after end table tag
856 sub post_table
857 {
858 107 ▶ my $self = shift;
Subroutine post_table: called CC=1 SCAR=0.0
Pod undocumented
859 107 my ($set) = @_;
860 107 my $content = '';
861 107 $content .= $self->caption($set, 'post_table');
862 107 return $content;
863 }
864  
865 # ------------------------------------------------------------------------
866 # Apply 'format' formatting
867 sub cell_format_format
868 {
869 28 ▶ my ($self, $data, $fattr, $row, $field) = @_;
Subroutine cell_format_format: called CC=6 SCAR=18.2
Pod undocumented
870 28 my $ref = ref $fattr->{format};
871 28 ▶ croak "[cell_format] invalid '$field' format: $ref" if $ref && $ref ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if $ref and $ref ne "CODE" 0 28 28
Condition66%2/3
$ref and $ref ne "CODE"
!ll&&!rl&&r
7210
MC/DC0%0/2
$ref and $ref ne "CODE"
$ref $ref ne "CODE"
Truth table66%2/3
A and B
A= $refB= $ref ne "CODE"
ABresult
0X0
100
111
872 28 ▶ $data = $fattr->{format}->($data, $row || {}, $field) if $ref eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if $ref eq "CODE" 21 7 28
Condition50%1/2
$row || {}
l!l
210
MC/DC0%0/1
$row || {}
$row
Truth table50%1/2
A
A= $row
Aresult
11
00
873 28 ▶ $data = sprintf $fattr->{format}, $data if ! $ref;
Branch: 1 branch
BranchTrueFalseTotal
unless $ref 7 21 28
874 28 return $data;
875 }
876  
877 # Simple tag escaping
878 sub cell_format_escape
879 {
880 1515 ▶ my ($self, $data) = @_;
Subroutine cell_format_escape: called CC=1 SCAR=0.0
Pod undocumented
881 1515 $data =~ s/</&lt;/g;
882 1515 $data =~ s/>/&gt;/g;
883 1515 return $data;
884 }
885  
886 # Link formatting
887 sub cell_format_link
888 {
889 30 ▶ my ($self, $data, $fattr, $row, $field, $data_unformatted) = @_;
Subroutine cell_format_link: called CC=11 SCAR=24.1
Pod undocumented
890 30 my $ldata;
891 30 my $ref = ref $fattr->{link};
892 30 ▶ croak "[cell_format] invalid '$field' link: $ref"
Branch: 1 branch
BranchTrueFalseTotal
if $ref and $ref ne "CODE" 0 30 30
Condition66%2/3
$ref and $ref ne "CODE"
!ll&&!rl&&r
14160
MC/DC0%0/2
$ref and $ref ne "CODE"
$ref $ref ne "CODE"
Truth table66%2/3
A and B
A= $refB= $ref ne "CODE"
ABresult
0X0
100
111
893 if $ref && $ref ne 'CODE';
894 30 ▶ $ldata = $fattr->{link}->($data_unformatted, $row || {}, $field)
Branch: 1 branch
BranchTrueFalseTotal
if $ref eq "CODE" 16 14 30
Condition100%2/2
$row || {}
l!l
133
MC/DC100%1/1
$row || {}
$row
Truth table100%2/2
A
A= $row
Aresult
11
00
895 if $ref eq 'CODE';
896 30 ▶ $ldata = sprintf $fattr->{link}, $data_unformatted
Branch: 1 branch
BranchTrueFalseTotal
unless $ref 14 16 30
897 if ! $ref;
898 30 ▶ if ($ldata) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ldata) 30 0 30
899 # $data = sprintf qq(<a href="%s">%s</a>),
900 # uri_escape($ldata, $URI_ESCAPE_CHARS), $data;
901 30 my $link_attr = { href => uri_escape($ldata, $URI_ESCAPE_CHARS)};
902 30 for my $attr (keys %$fattr) {
903 151 ▶ if ($attr =~ m/^link_/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr =~ /^link_/) 34 117 151
904 34 my $val = $fattr->{$attr};
905 34 $attr =~ s/^link_//;
906 34 ▶ $link_attr->{$attr} = ref $val eq 'CODE' ?
Branch: 1 branch
BranchTrueFalseTotal
ref $val eq 'CODE' ? : 7 27 34
Condition100%2/2
$row || {}
l!l
43
MC/DC100%1/1
$row || {}
$row
Truth table100%2/2
A
A= $row
Aresult
11
00
907 $val->($data_unformatted, $row || {}, $field) :
908 $val;
909 }
910 }
911 30 $data = $self->start_tag('a', $link_attr) . $data . $self->end_tag('a');
912 }
913 30 return $data;
914 }
915  
916 #
917 # Format the given data item using formatting field attributes (e.g. format,
918 # link, escape etc.)
919 #
920 sub cell_format
921 {
922 1630 ▶ my $self = shift;
Subroutine cell_format: called CC=11 SCAR=24.0
Pod undocumented
923 1630 my ($data, $fattr, $row, $field) = @_;
924 1630 my $defn = $self->{defn_t};
925  
926 # Trim
927 1630 ▶ $data =~ s/^\s*(.*?)\s*$/$1/ if $data ne '' && $defn->{trim};
Branch: 1 branch
BranchTrueFalseTotal
if $data ne "" and $defn->{"trim"} 108 1522 1630
Condition100%3/3
$data ne "" and $defn->{"trim"}
!ll&&!rl&&r
1151407108
MC/DC100%2/2
$data ne "" and $defn->{"trim"}
$data ne "" $defn->{"trim"}
Truth table100%3/3
A and B
A= $data ne ""B= $defn->{"trim"}
ABresult
0X0
100
111
928  
929 1630 my $data_unformatted = $data;
930  
931 # 'escape' boolean for simple tag escaping (defaults to on)
932 $data = $self->cell_format_escape($data)
933 1630 ▶ if $data ne '' && ($fattr->{escape} || ! exists $fattr->{escape});
Branch: 1 branch
BranchTrueFalseTotal
if $data ne "" and $fattr->{"escape"} || !exists $fattr->{"escape"} 1515 115 1630
Condition50%3/6
$data ne "" and $fattr->{"escape"} || !exists $fattr->{"escape"}
!ll&&!rl&&r
11501515
$fattr->{"escape"} or not exists $fattr->{"escape"}
l!l&&r!l&&!r
015150
MC/DC33%1/3
$data ne "" and $fattr->{"escape"} || !exists $fattr->{"escape"}
$data ne "" $fattr->{"escape"} not exists $fattr->{"escape"}
Truth table50%2/4
A and B or not(C)
A= $data ne ""B= $fattr->{"escape"}C= not exists $fattr->{"escape"}
ABCresult
0XX0
1000
11X1
1011
934  
935 # 'format' subroutine or sprintf pattern
936 $data = $self->cell_format_format(@_)
937 1630 ▶ if $fattr->{format};
Branch: 1 branch
BranchTrueFalseTotal
if $fattr->{"format"} 28 1602 1630
938  
939 # 'link' subroutine or sprintf pattern
940 $data = $self->cell_format_link($data, $fattr, $row, $field, $data_unformatted)
941 1630 ▶ if $data ne '' && $fattr->{link};
Branch: 1 branch
BranchTrueFalseTotal
if $data ne "" and $fattr->{"link"} 30 1600 1630
Condition100%3/3
$data ne "" and $fattr->{"link"}
!ll&&!rl&&r
113148730
MC/DC100%2/2
$data ne "" and $fattr->{"link"}
$data ne "" $fattr->{"link"}
Truth table100%3/3
A and B
A= $data ne ""B= $fattr->{"link"}
ABresult
0X0
100
111
942  
943 # 'null' defaults
944 $data = $defn->{null}
945 1630 ▶ if defined $defn->{null} && $data eq '';
Branch: 1 branch
BranchTrueFalseTotal
if defined $defn->{"null"} and $data eq "" 6 1624 1630
Condition100%3/3
defined $defn->{"null"} and $data eq ""
!ll&&!rl&&r
15041206
MC/DC100%2/2
defined $defn->{"null"} and $data eq ""
defined $defn->{"null"} $data eq ""
Truth table100%3/3
A and B
A= defined $defn->{"null"}B= $data eq ""
ABresult
0X0
100
111
946  
947 1630 return $data;
948 }
949  
950 sub label
951 {
952 391 ▶ my ($self, $label, $field) = @_;
Subroutine label: called CC=5 SCAR=16.1
Pod documented
953  
954 # Use first label if arrayref
955 391 my $l;
956 391 ▶ if (ref $label eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $label eq 'CODE') { } 2 389 391
957 2 $l = $label->($field);
958 }
959 else {
960 389 $l = $label;
961 }
962 391 ▶ $l = $self->derive_label($field) unless defined $l;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $l 356 35 391
963 391 ▶ $l = $self->{defn_t}->{null} if $l eq '' && defined $self->{defn_t}->{null};
Branch: 1 branch
BranchTrueFalseTotal
if $l eq "" and defined $self->{"defn_t"}{"null"} 2 389 391
Condition66%2/3
$l eq "" and defined $self->{"defn_t"}{"null"}
!ll&&!rl&&r
38902
MC/DC50%1/2
$l eq "" and defined $self->{"defn_t"}{"null"}
$l eq "" defined $self->{"defn_t"}{"null"}
Truth table66%2/3
A and B
A= $l eq ""B= defined $self->{"defn_t"}{"null"}
ABresult
0X0
100
111
964 391 return $l;
965 }
966  
967 #
968 # Add in any extra (conditional) defaults for this field.
969 # Provided for subclassing.
970 #
971 sub cell_merge_extras
972 {
973 391 ▶ return ();
Subroutine cell_merge_extras: called CC=1 SCAR=0.0
Pod undocumented
974 }
975  
976 #
977 # Split field attr data into label, tfoot, and data buckets
978 sub cell_split_label_tfoot_data {
979 391 ▶ my ($self, $fattr, $field) = @_;
Subroutine cell_split_label_tfoot_data: called CC=7 SCAR=19.7
Pod undocumented
980  
981 391 ▶ $self->{defn_t}->{label_attr}->{$field} ||= {};
Condition50%1/2
$self->{"defn_t"}{"label_attr"}{$field} ||= {}
l!l
0391
MC/DC0%0/1
$self->{"defn_t"}{"label_attr"}{$field} ||= {}
$self->{"defn_t"}{"label_attr"}{$field}
Truth table50%1/2
A
A= $self->{"defn_t"}{"label_attr"}{$field}
Aresult
11
00
982 391 ▶ $self->{defn_t}->{tfoot_attr}->{$field} ||= {};
Condition50%1/2
$self->{"defn_t"}{"tfoot_attr"}{$field} ||= {}
l!l
0391
MC/DC0%0/1
$self->{"defn_t"}{"tfoot_attr"}{$field} ||= {}
$self->{"defn_t"}{"tfoot_attr"}{$field}
Truth table50%1/2
A
A= $self->{"defn_t"}{"tfoot_attr"}{$field}
Aresult
11
00
983 391 ▶ $self->{defn_t}->{data_attr}->{$field} ||= {};
Condition50%1/2
$self->{"defn_t"}{"data_attr"}{$field} ||= {}
l!l
0391
MC/DC0%0/1
$self->{"defn_t"}{"data_attr"}{$field} ||= {}
$self->{"defn_t"}{"data_attr"}{$field}
Truth table50%1/2
A
A= $self->{"defn_t"}{"data_attr"}{$field}
Aresult
11
00
984  
985 391 for (keys %$fattr) {
986 202 ▶ if (substr($_,0,6) eq 'label_') {
Branch: 2 branches
BranchTrueFalseTotal
if (substr($_, 0, 6) eq 'label_') { } 52 150 202
elsif (substr($_, 0, 6) eq 'tfoot_') { } 18 132 150
987 52 $self->{defn_t}->{label_attr}->{$field}->{substr($_,6)} = $fattr->{$_};
988 }
989 elsif (substr($_,0,6) eq 'tfoot_') {
990 18 $self->{defn_t}->{tfoot_attr}->{$field}->{substr($_,6)} = $fattr->{$_};
991 }
992 else {
993 132 $self->{defn_t}->{data_attr}->{$field}->{$_} = $fattr->{$_};
994 }
995 }
996 391 $self->{defn_t}->{label_attr}->{$field}->{value} = $self->label(delete $fattr->{label}, $field);
997 }
998  
999 #
1000 # Create tx_attr for each attr bucket by removing attributes in $field_attr
1001 #
1002 sub cell_split_out_tx_attr {
1003 391 ▶ my ($self, $field) = @_;
Subroutine cell_split_out_tx_attr: called CC=6 SCAR=17.9
Pod undocumented
1004  
1005 391 for my $attr (qw(label_attr tfoot_attr data_attr)) {
1006 1173 my %tx_attr = %{ $self->{defn_t}->{$attr}->{$field} };
1007 1173 my $tx_code = 0;
1008 1173 for (keys %tx_attr) {
1009 593 ▶ delete $tx_attr{$_} if exists $self->{field_attr}->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"field_attr"}{$_} 481 112 593
1010 593 ▶ delete $tx_attr{$_} if m/^link_/;
Branch: 1 branch
BranchTrueFalseTotal
if /^link_/ 13 580 593
1011 593 ▶ $tx_code = 1 if ref $tx_attr{$_} eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $tx_attr{$_} eq "CODE" 25 568 593
1012 }
1013 1173 $self->{defn_t}->{$attr}->{$field}->{tx_attr} = \%tx_attr;
1014 1173 $self->{defn_t}->{$attr}->{$field}->{tx_code} = $tx_code;
1015 }
1016 }
1017  
1018 #
1019 # Merge default and field attributes once each per-field for labels and data
1020 #
1021 sub cell_merge_defaults
1022 {
1023 391 ▶ my ($self, $row, $field) = @_;
Subroutine cell_merge_defaults: called CC=4 SCAR=13.9
Pod undocumented
1024  
1025 391 ▶ return if $self->{defn_t}->{data_attr}->{$field};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"defn_t"}{"data_attr"}{$field} 0 391 391
1026  
1027 # Create a temp $fattr hash merging defaults, regexes, and field attrs
1028 391 my $fattr = { %{$self->{defn_t}->{field_attr}->{-defaults}},
1029 $self->cell_merge_extras($row, $field) };
1030 391 for my $regex (sort keys %{$self->{defn_t}->{field_attr}->{-regex}}) {
1031 15 ▶ next unless $field =~ $regex;
Branch: 1 branch
BranchTrueFalseTotal
unless $field =~ /$regex/ 9 6 15
1032 6 @$fattr{ keys %{$self->{defn_t}->{field_attr}->{-regex}->{$regex}} } =
1033 6 values %{$self->{defn_t}->{field_attr}->{-regex}->{$regex}};
1034 }
1035 391 @$fattr{ keys %{$self->{defn_t}->{field_attr}->{$field}} } =
1036 391 values %{$self->{defn_t}->{field_attr}->{$field}};
1037
1038 # Split out label, data, and tfoot attributes
1039 391 $self->cell_split_label_tfoot_data($fattr, $field);
1040  
1041 # Remove tx_attr for label, data, and tfoot attr buckets
1042 391 $self->cell_split_out_tx_attr($field);
1043 }
1044  
1045 #
1046 # Set and format the data for a single (data) cell or item
1047 #
1048 sub cell_value
1049 {
1050 1666 ▶ my $self = shift;
Subroutine cell_value: called CC=21 SCAR=35.8
Pod undocumented
1051 1666 my ($row, $field, $fattr) = @_;
1052 1666 my $defn = $self->{defn_t};
1053 1666 my $value;
1054  
1055 # 'value' literal takes precedence over row
1056 1666 ▶ if (exists $fattr->{value} && ! ref $fattr->{value}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $fattr->{'value'} and not ref $fattr->{'value'}) { } 175 1491 1666
elsif (ref $row and not $fattr->{'derived'}) { } 1482 18 1500
Condition100%3/3
exists $fattr->{"value"} and not ref $fattr->{"value"}
!ll&&!rl&&r
145437175
MC/DC100%2/2
exists $fattr->{"value"} and not ref $fattr->{"value"}
exists $fattr->{"value"} not ref $fattr->{"value"}
Truth table100%3/3
A and not(B)
A= exists $fattr->{"value"}B= not ref $fattr->{"value"}
ABresult
0X0
100
111
1057 175 ▶ $value = defined $fattr->{value} ? $fattr->{value} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $fattr->{'value'} ? : 175 0 175
1058 }
1059  
1060 # Get value from $row (but skip 'derived' fields)
1061 ▶ elsif (ref $row && ! $fattr->{derived}) {
Condition66%2/3
ref $row and not $fattr->{"derived"}
!ll&&!rl&&r
901482
MC/DC50%1/2
ref $row and not $fattr->{"derived"}
ref $row not $fattr->{"derived"}
Truth table66%2/3
A and not(B)
A= ref $rowB= not $fattr->{"derived"}
ABresult
0X0
100
111
1062 1482 ▶ if (blessed $row) {
Branch: 3 branches
BranchTrueFalseTotal
if (builtin::blessed($row)) { } 0 1482 1482
elsif (ref $row eq 'ARRAY') { } 1149 333 1482
elsif (ref $row eq 'HASH' and exists $row->{$field}) { } 295 38 333
1063 # Field-methods e.g. Class::DBI, DBIx::Class
1064 0 ▶ if (eval { $row->can($field) }
Branch: 2 branches
BranchTrueFalseTotal
if (eval { do { $row->can($field) } } and $field ne 'delete') { } 0 0 0
elsif (eval { do { $row->can('get_column') } }) { } 0 0 0
Condition0%0/3
eval { do { $row->can($field) } } and $field ne "delete"
!ll&&!rl&&r
000
MC/DC0%0/2
eval { do { $row->can($field) } } and $field ne "delete"
eval { do { $row->can($field) } } $field ne "delete"
Truth table0%0/3
A and B
A= eval { do { $row->can($field) } }B= $field ne "delete"
ABresult
0X0
100
111
1065 && $field ne 'delete') { # special DBIx::Class protection :-)
1066 0 $value = eval { $row->$field() };
1067 }
1068 # For DBIx::Class we need to check both methods and get_column() values,
1069 # since joined fields (+columns/+select) are only available via the latter
1070 0 elsif (eval { $row->can('get_column') }) {
1071 0 $value = eval { $row->get_column($field) };
1072 }
1073 }
1074 elsif (ref $row eq 'ARRAY') {
1075 1149 ▶ my $i = keys %{$defn->{field_map}} ? $defn->{field_map}->{$field} : $field;
Branch: 1 branch
BranchTrueFalseTotal
keys %{$$defn{'field_map'};} ? : 1149 0 1149
1076 1149 ▶ $value = $row->[ $i ] if defined $i;
Branch: 1 branch
BranchTrueFalseTotal
if defined $i 1143 6 1149
1077 }
1078 ▶ elsif (ref $row eq 'HASH' && exists $row->{$field}) {
Condition66%2/3
ref $row eq "HASH" and exists $row->{$field}
!ll&&!rl&&r
038295
MC/DC50%1/2
ref $row eq "HASH" and exists $row->{$field}
ref $row eq "HASH" exists $row->{$field}
Truth table66%2/3
A and B
A= ref $row eq "HASH"B= exists $row->{$field}
ABresult
0X0
100
111
1079 295 $value = $row->{$field};
1080 }
1081 }
1082  
1083 # Handle 'value' subref
1084 1666 ▶ if (exists $fattr->{value} && ref $fattr->{value}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $fattr->{"value"} and ref $fattr->{"value"}) 37 1629 1666
Condition100%3/3
exists $fattr->{"value"} and ref $fattr->{"value"}
!ll&&!rl&&r
145417537
MC/DC100%2/2
exists $fattr->{"value"} and ref $fattr->{"value"}
exists $fattr->{"value"} ref $fattr->{"value"}
Truth table100%3/3
A and B
A= exists $fattr->{"value"}B= ref $fattr->{"value"}
ABresult
0X0
100
111
1085 37 my $ref = ref $fattr->{value};
1086 37 ▶ if ($ref eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq 'CODE') { } 37 0 37
1087 37 $value = $fattr->{value}->($value, $row, $field);
1088 }
1089 else {
1090 0 croak "[cell_value] invalid '$field' value (not scalar or code ref): $ref";
1091 };
1092 }
1093  
1094 1666 ▶ $value = $fattr->{default} if ! defined $value && exists $fattr->{default};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value and exists $fattr->{"default"} 0 1666 1666
Condition66%2/3
not defined $value and exists $fattr->{"default"}
!ll&&!rl&&r
1584820
MC/DC0%0/2
not defined $value and exists $fattr->{"default"}
not defined $value exists $fattr->{"default"}
Truth table66%2/3
not(A) and B
A= not defined $valueB= exists $fattr->{"default"}
ABresult
0X0
100
111
1095  
1096 1666 ▶ return defined $value ? $value : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 1584 82 1666
1097 }
1098  
1099 #
1100 # Return a cell value created from one or more other cells
1101 #
1102 sub cell_composite
1103 {
1104 6 ▶ my $self = shift;
Subroutine cell_composite: called CC=5 SCAR=16.8
Pod undocumented
1105 6 my ($row, $field, $fattr) = @_;
1106  
1107 my $composite = $fattr->{composite}
1108 6 ▶ or die "Missing composite field attribute";
Branch: 1 branch
BranchTrueFalseTotal
my $composite = $fattr->{"composite"} or die "Missing composite field attribute" 0 6 6
1109 6 my @composite = ();
1110 6 for my $f (@$composite) {
1111 12 push @composite, $self->cell_single(row => $row, field => $f, tags => 0);
1112 }
1113  
1114 6 ▶ my $composite_join = $fattr->{composite_join} || ' ';
Condition50%1/2
$fattr->{'composite_join'} || ' '
l!l
06
MC/DC0%0/1
$fattr->{'composite_join'} || ' '
$fattr->{'composite_join'}
Truth table50%1/2
A
A= $fattr->{'composite_join'}
Aresult
11
00
1115 6 ▶ if (ref $composite_join eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $composite_join eq 'CODE') { } 0 6 6
1116 0 return $composite_join->(\@composite, $row, $field);
1117 }
1118 else {
1119 6 return join ' ', @composite;
1120 }
1121 }
1122  
1123 #
1124 # Set and format the data for a single (data) cell or item
1125 #
1126 sub cell_content
1127 {
1128 1630 ▶ my $self = shift;
Subroutine cell_content: called CC=3 SCAR=11.0
Pod undocumented
1129 1630 my ($row, $field, $fattr) = @_;
1130 1630 my $value;
1131  
1132 # Composite fields - concatenate members together
1133 1630 ▶ if ($fattr->{composite}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fattr->{'composite'}) { } 6 1624 1630
1134 6 $value = $self->cell_composite(@_);
1135 }
1136  
1137 # Standard field - get value from $row
1138 else {
1139 1624 $value = $self->cell_value(@_);
1140 }
1141  
1142 # Format
1143 1630 my $fvalue = $self->cell_format($value, $fattr, $row, $field);
1144  
1145 1630 ▶ return wantarray ? ($fvalue, $value) : $fvalue;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1630 0 1630
1146 }
1147  
1148 #
1149 # Wrap cell in <th> or <td> table tags
1150 #
1151 sub cell_tags
1152 {
1153 1610 ▶ my ($self, $data, $row, $field, $tx_attr) = @_;
Subroutine cell_tags: called CC=3 SCAR=11.0
Pod undocumented
1154  
1155 1610 ▶ my $tag = ! defined $row ? 'th' : 'td';
Branch: 1 branch
BranchTrueFalseTotal
!defined($row) ? : 153 1457 1610
1156 1610 ▶ $data = '' unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 1610 1610
1157 1610 return $self->start_tag($tag, $tx_attr) . $data . $self->end_tag($tag);
1158 }
1159  
1160 #
1161 # Execute any th or td attribute subrefs
1162 #
1163 sub cell_tx_execute
1164 {
1165 63 ▶ my $self = shift;
Subroutine cell_tx_execute: called CC=3 SCAR=11.0
Pod undocumented
1166 63 my ($tx_attr, $value, $row, $field) = @_;
1167 63 my %tx2 = ();
1168 63 ▶ while (my ($k,$v) = each %$tx_attr) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$tx_attr) 82 63 145
1169 82 ▶ if (ref $v eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v eq 'CODE') { } 79 3 82
1170 79 $tx2{$k} = $v->($value, $row, $field);
1171 }
1172 else {
1173 3 $tx2{$k} = $v;
1174 }
1175 }
1176 63 return \%tx2;
1177 }
1178  
1179 #
1180 # Render a single table cell or item
1181 #
1182 sub cell_single
1183 {
1184 1630 ▶ my ($self, %args) = @_;
Subroutine cell_single: called CC=17 SCAR=28.7
Pod undocumented
1185 1630 my $row = delete $args{row};
1186 1630 my $field = delete $args{field};
1187 1630 my $fattr = delete $args{field_attr};
1188 1630 my $tx_attr = delete $args{tx_attr};
1189 1630 my $tx_attr_extra = delete $args{tx_attr_extra};
1190 1630 my $skip_count = delete $args{skip_count};
1191 1630 my $tags = delete $args{tags};
1192 1630 ▶ $tags = 1 unless defined $tags;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tags 1610 20 1630
1193 1630 ▶ die "Unknown arguments to cell_single: " . join(',', keys %args) if %args;
Branch: 1 branch
BranchTrueFalseTotal
if %args 0 1630 1630
1194  
1195 # Merge default and field attributes once for each field
1196 $self->cell_merge_defaults($row, $field)
1197 1630 ▶ if ! $self->{defn_t}->{data_attr}->{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"defn_t"}{"data_attr"}{$field} 391 1239 1630
1198  
1199 1630 my $tx_code = 0;
1200 1630 ▶ unless ($fattr && $tx_attr) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fattr and $tx_attr) 1630 0 1630
Condition33%1/3
$fattr and $tx_attr
!ll&&!rl&&r
163000
MC/DC0%0/2
$fattr and $tx_attr
$fattr $tx_attr
Truth table33%1/3
A and B
A= $fattrB= $tx_attr
ABresult
0X0
100
111
1201 1630 ▶ if (! defined $row || $row eq 'thead') {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $row or $row eq 'thead') { } 161 1469 1630
elsif ($row eq 'tfoot') { } 12 1457 1469
Condition66%2/3
not defined $row or $row eq "thead"
l!l&&r!l&&!r
16101469
MC/DC50%1/2
not defined $row or $row eq "thead"
not defined $row $row eq "thead"
Truth table66%2/3
not(A) or B
A= not defined $rowB= $row eq "thead"
ABresult
1X1
011
000
1202 161 $fattr = $self->{defn_t}->{label_attr}->{$field};
1203 }
1204 elsif ($row eq 'tfoot') {
1205 12 $fattr = $self->{defn_t}->{tfoot_attr}->{$field};
1206 }
1207 else {
1208 1457 $fattr = $self->{defn_t}->{data_attr}->{$field};
1209 }
1210 1630 $tx_attr = $fattr->{tx_attr};
1211 1630 $tx_code = $fattr->{tx_code};
1212 }
1213  
1214 # Standard (non-composite) fields
1215 1630 my ($fvalue, $value) = $self->cell_content($row, $field, $fattr);
1216  
1217 # If $tx_attr includes coderefs, execute them
1218 1630 ▶ $tx_attr = $self->cell_tx_execute($tx_attr, $value, $row, $field)
Branch: 1 branch
BranchTrueFalseTotal
if $tx_code 63 1567 1630
1219 if $tx_code;
1220  
1221 1630 my $tx_attr_merged = $tx_attr;
1222 3 $tx_attr_merged = { %$tx_attr, %{$tx_attr_extra->{$field}} }
1223 1630 ▶ if $tx_attr_extra && $tx_attr_extra->{$field};
Branch: 1 branch
BranchTrueFalseTotal
if $tx_attr_extra and $tx_attr_extra->{$field} 3 1627 1630
Condition66%2/3
$tx_attr_extra and $tx_attr_extra->{$field}
!ll&&!rl&&r
162703
MC/DC50%1/2
$tx_attr_extra and $tx_attr_extra->{$field}
$tx_attr_extra $tx_attr_extra->{$field}
Truth table66%2/3
A and B
A= $tx_attr_extraB= $tx_attr_extra->{$field}
ABresult
0X0
100
111
1224  
1225 # Generate tags
1226 1630 ▶ my $cell = $tags ? $self->cell_tags($fvalue, $row, $field, $tx_attr_merged) : $fvalue;
Branch: 1 branch
BranchTrueFalseTotal
$tags ? : 1610 20 1630
1227  
1228 1630 ▶ $$skip_count = $tx_attr->{colspan} ? ($tx_attr->{colspan}-1) : 0
Branch: 2 branches
BranchTrueFalseTotal
if $skip_count and ref $skip_count and ref $skip_count eq "SCALAR" 1610 20 1630
$tx_attr->{'colspan'} ? : 4 1606 1610
Condition66%4/6
$skip_count and ref $skip_count and ref $skip_count eq "SCALAR"
!ll&&!rl&&r
2001610
$skip_count and ref $skip_count
!ll&&!rl&&r
2001610
MC/DC33%1/3
$skip_count and ref $skip_count and ref $skip_count eq "SCALAR"
$skip_count ref $skip_count ref $skip_count eq "SCALAR"
Truth table50%2/4
A and B and C
A= $skip_countB= ref $skip_countC= ref $skip_count eq "SCALAR"
ABCresult
0XX0
10X0
1100
1111
1229 if $skip_count && ref $skip_count && ref $skip_count eq 'SCALAR';
1230  
1231 1630 return $cell;
1232 }
1233  
1234 #
1235 # Legacy interface (deprecated)
1236 #
1237 sub cell_wantarray
1238 {
1239 0 ▶ my ($self, $row, $field, $fattr, $tx_attr, %opts) = @_;
Subroutine cell_wantarray: not called CC=1 SCAR=6.9
Pod undocumented
1240  
1241 0 my $skip_count;
1242 0 my $cell = $self->cell_single(
1243 %opts,
1244 row => $row,
1245 field => $field,
1246 field_attr => $fattr,
1247 tx_attr => $tx_attr,
1248 skip_count => \$skip_count,
1249 );
1250  
1251 0 return ($cell, $skip_count);
1252 }
1253  
1254 #
1255 # Render a single table cell (legacy interface)
1256 #
1257 sub cell
1258 {
1259 0 ▶ my ($self, $row, $field, $fattr, $tx_attr, %opts) = @_;
Subroutine cell: not called CC=1 SCAR=6.9
Pod undocumented
1260  
1261 0 $self->cell_single(
1262 %opts,
1263 row => $row,
1264 field => $field,
1265 field_attr => $fattr,
1266 tx_attr => $tx_attr,
1267 );
1268 }
1269  
1270 #
1271 # Modify the $tr hashref for striping. If $type is 'SCALAR', the stripe is
1272 # a HTML colour string for a bgcolor attribute for the relevant row; if
1273 # $type is 'HASH' the stripe is a set of attributes to be merged.
1274 # $stripe has already been coerced to an arrayref if something else.
1275 #
1276 sub stripe
1277 {
1278 459 ▶ my ($self, $tr, $rownum) = @_;
Subroutine stripe: called CC=12 SCAR=24.9
Pod documented
1279 459 my $stripe = $self->{defn_t}->{stripe};
1280 459 ▶ return $tr unless $stripe;
Branch: 1 branch
BranchTrueFalseTotal
unless $stripe 428 31 459
1281
1282 31 my $r = int($rownum % scalar(@$stripe)) - 1;
1283 31 ▶ if (defined $stripe->[$r]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $stripe->[$r]) 22 9 31
1284 22 ▶ if (! ref $stripe->[$r]) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $stripe->[$r]) { } 13 9 22
elsif (ref $stripe->[$r] eq 'HASH') { } 9 0 9
1285 # Set bgcolor to stripe (exception: header where bgcolor already set)
1286 $tr->{bgcolor} = $stripe->[$r]
1287 13 ▶ unless $rownum == 0 && exists $tr->{bgcolor};
Branch: 1 branch
BranchTrueFalseTotal
unless $rownum == 0 and exists $tr->{"bgcolor"} 12 1 13
Condition100%3/3
$rownum == 0 and exists $tr->{"bgcolor"}
!ll&&!rl&&r
931
MC/DC100%2/2
$rownum == 0 and exists $tr->{"bgcolor"}
$rownum == 0 exists $tr->{"bgcolor"}
Truth table100%3/3
A and B
A= $rownum == 0B= exists $tr->{"bgcolor"}
ABresult
0X0
100
111
1288 }
1289 elsif (ref $stripe->[$r] eq 'HASH') {
1290 # Class attributes are special in that they're additive,
1291 # so we can merge instead of overwriting
1292 9 ▶ if ($stripe->[$r]->{class} && $tr->{class}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($stripe->[$r]{'class'} and $tr->{'class'}) { } 5 4 9
elsif ($rownum == 0) { } 1 3 4
Condition66%2/3
$stripe->[$r]{"class"} and $tr->{"class"}
!ll&&!rl&&r
045
MC/DC50%1/2
$stripe->[$r]{"class"} and $tr->{"class"}
$stripe->[$r]{"class"} $tr->{"class"}
Truth table66%2/3
A and B
A= $stripe->[$r]{"class"}B= $tr->{"class"}
ABresult
0X0
100
111
1293 5 $tr->{class} = "$stripe->[$r]->{class} $tr->{class}";
1294 }
1295  
1296 # Existing attributes take precedence over stripe ones for header
1297 elsif ($rownum == 0) {
1298 1 for (keys %{$stripe->[$r]}) {
1299 1 ▶ $tr->{$_} = $stripe->[$r]->{$_} unless exists $tr->{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $tr->{$_} 1 0 1
1300 }
1301 }
1302  
1303 # For non-header rows, merge attributes straight into $tr
1304 else {
1305 3 @$tr{keys %{$stripe->[$r]}} = values %{$stripe->[$r]};
1306 }
1307 }
1308 # Else silently ignore
1309 }
1310 31 return $tr;
1311 }
1312  
1313 #
1314 # Return tbody close and/or open tags if appropriate, '' otherwise
1315 #
1316 sub tbody
1317 {
1318 407 ▶ my $self = shift;
Subroutine tbody: called CC=14 SCAR=26.4
Pod documented
1319 407 my ($row, $rownum) = @_;
1320 407 my $generate = 0;
1321  
1322 407 ▶ return '' unless $self->{defn_t}->{tbody};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"defn_t"}{"tbody"} 286 121 407
1323  
1324 # Scalar tbody - generate once only
1325 121 ▶ if (! ref $self->{defn_t}->{tbody}) {
Branch: 3 branches
BranchTrueFalseTotal
if (not ref $self->{'defn_t'}{'tbody'}) { } 36 85 121
elsif ($self->{'defn_t'}{'tbody'}{'-field'}) { } 18 67 85
elsif (my $r = $self->{'defn_t'}{'tbody'}{'-rows'}) { } 54 13 67
1326 36 ▶ $generate++ if ! $self->{defn_t}->{tbody_open};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"defn_t"}{"tbody_open"} 10 26 36
1327 }
1328
1329 # tbody with -field - generate when field value changes
1330 elsif ($self->{defn_t}->{tbody}->{'-field'}) {
1331 18 my $value = $self->cell_value($row, $self->{defn_t}->{tbody}->{'-field'});
1332 18 ▶ if (exists $self->{defn_t}->{tbody_field_value}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'defn_t'}{'tbody_field_value'}) { } 15 3 18
1333 15 ▶ if ($value eq $self->{defn_t}->{tbody_field_value} ||
Branch: 1 branch
BranchTrueFalseTotal
if ($value eq $self->{'defn_t'}{'tbody_field_value'} or not defined $value || defined $self->{'defn_t'}{'tbody_field_value'}) { } 8 7 15
Condition50%3/6
$value eq $self->{"defn_t"}{"tbody_field_value"} or not defined $value || defined $self->{"defn_t"}{"tbody_field_value"}
l!l&&r!l&&!r
807
defined $value || defined $self->{'defn_t'}{'tbody_field_value'}
l!l&&r!l&&!r
700
MC/DC33%1/3
$value eq $self->{"defn_t"}{"tbody_field_value"} or not defined $value || defined $self->{"defn_t"}{"tbody_field_value"}
$value eq $self->{"defn_t"}{"tbody_field_value"} defined $value defined $self->{'defn_t'}{'tbody_field_value'}
Truth table50%2/4
A or not(B || C)
A= $value eq $self->{"defn_t"}{"tbody_field_value"}B= defined $valueC= defined $self->{'defn_t'}{'tbody_field_value'}
ABCresult
1XX1
0001
01X0
0010
1334 (! defined $value &&
1335 ! defined $self->{defn_t}->{tbody_field_value})) {
1336 8 return '';
1337 }
1338 else {
1339 7 $generate++;
1340 }
1341 }
1342 else {
1343 3 $generate++;
1344 }
1345 10 $self->{defn_t}->{tbody_field_value} = $value;
1346 }
1347  
1348 # tbody with -rows - generate when $rownum == $r ** n + 1
1349 elsif (my $r = $self->{defn_t}->{tbody}->{'-rows'}) {
1350 54 ▶ $generate++ if int(($rownum-1) % $r) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if int(($rownum - 1) % $r) == 0 28 26 54
1351 }
1352  
1353 # else a hashref - treat like a scalar
1354 else {
1355 13 ▶ $generate++ if ! $self->{defn_t}->{tbody_open};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"defn_t"}{"tbody_open"} 2 11 13
1356 }
1357  
1358 113 my $tbody = '';
1359 113 ▶ if ($generate) {
Branch: 1 branch
BranchTrueFalseTotal
if ($generate) 50 63 113
1360 50 ▶ if ($self->{defn_t}->{tbody_open}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"defn_t"}{"tbody_open"}) 27 23 50
1361 27 $tbody .= $self->end_tag('tbody') . "\n";
1362 }
1363 50 $tbody .= $self->start_tag('tbody', $self->{defn_t}->{tbody_attr}) . "\n";
1364 50 $self->{defn_t}->{tbody_open} = 1;
1365 }
1366 113 return $tbody;
1367 }
1368  
1369 #
1370 # Return an attribute hash for table rows
1371 #
1372 sub tr_attr
1373 {
1374 459 ▶ my ($self, $rownum, $row, $dataset) = @_;
Subroutine tr_attr: called CC=10 SCAR=23.1
Pod undocumented
1375 459 my $defn_t = $self->{defn_t};
1376 459 my $tr = undef;
1377 459 ▶ if ($rownum == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rownum == 0) { } 38 421 459
1378 38 ▶ $tr = $defn_t->{thtr} if $defn_t->{thtr};
Branch: 1 branch
BranchTrueFalseTotal
if $defn_t->{"thtr"} 5 33 38
1379 38 ▶ $tr ||= $self->deepcopy($defn_t->{tr_base});
Condition66%2/3
$tr ||= $self->deepcopy($defn_t->{"tr_base"})
l!l&&r!l&&!r
5033
MC/DC50%1/2
$tr ||= $self->deepcopy($defn_t->{"tr_base"})
$tr $self->deepcopy($defn_t->{"tr_base"})
Truth table66%2/3
A ||= B
A= $trB= $self->deepcopy($defn_t->{"tr_base"})
ABresult
1X1
011
000
1380 }
1381 else {
1382 421 ▶ if (ref $defn_t->{tr} eq 'CODE' && $row) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $defn_t->{'tr'} eq 'CODE' and $row) { } 3 418 421
Condition66%2/3
ref $defn_t->{"tr"} eq "CODE" and $row
!ll&&!rl&&r
41803
MC/DC50%1/2
ref $defn_t->{"tr"} eq "CODE" and $row
ref $defn_t->{"tr"} eq "CODE" $row
Truth table66%2/3
A and B
A= ref $defn_t->{"tr"} eq "CODE"B= $row
ABresult
0X0
100
111
1383 3 $tr = $defn_t->{tr}->($row, $dataset);
1384 }
1385 else {
1386 418 ▶ $defn_t->{tr} = {} unless ref $defn_t->{tr} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $defn_t->{"tr"} eq "HASH" 97 321 418
1387 418 $tr = $self->deepcopy($defn_t->{tr});
1388 # Evaluate any code attributes
1389 418 ▶ $tr ||= {};
Condition50%1/2
$tr ||= {}
l!l
4180
MC/DC0%0/1
$tr ||= {}
$tr
Truth table50%1/2
A
A= $tr
Aresult
11
00
1390 418 ▶ while (my ($k,$v) = each %$tr) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$tr) 29 418 447
1391 29 ▶ $tr->{$k} = $v->($row, $dataset) if ref $v eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $v eq "CODE" 16 13 29
1392 }
1393 }
1394 }
1395 # Stripe and return
1396 459 return $self->stripe($tr, $rownum);
1397 }
1398  
1399 #
1400 # Render a single table row (style 'down')
1401 #
1402 sub row_down
1403 {
1404 451 ▶ my ($self, $row, $rownum, %args) = @_;
Subroutine row_down: called CC=6 SCAR=17.9
Pod undocumented
1405 451 my $fields = delete $args{fields};
1406 451 ▶ $fields ||= $self->{defn_t}->{fields};
Condition66%2/3
$fields ||= $self->{"defn_t"}{"fields"}
l!l&&r!l&&!r
24490
MC/DC0%0/2
$fields ||= $self->{"defn_t"}{"fields"}
$fields $self->{"defn_t"}{"fields"}
Truth table66%2/3
A ||= B
A= $fieldsB= $self->{"defn_t"}{"fields"}
ABresult
1X1
011
000
1407 451 my $tx_attr_extra = delete $args{tx_attr_extra};
1408 451 ▶ my %tx_attr_extra = $tx_attr_extra ? ( tx_attr_extra => $tx_attr_extra ) : ();
Branch: 1 branch
BranchTrueFalseTotal
$tx_attr_extra ? : 1 450 451
1409  
1410 # Open tr
1411 451 my $out = '';
1412 451 $out .= $self->start_tag('tr', $self->tr_attr($rownum, $row));
1413  
1414 # Render cells
1415 451 my @cells = ();
1416 451 my $skip_count = 0;
1417 451 for my $f (@$fields) {
1418 1588 ▶ if ($skip_count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($skip_count > 0) 10 1578 1588
1419 10 $skip_count--;
1420 10 next;
1421 }
1422  
1423 1578 ▶ if (! $row) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $row) { } 145 1433 1578
1424 145 $out .= $self->cell_single(field => $f, skip_count => \$skip_count, %tx_attr_extra);
1425 }
1426 else {
1427 1433 $out .= $self->cell_single(row => $row, field => $f, skip_count => \$skip_count, , %tx_attr_extra);
1428 }
1429 }
1430  
1431 451 $out .= $self->end_tag('tr') . "\n";
1432 451 return $out;
1433 }
1434  
1435 #
1436 # Return a generalised iterator function to walk the set, returning undef at eod
1437 #
1438 sub data_iterator
1439 {
1440 105 ▶ my ($self, $set, $fields) = @_;
Subroutine data_iterator: called CC=12 SCAR=39.3
Pod undocumented
1441 105 my $row = 0;
1442  
1443 105 ▶ croak "invalid Tabulate data type '$set'" unless ref $set;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $set 0 105 105
1444 105 ▶ if (ref $set eq 'CODE') {
Branch: 5 branches
BranchTrueFalseTotal
if (ref $set eq 'CODE') { } 0 105 105
elsif (builtin::blessed($set) and $set->can('Next')) { } 0 105 105
elsif (builtin::blessed($set) and $set->can('next')) { } 0 105 105
elsif (ref $set eq 'ARRAY') { } 99 6 105
elsif (ref $set eq 'HASH' or eval { do { keys %$set } }) { } 6 0 6
1445 return sub {
1446 0 ▶ $row = $row ? $set->() : ($self->{prefetch} || $set->());
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
$row ? : 0 0 0
Condition0%0/3
$self->{"prefetch"} or &$set()
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"prefetch"} or &$set()
$self->{"prefetch"} &$set()
Truth table0%0/3
A or B
A= $self->{"prefetch"}B= &$set()
ABresult
1X1
011
000
1447 0 };
1448 }
1449 ▶ elsif (blessed $set and $set->can('Next')) {
Condition33%1/3
builtin::blessed($set) and $set->can("Next")
!ll&&!rl&&r
10500
MC/DC0%0/2
builtin::blessed($set) and $set->can("Next")
builtin::blessed($set) $set->can("Next")
Truth table33%1/3
A and B
A= builtin::blessed($set)B= $set->can("Next")
ABresult
0X0
100
111
1450 return sub {
1451 0 ▶ $row = $row ? $set->Next : ($self->{prefetch} || eval { $set->First } || $set->Next);
Subroutine __ANON__: not called CC=4 SCAR=30.0
Branch: 1 branch
BranchTrueFalseTotal
$row ? : 0 0 0
Condition0%0/6
$self->{"prefetch"} or eval { do { $set->First } } or $set->Next
l!l&&r!l&&!r
000
$self->{"prefetch"} or eval { do { $set->First } }
l!l&&r!l&&!r
000
MC/DC0%0/3
$self->{"prefetch"} or eval { do { $set->First } } or $set->Next
$self->{"prefetch"} eval { do { $set->First } } $set->Next
Truth table0%0/4
A or B or C
A= $self->{"prefetch"}B= eval { do { $set->First } }C= $set->Next
ABCresult
1XX1
01X1
0011
0000
1452 0 };
1453 }
1454 ▶ elsif (blessed $set and $set->can('next')) {
Condition33%1/3
builtin::blessed($set) and $set->can("next")
!ll&&!rl&&r
10500
MC/DC0%0/2
builtin::blessed($set) and $set->can("next")
builtin::blessed($set) $set->can("next")
Truth table33%1/3
A and B
A= builtin::blessed($set)B= $set->can("next")
ABresult
0X0
100
111
1455 return sub {
1456 0 ▶ $row = $row ? $set->next : ($self->{prefetch} || eval { $set->first } || $set->next);
Subroutine __ANON__: not called CC=4 SCAR=30.0
Branch: 1 branch
BranchTrueFalseTotal
$row ? : 0 0 0
Condition0%0/6
$self->{"prefetch"} or eval { do { $set->first } } or $set->next
l!l&&r!l&&!r
000
$self->{"prefetch"} or eval { do { $set->first } }
l!l&&r!l&&!r
000
MC/DC0%0/3
$self->{"prefetch"} or eval { do { $set->first } } or $set->next
$self->{"prefetch"} eval { do { $set->first } } $set->next
Truth table0%0/4
A or B or C
A= $self->{"prefetch"}B= eval { do { $set->first } }C= $set->next
ABCresult
1XX1
01X1
0011
0000
1457 0 };
1458 }
1459 elsif (ref $set eq 'ARRAY') {
1460 return sub {
1461 489 ▶ return undef if $row > $#$set;
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if $row > $set->$#* 99 390 489
1462 390 $set->[$row++];
1463 99 };
1464 }
1465 0 ▶ elsif (ref $set eq 'HASH' || eval { keys %$set }) {
Condition33%1/3
ref $set eq "HASH" or eval { do { keys %$set } }
l!l&&r!l&&!r
600
MC/DC0%0/2
ref $set eq "HASH" or eval { do { keys %$set } }
ref $set eq "HASH" eval { do { keys %$set } }
Truth table33%1/3
A or B
A= ref $set eq "HASH"B= eval { do { keys %$set } }
ABresult
1X1
011
000
1466 # Check first value - drill down further unless non-reference
1467 6 ▶ my $k = $fields->[0] || (sort keys %$set)[0];
Condition33%1/3
$fields->[0] || (sort keys %$set)[0]
l!l&&r!l&&!r
600
MC/DC0%0/2
$fields->[0] || (sort keys %$set)[0]
$fields->[0] (sort keys %$set)[0]
Truth table33%1/3
A || B
A= $fields->[0]B= (sort keys %$set)[0]
ABresult
1X1
011
000
1468 # For hashes of scalars, just return the hash once-only
1469 6 ▶ if (! ref $set->{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $set->{$k}) { } 6 0 6
1470 return sub {
1471 12 ▶ return undef if $row++;
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if $row++ 6 6 12
1472 6 $set;
1473 6 };
1474 }
1475 # For hashes of refs, return the refs in key order
1476 else {
1477 return sub {
1478 0 ▶ my @k = sort keys %$set;
Subroutine __ANON__: not called CC=2 SCAR=17.9
1479 0 ▶ return undef if $row > $#k;
Branch: 1 branch
BranchTrueFalseTotal
if $row > $#k 0 0 0
1480 0 return $k[$row++];
1481 0 };
1482 }
1483 }
1484 else {
1485 0 croak "invalid Tabulate data type '$set'";
1486 }
1487 }
1488  
1489 #
1490 # Render the table body with successive records down the page
1491 #
1492 sub body_down
1493 {
1494 105 ▶ my ($self, $set) = @_;
Subroutine body_down: called CC=20 SCAR=30.1
Pod undocumented
1495  
1496 # Get data_iterator
1497 105 my @fields = @{$self->{defn_t}->{fields}}
1498 105 ▶ if ref $self->{defn_t}->{fields} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->{"defn_t"}{"fields"} eq "ARRAY" 105 0 105
1499 105 my $data_next = $self->data_iterator($set, \@fields);
1500 105 my $data_prepend = $self->{defn_t}->{data_prepend};
1501  
1502 # Labels/headings
1503 105 my $thead = '';
1504 105 ▶ if ($self->{defn_t}->{labels} && @fields) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'defn_t'}{'labels'} and @fields) { } 37 68 105
elsif ($self->{'defn_t'}{'thead'}) { } 3 65 68
Condition66%2/3
$self->{"defn_t"}{"labels"} and @fields
!ll&&!rl&&r
68037
MC/DC50%1/2
$self->{"defn_t"}{"labels"} and @fields
$self->{"defn_t"}{"labels"} @fields
Truth table66%2/3
A and B
A= $self->{"defn_t"}{"labels"}B= @fields
ABresult
0X0
100
111
1505 $thead .= $self->start_tag('thead', $self->{defn_t}->{thead}) . "\n"
1506 37 ▶ if $self->{defn_t}->{thead};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"defn_t"}{"thead"} 4 33 37
1507  
1508 37 ▶ if ($self->{defn_t}->{labelgroups}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'defn_t'}{'labelgroups'}) { } 1 36 37
1509 1 my ($fields1, $fields2, $field1_tx_attr) = $self->labelgroup_fields;
1510 1 $thead .= $self->row_down(undef, 0, fields => $fields1, tx_attr_extra => $field1_tx_attr);
1511 1 ▶ $thead .= $self->row_down(undef, 0, fields => $fields2) if @$fields2;
Branch: 1 branch
BranchTrueFalseTotal
if @$fields2 1 0 1
1512 }
1513 else {
1514 36 $thead .= $self->row_down(undef, 0);
1515 }
1516  
1517 37 ▶ if ($self->{defn_t}->{thead}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"defn_t"}{"thead"}) 4 33 37
1518 4 $thead .= $self->end_tag('thead') . "\n";
1519 4 $self->{defn_t}->{thead} = 0;
1520 }
1521 }
1522 elsif ($self->{defn_t}->{thead}) {
1523 # If thead set and labels isn't, use the first data row
1524 3 ▶ my $row = $data_prepend && @$data_prepend ? shift @$data_prepend : $data_next->();
Branch: 1 branch
BranchTrueFalseTotal
$data_prepend && @$data_prepend ? : 0 3 3
Condition33%1/3
$data_prepend and @$data_prepend
!ll&&!rl&&r
300
MC/DC0%0/2
$data_prepend and @$data_prepend
$data_prepend @$data_prepend
Truth table33%1/3
A and B
A= $data_prependB= @$data_prepend
ABresult
0X0
100
111
1525 3 ▶ if ($row) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row) 3 0 3
1526 3 $thead .= $self->start_tag('thead', $self->{defn_t}->{thead}) . "\n";
1527 3 $thead .= $self->row_down($row, 1);
1528 3 $thead .= $self->end_tag('thead') . "\n";
1529 }
1530 }
1531  
1532 # Table body
1533 105 my $tbody = '';
1534 105 my $rownum = 1;
1535 105 ▶ if ($data_prepend && @$data_prepend) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data_prepend and @$data_prepend) 3 102 105
Condition100%3/3
$data_prepend and @$data_prepend
!ll&&!rl&&r
10113
MC/DC100%2/2
$data_prepend and @$data_prepend
$data_prepend @$data_prepend
Truth table100%3/3
A and B
A= $data_prependB= @$data_prepend
ABresult
0X0
100
111
1536 3 for my $row (@$data_prepend) {
1537 7 $tbody .= $self->tbody($row, $rownum);
1538 7 $tbody .= $self->row_down($row, $rownum);
1539 7 $rownum++;
1540 }
1541 }
1542 105 ▶ while (my $row = $data_next->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = &$data_next()) 393 105 498
1543 393 $tbody .= $self->tbody($row, $rownum);
1544 393 $tbody .= $self->row_down($row, $rownum);
1545 393 $rownum++;
1546 }
1547 105 ▶ if (my $data_append = $self->{defn_t}->{data_append}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $data_append = $self->{"defn_t"}{"data_append"}) 4 101 105
1548 4 for my $row (@$data_append) {
1549 7 $tbody .= $self->tbody($row, $rownum);
1550 7 $tbody .= $self->row_down($row, $rownum);
1551 7 $rownum++;
1552 }
1553 }
1554  
1555 105 ▶ $tbody .= $self->end_tag('tbody') . "\n" if $self->{defn_t}->{tbody_open};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"defn_t"}{"tbody_open"} 23 82 105
1556  
1557 105 my $tfoot = '';
1558 105 ▶ if ($self->{defn_t}->{tfoot}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"defn_t"}{"tfoot"}) 3 102 105
1559 3 $tfoot .= $self->start_tag('tfoot', $self->{defn_t}->{tfoot}) . "\n";
1560 3 $tfoot .= $self->row_down('tfoot', $rownum);
1561 3 $tfoot .= $self->end_tag('tfoot') . "\n";
1562 }
1563  
1564 105 return $thead . $tfoot . $tbody;
1565 }
1566  
1567 #
1568 # Render a single table row (style 'across')
1569 #
1570 sub row_across
1571 {
1572 8 ▶ my ($self, $data, $rownum, $field) = @_;
Subroutine row_across: called CC=4 SCAR=14.2
Pod undocumented
1573 8 my @cells = ();
1574 8 my @across_row = ();
1575 8 my $skip_count = 0;
1576  
1577 # Label/heading
1578 8 ▶ if ($self->{defn_t}->{labels}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"defn_t"}{"labels"}) 8 0 8
1579 8 push @cells, $self->cell_single(field => $field, skip_count => \$skip_count);
1580 8 push @across_row, $self->cell_single(field => $field, tags => 0);
1581 }
1582  
1583 # Data
1584 8 for my $row (@$data) {
1585 24 ▶ if ($skip_count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($skip_count > 0) 0 24 24
1586 0 $skip_count--;
1587 0 next;
1588 }
1589  
1590 24 push @cells, $self->cell_single(row => $row, field => $field, skip_count => \$skip_count);
1591 24 push @across_row, $self->cell_value($row, $field);
1592 }
1593  
1594 # Build row
1595 8 my $out = $self->start_tag('tr', $self->tr_attr($rownum, $data, \@across_row));
1596 8 $out .= join('', @cells);
1597 8 $out .= $self->end_tag('tr') . "\n";
1598 }
1599  
1600 sub get_dataset
1601 {
1602 2 ▶ my ($self, $set) = @_;
Subroutine get_dataset: called CC=17 SCAR=50.2
Pod undocumented
1603  
1604 # Fetch the full data set
1605 2 my @data = ();
1606 2 ▶ croak "invalid Tabulate data type '$set'" unless ref $set;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $set 0 2 2
1607 2 ▶ if (ref $set eq 'CODE') {
Branch: 5 branches
BranchTrueFalseTotal
if (ref $set eq 'CODE') { } 0 2 2
elsif (builtin::blessed($set) and $set->can('Next')) { } 0 2 2
elsif (builtin::blessed($set) and $set->can('next')) { } 0 2 2
elsif (ref $set eq 'ARRAY') { } 2 0 2
elsif (ref $set eq 'HASH' or eval { do { keys %$set } }) { } 0 0 0
1608 0 ▶ while (my $row = $set->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = &$set()) 0 0 0
1609 0 push @data, $row;
1610 }
1611 }
1612 ▶ elsif (blessed $set and $set->can('Next')) {
Condition33%1/3
builtin::blessed($set) and $set->can("Next")
!ll&&!rl&&r
200
MC/DC0%0/2
builtin::blessed($set) and $set->can("Next")
builtin::blessed($set) $set->can("Next")
Truth table33%1/3
A and B
A= builtin::blessed($set)B= $set->can("Next")
ABresult
0X0
100
111
1613 0 ▶ my $row = eval { $set->First } || $set->Next;
Condition0%0/3
eval { do { $set->First } } || $set->Next
l!l&&r!l&&!r
000
MC/DC0%0/2
eval { do { $set->First } } || $set->Next
eval { do { $set->First } } $set->Next
Truth table0%0/3
A || B
A= eval { do { $set->First } }B= $set->Next
ABresult
1X1
011
000
1614 0 ▶ if (ref $row) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $row) 0 0 0
1615 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$row = $set->Next and do { do { push @data, $row } } 0 0 0
1616 0 push @data, $row;
1617 }
1618 while ($row = $set->Next);
1619 }
1620 }
1621 ▶ elsif (blessed $set and $set->can('next')) {
Condition33%1/3
builtin::blessed($set) and $set->can("next")
!ll&&!rl&&r
200
MC/DC0%0/2
builtin::blessed($set) and $set->can("next")
builtin::blessed($set) $set->can("next")
Truth table33%1/3
A and B
A= builtin::blessed($set)B= $set->can("next")
ABresult
0X0
100
111
1622 0 ▶ my $row = eval { $set->first } || $set->next;
Condition0%0/3
eval { do { $set->first } } || $set->next
l!l&&r!l&&!r
000
MC/DC0%0/2
eval { do { $set->first } } || $set->next
eval { do { $set->first } } $set->next
Truth table0%0/3
A || B
A= eval { do { $set->first } }B= $set->next
ABresult
1X1
011
000
1623 0 ▶ if (ref $row) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $row) 0 0 0
1624 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$row = $set->next and do { do { push @data, $row } } 0 0 0
1625 0 push @data, $row;
1626 }
1627 while ($row = $set->next);
1628 }
1629 }
1630 elsif (ref $set eq 'ARRAY') {
1631 2 @data = @$set;
1632 }
1633 0 ▶ elsif (ref $set eq 'HASH' || eval { keys %$set }) {
Condition0%0/3
ref $set eq "HASH" or eval { do { keys %$set } }
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $set eq "HASH" or eval { do { keys %$set } }
ref $set eq "HASH" eval { do { keys %$set } }
Truth table0%0/3
A or B
A= ref $set eq "HASH"B= eval { do { keys %$set } }
ABresult
1X1
011
000
1634 0 @data = ( $set );
1635 }
1636 else {
1637 0 croak "[body_across] invalid Tabulate data type '$set'";
1638 }
1639  
1640 2 return @data;
1641 }
1642  
1643 #
1644 # Render the table body with successive records across the page
1645 # (i.e. fields down the page)
1646 #
1647 sub body_across
1648 {
1649 2 ▶ my ($self, $set) = @_;
Subroutine body_across: called CC=2 SCAR=6.9
Pod undocumented
1650  
1651 # Iterate over fields (instead of data rows)
1652 2 my @data = $self->get_dataset($set);
1653 2 my $rownum = 1;
1654 2 my $body = '';
1655 2 for my $field (@{$self->{defn_t}->{fields}}) {
1656 8 $body .= $self->row_across(\@data, $rownum, $field);
1657 8 $rownum++;
1658 }
1659  
1660 2 return $body;
1661 }
1662  
1663 # -------------------------------------------------------------------------
1664 sub render_table
1665 {
1666 107 ▶ my ($self, $set) = @_;
Subroutine render_table: called CC=3 SCAR=11.0
Pod undocumented
1667 107 my $defn_t = $self->{defn_t};
1668  
1669 # Style-specific bodies (default is 'down')
1670 107 my $body;
1671 107 ▶ if ($defn_t->{style} eq 'down') {
Branch: 2 branches
BranchTrueFalseTotal
if ($defn_t->{'style'} eq 'down') { } 105 2 107
elsif ($defn_t->{'style'} eq 'across') { } 2 0 2
1672 105 $body .= $self->body_down($set);
1673 }
1674 elsif ($defn_t->{style} eq 'across') {
1675 2 $body .= $self->body_across($set);
1676 }
1677 else {
1678 0 croak sprintf "[render] invalid style '%s'", $defn_t->{style};
1679 }
1680  
1681 # Build table
1682 107 my $table = '';
1683 107 $table .= $self->pre_table($set);
1684 107 $table .= $self->start_table();
1685 107 $table .= $self->caption($set);
1686 107 $table .= $self->colgroups($set);
1687 107 $table .= $body;
1688 107 $table .= $self->end_table();
1689 107 $table .= $self->post_table($set);
1690
1691 107 return $table;
1692 }
1693  
1694 #
1695 # Render the data set $set using the settings in $self->{defn} + $defn,
1696 # returning the resulting string.
1697 #
1698 sub render
1699 {
1700 107 ▶ my ($self, $set, $defn) = @_;
Subroutine render: called CC=7 SCAR=19.7
Pod documented
1701 107 ▶ $set = {} unless ref $set;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $set 0 107 107
1702  
1703 # If $self is not a subclass of HTML::Tabulate, this is a procedural call, $self is $set
1704 107 ▶ if (! ref $self || ! blessed $self || ! $self->isa('HTML::Tabulate')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self and builtin::blessed($self) and $self->isa("HTML::Tabulate")) 6 101 107
Condition66%4/6
ref $self and builtin::blessed($self) and $self->isa("HTML::Tabulate")
!ll&&!rl&&r
60101
ref $self and builtin::blessed($self)
!ll&&!rl&&r
06101
MC/DC33%1/3
ref $self and builtin::blessed($self) and $self->isa("HTML::Tabulate")
ref $self builtin::blessed($self) $self->isa("HTML::Tabulate")
Truth table50%2/4
A and B and C
A= ref $selfB= builtin::blessed($self)C= $self->isa("HTML::Tabulate")
ABCresult
0XX0
10X0
1100
1111
1705 6 $defn = $set;
1706 6 $set = $self;
1707 6 $self = __PACKAGE__->new($defn);
1708 6 undef $defn;
1709 }
1710
1711 # If $defn defined, merge with $self->{defn} for this render only
1712 107 ▶ if (ref $defn eq 'HASH' && keys %$defn) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $defn eq 'HASH' and keys %$defn) { } 90 17 107
Condition66%2/3
ref $defn eq "HASH" and keys %$defn
!ll&&!rl&&r
17090
MC/DC50%1/2
ref $defn eq "HASH" and keys %$defn
ref $defn eq "HASH" keys %$defn
Truth table66%2/3
A and B
A= ref $defn eq "HASH"B= keys %$defn
ABresult
0X0
100
111
1713 90 $defn = $self->merge($self->{defn}, $defn);
1714 90 $self->prerender_munge($set, $defn);
1715 }
1716 else {
1717 17 $self->prerender_munge($set);
1718 }
1719  
1720 107 $self->render_table($set);
1721 }
1722  
1723 # -------------------------------------------------------------------------
1724  
1725 26 1;
1726  
1727 __END__
↑ Summary