Process.pm

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