| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
$help or $man_page
MC/DC0%0/2
$help or $man_page
$help
$man_page
Truth table33%1/3
A or B
A= $helpB= $man_page
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | ▶ | pod2usage({ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | -exitval => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 44 | -verbose => $man_page ? 2 : 1 | ||||||||||||||||||||||||||||||||||||||||||
| 45 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 46 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | 6 | ▶ | if ( $show_version ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | pod2usage('Please supply two or more files'); | |||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | 6 | ▶ | my @files = @ARGV or pod2usage('No input files'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | 6 | ▶ | $min = 1 unless $max or $fields; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$max or $fields
MC/DC100%2/2
$max or $fields
$max
$fields
Truth table100%3/3
A or B
A= $maxB= $fields
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | 1 | list( $p, @files ); | |||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||
| 70 | ▶ | elsif ( $min || $max || $fields ) { | |||||||||||||||||||||||||||||||||||||||||
|
Condition83%5/6
$min or $max or $fields
$min or $max
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
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 5 | ▶ | intersection( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
MC/DC50%1/2
$args{'fields'} || $EMPTY_STR
$args{'fields'}
$EMPTY_STR
Truth table66%2/3
A || B
A= $args{'fields'}B= $EMPTY_STR
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | 5 | ▶ | my $sort = $args{'sort'} || $EMPTY_STR; # optional | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$args{'sort'} || $EMPTY_STR
MC/DC50%1/2
$args{'sort'} || $EMPTY_STR
$args{'sort'}
$EMPTY_STR
Truth table66%2/3
A || B
A= $args{'sort'}B= $EMPTY_STR
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition83%5/6
$type eq "max" or $type eq "min" and $fields{$fld} == $no_files
$type eq "min" and $fields{$fld} == $no_files
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
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | ; | ||||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | 2 | @intersect = sort @intersect; | |||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | 5 | ▶ | die "No intersection!\n" unless @intersect; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||
| 179 | 5 | ▶ | if ( @bad ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 180 | 0 | ▶ | die sprintf "Bad field name%s: %s$NEWLINE", | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||