blib/lib/Script/Toolbox/Util/Formatter.pm

filter: Statementstmt 70.3% 228 / 324 Branchbran 66.9% 83 / 124 Conditioncond 77.7% 7 / 9 MC/DCmcdc 66.6% 4 / 6 Subroutinesub 68.2% 28 / 41 Podpod 0.0% 0 / 7 totaltotal 68.4% 350 / 511 SCAR 55.8
CC
81
Cov
70%
_noGroupCol
30.0
_isGroupEnd
20.0
sumBy
12.0
CRAP 265.6
« blib/lib/Script/Toolbox/Util/Menues.pm ↑ Summary blib/lib/Script/Toolbox/Util/Opt.pm »
1 package Script::Toolbox::Util::Formatter;
2  
3 21 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 21 ▶use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 #use Script::Toolbox::Util qw(Log);
6  
7 21 require Exporter;
8  
9 21 @ISA = qw(Exporter);
10 # Items to export into callers namespace by default. Note: do not export
11 # names by default without a very good reason. Use EXPORT_OK instead.
12 # Do not simply export all your public functions/methods/constants.
13 21 @EXPORT = qw(
14
15 );
16 #$VERSION = '0.03';
17  
18  
19 # Preloaded methods go here.
20  
21 #-----------------------------------------------------------------------------
22 #-----------------------------------------------------------------------------
23 sub new
24 {
25 10 ▶ my $classname = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
26 10 my $self = {};
27 10 bless( $self, $classname );
28 10 $self->_init( @_ );
29 10 return $self;
30 }
31  
32 #-----------------------------------------------------------------------------
33 #-----------------------------------------------------------------------------
34 sub _init
35 {
36 10 ▶ my ($self, $container) = @_;
Subroutine _init: called CC=8 SCAR=20.8
37  
38 $self->{'title'}= defined $container->{'title'} ?
39 10 ▶ $container->{'title'} :
Branch: 1 branch
BranchTrueFalseTotal
defined $container->{'title'} ? : 6 4 10
40 'Title';
41  
42 10 ▶ if( ref $container->{'data'} eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $container->{"data"} eq "ARRAY") 9 1 10
43 9 ▶ $self->_initHashArray ( $container ) if( ref $container->{'data'}[0] eq 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $container->{"data"}[0] eq "HASH" 1 8 9
44 9 ▶ $self->_initArray ( $container ) if( ref $container->{'data'}[0] eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if ref $container->{"data"}[0] eq "ARRAY" 7 2 9
45 }
46  
47 10 ▶ if( ref $container->{'data'} eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $container->{"data"} eq "HASH") 1 9 10
48 1 ▶ $self->_initHashHash ( $container ) if( ref $container->{'data'} eq 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $container->{"data"} eq "HASH" 1 0 1
49 }
50  
51 $self->{'head'} = defined $container->{'head'} ?
52 10 ▶ $container->{'head'} :
Branch: 1 branch
BranchTrueFalseTotal
defined $container->{'head'} ? : 4 6 10
53 _getDefaultHeader($container);
54  
55  
56 }
57  
58 #------------------------------------------------------------------------------
59 # Extract column keys from first data row.
60 # 'data' => {
61 # key1 => {F1=>'aaa', F2=>'bb ', F3=>3}
62 # key2 => {F1=>11111, F2=>2222222, F3=>3}
63 # }
64 #------------------------------------------------------------------------------
65 sub _getHeadsFromFirstRow($)
66 {
67 1 ▶ my ($data) = @_;
Subroutine _getHeadsFromFirstRow: called CC=2 SCAR=6.9
68 1 my @H;
69 1 push( @H, "KEY" );
70 1 foreach my $d ( values %{$data} )
71 {
72 1 map{ push( @H, $_ ) } sort keys %{$d};
73 1 last;
74 }
75 #return \@H;
76 1 return @H;
77 }
78  
79 #------------------------------------------------------------------------------
80 # 'title' => 'Test1',
81 # 'head' => ['RowKey', Field1', 'Field2', 'Field3'],
82 # 'data' => {
83 # key1 => {F1=>'aaa', F2=>'bb ', F3=>3}
84 # key2 => {F1=>11111, F2=>2222222, F3=>3}
85 # }
86 #------------------------------------------------------------------------------
87 sub _initHashHash($$)
88 {
89 1 ▶ my ($self,$container) = @_;
Subroutine _initHashHash: called CC=6 SCAR=18.0
90  
91 1 ▶ if( ref $container->{'data'} eq 'HASH' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $container->{"data"} eq "HASH") 1 0 1
92 {
93 1 @{$self->{'head'}} = _getHeadsFromFirstRow( $container->{'data'} );
94 1 my @D;
95 1 foreach my $lk ( sort keys %{$container->{'data'}} )
96 {
97 2 my $l = $container->{'data'}{$lk};
98 2 my @L;
99 2 foreach my $k ( @{$self->{'head'}} )
100 {
101 # auto generated meta column
102 8 ▶ if( $k eq 'KEY' ) { push @L,$lk; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($k eq "KEY") 2 6 8
103  
104 6 ▶ $self->_logit( $k, $l ) if( !defined $l->{$k} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $l->{$k} 0 6 6
105 6 push @L, $l->{$k};
106 }
107 2 push @D, \@L;
108 }
109 1 $self->{data} = \@D;
110 }
111 }
112  
113 #------------------------------------------------------------------------------
114 # 'title' => 'Test1',
115 # 'head' => ['Feld1', 'Feld2', 'Feld3'],
116 # 'data' => [
117 # [ 'aaa', 'bb ', 'cc ' ],
118 # [ 11111, 2222222, 3 ]
119 # ]
120 # OR data part
121 # 'data' => [
122 # {F1=>'aaa', F2=>'bb ', F3=>3}
123 # {F1=>11111, F2=>2222222, F3=>3}
124 # ]
125 #------------------------------------------------------------------------------
126 sub _initHashArray($$)
127 {
128 1 ▶ my ($self,$container) = @_;
Subroutine _initHashArray: called CC=1 SCAR=0.0
129 1 $self->{'data'} = $self->_getData($container);
130 }
131  
132 #------------------------------------------------------------------------------
133 # [
134 # 'title',
135 # ['COL-HEAD','COL-HEAD2','COL-HEAD3'],
136 # [1, 2, 3],
137 # [4, 5, 6],
138 # ]
139 # OR:
140 # [
141 # [1, 2, 3],
142 # [4, 5, 6],
143 # ]
144 # OR:
145 # 'data' => [
146 # {F1=>'aaa', F2=>'bb ', F3=>3}
147 # {F1=>11111, F2=>2222222, F3=>3}
148 # ]
149 #------------------------------------------------------------------------------
150 sub _initArray($$)
151 {
152 7 ▶ my ($self,$container) = @_;
Subroutine _initArray: called CC=1 SCAR=0.0
153  
154 7 $self->{'data'} = $container->{'data'};
155 }
156  
157 #------------------------------------------------------------------------------
158 #------------------------------------------------------------------------------
159 sub _getData($$)
160 {
161 1 ▶ my ($self, $container) = @_;
Subroutine _getData: called CC=7 SCAR=19.7
162  
163 1 ▶ return [] if( !defined $container->{'data'}[0] );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $container->{"data"}[0] 0 1 1
164 1 ▶ return $container if( ref($container->{'data'}[0]) eq 'ARRAY' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $container->{"data"}[0] eq "ARRAY" 0 1 1
165 1 ▶ if( ref($container->{'data'}[0]) eq 'HASH' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $container->{"data"}[0] eq "HASH") 1 0 1
166 {
167 1 @{$self->{'head'}} = sort keys %{$container->{'data'}[0]};
168 1 my @D;
169 1 foreach my $l ( @{$container->{'data'}} )
170 {
171 2 my @L;
172 2 foreach my $k ( @{$self->{'head'}} )
173 {
174 6 ▶ $self->_logit( $k, $l ) if( !defined $l->{$k} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $l->{$k} 0 6 6
175 6 push @L, $l->{$k};
176 }
177 2 push @D, \@L;
178 }
179 1 return \@D;
180 }
181 }
182  
183 #------------------------------------------------------------------------------
184 #------------------------------------------------------------------------------
185 sub _logit($$)
186 {
187 0 ▶ my ($self,$k,$line) = @_;
Subroutine _logit: not called CC=1 SCAR=6.9
188  
189 print STDERR
190 "Warning: inconsistent data hash, missing key $k in line: " .
191 0 join ";", each %{$line};
192 }
193 #------------------------------------------------------------------------------
194 #------------------------------------------------------------------------------
195 sub _getDefaultHeader($)
196 {
197 6 ▶ my ($cont) = @_;
Subroutine _getDefaultHeader: called CC=9 SCAR=22.0
198 6 my @hd;
199  
200 6 ▶ if( ref $cont->{'data'} eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cont->{"data"} eq "ARRAY") 5 1 6
201 5 ▶ if( ref $cont->{'data'}[0] eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cont->{"data"}[0] eq "HASH") 1 4 5
202 1 foreach my $h ( sort keys %{$cont->{'data'}[0]} ) { push @hd, $h; }
203 }
204 5 ▶ if( ref $cont->{'data'}[0] eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cont->{"data"}[0] eq "ARRAY") 4 1 5
205 4 ▶ for( my $i=0; $i <= $#{$cont->{'data'}[0]}; $i++ ) { push @hd, "Col-$i"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#{$$cont{"data"}[0];}) 12 4 16
206 }
207 }
208  
209 # 'data' => {
210 # 'line1' => { 'F1' => 'aaaa', 'F2' => 'bbb ', 'F3' => 'c' },
211 # 'line2' => { 'F1' => 'dddd', 'F2' => 'eee ', 'F3' => 'f' }
212 # }
213 6 ▶ if( ref $cont->{'data'} eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cont->{"data"} eq "HASH") 1 5 6
214 1 foreach my $line ( values %{$cont->{'data'}} )
215 {
216 1 push @hd, "KEY";
217 1 foreach my $fldName ( sort keys %{$line} )
218 {
219 3 push @hd, $fldName;
220 }
221 1 last;
222 }
223 }
224  
225 6 return \@hd;
226 }
227  
228 #-----------------------------------------------------------------------------
229 #-----------------------------------------------------------------------------
230 sub matrix
231 {
232 10 ▶ my ($self) = @_;
Subroutine matrix: called CC=3 SCAR=11.0
Pod undocumented
233 10 ▶ return [] if( !defined $self->{'data'} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"data"} 1 9 10
234 9 ▶ return [] if( scalar @{$self->{'data'}} == 0 );
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$self->{"data"};} == 0 0 9 9
235  
236 9 my @result;
237 9 $self->_matrix( \@result );
238 9 return \@result ;
239 }
240  
241 #-----------------------------------------------------------------------------
242 #-----------------------------------------------------------------------------
243 sub _matrix
244 {
245 9 ▶ my ($self, $result) = @_;
Subroutine _matrix: called CC=1 SCAR=0.0
246  
247 9 my @maxColW = $self->_maxColWidth();
248 9 my $format = $self->_getFormat( \@maxColW );
249 9 my $formatHd= $format;
250 9 $formatHd=~ s/([.]\d*)?[df]/s/g;
251  
252 9 push @{$result}, sprintf "== %s ==", $self->{'title'};
253 9 push @{$result}, sprintf $formatHd, @{$self->{'head'}};
254 9 push @{$result}, _underline( @maxColW );
255  
256 24 map { push @{$result},
257 9 sprintf $format, _getLineArray($_); } @{$self->{'data'}};
258 }
259  
260 #-----------------------------------------------------------------------------
261 #-----------------------------------------------------------------------------
262 sub _underline
263 {
264 9 ▶ my (@maxColWidth) = @_;
Subroutine _underline: called CC=1 SCAR=0.0
265  
266 9 my $x;
267 9 map { $_ =~s/([.]\d+)?[fds]$//;
268 28 $_ =~s/-//;
269 28 $x .= sprintf "%s ", '-' x $_ } @maxColWidth;
270  
271 9 $x =~ s/\s$//;
272 9 return $x;
273 }
274 #-----------------------------------------------------------------------------
275 #-----------------------------------------------------------------------------
276 sub _getLineArray
277 {
278 24 ▶ my ($line) = @_;
Subroutine _getLineArray: called CC=4 SCAR=22.9
279  
280 24 ▶ return @{$line} if( ref $line eq 'ARRAY' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $line eq "ARRAY" 24 0 24
281  
282 0 ▶ if( ref $line eq 'HASH' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $line eq "HASH") 0 0 0
283 {
284 0 my @R;
285 0 foreach my $key ( sort keys %{$line} ) {
286 0 push @R, ${$line}{$key};
287 }
288 0 return @R;
289 }
290 }
291  
292 #-----------------------------------------------------------------------------
293 #-----------------------------------------------------------------------------
294 sub _getFormat
295 {
296 9 ▶ my ($self, $maxColWidth ) = @_;
Subroutine _getFormat: called CC=3 SCAR=11.0
297  
298 9 my $form='';
299 9 _mkFloatLen( $maxColWidth );
300 9 foreach my $f ( @{$maxColWidth} )
301 {
302 28 ▶ $f = '-' . $f if( $f =~ /s$/ );
Branch: 1 branch
BranchTrueFalseTotal
if $f =~ /s$/ 16 12 28
303 28 $form .= sprintf "%%%s ", $f;
304 }
305 9 $form =~ s/\s$//;
306 9 return $form;
307 }
308  
309 #------------------------------------------------------------------------------
310 #------------------------------------------------------------------------------
311 sub _mkFloatLen($)
312 {
313 9 ▶ my ($maxColRef) = @_;
Subroutine _mkFloatLen: called CC=3 SCAR=11.0
314  
315 9 ▶ for( my $i=@{$maxColRef}-1; $i >= 0; $i-- )
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 28 9 37
316 {
317 28 ▶ if( ${$maxColRef}[$i] =~ /(\d+)[.](\d+)f$/ )
Branch: 1 branch
BranchTrueFalseTotal
if (do { $maxColRef }->[$i] =~ /(\d+)[.](\d+)f$/) 6 22 28
318 {
319 6 my $len = $1;
320 6 my $dig = $2;
321 6 ${$maxColRef}[$i] = $len+$dig+1 .'.'. $dig .'f';
322 }
323 }
324 }
325  
326 #-----------------------------------------------------------------------------
327 #-----------------------------------------------------------------------------
328 sub _maxColWidth
329 {
330 9 ▶ my ($self) = @_;
Subroutine _maxColWidth: called CC=5 SCAR=16.2
331 9 my @maxColWidth;
332  
333 my @X;
334 9 push @X, $self->{'head'};
335 9 map { push @X, $_ } @{$self->{'data'}};
336 9 my $i=0;
337 9 foreach my $line ( @X )
338 {
339 33 ▶ next if( $i++ == 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $i++ == 0 9 24 33
340 24 ▶ _maxColHashLine( $line, \@maxColWidth ) if( ref $line eq 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $line eq "HASH" 0 24 24
341 24 ▶ _maxColArrayLine($line, \@maxColWidth ) if( ref $line eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if ref $line eq "ARRAY" 24 0 24
342 }
343 9 _checkMaxHeader( $X[0], \@maxColWidth );
344 9 return @maxColWidth;
345 }
346  
347 #------------------------------------------------------------------------------
348 #------------------------------------------------------------------------------
349 sub _checkMaxHeader($$)
350 {
351 9 ▶ my ($line, $maxColWidth) = @_;
Subroutine _checkMaxHeader: called CC=2 SCAR=6.9
352  
353 9 ▶ for( my $i=0; $i<= $#{$line}; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#{$line;}) 28 9 37
354 {
355 28 _trimBlanks( \${$line}[$i] );
356 28 ${$maxColWidth}[$i] = _getMaxColWidthHead(${$maxColWidth}[$i],
357 28 ${$line}[$i] );
358 }
359 }
360  
361 #------------------------------------------------------------------------------
362 #------------------------------------------------------------------------------
363 sub _getMaxColWidthHead($$)
364 {
365 28 ▶ my ($old,$new) = @_;
Subroutine _getMaxColWidthHead: called CC=4 SCAR=14.2
366  
367 28 my $nl= length( $new );
368 28 $old =~ /(\d+)[.]?(\d*)([fds])/; my ($ol,$od, $ot) = ($1,$2,$3);
369  
370 28 ▶ return stringType($nl,$ol) if($ot eq 's' );
Branch: 1 branch
BranchTrueFalseTotal
if $ot eq "s" 16 12 28
371 12 ▶ return floatType($nl,0,$ol,$od) if($ot eq 'f');
Branch: 1 branch
BranchTrueFalseTotal
if $ot eq "f" 6 6 12
372 6 ▶ return intType($nl,$ol) if($ot eq 'd' );
Branch: 1 branch
BranchTrueFalseTotal
if $ot eq "d" 6 0 6
373  
374 0 printf STDERR "ERROR format\n";
375 0 return 0;
376 }
377  
378  
379 #-----------------------------------------------------------------------------
380 #-----------------------------------------------------------------------------
381 sub _maxColArrayLine
382 {
383 24 ▶ my ($line, $maxColWidth) = @_;
Subroutine _maxColArrayLine: called CC=2 SCAR=6.9
384 24 ▶ for( my $i=0; $i<= $#{$line}; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#{$line;}) 74 24 98
385 {
386 74 _trimBlanks( \${$line}[$i] );
387 74 my $type= _getTypeLen( ${$line}[$i] );
388 74 ${$maxColWidth}[$i] = _getMaxColWidth(${$maxColWidth}[$i], $type);
389 }
390 }
391  
392 #------------------------------------------------------------------------------
393 #------------------------------------------------------------------------------
394 sub _getMaxColWidth($$)
395 {
396 74 ▶ my ($old,$new) = @_;
Subroutine _getMaxColWidth: called CC=8 SCAR=21.2
397  
398 74 $new =~ /(\d+)[.]?(\d*)([fds])/; my ($nl,$nd, $nt) = ($1,$2,$3);
399 74 ▶ $old = $new if( !defined $old );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $old 28 46 74
400 74 $old =~ /(\d+)[.]?(\d*)([fds])/; my ($ol,$od, $ot) = ($1,$2,$3);
401  
402 74 ▶ return stringType($nl,$ol) if($nt eq 's' || $ot eq 's' );
Branch: 1 branch
BranchTrueFalseTotal
if $nt eq "s" or $ot eq "s" 38 36 74
Condition100%3/3
$nt eq "s" or $ot eq "s"
l!l&&r!l&&!r
29936
MC/DC100%2/2
$nt eq "s" or $ot eq "s"
$nt eq "s" $ot eq "s"
Truth table100%3/3
A or B
A= $nt eq "s"B= $ot eq "s"
ABresult
1X1
011
000
403 36 ▶ return floatType($nl,$nd,$ol,$od) if($nt eq 'f' || $ot eq 'f');
Branch: 1 branch
BranchTrueFalseTotal
if $nt eq "f" or $ot eq "f" 12 24 36
Condition100%3/3
$nt eq "f" or $ot eq "f"
l!l&&r!l&&!r
6624
MC/DC100%2/2
$nt eq "f" or $ot eq "f"
$nt eq "f" $ot eq "f"
Truth table100%3/3
A or B
A= $nt eq "f"B= $ot eq "f"
ABresult
1X1
011
000
404 24 ▶ return intType($nl,$ol) if($nt eq 'd' && $ot eq 'd' );
Branch: 1 branch
BranchTrueFalseTotal
if $nt eq "d" and $ot eq "d" 24 0 24
Condition33%1/3
$nt eq "d" and $ot eq "d"
!ll&&!rl&&r
0024
MC/DC0%0/2
$nt eq "d" and $ot eq "d"
$nt eq "d" $ot eq "d"
Truth table33%1/3
A and B
A= $nt eq "d"B= $ot eq "d"
ABresult
0X0
100
111
405  
406 0 printf STDERR "ERROR format\n";
407 0 return 0;
408 }
409  
410 #------------------------------------------------------------------------------
411 #------------------------------------------------------------------------------
412 sub intType($$)
413 {
414 30 ▶ my ($nl,$ol) = @_;
Subroutine intType: called CC=2 SCAR=6.9
Pod undocumented
415 30 ▶ my $len = $nl > $ol ? $nl : $ol;
Branch: 1 branch
BranchTrueFalseTotal
$nl > $ol ? : 21 9 30
416 30 return $len . 'd';
417 }
418 #------------------------------------------------------------------------------
419 #------------------------------------------------------------------------------
420 sub floatType($$$$$$)
421 {
422 18 ▶ my ($nl,$nd,$ol,$od) = @_;
Subroutine floatType: called CC=7 SCAR=19.5
Pod undocumented
423 18 ▶ $nl = $nl eq '' ? 0 : $nl;
Branch: 1 branch
BranchTrueFalseTotal
$nl eq '' ? : 0 18 18
424 18 ▶ $nd = $nd eq '' ? 0 : $nd;
Branch: 1 branch
BranchTrueFalseTotal
$nd eq '' ? : 6 12 18
425 18 ▶ $od = $od eq '' ? 0 : $od;
Branch: 1 branch
BranchTrueFalseTotal
$od eq '' ? : 3 15 18
426 18 ▶ $ol = $ol eq '' ? 0 : $ol;
Branch: 1 branch
BranchTrueFalseTotal
$ol eq '' ? : 0 18 18
427 18 ▶ my $len = $nl > $ol ? $nl : $ol;
Branch: 1 branch
BranchTrueFalseTotal
$nl > $ol ? : 9 9 18
428 18 ▶ my $dig = $nd > $od ? $nd : $od;
Branch: 1 branch
BranchTrueFalseTotal
$nd > $od ? : 3 15 18
429  
430 18 return $len .'.'. $dig .'f';
431 }
432  
433 #------------------------------------------------------------------------------
434 #------------------------------------------------------------------------------
435 sub stringType($$)
436 {
437 54 ▶ my ($nl,$ol) = @_;
Subroutine stringType: called CC=2 SCAR=6.9
Pod undocumented
438 54 ▶ my $len = $nl > $ol ? $nl : $ol;
Branch: 1 branch
BranchTrueFalseTotal
$nl > $ol ? : 8 46 54
439  
440 54 return $len .'s';
441 }
442  
443 #------------------------------------------------------------------------------
444 #------------------------------------------------------------------------------
445 sub _getTypeLen($)
446 {
447 74 ▶ my ($field) = @_;
Subroutine _getTypeLen: called CC=3 SCAR=11.0
448  
449 74 my $type;
450 74 ▶ $type = _isFloat($field); return $type if( defined $type );
Branch: 1 branch
BranchTrueFalseTotal
if defined $type 6 68 74
451 68 ▶ $type = _isInt($field); return $type if( defined $type );
Branch: 1 branch
BranchTrueFalseTotal
if defined $type 39 29 68
452  
453 29 return length($field) .'s';
454 }
455  
456 #------------------------------------------------------------------------------
457 #------------------------------------------------------------------------------
458 sub _isInt($)
459 {
460 68 ▶ my ($field) = @_;
Subroutine _isInt: called CC=2 SCAR=6.9
461 68 ▶ return undef if( $field !~ /^[-]?\d+$/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $field =~ /^[-]?\d+$/ 29 39 68
462 39 return length($field) .'d';
463 }
464  
465 #------------------------------------------------------------------------------
466 #------------------------------------------------------------------------------
467 sub _isFloat($)
468 {
469 74 ▶ my ($field) = @_;
Subroutine _isFloat: called CC=2 SCAR=6.9
470 74 ▶ return undef if( $field !~ /^[-]?(\d+)[.](\d*)$/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $field =~ /^[-]?(\d+)[.](\d*)$/ 68 6 74
471  
472 6 my $int = $1; my $li = length($int);
473 6 my $frac= $2; my $lf = length($frac);
474  
475 6 my $form= $li+$lf+1 .'.'. $lf .'f';
476  
477 6 return $form;
478 }
479 #-----------------------------------------------------------------------------
480 #-----------------------------------------------------------------------------
481 sub _maxColHashLine
482 {
483 0 ▶ my ($line, $maxColWidth) = @_;
Subroutine _maxColHashLine: not called CC=2 SCAR=17.9
484 0 my $i=0;
485 0 foreach my $key ( sort keys %{$line} )
486 {
487 0 _trimBlanks( \${$line}{$key} );
488 0 my $type= _getTypeLen( ${$line}{$key} );
489 0 ${$maxColWidth}[$i] = _getMaxColWidth(${$maxColWidth}[$i], $type);
490 0 $i++;
491 }
492 }
493 #-----------------------------------------------------------------------------
494 #-----------------------------------------------------------------------------
495 sub _trimBlanks
496 {
497 102 ▶ my ($field) = @_;
Subroutine _trimBlanks: called CC=1 SCAR=0.0
498  
499 102 $$field =~ s/^\s+//;
500 102 $$field =~ s/\s+$//;
501  
502 102 return length( $$field );
503 }
504  
505 #------------------------------------------------------------------------------
506 #------------------------------------------------------------------------------
507 sub sumBy()
508 {
509 0 ▶ my ($self, $raw, $colIdxRef, $notGroupBy) = @_;
Subroutine sumBy: not called CC=3 SCAR=24.8
Pod undocumented
510 0 my $colIdx = $colIdxRef->[0]; #FIXME may be more than one colum in future
511  
512 0 my @LEN = _getColLen( $raw->[2] );
513 0 my $fmt = _getSumFormat( $raw->[3],$colIdx,@LEN );
514 0 my $pattern = _getSplitPattern(@LEN);
515 0 my $sum = 0;
516 0 my $gSum= _getSumField( $raw->[3],$pattern, $colIdx );
517  
518 0 my $old = $raw->[3];
519 0 my @NEW = @{$raw}[0..3];
520  
521 0 ▶ for( my $i=4; $i <= $#{$raw}; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#{$raw;}) 0 0 0
522 {
523 0 ▶ push @NEW, _endGroup(\$gSum,$fmt,\$sum)
Branch: 1 branch
BranchTrueFalseTotal
if &_isGroupEnd($raw, $i, $colIdx, $pattern, $notGroupBy) 0 0 0
524 if( _isGroupEnd($raw,$i,$colIdx,$pattern,$notGroupBy));
525 0 $gSum += _getSumField( $raw->[$i],$pattern, $colIdx );
526 0 push @NEW, $raw->[$i];
527 }
528 0 push @NEW, sprintf $fmt, $gSum;
529 0 push @NEW, sprintf $fmt, $sum;
530 0 return \@NEW;
531 }
532  
533 #------------------------------------------------------------------------------
534 #------------------------------------------------------------------------------
535 sub _endGroup($$)
536 {
537 0 ▶ my ($sumRef, $fmt, $totSumRef) = @_;
Subroutine _endGroup: not called CC=1 SCAR=6.9
538
539 0 my $line = sprintf $fmt, $$sumRef;
540 0 $$totSumRef += $$sumRef;
541 0 $$sumRef = 0;
542  
543 0 return $line;
544 }
545  
546 #------------------------------------------------------------------------------
547 #------------------------------------------------------------------------------
548 sub _isGroupEnd($$$$)
549 {
550 0 ▶ my ($raw,$currIdx,$colIdx,$pattern,$notGroupBy) = @_;
Subroutine _isGroupEnd: not called CC=4 SCAR=30.0
551  
552 0 my @PREV = _getSplitedLine($raw->[$currIdx-1],$pattern);
553 0 my @CURR = _getSplitedLine($raw->[$currIdx], $pattern);
554 0 ▶ for( my $i=0; $i <= $#CURR; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#CURR) 0 0 0
555 {
556 0 ▶ next if( _noGroupCol($i,$colIdx,$notGroupBy) );
Branch: 1 branch
BranchTrueFalseTotal
if &_noGroupCol($i, $colIdx, $notGroupBy) 0 0 0
557 0 ▶ return 1 if( $PREV[$i] ne $CURR[$i] );
Branch: 1 branch
BranchTrueFalseTotal
if $PREV[$i] ne $CURR[$i] 0 0 0
558 }
559 0 return 0;
560 }
561  
562 #------------------------------------------------------------------------------
563 #------------------------------------------------------------------------------
564 sub _noGroupCol($$$)
565 {
566 0 ▶ my ($idx,$sumIdx,$notGroupBy) = @_;
Subroutine _noGroupCol: not called CC=5 SCAR=34.0
567  
568 0 ▶ return 1 if( $idx == $sumIdx );
Branch: 1 branch
BranchTrueFalseTotal
if $idx == $sumIdx 0 0 0
569 0 ▶ return 0 if( !defined $notGroupBy );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $notGroupBy 0 0 0
570  
571 0 ▶ foreach my $col ( @{$notGroupBy} ) { return 1 if( $col == $idx ); }
Branch: 1 branch
BranchTrueFalseTotal
if $col == $idx 0 0 0
572 0 return 0;
573 }
574 #------------------------------------------------------------------------------
575 #------------------------------------------------------------------------------
576 sub _getSumField($$$)
577 {
578 0 ▶ my ($line,$pattern,$idx) = @_;
Subroutine _getSumField: not called CC=1 SCAR=6.9
579  
580 0 my @L = _getSplitedLine($line,$pattern);
581 0 return $L[$idx];
582 }
583  
584 #------------------------------------------------------------------------------
585 #------------------------------------------------------------------------------
586 sub _getSumFormat($$@)
587 {
588 0 ▶ my ($line, $colIdx, @LEN) = @_;
Subroutine _getSumFormat: not called CC=3 SCAR=24.8
589  
590 0 my $form='';
591 0 ▶ for( my $i=0; $i <= $#LEN; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#LEN) 0 0 0
592 {
593 0 ▶ if( $i == $colIdx ) { $form .= _getSumColForm($line, $colIdx, @LEN); }
Branch: 1 branch
BranchTrueFalseTotal
if ($i == $colIdx) { } 0 0 0
594 0 else { $form .= sprintf "%s ", ' ' x $LEN[$i]; }
595 }
596 0 return $form;
597 }
598  
599 #------------------------------------------------------------------------------
600 #------------------------------------------------------------------------------
601 sub _getSumColForm($$@)
602 {
603 0 ▶ my ($line, $colIdx, @LEN) = @_;
Subroutine _getSumColForm: not called CC=2 SCAR=17.9
604  
605 0 my $pattern = _getSplitPattern(@LEN);
606 0 my @splited = _getSplitedLine($line,$pattern);
607 0 my $sumField= $splited[$colIdx];
608 0 my @I = $sumField =~ /(\d+)([.]?)(\d*)/;
609 0 my $decimal = $I[2];
610  
611 0 ▶ return '%'. $LEN[$colIdx] .
Branch: 1 branch
BranchTrueFalseTotal
if $I[1] eq "." 0 0 0
612 '.'. length($decimal) .'f' if( $I[1] eq '.' );
613  
614 0 return '%'. $LEN[$colIdx] .'d';
615 }
616  
617 #------------------------------------------------------------------------------
618 #------------------------------------------------------------------------------
619 sub _getSplitedLine($$)
620 {
621 0 ▶ my ($line,$pattern) = @_;
Subroutine _getSplitedLine: not called CC=1 SCAR=6.9
622 0 return $line =~ m/$pattern/;
623 }
624  
625 #------------------------------------------------------------------------------
626 #------------------------------------------------------------------------------
627 sub _getSplitPattern(@)
628 {
629 0 ▶ my (@LEN) = @_;
Subroutine _getSplitPattern: not called CC=1 SCAR=6.9
630  
631 0 return join ' ', map { '(.{'. $_ .'})' } @LEN;
632 }
633  
634 #------------------------------------------------------------------------------
635 #------------------------------------------------------------------------------
636 sub _getColLen
637 {
638 0 ▶ my ($cols) = @_;
Subroutine _getColLen: not called CC=2 SCAR=17.9
639  
640 0 my @len;
641 0 foreach my $col ( split /\s+/, $cols )
642 {
643 0 push @len, length $col;
644 }
645 0 return @len;
646 }
647  
648 #------------------------------------------------------------------------------
649 #------------------------------------------------------------------------------
650 sub newGroup($$$)
651 {
652 0 ▶ my ($O, $L, $idx) = @_;
Subroutine newGroup: not called CC=3 SCAR=24.8
Pod undocumented
653  
654 0 foreach my $i ( @{$idx} )
655 {
656 0 ▶ return 1 if( $O->[$i] ne $L->[$i] );
Branch: 1 branch
BranchTrueFalseTotal
if $O->[$i] ne $L->[$i] 0 0 0
657 }
658 0 return 0;
659 }
660  
661 21 1;
662 __END__
« blib/lib/Script/Toolbox/Util/Menues.pm ↑ Summary blib/lib/Script/Toolbox/Util/Opt.pm »