Process/Utils.pm

filter: Statementstmt 96.5% 111 / 115 Branchbran 89.1% 41 / 46 Conditioncond 58.8% 10 / 17 MC/DCmcdc 40.0% 4 / 10 Subroutinesub 100% 16 / 16 Podpod 25.0% 1 / 4 totaltotal 87.9% 183 / 208 CC 35 SCAR 26.5
CC
35
Cov
91%
__ANON__
26.0
is_hash
4.0
is_array
4.0
CRAP 14.2
↑ Summary Process.pm »
1 package File::Process::Utils;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 5 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 5 Readonly::Scalar our $SUCCESS => 1;
13 5 Readonly::Scalar our $FAILURE => 0;
14 5 Readonly::Scalar our $TRUE => 1;
15 5 Readonly::Scalar our $FALSE => 0;
16  
17 5 Readonly::Scalar our $EMPTY => q{};
18 5 Readonly::Scalar our $NL => "\n";
19 5 Readonly::Scalar our $TAB => "\t";
20 5 Readonly::Scalar our $PIPE => q{|};
21 5 Readonly::Scalar our $COMMA => q{,};
22  
23 5 ▶use parent qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 5 our @EXPORT_OK = qw(
26 $COMMA
27 $EMPTY
28 $FAILURE
29 $FALSE
30 $NL
31 $PIPE
32 $SUCCESS
33 $TAB
34 $TRUE
35 is_array
36 is_hash
37 process_csv
38 );
39  
40 5 our %EXPORT_TAGS = (
41 'booleans' => [qw($TRUE $FALSE $SUCCESS $FAILURE is_array is_hash)],
42 'chars' => [qw($NL $EMPTY $PIPE $TAB $COMMA)],
43 'all' => \@EXPORT_OK,
44 );
45  
46 5 our $VERSION = '1.0.0';
47  
48 ########################################################################
49 236 ▶sub _is_array { push @_, 'ARRAY'; goto &_is_type; }
Subroutine _is_array: called CC=1 SCAR=0.0
50 136 ▶sub _is_hash { push @_, 'HASH'; goto &_is_type; }
Subroutine _is_hash: called CC=1 SCAR=0.0
51 108 ▶sub is_code { push @_, 'CODE'; goto &_is_type; }
Subroutine is_code: called CC=1 SCAR=0.0
Pod undocumented
52 ########################################################################
53  
54 ########################################################################
55 136 ▶sub is_hash { ## no critic (RequireArgUnpacking)
Subroutine is_hash: called CC=4 SCAR=13.9
Pod undocumented
56 ########################################################################
57 136 my $result = _is_hash( $_[0] );
58  
59 return
60 136 ▶ if !$result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 116 20 136
61  
62 20 ▶ return wantarray ? %{ ref $_[0] ? $_[0] : {} } : $result;
Branch: 2 branches
BranchTrueFalseTotal
wantarray ? : 19 1 20
ref $_[0] ? : 19 0 19
63 }
64  
65 ########################################################################
66 236 ▶sub is_array { ## no critic (RequireArgUnpacking)
Subroutine is_array: called CC=4 SCAR=13.9
Pod undocumented
67 ########################################################################
68 236 my $result = _is_array( $_[0] );
69  
70 return
71 236 ▶ if !$result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 178 58 236
72  
73 58 ▶ return wantarray ? @{ ref $_[0] ? $_[0] : [] } : $result;
Branch: 2 branches
BranchTrueFalseTotal
wantarray ? : 11 47 58
ref $_[0] ? : 11 0 11
74 }
75  
76 ########################################################################
77 480 ▶sub _is_type { return ref $_[0] && reftype( $_[0] ) eq $_[1]; }
Subroutine _is_type: called CC=2 SCAR=6.9
Condition100%3/3
ref $_[0] && builtin::reftype($_[0]) eq $_[1]
!ll&&!rl&&r
32721132
MC/DC100%2/2
ref $_[0] && builtin::reftype($_[0]) eq $_[1]
ref $_[0] builtin::reftype($_[0]) eq $_[1]
Truth table100%3/3
A && B
A= ref $_[0]B= builtin::reftype($_[0]) eq $_[1]
ABresult
0X0
100
111
78 ########################################################################
79  
80 ########################################################################
81 9 ▶sub process_csv {
Subroutine process_csv: called CC=3 SCAR=11.0
Pod documented
82 ########################################################################
83 9 my ( $file, %options ) = @_;
84  
85 9 require File::Process;
86  
87 9 ▶ my $csv_options = $options{csv_options} // {};
Condition50%1/2
$options{"csv_options"} // {}
l!l
90
MC/DC0%0/1
$options{"csv_options"} // {}
$options{"csv_options"}
Truth table50%1/2
A
A= $options{"csv_options"}
Aresult
11
00
88  
89 9 require Text::CSV_XS;
90  
91 9 my $csv = Text::CSV_XS->new($csv_options);
92  
93 9 ▶ $options{chomp} //= $TRUE;
Condition33%1/3
$options{"chomp"} //= $File::Process::Utils::TRUE
l!l&&r!l&&!r
090
MC/DC0%0/2
$options{"chomp"} //= $File::Process::Utils::TRUE
$options{"chomp"} $File::Process::Utils::TRUE
Truth table33%1/3
A //= B
A= $options{"chomp"}B= $File::Process::Utils::TRUE
ABresult
1X1
011
000
94  
95 my ( $csv_lines, %info ) = File::Process::process_file(
96 $file,
97 csv => $csv,
98 %options,
99 9 ▶ pre => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
100 9 my ( $file, $args ) = @_;
101  
102 9 my ( $fh, $all_lines ) = File::Process::pre( $file, $args );
103  
104 9 ▶ if ( $args->{'has_headers'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"has_headers"}) 1 8 9
105 1 my @column_names = $args->{csv}->getline($fh);
106 1 $args->{csv}->column_names(@column_names);
107 }
108  
109 9 return ( $fh, $all_lines );
110 },
111 90 ▶ next_line => sub {
Subroutine __ANON__: called CC=25 SCAR=32.6
112 90 my ( $fh, $all_lines, $args ) = @_;
113  
114 return
115 if defined $args->{max_rows}
116 0 && @{$all_lines}
117 0 ▶ && @{$all_lines} >= $args->{max_rows};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args->{"max_rows"} and @{$all_lines;} and @{$all_lines;} >= $args->{"max_rows"} 0 90 90
Condition33%2/6
defined $args->{"max_rows"} and @{$all_lines;} and @{$all_lines;} >= $args->{"max_rows"}
!ll&&!rl&&r
9000
defined $args->{"max_rows"} and @{$all_lines;}
!ll&&!rl&&r
9000
MC/DC0%0/3
defined $args->{"max_rows"} and @{$all_lines;} and @{$all_lines;} >= $args->{"max_rows"}
defined $args->{"max_rows"} @{$all_lines;} @{$all_lines;} >= $args->{"max_rows"}
Truth table25%1/4
A and B and C
A= defined $args->{"max_rows"}B= @{$all_lines;}C= @{$all_lines;} >= $args->{"max_rows"}
ABCresult
0XX0
10X0
1100
1111
118  
119 90 my $ref;
120  
121 90 ▶ if ( $args->{has_headers} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{'has_headers'}) { } 10 80 90
122 10 $ref = $args->{csv}->getline_hr($fh);
123  
124 10 ▶ if ( my (%skips) = is_hash( $args->{skip_list} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(%skips) = is_hash($args->{"skip_list"})) 0 10 10
125 0 for ( keys %skips ) {
126 0 delete $ref->{$_};
127 }
128 }
129 }
130 else {
131 80 $ref = $args->{csv}->getline($fh);
132  
133 80 ▶ return $ref
Branch: 1 branch
BranchTrueFalseTotal
unless $ref 8 72 80
134 if !$ref;
135  
136 72 ▶ if ( !$args->{keep_list} && is_array( $args->{skip_list} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $args->{"keep_list"} and is_array($args->{"skip_list"})) 1 71 72
Condition100%3/3
not $args->{"keep_list"} and is_array($args->{"skip_list"})
!ll&&!rl&&r
8631
MC/DC100%2/2
not $args->{"keep_list"} and is_array($args->{"skip_list"})
not $args->{"keep_list"} is_array($args->{"skip_list"})
Truth table100%3/3
not(A) and B
A= not $args->{"keep_list"}B= is_array($args->{"skip_list"})
ABresult
0X0
100
111
137  
138 1 my @keep_list = ( 0 .. $#{$ref} );
139  
140 1 for ( @{ $args->{skip_list} } ) {
141 1 splice @keep_list, $_, 1;
142 }
143  
144 1 $args->{keep_list} = \@keep_list;
145 }
146  
147 72 ▶ if ( $args->{keep_list} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"keep_list"}) 9 63 72
148 9 $ref = [ @{$ref}[ @{ $args->{keep_list} } ] ];
149 }
150 }
151  
152 82 my %row;
153  
154 82 my $column_keys = $args->{column_names};
155  
156 82 ▶ if ( is_array($column_keys) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_array($column_keys)) 45 37 82
157  
158 45 ▶ if ( !@{$column_keys} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$column_keys;}) 4 41 45
159 # generated extra column names as needed
160 4 $column_keys = [ map {"col$_"} ( 0 .. $#{$ref} ) ];
161 4 $args->{column_names} = $column_keys;
162 }
163 }
164  
165 82 ▶ if ($column_keys) {
Branch: 1 branch
BranchTrueFalseTotal
if ($column_keys) 45 37 82
166 45 %row = map { $column_keys->[$_] => $ref->[$_] } ( 0 .. $#{$ref} );
167 45 ▶ if ( my (%skips) = is_hash( $args->{skip_list} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(%skips) = is_hash($args->{"skip_list"})) 9 36 45
168 9 for ( keys %skips ) {
169 9 delete $row{$_};
170 }
171 }
172 }
173  
174 # hooks?
175 82 ▶ if ( my (@hooks) = is_array( $args->{hooks} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my(@hooks) = is_array($args->{'hooks'})) { } 9 73 82
elsif (my(%hooks) = is_hash($args->{'hooks'})) { } 9 64 73
176  
177 9 for my $col ( 0 .. $#{$ref} ) {
178 36 is_code $hooks[$col];
179  
180 36 ▶ next if !is_code $hooks[$col];
Branch: 1 branch
BranchTrueFalseTotal
unless is_code($hooks[$col]) 18 18 36
181  
182 18 $ref->[$col] = $hooks[$col]->( $ref->[$col] );
183 }
184 }
185 elsif ( my (%hooks) = is_hash( $args->{hooks} ) ) {
186  
187 croak "you just define column_names when 'hooks' is a hash\n"
188 9 ▶ if !@{$column_keys};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$column_keys;} 0 9 9
189  
190 9 for my $column_name ( @{$column_keys} ) {
191 36 ▶ next if !is_code $hooks{$column_name};
Branch: 1 branch
BranchTrueFalseTotal
unless is_code($hooks{$column_name}) 18 18 36
192  
193 $row{$column_name}
194 18 = $hooks{$column_name}->( $row{$column_name} );
195 }
196 }
197  
198 82 ▶ return $column_keys ? \%row : $ref;
Branch: 1 branch
BranchTrueFalseTotal
$column_keys ? : 45 37 82
199 }
200 9 );
201  
202 9 return ( $csv_lines, %info );
203 }
204  
205 5 1;
206  
207 __END__
↑ Summary Process.pm »