bin/tabmerge

filter: Statementstmt 94.1% 113 / 120 Branchbran 67.5% 27 / 40 Conditioncond 75.0% 18 / 24 MC/DCmcdc 42.8% 6 / 14 Subroutinesub 100% 13 / 13 Podpod n/a totaltotal 83.8% 177 / 211 CC 25 SCAR 23.4
CC
25
Cov
86%
intersection
18.1
die_if_bad
6.4
list
4.0
CRAP 10.4
↑ Summary blib/lib/Text/RecordParser.pm »
1 #!perl
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use English qw( -no_match_vars );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use Text::RecordParser;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use Text::TabularDisplay;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 6 Readonly our $VERSION => 1.12;
15 6 Readonly my $COMMA => q{,};
16 6 Readonly my $COMMA_SPACE => q{, };
17 6 Readonly my $DASH => q{-};
18 6 Readonly my $EMPTY_STR => q{};
19 6 Readonly my $TAB => qq{\t};
20 6 Readonly my $NEWLINE => qq{\n};
21  
22 6 my $fs = $TAB;
23 6 my $rs = $NEWLINE;
24 6 my ( $fields, $min, $max, $list, $sort, $stdout,
25 $help, $man_page, $show_version );
26  
27 6 ▶GetOptions(
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("f|fields:s", \$fields, "help", \$help, "fs:s", \$fs, "l|list", \$list, "man", \$man_page, "min", \$min, "max", \$max, "rs:s", \$rs, "s|sort:s", \$sort, "stdout", \$stdout, "version", \$show_version) or pod2usage() 6 0 6
28 'f|fields:s' => \$fields,
29 'help' => \$help,
30 'fs:s' => \$fs,
31 'l|list' => \$list,
32 'man' => \$man_page,
33 'min' => \$min,
34 'max' => \$max,
35 'rs:s' => \$rs,
36 's|sort:s' => \$sort,
37 'stdout' => \$stdout,
38 'version' => \$show_version,
39 ) or pod2usage;
40  
41 6 ▶if ( $help || $man_page ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($help or $man_page) 0 6 6
Condition33%1/3
$help or $man_page
l!l&&r!l&&!r
006
MC/DC0%0/2
$help or $man_page
$help $man_page
Truth table33%1/3
A or B
A= $helpB= $man_page
ABresult
1X1
011
000
42 0 ▶ pod2usage({
Branch: 1 branch
BranchTrueFalseTotal
$man_page ? : 0 0 0
43 -exitval => 0,
44 -verbose => $man_page ? 2 : 1
45 });
46 };
47  
48 6 ▶if ( $show_version ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($show_version) 0 6 6
49 0 my $prog = basename( $PROGRAM_NAME );
50 0 print "$prog $VERSION\n";
51 0 exit 0;
52 }
53  
54 6 ▶if ( scalar @ARGV < 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @ARGV < 2) 0 6 6
55 0 pod2usage('Please supply two or more files');
56 }
57  
58 6 ▶my @files = @ARGV or pod2usage('No input files');
Branch: 1 branch
BranchTrueFalseTotal
my(@files) = @ARGV or pod2usage("No input files") 6 0 6
59  
60 6 ▶$min = 1 unless $max or $fields;
Branch: 1 branch
BranchTrueFalseTotal
unless $max or $fields 2 4 6
Condition100%3/3
$max or $fields
l!l&&r!l&&!r
132
MC/DC100%2/2
$max or $fields
$max $fields
Truth table100%3/3
A or B
A= $maxB= $fields
ABresult
1X1
011
000
61  
62 6 my $p = Text::RecordParser->new(
63 field_separator => $fs,
64 record_separator => $rs,
65 );
66  
67 6 ▶if ( $list ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($list) { } 1 5 6
elsif ($min or $max or $fields) { } 5 0 5
68 1 list( $p, @files );
69 }
70 ▶elsif ( $min || $max || $fields ) {
Condition83%5/6
$min or $max or $fields
l!l&&r!l&&!r
230
$min or $max
l!l&&r!l&&!r
113
MC/DC0%0/3
$min or $max or $fields
$min $max $fields
Truth table75%3/4
A or B or C
A= $minB= $maxC= $fields
ABCresult
1XX1
01X1
0011
0000
71 5 ▶ intersection(
Branch: 1 branch
BranchTrueFalseTotal
$min ? : 1 4 5
72 fields => $fields,
73 files => \@files,
74 parser => $p,
75 sort => $sort,
76 type => $min ? 'min' : 'max',
77 );
78 }
79 else {
80 0 pod2usage(q[Can't figure out what to do.]);
81 }
82  
83 # -------------------------------------------------------------------
84 5 ▶sub intersection {
Subroutine intersection: called CC=18 SCAR=28.9
85 5 my %args = @_;
86 5 my $p = $args{'parser'};
87 5 my $type = $args{'type'};
88 5 my $files = $args{'files'};
89 5 ▶ my $fields = $args{'fields'} || $EMPTY_STR; # optional
Condition66%2/3
$args{'fields'} || $EMPTY_STR
l!l&&r!l&&!r
302
MC/DC50%1/2
$args{'fields'} || $EMPTY_STR
$args{'fields'} $EMPTY_STR
Truth table66%2/3
A || B
A= $args{'fields'}B= $EMPTY_STR
ABresult
1X1
011
000
90 5 ▶ my $sort = $args{'sort'} || $EMPTY_STR; # optional
Condition66%2/3
$args{'sort'} || $EMPTY_STR
l!l&&r!l&&!r
203
MC/DC50%1/2
$args{'sort'} || $EMPTY_STR
$args{'sort'} $EMPTY_STR
Truth table66%2/3
A || B
A= $args{'sort'}B= $EMPTY_STR
ABresult
1X1
011
000
91  
92 5 my %fields;
93 5 for my $file ( @$files ) {
94 15 $p->filename( $file );
95 15 $p->bind_header;
96 15 $fields{ $_ }++ for $p->field_list;
97 }
98  
99 5 my @intersect;
100 5 ▶ if ( $fields ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fields) { } 3 2 5
101 3 @intersect = map { s/^\s+|\s+$//g; $_ } split /$COMMA/, $fields;
102 3 die_if_bad( \%fields, \@intersect );
103 }
104 else {
105 2 my $no_files = scalar @$files;
106 2 for my $fld ( keys %fields ) {
107 push @intersect, $fld if
108 ( $type eq 'max' ) ||
109 8 ▶ ( $type eq 'min' && $fields{ $fld } == $no_files )
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "max" or $type eq "min" and $fields{$fld} == $no_files 6 2 8
Condition83%5/6
$type eq "max" or $type eq "min" and $fields{$fld} == $no_files
l!l&&r!l&&!r
422
$type eq "min" and $fields{$fld} == $no_files
!ll&&!rl&&r
022
MC/DC66%2/3
$type eq "max" or $type eq "min" and $fields{$fld} == $no_files
$type eq "max" $type eq "min" $fields{$fld} == $no_files
Truth table75%3/4
A or B and C
A= $type eq "max"B= $type eq "min"C= $fields{$fld} == $no_files
ABCresult
1XX1
0111
00X0
0100
110 ;
111 }
112  
113 2 @intersect = sort @intersect;
114 }
115  
116 5 ▶ die "No intersection!\n" unless @intersect;
Branch: 1 branch
BranchTrueFalseTotal
unless @intersect 0 5 5
117  
118 5 my @data;
119 5 for my $file ( @$files ) {
120 15 $p->filename( $file );
121 15 $p->bind_header;
122 15 ▶ while ( my $rec = $p->fetchrow_hashref ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $rec = $p->fetchrow_hashref) 45 15 60
123 45 push @data, { map { $_, $rec->{ $_ } } @intersect };
124 }
125 }
126  
127 5 ▶ if ( my @sort = map { s/^\s+|\s+$//g; $_ } split /$COMMA/, $sort ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@sort) = map({s/^\s+|\s+$//g; $_;} split(/$COMMA/, $sort, 0))) 2 3 5
128 2 die_if_bad( \%fields, \@sort );
129  
130 @data =
131 18 map { $_->[1] }
132 30 sort { $a->[0] cmp $b->[0] }
133 2 map { [ join($DASH, @{ $_ }{ @sort } ), $_ ] }
134 @data;
135 }
136  
137 5 ▶ if ( $stdout ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stdout) { } 1 4 5
138 1 print join( $fs, @intersect ), $rs;
139 1 for my $rec ( @data ) {
140 print join( $fs,
141 9 ▶ map { defined $rec->{$_} ? $rec->{$_} : $EMPTY_STR } @intersect
Branch: 1 branch
BranchTrueFalseTotal
defined $rec->{$_} ? : 12 6 18
142 ), $rs;
143 }
144 }
145 else {
146 4 my $tab = Text::TabularDisplay->new( @intersect );
147 4 for my $rec ( @data ) {
148 36 $tab->add( map { $rec->{ $_ } } @intersect );
149 }
150 4 print $tab->render, $NEWLINE;
151 }
152 }
153  
154 # -------------------------------------------------------------------
155 1 ▶sub list {
Subroutine list: called CC=4 SCAR=13.9
156 1 my ( $p, @files ) = @_;
157  
158 1 my %fields;
159 1 for my $file ( @files ) {
160 3 $p->filename( $file );
161 3 $p->bind_header;
162 3 $fields{ $_ }++ for $p->field_list;
163 }
164  
165 1 my $tab = Text::TabularDisplay->new('Field', 'No. Times Present');
166 1 $tab->add( $_, $fields{ $_ } ) for sort keys %fields;
167 1 print $tab->render, $NEWLINE;
168 }
169  
170 # -------------------------------------------------------------------
171 5 ▶sub die_if_bad {
Subroutine die_if_bad: called CC=5 SCAR=18.6
172 5 my ( $fields, $check ) = @_;
173  
174 5 my @bad;
175 5 for my $fld ( @$check ) {
176 8 ▶ push @bad, $fld unless $fields->{ $fld };
Branch: 1 branch
BranchTrueFalseTotal
unless $fields->{$fld} 0 8 8
177 }
178  
179 5 ▶ if ( @bad ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@bad) { } 0 5 5
180 0 ▶ die sprintf "Bad field name%s: %s$NEWLINE",
Branch: 1 branch
BranchTrueFalseTotal
scalar @bad > 1 ? : 0 0 0
181 scalar @bad > 1 ? 's' : $EMPTY_STR,
182 join $COMMA_SPACE, @bad
183 ;
184 }
185 else {
186 5 return 1;
187 }
188 }
189  
190 __END__
↑ Summary blib/lib/Text/RecordParser.pm »