RecordTree.pm

filter: Statementstmt 96.0% 267 / 278 Branchbran 75.5% 71 / 94 Conditioncond 73.3% 22 / 30 MC/DCmcdc 43.7% 7 / 16 Subroutinesub 97.6% 42 / 43 Podpod 75.0% 12 / 16 totaltotal 88.2% 421 / 477 CC 72 SCAR 19.2
CC
72
Cov
90%
sprint
20.0
_format_child_counts
9.0
sprint_columns
8.3
CRAP 6.8
↑ Summary RecordTree/IPv4.pm »
1 #!/usr/bin/perl
2 #===============================================================================
3 # PODNAME: Logwatch::RecordTree
4 # ABSTRACT: an object to collect and print Logwatch events
5 #
6 # AUTHOR: Reid Augustin (REID)
7 # EMAIL: reid@hellosix.com
8 # CREATED: Thu Mar 12 18:41:04 PDT 2015
9 #===============================================================================
10  
11 1 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 package Logwatch::RecordTree;
16 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use UNIVERSAL::require;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use List::Util qw ( max min sum );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use Sort::Key::Natural qw( natsort natkeysort );
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 our $VERSION = '2.056'; # VERSION
23  
24 1 ▶use overload '""' => \&sprint;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 1 my $_defaults = {}; # class variable
27  
28 96 ▶sub defaults {
Subroutine defaults: called CC=3 SCAR=11.0
Pod documented
29 96 my ($self) = @_;
30  
31 96 ▶ my $name = ref $self || $self;
Condition66%2/3
ref $self || $self
l!l&&r!l&&!r
9510
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table66%2/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
32 96 ▶ $_defaults->{$name} ||= {}; # a hash for each sub-class
Condition100%2/2
$_defaults->{$name} ||= {}
l!l
942
MC/DC100%1/1
$_defaults->{$name} ||= {}
$_defaults->{$name}
Truth table100%2/2
A
A= $_defaults->{$name}
Aresult
11
00
33 96 return $_defaults->{$name};
34 }
35  
36 1 ▶sub import {
Subroutine import: called CC=2 SCAR=7.6
37 1 my ($class, %hash) = @_;
38  
39 1 my $defaults = $class->defaults();
40 1 ▶ while (my ($key, $value) = each %hash) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %hash) 0 1 1
41 0 $defaults->{$key} = $value;
42 }
43 }
44  
45 221 ▶sub check_coderef { die 'Not a CODE ref' if (ref $_[0] ne 'CODE') };
Subroutine check_coderef: called CC=2 SCAR=7.6
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] ne "CODE" 0 221 221
46 99 ▶sub check_hashref { die 'Not a HASH ref' if (ref $_[0] ne 'HASH') };
Subroutine check_hashref: called CC=2 SCAR=7.6
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] ne "HASH" 0 99 99
47 162 ▶sub check_arryref { die 'Not an ARRAY ref' if (ref $_[0] ne 'ARRAY') };
Subroutine check_arryref: called CC=2 SCAR=7.6
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] ne "ARRAY" 0 162 162
48  
49 1 has name => ( # name/title for this item
50 is => 'ro',
51 );
52 has sprint_name => ( # callback to print the name
53 is => 'rw',
54 isa => \&check_coderef,
55 92 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
56 56 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
57 56 return $_[0]->name;
58 }
59 92 },
60 1 );
61 1 has sort_key => ( # this overrides ->name in sort_children
62 is => 'rw',
63 );
64 1 has case_sensitive => (
65 is => 'rw',
66 );
67 has count => ( # count how many times we log this event
68 is => 'rw',
69 95 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
70 312 ▶ trigger => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
71 312 $_[0]->no_count;
72 }
73 1 );
74 has count_fields => ( # fields to make the count
75 is => 'rw',
76 isa => \&check_arryref,
77 92 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
78 1 );
79 1 has count_formatted => ( # count and extended fields, after formatting
80 is => 'rw',
81 );
82 1 has no_count => ( # suppress count field (probably because it's the same as the parent)
83 is => 'rw',
84 );
85 has children => ( # a hash of child Logwtch::RecordTrees
86 is => 'rw',
87 isa => \&check_hashref,
88 95 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
89 1 );
90 has limit => ( # limit number of children printed
91 is => 'rw',
92 94 ▶ default => sub { 0 }, # default to no limit
Subroutine __ANON__: called CC=1 SCAR=0.0
93 1 );
94 1 has indent => ( # how much to indent this level of the tree
95 is => 'rw',
96 );
97 1 has no_indent => ( # flag to suppress indentation of children
98 is => 'rw',
99 );
100 has curr_indent => ( # total indentation of this level
101 is => 'rw',
102 95 ▶ default => sub { '' },
Subroutine __ANON__: called CC=1 SCAR=0.0
103 1 );
104 has post_callback => ( # when array is ready for printing, call this for final adjustments
105 is => 'rw',
106 isa => \&check_coderef,
107 95 ▶ default => sub { sub {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
108 1 );
109 1 has lines => ( # when array is ready for printing, store a ref to it here
110 is => 'rw',
111 isa => \&check_arryref,
112 );
113 1 has columnize => ( # flag to indicate we should columnize children
114 is => 'rw',
115 );
116 1 has neat_names => ( # for the neat freaks
117 is => 'rw',
118 );
119 has neat_format => ( # formatter for neatness, set by sprint
120 is => 'rw',
121 95 ▶ default => sub { "%s" }, # not neat
Subroutine __ANON__: called CC=1 SCAR=0.0
122 1 );
123 1 has extra => ( # a little something extra...
124 is => 'rw',
125 );
126  
127 95 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=7.6
Pod undocumented
128 95 my ($self) = @_;
129  
130 95 ▶ while (my ($key, $value) = each %{$self->defaults}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$self->defaults;}) 0 95 95
131 0 $self->$key($value);
132 }
133 }
134  
135 428 ▶sub child_by_name { # find child by name(s), follow down the tree
Subroutine child_by_name: called CC=3 SCAR=11.0
Pod documented
136 428 my ($self, @names) = @_;
137  
138 428 my $child = $self;
139 428 for my $name (@names) {
140 428 ▶ return if (not exists $child->children->{$name});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $child->children->{$name} 94 334 428
141 334 $child = $child->children->{$name};
142 }
143 334 return $child;
144 }
145  
146 91 ▶sub create_child { # create child, add to children
Subroutine create_child: called CC=4 SCAR=13.9
Pod documented
147 91 my ($self, $name, $type, $opts) = @_;
148  
149 91 ▶ $type ||= __PACKAGE__; # default to this package
Condition100%2/2
$type ||= __PACKAGE__
l!l
487
MC/DC100%1/1
$type ||= __PACKAGE__
$type
Truth table100%2/2
A
A= $type
Aresult
11
00
150 91 ▶ $opts ||= {};
Condition100%2/2
$opts ||= {}
l!l
685
MC/DC100%1/1
$opts ||= {}
$opts
Truth table100%2/2
A
A= $opts
Aresult
11
00
151 91 $opts->{name} = $name;
152 91 ▶ $type->require or croak($@);
Branch: 1 branch
BranchTrueFalseTotal
$type->require or croak($@) 91 0 91
153 91 return $self->children->{$name} = $type->new( %{$opts} );
154 }
155  
156 # adopt items, handle name conflicts
157 6 ▶sub adopt {
Subroutine adopt: called CC=4 SCAR=13.9
Pod documented
158 6 my ($self, $item) = @_;
159  
160 6 my $item_name = $item->name;
161 6 my $my_child = $self->child_by_name($item_name);
162 6 ▶ if ($my_child) {
Branch: 1 branch
BranchTrueFalseTotal
if ($my_child) { } 3 3 6
163 # name conflict. my_child must adopt $item's children
164 3 my @item_children = values %{$item->children};
165 3 ▶ if (@item_children) {
Branch: 1 branch
BranchTrueFalseTotal
if (@item_children) { } 2 1 3
166 2 for my $child (@item_children) {
167 2 $my_child->adopt($child);
168 }
169 }
170 else {
171 # no children, so transfer count directly from item to my_child
172 1 $my_child->count($my_child->count + $item->count);
173 }
174 }
175 else {
176 # no name conflict, just copy over
177 3 $self->children->{$item_name} = $item;
178 }
179 6 $self->count($self->count + $item->count);
180 }
181  
182 # log event, add new children if necessaary
183 197 ▶sub _log_children {
Subroutine _log_children: called CC=5 SCAR=16.1
184 197 my ($self, $name, @children) = @_;
185  
186 197 my ($type, $opts);
187 197 ▶ if (ref $name eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $name eq "ARRAY") 42 155 197
188 42 ($name, $type, $opts) = @{$name};
189 }
190  
191 197 ▶ $name = "<name not defined>" if (not defined $name); # supposed to be a list of names or array-refs
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 197 197
192  
193 197 my $child = $self->child_by_name($name);
194 197 ▶ if (not defined $child) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $child) 91 106 197
195 91 $child = $self->create_child($name, $type, $opts)
196 }
197  
198 197 ▶ if (@children) {
Branch: 1 branch
BranchTrueFalseTotal
if (@children) 111 86 197
199 111 return $child->_log_children(@children);
200 }
201  
202 86 return $child;
203 }
204  
205 283 ▶sub _count { # add 1 to count down the path
Subroutine _count: called CC=3 SCAR=11.0
206 283 my ($self, $name, @children) = @_;
207  
208 283 $self->count($self->count + 1);
209  
210 283 ▶ $name = $name->[0] if (ref $name);
Branch: 1 branch
BranchTrueFalseTotal
if ref $name 42 241 283
211 283 ▶ if (defined $name) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name) 197 86 283
212 197 return $self->child_by_name($name)->_count(@children);
213 }
214 86 return $self;
215 }
216  
217 0 ▶sub log_no_count { # log new event without counting, add children if necessary
Subroutine log_no_count: not called CC=1 SCAR=6.9
Pod documented
218 0 my ($self, @args) = @_;
219  
220 0 return $self->_log_children(@args);
221 }
222  
223 86 ▶sub log { # log new event adding to count, add children if necessary
Subroutine log: called CC=1 SCAR=0.0
Pod documented
224 86 my ($self, @args) = @_;
225  
226 86 $self->_log_children(@args);
227 86 return $self->_count(@args);
228 }
229  
230 # return sorted list of child names
231 62 ▶sub sort_children { # sort children
Subroutine sort_children: called CC=4 SCAR=14.2
Pod documented
232 62 my ($self) = @_;
233  
234 # make hash, value is name, key is sort_key or name
235 59 ▶ my %keys = map { (defined($_->sort_key) ? $_->sort_key : $_->name) => $_ }
Branch: 1 branch
BranchTrueFalseTotal
defined $_->sort_key ? : 1 58 59
236 62 values %{$self->children};
237  
238 # sort by hash keys, create array of values to get back to names
239 my @children = $self->case_sensitive
240 0 ? map { $keys{$_} } natsort keys %keys
241 62 ▶ : map { $keys{$_} } natkeysort { lc $_ } keys %keys;
Subroutine __ANON__: called CC=1 SCAR=0.1
Branch: 1 branch
BranchTrueFalseTotal
$self->case_sensitive ? : 0 62 62
242  
243 return wantarray
244 ? @children
245 62 ▶ : \@children;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 62 62
246 }
247  
248 # make neat column of child names
249 2 ▶sub _neaten_children {
Subroutine _neaten_children: called CC=2 SCAR=7.0
250 2 my ($self) = @_;
251  
252 2 my $max = max(1, map { length $_->sprint_name->($_) } values %{$self->children});
253 2 ▶ my $format = $self->neat_names < 0
Branch: 1 branch
BranchTrueFalseTotal
$self->neat_names < 0 ? : 2 0 2
254 ? "%-${max}s"
255 : "%${max}s";
256 2 map { $_->neat_format($format) } values %{$self->children};
257 }
258  
259 # make neat columns of all the count fields
260 69 ▶sub _format_child_counts {
Subroutine _format_child_counts: called CC=9 SCAR=22.0
261 69 my ($self, $children, $depth) = @_;
262  
263 # measure each field, save max length for each column
264 69 my @maxes;
265 69 for my $child (values %{$children}) {
266 72 unshift @{$child->count_fields}, $child->count;
267 72 my $ii = 0;
268 72 for my $field (@{$child->count_fields}) {
269 78 ▶ $maxes[$ii] = max($maxes[$ii] || 0, length $field);
Condition100%2/2
$maxes[$ii] || 0
l!l
4929
MC/DC100%1/1
$maxes[$ii] || 0
$maxes[$ii]
Truth table100%2/2
A
A= $maxes[$ii]
Aresult
11
00
270 78 $ii++;
271 }
272 }
273  
274 # string to indent children: total count field width or at least 3
275 69 my $min = sum(1, @maxes);
276 69 ▶ $min = 3 if ($min < 3);
Branch: 1 branch
BranchTrueFalseTotal
if $min < 3 65 4 69
277 69 my $child_indent = " " x $min;
278  
279 # pad all fields to the max for the column
280 69 for my $child (values %{$children}) {
281 72 my $ccf = $child->count_fields;
282 72 my @padded;
283 72 for my $ii (0 .. $#maxes) {
284 78 ▶ $padded[$ii] = sprintf "%*s", $maxes[$ii], $ccf->[$ii] || '';
Condition50%1/2
$ccf->[$ii] || ''
l!l
780
MC/DC0%0/1
$ccf->[$ii] || ''
$ccf->[$ii]
Truth table50%1/2
A
A= $ccf->[$ii]
Aresult
11
00
285 }
286 72 $child->count_formatted(join '', @padded);
287 72 ▶ $child->indent($child_indent) if (not defined $child->indent);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $child->indent 66 6 72
288 72 shift @{$child->count_fields}; # remove the count field we inserted above
289 }
290 }
291  
292 # compare our count fields to $other's (for suppression when identical)
293 11 ▶sub _count_fields_differ {
Subroutine _count_fields_differ: called CC=6 SCAR=20.3
294 11 my ($self, $other) = @_;
295  
296 11 ▶ return 1 if ($self->count != $other->count);
Branch: 1 branch
BranchTrueFalseTotal
if $self->count != $other->count 0 11 11
297 11 for my $ii (0 .. max($#{$self->count_fields}, $#{$other->count_fields})) {
298 1 ▶ return 1 if (
Branch: 1 branch
BranchTrueFalseTotal
if not defined $self->count_fields->[$ii] && defined $other->count_fields->[$ii] or $self->count_fields->[$ii] ne $other->count_fields->[$ii] 1 0 1
Condition33%2/6
not defined $self->count_fields->[$ii] && defined $other->count_fields->[$ii] or $self->count_fields->[$ii] ne $other->count_fields->[$ii]
l!l&&r!l&&!r
100
defined $self->count_fields->[$ii] && defined $other->count_fields->[$ii]
!ll&&!rl&&r
100
MC/DC0%0/3
not defined $self->count_fields->[$ii] && defined $other->count_fields->[$ii] or $self->count_fields->[$ii] ne $other->count_fields->[$ii]
defined $self->count_fields->[$ii] defined $other->count_fields->[$ii] $self->count_fields->[$ii] ne $other->count_fields->[$ii]
Truth table25%1/4
not(A && B) or C
A= defined $self->count_fields->[$ii]B= defined $other->count_fields->[$ii]C= $self->count_fields->[$ii] ne $other->count_fields->[$ii]
ABCresult
0XX1
10X1
1111
1100
299 not defined $self->count_fields->[$ii] or
300 not defined $other->count_fields->[$ii] or
301 $self->count_fields->[$ii] ne $other->count_fields->[$ii]);
302 }
303 10 return 0; # match
304 }
305  
306 65 ▶sub sprint {
Subroutine sprint: called CC=20 SCAR=30.0
Pod documented
307 65 my ($self, $callback, $path, $parent_indent, $depth) = @_;
308  
309 65 ▶ $path ||= [];
Condition100%2/2
$path ||= []
l!l
614
MC/DC100%1/1
$path ||= []
$path
Truth table100%2/2
A
A= $path
Aresult
11
00
310 65 ▶ $parent_indent ||= '';
Condition100%2/2
$parent_indent ||= ""
l!l
1253
MC/DC100%1/1
$parent_indent ||= ""
$parent_indent
Truth table100%2/2
A
A= $parent_indent
Aresult
11
00
311 65 ▶ $depth ||= 1;
Condition100%2/2
$depth ||= 1
l!l
614
MC/DC100%1/1
$depth ||= 1
$depth
Truth table100%2/2
A
A= $depth
Aresult
11
00
312  
313 65 ▶ if ($depth == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth == 1) 4 61 65
314 # top level needs to format its own count field
315 4 $self->_format_child_counts({ top => $self }, 0);
316 }
317  
318 65 ▶ my $count = $self->no_count
Branch: 1 branch
BranchTrueFalseTotal
$self->no_count ? : 10 55 65
319 ? ''
320 : $self->count_formatted . ' ';
321  
322 65 $self->lines(my $lines = []);
323  
324 65 ▶ if (length($self->name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->name) { } 65 0 65
325 65 push @{$lines}, join( '',
326 $count,
327 sprintf $self->neat_format, $self->sprint_name->($self),
328 );
329 }
330 else {
331 0 push @{$lines}, ''; # name is blank, so don't add anything here
332 }
333  
334 # format count fields and calculate indent for all children,
335 65 $self->_format_child_counts($self->children, $depth);
336  
337 65 my $children = $self->sort_children;
338 65 my $child_count = @{$children};
339  
340 65 ▶ if ($child_count == 1) { # join single child to this line
Branch: 1 branch
BranchTrueFalseTotal
if ($child_count == 1) 11 54 65
341 11 my $child = $children->[0];
342 # save the child's flags we're going to alter
343 11 my %flags = map { $_ => $child->$_ } qw( no_count no_indent );
344 # suppress count field if child's is same as ours
345 11 ▶ $child->no_count(1) if (not $self->_count_fields_differ($child));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_count_fields_differ($child) 10 1 11
346 11 $child->no_indent(1);
347 11 $child->curr_indent($parent_indent); # no extra indent since we concat this line
348  
349 11 push @{$path}, $child->name;
350 11 $child->sprint($callback, $path, $self->curr_indent, $depth + 1);
351 11 ▶ if (length($lines->[0]) + length($child->lines->[0]) <= $self->width) {
Branch: 1 branch
BranchTrueFalseTotal
if (length($lines->[0]) + length($child->lines->[0]) <= $self->width) 11 0 11
352 11 $lines->[0] .= ' ' . shift @{$child->lines};
353 }
354 11 push @{$lines}, @{$child->lines};
355 11 pop @{$path};
356  
357 # restore child's flags
358 11 map { $child->$_($flags{$_}) } keys %flags;
359 }
360  
361 65 ▶ if ($child_count > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child_count > 1) 10 55 65
362 10 ▶ my $last = $self->limit || $child_count; # if limit is zero, print all
Condition66%2/3
$self->limit || $child_count
l!l&&r!l&&!r
190
MC/DC0%0/2
$self->limit || $child_count
$self->limit $child_count
Truth table66%2/3
A || B
A= $self->limitB= $child_count
ABresult
1X1
011
000
363 10 ▶ $last = $child_count if ($child_count - $last < 3); # if within 3, just print all
Branch: 1 branch
BranchTrueFalseTotal
if $child_count - $last < 3 9 1 10
364 10 $last = $child_count - $last; # convert from limit to the last index
365  
366 # handle neat_children flag
367 10 ▶ $self->_neaten_children if ($self->neat_names);
Branch: 1 branch
BranchTrueFalseTotal
if $self->neat_names 2 8 10
368  
369 10 for my $child (@{$children}) {
370 51 $child->curr_indent($parent_indent . $self->indent);
371 51 ▶ if ($child_count <= $last) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child_count <= $last) 1 50 51
372 1 push @{$lines}, "... and $child_count more";
373 1 last;
374 }
375 50 push @{$path}, $child->name;
376 50 $child->sprint($callback, $path, $self->curr_indent, $depth + 1);
377 50 push @{$lines}, @{$child->lines};
378 50 pop @{$path};
379 50 $child_count--;
380 }
381 }
382  
383 65 ▶ $self->sprint_columns if ($self->columnize);
Branch: 1 branch
BranchTrueFalseTotal
if $self->columnize 2 63 65
384  
385 # indent children
386 65 ▶ if (not $self->no_indent) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->no_indent) 54 11 65
387 54 for my $ii (1 .. $#{$lines}) {
388 90 $lines->[$ii] = $self->indent . $lines->[$ii];
389 }
390 }
391  
392 65 $self->post_callback->($self, $path);
393  
394 65 ▶ $callback->($self, $path) if ($callback);
Branch: 1 branch
BranchTrueFalseTotal
if $callback 60 5 65
395  
396 65 return join "\n", @{$lines};
397 }
398  
399 13 ▶sub width {
Subroutine width: called CC=3 SCAR=12.0
Pod documented
400 13 my ($self, $new) = @_;
401  
402 13 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 13 13
403 0 $self->{_width} = $new;
404 }
405 13 ▶ return $self->{_width} if (exists $self->{_width});
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"_width"} 0 13 13
406 13 return 80; # default page width
407 }
408  
409 4 ▶sub col_width {
Subroutine col_width: called CC=3 SCAR=11.1
Pod documented
410 4 my ($self, $new) = @_;
411  
412 4 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 4 4
413 0 $self->{_col_width} = $new;
414 }
415  
416 4 ▶ if (not exists $self->{_col_width}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"_col_width"}) 2 2 4
417 # find longest line length, excluding the first line
418 2 my $lines = $self->lines;
419 2 $self->{_col_width} = max( 1, map { length $lines->[$_] } (1 .. $#{$lines}));
420 }
421 4 return $self->{_col_width};
422 }
423  
424 2 ▶sub col_count {
Subroutine col_count: called CC=4 SCAR=14.8
Pod documented
425 2 my ($self, $new) = @_;
426  
427 2 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 2 2
428 0 $self->{_col_count} = $new;
429 }
430  
431 2 ▶ if (not exists $self->{_col_count}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"_col_count"}) 2 0 2
432 # calculate from curr_indent, width, and col_width
433 2 my $width = $self->width - length($self->curr_indent);
434 2 $width = max(0, $width); # at least 0
435 2 ▶ $self->{_col_count} = int ($width / $self->col_width) || 1; # at least 1
Condition50%1/2
int $width / $self->col_width || 1
l!l
20
MC/DC0%0/1
int $width / $self->col_width || 1
int $width / $self->col_width
Truth table50%1/2
A
A= int $width / $self->col_width
Aresult
11
00
436 }
437 2 return $self->{_col_count};
438 }
439  
440 # re-arrange children into columns
441 2 ▶sub sprint_columns {
Subroutine sprint_columns: called CC=8 SCAR=21.2
Pod documented
442 2 my ($self, $width, $col_count, $col_width) = @_;
443  
444 # allow caller to override width, col_count, and col_width
445 2 ▶ $self->width($width) if (defined $width);
Branch: 1 branch
BranchTrueFalseTotal
if defined $width 0 2 2
446 2 ▶ $self->col_count($col_count) if (defined $col_count);
Branch: 1 branch
BranchTrueFalseTotal
if defined $col_count 0 2 2
447 2 ▶ $self->col_width($col_width) if (defined $col_width);
Branch: 1 branch
BranchTrueFalseTotal
if defined $col_width 0 2 2
448  
449 2 $col_count = $self->col_count;
450 2 $col_width = $self->col_width;
451  
452 2 my $lines = $self->lines;
453 2 my @new_lines = shift @{$lines}; # first line is unchanged
454  
455 2 my $lines_per_col = int ((@{$lines} + $col_count - 1) / $col_count);
456  
457 2 for my $ii (0 .. $lines_per_col - 1) {
458 4 my @line;
459 4 ▶ if ($col_count <= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col_count <= 1) { } 0 4 4
460 # single column, just prepend indent
461 0 push @new_lines, join '',
462 $self->indent,
463 $lines->[$ii];
464 }
465 else {
466 # join segments into a line of columns
467 4 for my $jj (0 .. $col_count - 1) {
468 27 my $l = $lines->[$ii + $jj * $lines_per_col];
469 27 ▶ push @line, sprintf "%-*s", $col_width, $l if (defined $l);
Branch: 1 branch
BranchTrueFalseTotal
if defined $l 21 6 27
470 }
471 4 push @new_lines, join '',
472 $self->indent,
473 join(' ', @line);
474 }
475 }
476 2 $self->lines(\@new_lines);
477 }
478  
479 1 1;
480  
481 __END__
↑ Summary RecordTree/IPv4.pm »