bin/tablify

filter: Statementstmt 91.4% 150 / 164 Branchbran 71.5% 63 / 88 Conditioncond 60.0% 9 / 15 MC/DCmcdc 40.0% 4 / 10 Subroutinesub 91.6% 11 / 12 Podpod n/a totaltotal 82.0% 237 / 289 CC 1 SCAR 0.8
CC
1
Cov
83%
__ANON__
2.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« blib/lib/Text/RecordParser.pm ↑ Summary blib/lib/Text/RecordParser/Object.pm »
1 #!perl
2  
3 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use List::Util 'max';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use Text::Autoformat 'autoformat';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 10 ▶use Text::RecordParser;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 10 ▶use Text::TabularDisplay;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 10 Readonly our $VERSION => 1.15;
16 10 Readonly my $DASH => q{-};
17 10 Readonly my $EMPTY_STR => q{};
18 10 Readonly my $TAB => qq{\t};
19 10 Readonly my $NEWLINE => qq{\n};
20 10 Readonly my $WIDTH => 78;
21  
22 10 my $comment_start = '';
23 10 my $fs = $TAB;
24 10 my $rs = $NEWLINE;
25 10 my $headers = '';
26 10 my $no_headers = 0;
27 10 my $show_vertically = 0;
28 10 my $no_pager = 0;
29 10 my $strip_quotes = 0;
30  
31 10 my ( $show_fields, $list, $limit, @where, $help, $man_page, $show_version );
32 10 ▶GetOptions(
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("c|comment:s", \$comment_start, "fs:s", \$fs, "rs:s", \$rs, "f|fields:s", \$show_fields, "l|list", \$list, "i|limit:i", \$limit, "w|where:s", \@where, "v|vertical", \$show_vertically, "strip-quotes", \$strip_quotes, "n|no-headers", \$no_headers, "h|headers:s", \$headers, "no-pager", \$no_pager, "help", \$help, "man", \$man_page, "version", \$show_version) or pod2usage() 10 0 10
33 'c|comment:s' => \$comment_start,
34 'fs:s' => \$fs,
35 'rs:s' => \$rs,
36 'f|fields:s' => \$show_fields,
37 'l|list' => \$list,
38 'i|limit:i' => \$limit,
39 'w|where:s' => \@where,
40 'v|vertical' => \$show_vertically,
41 'strip-quotes' => \$strip_quotes,
42 'n|no-headers' => \$no_headers,
43 'h|headers:s' => \$headers,
44 'no-pager' => \$no_pager,
45 'help' => \$help,
46 'man' => \$man_page,
47 'version' => \$show_version,
48 ) or pod2usage;
49  
50 10 ▶if ( $help || $man_page ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($help or $man_page) 0 10 10
Condition33%1/3
$help or $man_page
l!l&&r!l&&!r
0010
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
51 0 ▶ pod2usage({
Branch: 1 branch
BranchTrueFalseTotal
$man_page ? : 0 0 0
52 -exitval => 0,
53 -verbose => $man_page ? 2 : 1
54 });
55 };
56  
57 10 ▶if ( $show_version ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($show_version) 0 10 10
58 0 my $prog = basename( $0 );
59 0 print "$prog $VERSION\n";
60 0 exit 0;
61 }
62  
63 10 ▶my $file = shift or pod2usage('No file');
Branch: 1 branch
BranchTrueFalseTotal
my $file = shift @ARGV or pod2usage("No file") 10 0 10
64 10 ▶my $p = Text::RecordParser->new(
Branch: 1 branch
BranchTrueFalseTotal
$comment_start ? : 0 10 10
65 field_separator => $fs,
66 record_separator => $rs,
67 comment => $comment_start ? qr/^$comment_start/ : undef,
68 );
69  
70 10 ▶if ( $strip_quotes ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($strip_quotes) 0 10 10
71 0 ▶ $p->field_filter( sub { s/^["']//; s/["']$//; $_ } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
72 }
73  
74 10 ▶if ( $file eq $DASH ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file eq $DASH) { } 0 10 10
75 0 $p->fh( \*STDIN );
76 }
77 else {
78 10 $p->filename( $file );
79 }
80  
81 10 my @fields;
82 10 ▶if ( @fields = split( /\s*,\s*/, $headers ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@fields = split(/\s*,\s*/, $headers, 0)) { } 0 10 10
elsif (not $no_headers) { } 8 2 10
83 0 $p->bind_fields( @fields );
84 }
85 elsif ( !$no_headers ) {
86 8 $p->bind_header;
87 8 @fields = $p->field_list;
88 }
89  
90 10 ▶if ( $list ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($list) 1 9 10
91 1 ▶ pod2usage(q[Can't list fields with --no-headers]) if $no_headers;
Branch: 1 branch
BranchTrueFalseTotal
if $no_headers 0 1 1
92 1 my $tab = Text::TabularDisplay->new('Field No.', 'Field');
93 1 my $i = 1;
94 1 $tab->add( $i++, $_ ) for @fields;
95 1 print $tab->render, $NEWLINE;
96 1 exit 0;
97 }
98  
99 9 my %where;
100 9 for ( @where ) {
101 5 ▶ if ( /([\w\d]+)\s*(==|eq|>=?|<=?|=~)\s*(.*)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/([\w\d]+)\s*(==|eq|>=?|<=?|=~)\s*(.*)/) 5 0 5
102 5 my $field = $1;
103 5 my $op = $2;
104 5 my $value = $3;
105  
106 5 ▶ unless ( $no_headers ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($no_headers) 4 1 5
107 4 my %available = map { $_, 1 } @fields;
108 4 ▶ next unless $available{ $field };
Branch: 1 branch
BranchTrueFalseTotal
unless $available{$field} 0 4 4
109 }
110  
111 5 ▶ $field-- if $field =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $field =~ /^\d+$/ 1 4 5
112 5 $where{ $field } = [ $op, $value ];
113 }
114 }
115  
116 9 ▶if ( $show_fields ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($show_fields) 4 5 9
117 9 ▶ my @show = map { $_ =~ m/^\d+$/ && @fields
Branch: 2 branches
BranchTrueFalseTotal
$_ =~ /^\d+$/ && @fields ? : 4 5 9
$_ - 1 < scalar @fields ? : 4 0 4
Condition66%2/3
$_ =~ /^\d+$/ and @fields
!ll&&!rl&&r
504
MC/DC50%1/2
$_ =~ /^\d+$/ and @fields
$_ =~ /^\d+$/ @fields
Truth table66%2/3
A and B
A= $_ =~ /^\d+$/B= @fields
ABresult
0X0
100
111
118 ? $_-1 < scalar @fields ? $_ : () : $_ }
119 4 ▶ map { $_ =~ m/(\d+)-(\d+)/ ? ( $1..$2 ) : $_ }
Branch: 1 branch
BranchTrueFalseTotal
$_ =~ /(\d+)-(\d+)/ ? : 1 6 7
120 split /,/, $show_fields;
121 4 my @numbers = grep { /^(\d+)$/ } @show;
122  
123 4 ▶ if ( $no_headers ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($no_headers) { } 0 4 4
124 0 @fields = @show;
125 }
126 else {
127 4 ▶ if ( scalar @show == scalar @numbers ) { # all numbers
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @show == scalar @numbers) { } 1 3 4
elsif (@show) { } 3 0 3
128 1 @numbers = map { $_ - 1 } @numbers;
129 1 @fields = @fields[ @numbers ];
130 }
131 elsif ( @show ) {
132 3 my %available = map { $_, 1 } @fields;
133 3 my @temp = @fields;
134 3 ▶ @fields = map { $available{ $_ } ? $_ : () } @show;
Branch: 1 branch
BranchTrueFalseTotal
$available{$_} ? : 5 0 5
135 }
136  
137 4 ▶ if ( !@fields ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@fields) 0 4 4
138 0 die "No fields match in list '$show_fields'\n";
139 }
140 }
141 }
142  
143 9 my $fh;
144 9 my $pager = $ENV{'PAGER'};
145 9 ▶if ( !$no_pager && $pager ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $no_pager and $pager) { } 0 9 9
Condition33%1/3
not $no_pager and $pager
!ll&&!rl&&r
090
MC/DC0%0/2
not $no_pager and $pager
not $no_pager $pager
Truth table33%1/3
not(A) and B
A= not $no_pagerB= $pager
ABresult
0X0
100
111
146 0 open $fh, "| $pager";
147 }
148 else {
149 9 $fh = \*STDOUT;
150 }
151  
152 9 my $tab = Text::TabularDisplay->new( @fields );
153  
154 9 my $max_col_length = 0;
155 9 my $num_records = 0;
156 9 my $separator = "************ Record %s ************\n";
157 9 ▶if ( $no_headers ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($no_headers) { } 2 7 9
158 2 my @field_names;
159 RECORD:
160 2 ▶ while ( my @data = $p->fetchrow_array ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@data) = $p->fetchrow_array) 6 1 7
161 6 ▶ if ( !@fields ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@fields) 2 4 6
162 2 @fields = ( 0..$#data );
163 2 @field_names = map { 'Field' . ($_+1) } @fields;
164 2 $max_col_length = max( map { length $_ } @field_names );
165 2 $tab->columns( @field_names );
166 }
167  
168 6 for my $field ( keys %where ) {
169 5 my ( $op, $value ) = @{ $where{ $field } };
170 5 my $cmd = "'$data[ ($field - 1) ]' $op $value";
171 5 ▶ next RECORD unless eval $cmd;
Branch: 1 branch
BranchTrueFalseTotal
unless eval $cmd 2 3 5
172 }
173  
174 4 $num_records++;
175 4 ▶ if ( $show_vertically ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($show_vertically) { } 1 3 4
176 1 printf $fh $separator, $num_records;
177 1 for my $i ( @fields ) {
178 5 ▶ printf $fh "%${max_col_length}s: %s\n",
Branch: 1 branch
BranchTrueFalseTotal
defined $data[$i] ? : 5 0 5
179 $field_names[ $i ],
180 defined $data[ $i ] ? $data[ $i ] : q{};
181 }
182 }
183 else {
184 3 $tab->add( map { $data[ $_ ] } @fields );
185 }
186  
187 4 ▶ last if $limit && $num_records >= $limit;
Branch: 1 branch
BranchTrueFalseTotal
if $limit and $num_records >= $limit 1 3 4
Condition66%2/3
$limit and $num_records >= $limit
!ll&&!rl&&r
301
MC/DC50%1/2
$limit and $num_records >= $limit
$limit $num_records >= $limit
Truth table66%2/3
A and B
A= $limitB= $num_records >= $limit
ABresult
0X0
100
111
188 }
189 }
190 else {
191 7 ▶ $max_col_length = max map { $_ ? length $_ : 0 } $p->field_list;
Branch: 1 branch
BranchTrueFalseTotal
$_ ? : 35 0 35
192  
193 RECORD:
194 7 ▶ while ( my $data = $p->fetchrow_hashref ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $data = $p->fetchrow_hashref) 32 6 38
195 32 for my $field ( keys %where ) {
196 18 my ( $op, $value ) = @{ $where{ $field } };
197 18 my $cmd = "'$data->{ $field }' $op $value";
198 18 ▶ next RECORD unless eval $cmd;
Branch: 1 branch
BranchTrueFalseTotal
unless eval $cmd 10 8 18
199 }
200  
201 22 $num_records++;
202 22 ▶ if ( $show_vertically ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($show_vertically) { } 5 17 22
203 5 printf $fh $separator, $num_records;
204 5 for my $field ( @fields ) {
205 25 ▶ next unless $field;
Branch: 1 branch
BranchTrueFalseTotal
unless $field 0 25 25
206 25 ▶ my $v = defined $data->{ $field } ? $data->{ $field } : q{};
Branch: 1 branch
BranchTrueFalseTotal
defined $data->{$field} ? : 25 0 25
207 25 ▶ if ( length $v > $WIDTH ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $v > $WIDTH) 0 25 25
208 0 ( $v = autoformat( $v, { left => $max_col_length + 3 } ) )
209 =~ s/^\s+|\s+$//g;
210 }
211  
212 25 printf $fh "%${max_col_length}s: %s\n", $field, $v;
213 }
214 }
215 else {
216 17 $tab->add( map { $data->{ $_ } } @fields );
217 }
218 22 ▶ last if $limit && $num_records >= $limit;
Branch: 1 branch
BranchTrueFalseTotal
if $limit and $num_records >= $limit 1 21 22
Condition100%3/3
$limit and $num_records >= $limit
!ll&&!rl&&r
2011
MC/DC100%2/2
$limit and $num_records >= $limit
$limit $num_records >= $limit
Truth table100%3/3
A and B
A= $limitB= $num_records >= $limit
ABresult
0X0
100
111
219 }
220 }
221  
222 9 ▶if ( !$show_vertically ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($show_vertically) 7 2 9
223 7 print $fh $tab->render;
224 }
225  
226 9 ▶print $fh $num_records
Branch: 2 branches
BranchTrueFalseTotal
$num_records ? : 9 0 9
$num_records > 1 ? : 6 3 9
227 ? sprintf(
228 "\n%s record%s returned\n", $num_records, $num_records > 1
229 ? 's' : $EMPTY_STR
230 )
231 : "\nNo records returned\n";
232  
233 9 close $fh;
234  
235 __END__
« blib/lib/Text/RecordParser.pm ↑ Summary blib/lib/Text/RecordParser/Object.pm »