Process.pm

filter: Statementstmt 70.7% 111 / 157 Branchbran 74.0% 40 / 54 Conditioncond 75.0% 18 / 24 MC/DCmcdc 62.5% 10 / 16 Subroutinesub 66.6% 18 / 27 Podpod 87.5% 7 / 8 totaltotal 71.3% 204 / 286 CC 37 SCAR 20.8
CC
37
Cov
72%
_filter
16.2
process_file
12.9
__ANON__
6.0
CRAP 8.0
« Process/Utils.pm ↑ Summary
1 package File::Process;
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 English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use File::Process::Utils qw(:booleans :chars);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use IO::Scalar;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Scalar::Util qw( openhandle );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 5 ▶use parent qw( Exporter );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 5 our $VERSION = '1.0.0';
16  
17 5 our %DEFAULT_PROCESSORS = (
18 pre => \&_pre,
19 next_line => \&_next_line,
20 filter => \&_filter,
21 process => \&_process,
22 post => \&_post,
23 );
24  
25 5 ▶use parent qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 5 our @EXPORT = qw( process_file ); ## no critic (ProhibitAutomaticExportation)
28  
29 5 our @EXPORT_OK = qw(
30 post
31 pre
32 process
33 process_csv
34 filter
35 next_line
36 );
37  
38 5 our %EXPORT_TAGS = (
39 'booleans' => [qw($TRUE $FALSE $SUCCESS $FAILURE)],
40 'all' => \@EXPORT_OK,
41 );
42  
43 5 ▶caller or __PACKAGE__->main();
Branch: 1 branch
BranchTrueFalseTotal
caller or __PACKAGE__->main 5 0 5
44  
45 ########################################################################
46 9 ▶sub process_csv {
Subroutine process_csv: called CC=1 SCAR=0.0
Pod documented
47 ########################################################################
48 9 require File::Process::Utils;
49  
50 9 return File::Process::Utils::process_csv(@_);
51 }
52  
53 ########################################################################
54 18 ▶sub process_file {
Subroutine process_file: called CC=12 SCAR=25.6
Pod documented
55 ########################################################################
56 18 my ( $file, %args ) = @_;
57  
58 18 my $chomp = $args{'chomp'};
59  
60 18 ▶ $args{'file'} = $file || $EMPTY;
Condition33%1/3
$file || $File::Process::EMPTY
l!l&&r!l&&!r
1800
MC/DC0%0/2
$file || $File::Process::EMPTY
$file $File::Process::EMPTY
Truth table33%1/3
A || B
A= $fileB= $File::Process::EMPTY
ABresult
1X1
011
000
61  
62 my %processors
63 18 = map { ( $_, $args{$_} ) } qw( pre filter next_line process post );
64  
65 18 foreach (qw( pre filter next_line process post)) {
66 90 ▶ if ( !$processors{$_} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($processors{$_}) 68 22 90
67 68 $processors{$_} = $DEFAULT_PROCESSORS{$_};
68 }
69 }
70  
71 18 $args{'default_processors'} = \%DEFAULT_PROCESSORS;
72  
73 18 my ( $fh, $all_lines ) = $processors{'pre'}->( $file, \%args );
74  
75 18 ▶ if ( !$fh || !ref $all_lines ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh and ref $all_lines) 0 18 18
Condition33%1/3
$fh and ref $all_lines
!ll&&!rl&&r
0018
MC/DC0%0/2
$fh and ref $all_lines
$fh ref $all_lines
Truth table33%1/3
A and B
A= $fhB= ref $all_lines
ABresult
0X0
100
111
76 0 croak "invalid pre processor return values: wanted file handle, array ref\n";
77 }
78  
79 18 LINE: while (1) {
80 144 my $current_line = $processors{'next_line'}->( $fh, $all_lines, \%args );
81 144 ▶ last LINE if !defined $current_line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $current_line 18 126 144
82  
83 126 $args{'raw_count'}++;
84  
85 126 foreach my $p ( @processors{qw( filter process )} ) {
86  
87 250 $current_line = eval { return $p->( $fh, $all_lines, \%args, $current_line ); };
88  
89 250 ▶ last LINE if $EVAL_ERROR;
Branch: 1 branch
BranchTrueFalseTotal
if $File::Process::EVAL_ERROR 0 250 250
90  
91 250 ▶ next LINE if !defined $current_line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $current_line 2 248 250
92 }
93  
94 124 ▶ if ( $args{merge_lines} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'merge_lines'}) { } 4 120 124
95 4 $all_lines->print($current_line);
96 }
97 else {
98 120 push @{$all_lines}, $current_line;
99 }
100 }
101  
102 18 ▶ if ($EVAL_ERROR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($File::Process::EVAL_ERROR) 0 18 18
103 0 croak "$EVAL_ERROR";
104 }
105  
106 18 return $processors{'post'}->( $fh, $all_lines, \%args );
107 }
108  
109 ########################################################################
110 18 ▶sub _pre {
Subroutine _pre: called CC=4 SCAR=14.7
111 ########################################################################
112 18 my ( $file, $args ) = @_;
113  
114 18 my $fh;
115  
116 18 ▶ if ( openhandle $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&openhandle($file)) { } 18 0 18
117 18 $fh = $file;
118  
119 18 $args->{file} = ref $fh; # GLOB
120 }
121 else {
122 0 ▶ open $fh, '<', $file ## no critic (RequireBriefOpen)
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $file or croak('could not open ' . $file . $File::Process::NL) 0 0 0
123 or croak 'could not open ' . $file . $NL;
124  
125 0 $args->{'file'} = $file;
126 }
127  
128 18 $args->{'raw_count'} = 0;
129 18 $args->{'skipped'} = 0;
130 18 $args->{'start_time'} = time;
131  
132 18 ▶ my $lines = $args->{merge_lines} ? IO::Scalar->new : [];
Branch: 1 branch
BranchTrueFalseTotal
$args->{'merge_lines'} ? : 1 17 18
133  
134 18 return ( $fh, $lines );
135 }
136  
137 ########################################################################
138 52 ▶sub _next_line {
Subroutine _next_line: called CC=4 SCAR=14.0
139 ########################################################################
140 52 my ( $fh, $all_lines, $args ) = @_;
141  
142 52 my $current_line;
143  
144 52 ▶ if ( openhandle $fh ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&openhandle($fh)) 52 0 52
145 52 ▶ if ( !eof $fh ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eof $fh) 44 8 52
146 44 ▶ defined( $current_line = readline $fh )
Branch: 1 branch
BranchTrueFalseTotal
defined($current_line = readline $fh) or croak("readline failed: $File::Process::OS_ERROR\n") 44 0 44
147 or croak "readline failed: $OS_ERROR\n";
148 }
149 }
150  
151 52 return $current_line;
152 }
153  
154 ########################################################################
155 126 ▶sub _filter {
Subroutine _filter: called CC=16 SCAR=27.9
156 ########################################################################
157 126 my ( $fh, $all_lines, $args, $current_line ) = @_;
158  
159 126 ▶ if ( $args->{'chomp'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"chomp"}) 126 0 126
160 126 chomp $current_line;
161 }
162  
163 126 ▶ if ( $args->{'trim'} && $args->{'trim'} =~ /(front|both)/xsm ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"trim"} and $args->{"trim"} =~ /(front|both)/msx) 12 114 126
Condition100%3/3
$args->{"trim"} and $args->{"trim"} =~ /(front|both)/msx
!ll&&!rl&&r
108612
MC/DC100%2/2
$args->{"trim"} and $args->{"trim"} =~ /(front|both)/msx
$args->{"trim"} $args->{"trim"} =~ /(front|both)/msx
Truth table100%3/3
A and B
A= $args->{"trim"}B= $args->{"trim"} =~ /(front|both)/msx
ABresult
0X0
100
111
164 12 $current_line =~ s/^\s+//xsm;
165 }
166  
167 126 ▶ if ( $args->{'trim'} && $args->{'trim'} =~ /(both|back)/xsm ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"trim"} and $args->{"trim"} =~ /(both|back)/msx) 12 114 126
Condition100%3/3
$args->{"trim"} and $args->{"trim"} =~ /(both|back)/msx
!ll&&!rl&&r
108612
MC/DC100%2/2
$args->{"trim"} and $args->{"trim"} =~ /(both|back)/msx
$args->{"trim"} $args->{"trim"} =~ /(both|back)/msx
Truth table100%3/3
A and B
A= $args->{"trim"}B= $args->{"trim"} =~ /(both|back)/msx
ABresult
0X0
100
111
168 12 $current_line =~ s/\s+$//xsm;
169 }
170  
171 # skip?
172 126 my $skip = $FALSE;
173  
174 126 ▶ if ( $args->{'skip_blank_lines'} || $args->{'skip_comments'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"skip_blank_lines"} or $args->{"skip_comments"}) 12 114 126
Condition100%3/3
$args->{"skip_blank_lines"} or $args->{"skip_comments"}
l!l&&r!l&&!r
66114
MC/DC100%2/2
$args->{"skip_blank_lines"} or $args->{"skip_comments"}
$args->{"skip_blank_lines"} $args->{"skip_comments"}
Truth table100%3/3
A or B
A= $args->{"skip_blank_lines"}B= $args->{"skip_comments"}
ABresult
1X1
011
000
175  
176 12 ▶ if ( $args->{'skip_blank_lines'} && "$current_line" eq $EMPTY ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"skip_blank_lines"} and "$current_line" eq $File::Process::EMPTY) 1 11 12
Condition100%3/3
$args->{"skip_blank_lines"} and "$current_line" eq $File::Process::EMPTY
!ll&&!rl&&r
651
MC/DC100%2/2
$args->{"skip_blank_lines"} and "$current_line" eq $File::Process::EMPTY
$args->{"skip_blank_lines"} "$current_line" eq $File::Process::EMPTY
Truth table100%3/3
A and B
A= $args->{"skip_blank_lines"}B= "$current_line" eq $File::Process::EMPTY
ABresult
0X0
100
111
177 1 $skip = $TRUE;
178 }
179  
180 # if we're not chomping, then consider new line a blank line?
181 12 ▶ if ( !$args->{chomp} && "$current_line" eq $NL ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $args->{"chomp"} and "$current_line" eq $File::Process::NL) 0 12 12
Condition33%1/3
not $args->{"chomp"} and "$current_line" eq $File::Process::NL
!ll&&!rl&&r
1200
MC/DC0%0/2
not $args->{"chomp"} and "$current_line" eq $File::Process::NL
not $args->{"chomp"} "$current_line" eq $File::Process::NL
Truth table33%1/3
not(A) and B
A= not $args->{"chomp"}B= "$current_line" eq $File::Process::NL
ABresult
0X0
100
111
182 0 $skip = $TRUE;
183 }
184  
185 12 ▶ if ( $args->{'skip_comments'} && $current_line =~ /^\#/xsm ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"skip_comments"} and $current_line =~ /^\#/msx) 1 11 12
Condition100%3/3
$args->{"skip_comments"} and $current_line =~ /^\#/msx
!ll&&!rl&&r
651
MC/DC100%2/2
$args->{"skip_comments"} and $current_line =~ /^\#/msx
$args->{"skip_comments"} $current_line =~ /^\#/msx
Truth table100%3/3
A and B
A= $args->{"skip_comments"}B= $current_line =~ /^\#/msx
ABresult
0X0
100
111
186 1 $skip = $TRUE;
187 }
188 }
189  
190 126 ▶ $args->{skipped} = $args->{skipped} + $skip ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$args->{'skipped'} + $skip ? : 9 117 126
191  
192 126 ▶ return $skip ? undef : $current_line;
Branch: 1 branch
BranchTrueFalseTotal
$skip ? : 2 124 126
193 }
194  
195 ########################################################################
196 115 ▶sub _process {
Subroutine _process: called CC=1 SCAR=0.0
197 ########################################################################
198 115 my ( $fh, $all_lines, $args, $current_line ) = @_;
199  
200 115 return $current_line;
201 }
202  
203 ########################################################################
204 17 ▶sub _post {
Subroutine _post: called CC=4 SCAR=13.9
205 ########################################################################
206 17 my ( $fh, $all_lines, $args ) = @_;
207  
208 17 $args->{end_time} = time;
209  
210 17 my $retval;
211  
212 17 ▶ if ( $args->{merge_lines} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{'merge_lines'}) { } 1 16 17
213 1 $retval = ${ $all_lines->sref };
214 }
215 else {
216 16 $retval = $all_lines;
217 }
218  
219 17 ▶ if ( !$args->{'keep_open'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args->{"keep_open"}) 2 15 17
220 close $fh
221 2 ▶ or croak 'could not close' . $args->{file} . $NL;
Branch: 1 branch
BranchTrueFalseTotal
close $fh or croak('could not close' . $args->{"file"} . $File::Process::NL) 2 0 2
222 }
223  
224 17 return ( $retval, %{$args} );
225 }
226  
227 ########################################################################
228 0 ▶sub post { ## no critic [Subroutines::RequireArgUnpacking]
Subroutine post: not called CC=1 SCAR=6.9
Pod documented
229 ########################################################################
230 0 return $_[2]->{default_processors}->{post}->(@_);
231 }
232  
233 ########################################################################
234 0 ▶sub filter { ## no critic [Subroutines::RequireArgUnpacking]
Subroutine filter: not called CC=1 SCAR=6.9
Pod documented
235 ########################################################################
236 0 return $_[2]->{default_processors}->{filter}->(@_);
237 }
238  
239 ########################################################################
240 10 ▶sub pre { ## no critic [Subroutines::RequireArgUnpacking]
Subroutine pre: called CC=1 SCAR=0.0
Pod documented
241 ########################################################################
242 10 return $_[1]->{default_processors}->{pre}->(@_);
243 }
244  
245 ########################################################################
246 0 ▶sub process { ## no critic [Subroutines::RequireArgUnpacking]
Subroutine process: not called CC=1 SCAR=6.9
Pod documented
247 ########################################################################
248 0 return $_[2]->{default_processors}->{process}->(@_);
249 }
250  
251 ########################################################################
252 0 ▶sub next_line { ## no critic [Subroutines::RequireArgUnpacking]
Subroutine next_line: not called CC=1 SCAR=6.9
Pod documented
253 ########################################################################
254 0 return $_[2]->{default_processors}->{next_line}->(@_);
255 }
256  
257 ########################################################################
258 0 ▶sub main {
Subroutine main: not called CC=1 SCAR=6.9
Pod undocumented
259 ########################################################################
260 0 require IO::Scalar;
261 0 require Data::Dumper;
262 0 require JSON;
263 0 require Text::CSV_XS;
264  
265 0 JSON->import('decode_json');
266  
267 0 Data::Dumper->import('Dumper');
268  
269 # +------------------+
270 # | READ A TEXT FILE |
271 # +------------------+
272  
273 0 my $buffer = <<'END_OF_TEXT';
274 line 1
275 line 2
276
277 line 4
278  
279 line 5
280 END_OF_TEXT
281  
282 0 my $fh = IO::Scalar->new( \$buffer );
283  
284 0 print Dumper(
285 process_file(
286 $fh,
287 skip_blank_lines => $TRUE,
288 chomp => $TRUE,
289 trim => 'both'
290 )
291 );
292  
293 0 $fh = IO::Scalar->new( \$buffer );
294 print Dumper(
295 process_file(
296 $fh,
297 0 ▶ post => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
298 0 my @retval = post(@_);
299 0 $retval[0] = join $EMPTY, @{ $_[1] };
300 0 return @retval;
301 }
302 )
303 0 );
304  
305 # +------------------+
306 # | READ A JSON FILE |
307 # +------------------+
308  
309 0 my $json_text = <<'END_OF_TEXT';
310 {
311 "foo" : "bar",
312 "baz" : "buz"
313 }
314  
315 END_OF_TEXT
316  
317 0 $fh = IO::Scalar->new( \$json_text );
318  
319 print Dumper(
320 process_file(
321 $fh,
322 chomp => 1,
323 0 ▶ post => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
324 0 post(@_);
325 0 return decode_json( join $EMPTY, @{ $_[1] } );
326 }
327 )
328 0 );
329  
330 0 $fh = IO::Scalar->new( \$json_text );
331  
332 0 print Dumper( decode_json( process_file( $fh, merge_lines => 1, chomp => 1 ) ) );
333  
334 # +-----------------+
335 # | READ A CSV FILE |
336 # +-----------------+
337  
338 0 my $csv_text = <<'END_OF_TEXT';
339 "id","first_name","last_name"
340 0,"Rob","Lauer"
341 END_OF_TEXT
342  
343 0 $fh = IO::Scalar->new( \$csv_text );
344  
345 0 my $csv = Text::CSV_XS->new;
346  
347 my ($csv_lines) = process_file(
348 $fh,
349 csv => $csv,
350 chomp => 1,
351 has_headers => 1,
352 0 ▶ pre => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
353 0 my ( $csv_fh, $args ) = @_;
354  
355 0 ▶ if ( $args->{'has_headers'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"has_headers"}) 0 0 0
356 0 my @column_names = $args->{csv}->getline($csv_fh);
357 0 $args->{csv}->column_names(@column_names);
358 }
359  
360 0 return ( pre( $fh, $args ) );
361 },
362 0 ▶ next_line => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
363 0 my ( $csv_fh, $all_lines, $args ) = @_;
364 0 my $ref = $args->{csv}->getline_hr($csv_fh);
365 0 return $ref;
366 }
367 0 );
368  
369 0 print Dumper($csv_lines);
370  
371 0 exit 0;
372 }
373  
374 5 1;
375  
376 __END__
« Process/Utils.pm ↑ Summary