ByLine/Object.pm

filter: Statementstmt 86.7% 327 / 377 Branchbran 77.4% 175 / 226 Conditioncond 85.7% 36 / 42 MC/DCmcdc 61.5% 16 / 26 Subroutinesub 97.5% 40 / 41 Podpod 0.0% 0 / 19 totaltotal 81.2% 594 / 731 CC 138 SCAR 22.0
CC
138
Cov
83%
_open_and_seek
18.5
_grepmap_chunk
17.1
_grepmap
12.4
CRAP 9.0
↑ Summary ByLine.pm »
1 #!/usr/bin/perl
2  
3 #
4 # Copyright (C) 2018 Joelle Maslak
5 # All Rights Reserved - See License
6 #
7  
8 package File::ByLine::Object;
9 79 $File::ByLine::Object::VERSION = '1.192590';
10 79 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # ABSTRACT: Internal object used by File::ByLine
13  
14 79 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 79 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 79 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 79 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 79 ▶use Fcntl;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 79 ▶use Scalar::Util qw(blessed reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 # We do this intentionally:
23 ## no critic (Subroutines::ProhibitBuiltinHomonyms)
24  
25 # Attributes and their accessors & defaults, used by the constructor
26 # Each attribute name is the key of the hash, with the value being a
27 # hashref of two values: accessor and default value.
28 79 my (%ATTRIBUTE) = (
29 file => [ \&file, undef, ['f'] ],
30 extended_info => [ \&extended_info, undef, ['ei'] ],
31 header_all_files => [ \&header_all_files, undef, ['haf'] ],
32 header_handler => [ \&header_handler, undef, ['hh'] ],
33 header_skip => [ \&header_skip, undef, ['hs'] ],
34 processes => [ \&processes, 1, ['p'] ],
35 skip_unreadable => [ \&skip_unreadable, undef, ['su'] ],
36 );
37  
38  
39 #
40 # Attribute Accessor - file
41 #
42 4 ▶sub f { goto &file }
Subroutine f: called CC=1 SCAR=0.0
Pod undocumented
43  
44 35 ▶sub file {
Subroutine file: called CC=4 SCAR=13.9
Pod undocumented
45 35 my ($self) = shift;
46 35 ▶ if ( scalar(@_) == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @_ == 0) { } 11 24 35
elsif (scalar @_ == 1) { } 23 1 24
47 11 return $self->{file};
48 } elsif ( scalar(@_) == 1 ) {
49 23 my $file = shift;
50 23 ▶ if ( !defined($file) ) { confess("Must pass a file or array ref as a file attribute") }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $file) 1 22 23
51 22 return $self->{file} = $file;
52 } else {
53 1 return $self->{file} = [@_];
54 }
55 }
56  
57 #
58 # Attribute Accessor - extended_info
59 #
60 # Do we pass an extended information hash to the user process?
61 4 ▶sub ei { goto &extended_info }
Subroutine ei: called CC=1 SCAR=0.0
Pod undocumented
62  
63 69 ▶sub extended_info {
Subroutine extended_info: called CC=3 SCAR=11.3
Pod undocumented
64 69 my ($self) = shift;
65 69 ▶ if ( scalar(@_) == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @_ == 0) { } 11 58 69
elsif (scalar @_ == 1) { } 58 0 58
66 11 return $self->{extended_info};
67 } elsif ( scalar(@_) == 1 ) {
68 58 return $self->{extended_info} = !!$_[0]; # !! to convert to fast boolean
69 } else {
70 0 confess("Invalid call");
71 }
72 }
73  
74 #
75 # Attribute Accessor - processes
76 #
77 # This is the degree of parallism we will attempt for most methods (the
78 # exception is "lines()")
79 3 ▶sub p { goto &processes }
Subroutine p: called CC=1 SCAR=0.0
Pod undocumented
80  
81 781 ▶sub processes {
Subroutine processes: called CC=6 SCAR=17.9
Pod undocumented
82 781 my ($self) = shift;
83 781 ▶ if ( scalar(@_) == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @_ == 0) { } 75 706 781
elsif (scalar @_ == 1) { } 705 1 706
84 75 return $self->{processes};
85 } elsif ( scalar(@_) == 1 ) {
86 705 my $procs = shift;
87  
88 705 ▶ if ( !_is_number($procs) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_is_number($procs)) 124 581 705
89 124 confess("processes only accepts integer values");
90 }
91  
92 581 ▶ if ( $procs < 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($procs < 1) 1 580 581
93 1 confess("Process count must be >= 1");
94 }
95 580 ▶ if ( $procs > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($procs > 1) 506 74 580
96 # Ensure we have the right packages installed
97 506 $self->_require_parallel();
98 }
99 580 return $self->{processes} = $procs;
100 } else {
101 1 confess("Invalid call");
102 }
103 }
104  
105 #
106 # Attribute Accessor - header_all_files
107 #
108 # If set to one, process all files for headers
109 5 ▶sub haf { goto &header_all_files }
Subroutine haf: called CC=1 SCAR=0.0
Pod undocumented
110  
111 66 ▶sub header_all_files {
Subroutine header_all_files: called CC=3 SCAR=11.3
Pod undocumented
112 66 my ($self) = shift;
113 66 ▶ if ( scalar(@_) == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @_ == 0) { } 59 7 66
elsif (scalar @_ == 1) { } 7 0 7
114 59 return $self->{header_all_files};
115 } elsif ( scalar(@_) == 1 ) {
116 7 return $self->{header_all_files} = $_[0];
117 } else {
118 0 confess("Invalid call");
119 }
120 }
121  
122 #
123 # Attribute Accessor - header_handler
124 #
125 # This is the code that handles the header line
126 4 ▶sub hh { goto &header_handler }
Subroutine hh: called CC=1 SCAR=0.0
Pod undocumented
127  
128 505 ▶sub header_handler {
Subroutine header_handler: called CC=6 SCAR=18.3
Pod undocumented
129 505 my ($self) = shift;
130 505 ▶ if ( scalar(@_) == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @_ == 0) { } 392 113 505
elsif (scalar @_ == 1) { } 113 0 113
131 392 return $self->{header_handler};
132 } elsif ( scalar(@_) == 1 ) {
133 113 my $code = shift;
134 113 ▶ if ( defined($code) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $code) 110 3 113
135 110 ▶ if ( !_codelike($code) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_codelike($code)) 3 107 110
136 3 confess("header_handler must be a code reference");
137 }
138 107 ▶ if ( $self->{header_skip} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"header_skip"}) 0 107 107
139 0 confess("Must unset header_skip before setting a header_handler");
140 }
141 }
142 110 return $self->{header_handler} = $code;
143 } else {
144 0 confess("Invalid call");
145 }
146 }
147  
148 #
149 # Attribute Accessor - header_skip
150 #
151 # If set to one, skip the header line
152 4 ▶sub hs { goto &header_skip }
Subroutine hs: called CC=1 SCAR=0.0
Pod undocumented
153  
154 326 ▶sub header_skip {
Subroutine header_skip: called CC=5 SCAR=17.5
Pod undocumented
155 326 my ($self) = shift;
156 326 ▶ if ( scalar(@_) == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @_ == 0) { } 276 50 326
elsif (scalar @_ == 1) { } 50 0 50
157 276 return $self->{header_skip};
158 } elsif ( scalar(@_) == 1 ) {
159 50 ▶ if ( $_[0] && $self->{header_handler} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] and $self->{"header_handler"}) 0 50 50
Condition66%2/3
$_[0] and $self->{"header_handler"}
!ll&&!rl&&r
1490
MC/DC0%0/2
$_[0] and $self->{"header_handler"}
$_[0] $self->{"header_handler"}
Truth table66%2/3
A and B
A= $_[0]B= $self->{"header_handler"}
ABresult
0X0
100
111
160 0 confess("Must undefine header_handler before setting header_skip");
161 }
162 50 return $self->{header_skip} = $_[0];
163 } else {
164 0 confess("Invalid call");
165 }
166 }
167  
168 #
169 # Attribute Accessor - skip_unreadable
170 #
171 5 ▶sub su { goto &skip_unreadable; }
Subroutine su: called CC=1 SCAR=0.0
Pod undocumented
172  
173 19 ▶sub skip_unreadable {
Subroutine skip_unreadable: called CC=3 SCAR=11.3
Pod undocumented
174 19 my ($self) = shift;
175 19 ▶ if ( scalar(@_) == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @_ == 0) { } 12 7 19
elsif (scalar @_ == 1) { } 7 0 7
176 12 return $self->{skip_unreadable};
177 } elsif ( scalar(@_) == 1 ) {
178 7 return $self->{skip_unreadable} = !!$_[0]; # !! to convert to fast boolean
179 } else {
180 0 confess("Invalid call");
181 }
182 }
183  
184 #
185 # Constructor
186 #
187 814 ▶sub new {
Subroutine new: called CC=12 SCAR=24.9
Pod undocumented
188 814 my $class = shift;
189  
190 814 my %options;
191 814 ▶ if ( scalar(@_) == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @_ == 1) { } 3 811 814
elsif (scalar @_ > 1) { } 3 808 811
192 # We assume this to be a hashref of options
193 3 %options = %{ $_[0] };
194 } elsif ( scalar(@_) > 1 ) {
195 3 ▶ if ( scalar(@_) % 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar(@_) % 2) { } 1 2 3
196 1 confess("Must pass options in key/value form or as a hashref");
197 } else {
198 2 %options = (@_);
199 }
200 }
201  
202 # Set defaults
203 813 my $self = {};
204 813 foreach my $attr ( keys %ATTRIBUTE ) {
205 5691 $self->{$attr} = $ATTRIBUTE{$attr}->[1]; # Default avlue
206 }
207  
208 813 bless $self, $class;
209  
210 # Build abbreviation list
211 813 my (%attr_short);
212 813 foreach my $attr ( keys %ATTRIBUTE ) {
213 5691 foreach my $abbr ( @{ $ATTRIBUTE{$attr}->[2] } ) {
214 5691 $attr_short{$abbr} = $attr; # Default avlue
215 }
216 }
217  
218 # Set attributes. We use the accessor so we don't duplicate type
219 # checks.
220 813 my %set; # Track set attributes
221 813 foreach my $key ( sort keys %options ) { # Sort for consistent tests
222 22 ▶ if ( exists( $ATTRIBUTE{$key} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $ATTRIBUTE{$key}) { } 13 9 22
elsif (exists $attr_short{$key}) { } 7 2 9
223 13 ▶ if ( exists( $set{$key} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $set{$key}) 0 13 13
224 0 confess("Duplicate attribute in constructor detected: $key");
225 }
226  
227 13 my $value = $options{$key};
228  
229 # Call the accessor
230 13 $ATTRIBUTE{$key}->[0]( $self, $value );
231 13 $set{$key} = 1; # Mark as set
232 } elsif ( exists( $attr_short{$key} ) ) {
233 7 my $cannonical = $attr_short{$key};
234  
235 7 ▶ if ( exists( $set{$key} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $set{$key}) 0 7 7
236 0 confess("Duplicate attribute in constructor detected: $key");
237 }
238  
239 7 my $value = $options{$key};
240  
241 # Call the accessor
242 7 $ATTRIBUTE{$cannonical}->[0]( $self, $value );
243 7 $set{$key} = 1; # Mark as set
244 } else {
245 2 confess("Invalid attribute: $key");
246 }
247 }
248  
249 811 return $self;
250 }
251  
252 #
253 # Method - do
254 #
255 # Executes the provided code on every line.
256 #
257 409 ▶sub do {
Subroutine do: called CC=9 SCAR=22.1
Pod undocumented
258 0 ▶ if ( scalar(@_) < 2 ) { confess "Invalid call"; }
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ < 2) 0 409 409
259 409 my ( $self, $code, $file ) = @_;
260  
261 409 ▶ if ( !defined($file) ) { $file = $self->{file} }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $file) 3 406 409
262 0 ▶ if ( !defined($file) ) { confess "Must provide filename"; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $file) 0 409 409
263 409 ▶ if ( !_listlike($file) ) { $file = [$file] }
Branch: 1 branch
BranchTrueFalseTotal
unless (_listlike($file)) 308 101 409
264  
265 409 ▶ if ( defined( $self->{header_handler} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"header_handler"}) 98 311 409
266 98 my $fileno = 0;
267 98 for my $f (@$file) {
268 145 $self->_read_header( $f, $fileno );
269 145 $fileno++;
270 }
271 }
272  
273 409 ▶ if ( $self->{processes} == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'processes'} == 1) { } 22 387 409
274 22 return $self->_forlines_chunk( $code, $file, 0 );
275 } else {
276 387 my $wu = Parallel::WorkUnit->new();
277 $wu->asyncs( $self->{processes},
278 387 ▶ sub { return $self->_forlines_chunk( $code, $file, $_[0] ); } );
Subroutine __ANON__: called CC=1 SCAR=0.0
279 345 my (@linecounts) = $wu->waitall();
280  
281 345 my $total_lines = 0;
282 345 foreach my $cnt (@linecounts) {
283 1386 $total_lines += $cnt;
284 }
285  
286 345 return $total_lines;
287 }
288 }
289  
290 #
291 # Method - grep
292 #
293 # Finds and returns matching lines
294 74 ▶sub grep {
Subroutine grep: called CC=3 SCAR=12.7
Pod undocumented
295 0 ▶ if ( scalar(@_) < 2 ) { confess "Invalid call, too few arguments"; }
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ < 2) 0 74 74
296 0 ▶ if ( scalar(@_) > 3 ) { confess "Invalid call, too many arguments"; }
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ > 3) 0 74 74
297 74 my ( $self, $code, $file ) = @_;
298  
299 74 return $self->_grepmap( 'grep', $code, $file );
300 }
301  
302 #
303 # Method - map
304 #
305 # Applies function to each entry and returns that result
306 115 ▶sub map {
Subroutine map: called CC=3 SCAR=12.7
Pod undocumented
307 0 ▶ if ( scalar(@_) < 2 ) { confess "Invalid call, too few arguments"; }
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ < 2) 0 115 115
308 0 ▶ if ( scalar(@_) > 3 ) { confess "Invalid call, too many arguments"; }
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ > 3) 0 115 115
309 115 my ( $self, $code, $file ) = @_;
310  
311 115 return $self->_grepmap( 'map', $code, $file );
312 }
313  
314 # Does the actual processing for map/grep
315 189 ▶sub _grepmap {
Subroutine _grepmap: called CC=12 SCAR=25.2
316 0 ▶ if ( scalar(@_) < 3 ) { confess "Invalid call, too few arguments"; }
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ < 3) 0 189 189
317 0 ▶ if ( scalar(@_) > 4 ) { confess "Invalid call, too many arguments"; }
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ > 4) 0 189 189
318 189 my ( $self, $type, $code, $file ) = @_;
319  
320 189 ▶ if ( !defined($file) ) { $file = $self->{file} }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $file) 2 187 189
321 0 ▶ if ( !defined($file) ) { confess "Must provide filename"; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $file) 0 189 189
322 189 ▶ if ( !_listlike($file) ) { $file = [$file] }
Branch: 1 branch
BranchTrueFalseTotal
unless (_listlike($file)) 142 47 189
323  
324 189 ▶ if ( defined( $self->{header_handler} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"header_handler"}) 4 185 189
325 4 my $fileno = 0;
326 4 for my $f (@$file) {
327 4 $self->_read_header( $f, $fileno );
328 4 $fileno++;
329 }
330 }
331  
332 189 my $procs = $self->{processes};
333  
334 # Is this a MAP or a GREP?
335 189 my $isgrep;
336 189 ▶ if ( $type eq 'grep' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'grep') { } 74 115 189
elsif ($type eq 'map') { } 115 0 115
337 74 $isgrep = 1;
338 } elsif ( $type eq 'map' ) {
339 115 $isgrep = 0;
340 } else {
341 0 confess("Invalid type passed to _grepmap: $type");
342 }
343  
344 189 ▶ if ( $procs > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($procs > 1) { } 119 70 189
345 119 my $wu = Parallel::WorkUnit->new();
346  
347 $wu->asyncs( $procs,
348 119 ▶ sub { return $self->_grepmap_chunk( $code, $file, $isgrep, $procs, $_[0] ); } );
Subroutine __ANON__: called CC=1 SCAR=0.0
349  
350 91 my @async_output = $wu->waitall();
351  
352 91 my @file_output;
353 91 ▶ for ( my $i = 0; $i < scalar(@$file); $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$file) 117 91 208
354 117 push @file_output, map { $_->[$i] } @async_output;
355 }
356 91 return map { @$_ } @file_output;
357 } else {
358 70 my $mapped_lines = $self->_grepmap_chunk( $code, $file, $isgrep, 1, 0 );
359  
360 70 return map { @$_ } @$mapped_lines;
361 }
362  
363 }
364  
365 #
366 # Method - lines
367 #
368 # Returns all lines in the file
369 21 ▶sub lines {
Subroutine lines: called CC=10 SCAR=23.1
Pod undocumented
370 0 ▶ if ( scalar(@_) < 1 ) { confess "Invalid call"; }
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ < 1) 0 21 21
371 21 my ( $self, $file ) = @_;
372  
373 21 ▶ if ( !defined($file) ) { $file = $self->{file} }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $file) 5 16 21
374 0 ▶ if ( !defined($file) ) { confess "Must provide filename"; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $file) 0 21 21
375 21 ▶ if ( !_listlike($file) ) { $file = [$file] }
Branch: 1 branch
BranchTrueFalseTotal
unless (_listlike($file)) 19 2 21
376  
377 21 my @lines;
378 21 my $fileno = 0;
379 21 my $lineno = 0;
380  
381 21 for my $f (@$file) {
382 24 $fileno++;
383  
384 24 my $fh = $self->_open($f);
385 24 ▶ if ( !defined($fh) ) { next; } # Next file
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fh) 1 23 24
386  
387 23 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 1002 23 1025
388 1002 $lineno++;
389 1002 chomp;
390  
391 1002 ▶ if ( $lineno == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lineno == 1) 21 981 1002
392 21 ▶ if ( $self->_handle_header( $f, $_, 0, $fileno - 1 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_handle_header($f, $_, 0, $fileno - 1)) 4 17 21
393 4 next;
394 }
395 }
396  
397 998 push @lines, $_;
398 }
399  
400 23 close $fh;
401 }
402  
403 21 return @lines;
404 }
405  
406 # Internal function to read header line (if we need to)
407 149 ▶sub _read_header {
Subroutine _read_header: called CC=3 SCAR=11.3
408 149 my ( $self, $file, $fileno ) = @_;
409  
410 149 my ( $fh, undef ) = $self->_open_and_seek( $file, 1, 0 );
411 0 ▶ if ( !defined($fh) ) { return; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fh) 0 149 149
412 149 my $line = <$fh>;
413 149 close $fh;
414  
415 149 ▶ if ( defined($line) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line) 149 0 149
416 149 chomp($line);
417 149 $self->_handle_header( $file, $line, 0, $fileno );
418 }
419  
420 149 return $line;
421 }
422  
423 # Internal function to perform a for loop on a single chunk of the file.
424 #
425 # Procs should be >= 1. It represents the number of chunks the file
426 # has.
427 #
428 # Part should be >= 0 and < Procs. It represents the zero-indexed chunk
429 # number this invocation is processing.
430 64 ▶sub _forlines_chunk {
Subroutine _forlines_chunk: called CC=9 SCAR=22.0
431 64 my ( $self, $code, $file, $part ) = @_;
432  
433 64 my $fileno = 0;
434 64 my $lineno = 0;
435 64 my $extended_info = $self->{extended_info};
436  
437 64 for my $f (@$file) {
438 70 $fileno++;
439  
440 70 my $extended = $self->_extended( $f, $part );
441  
442 70 my $procs = $self->{processes};
443 70 my ( $fh, $end ) = $self->_open_and_seek( $f, $procs, $part );
444 0 ▶ if ( !defined($fh) ) { next; } # Next file
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fh) 0 70 70
445  
446 70 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 72 45 117
447 72 $lineno++;
448  
449 72 chomp;
450  
451 72 ▶ if ( $lineno == 1 && $self->_handle_header( $f, $_, $part, $fileno - 1 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lineno == 1 and $self->_handle_header($f, $_, $part, $fileno - 1)) { } 8 64 72
Condition100%3/3
$lineno == 1 and $self->_handle_header($f, $_, $part, $fileno - 1)
!ll&&!rl&&r
34308
MC/DC100%2/2
$lineno == 1 and $self->_handle_header($f, $_, $part, $fileno - 1)
$lineno == 1 $self->_handle_header($f, $_, $part, $fileno - 1)
Truth table100%3/3
A and B
A= $lineno == 1B= $self->_handle_header($f, $_, $part, $fileno - 1)
ABresult
0X0
100
111
452 # Do nothing, we handled the header.
453 } else {
454 64 ▶ if ($extended_info) {
Branch: 1 branch
BranchTrueFalseTotal
if ($extended_info) { } 19 45 64
455 19 $code->( $_, $extended );
456 } else {
457 45 $code->($_);
458 }
459 }
460  
461 # If we're reading multi-parts, do we need to end the read?
462 72 ▶ if ( ( $end > 0 ) && ( tell($fh) > $end ) ) { last; }
Branch: 1 branch
BranchTrueFalseTotal
if ($end > 0 and tell $fh > $end) 25 47 72
Condition66%2/3
$end > 0 and tell $fh > $end
!ll&&!rl&&r
47025
MC/DC50%1/2
$end > 0 and tell $fh > $end
$end > 0 tell $fh > $end
Truth table66%2/3
A and B
A= $end > 0B= tell $fh > $end
ABresult
0X0
100
111
463 }
464  
465 70 close $fh;
466 }
467  
468 64 return $lineno;
469 }
470  
471 # Internal function to perform a map/grep on a single chunk of the file.
472 #
473 # Procs should be >= 1. It represents the number of chunks the file
474 # has.
475 #
476 # Part should be >= 0 and < Procs. It represents the zero-indexed chunk
477 # number this invocation is processing.
478 #
479 # isgrep = true if we want to just apply the code as a grep, not as a
480 # map.
481 98 ▶sub _grepmap_chunk {
Subroutine _grepmap_chunk: called CC=17 SCAR=28.4
482 98 my ( $self, $code, $file, $isgrep, $procs, $part ) = @_;
483  
484 98 my @mapped_lines;
485 98 my $fileno = 0;
486 98 my $lineno = 0;
487 98 my $extended_info = $self->{extended_info};
488  
489 98 for my $f (@$file) {
490 119 $fileno++;
491  
492 119 my $extended = $self->_extended( $f, $part );
493  
494 119 my ( $fh, $end ) = $self->_open_and_seek( $f, $procs, $part );
495 0 ▶ if ( !defined($fh) ) { push @mapped_lines, []; next; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fh) 0 119 119
496 ; # Go to next loop
497  
498 119 my @filelines;
499 119 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 592 92 684
500 592 $lineno++;
501  
502 592 chomp;
503  
504 592 ▶ if ( $lineno == 1 && $self->_handle_header( $f, $_, $part, $fileno - 1 ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($lineno == 1 and $self->_handle_header($f, $_, $part, $fileno - 1)) { } 6 586 592
elsif (not $part and $fileno == 1 and $lineno == 1 and $self->{'header_skip'}) { } 0 586 586
Condition100%3/3
$lineno == 1 and $self->_handle_header($f, $_, $part, $fileno - 1)
!ll&&!rl&&r
499876
MC/DC100%2/2
$lineno == 1 and $self->_handle_header($f, $_, $part, $fileno - 1)
$lineno == 1 $self->_handle_header($f, $_, $part, $fileno - 1)
Truth table100%3/3
A and B
A= $lineno == 1B= $self->_handle_header($f, $_, $part, $fileno - 1)
ABresult
0X0
100
111
505 # Do nothing, we handled the header.
506 ▶ } elsif ( ( !$part )
Condition88%8/9
not $part and $fileno == 1 and $lineno == 1 and $self->{"header_skip"}
!ll&&!rl&&r
515710
not $part and $fileno == 1 and $lineno == 1
!ll&&!rl&&r
26724871
not $part and $fileno == 1
!ll&&!rl&&r
22641319
MC/DC0%0/4
not $part and $fileno == 1 and $lineno == 1 and $self->{"header_skip"}
not $part $fileno == 1 $lineno == 1 $self->{"header_skip"}
Truth table80%4/5
not(A) and B and C and D
A= not $partB= $fileno == 1C= $lineno == 1D= $self->{"header_skip"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
507 && ( $fileno == 1 )
508 && ( $lineno == 1 )
509 && ( $self->{header_skip} ) )
510 {
511 # Do nothing, we're skipping the header.
512 } else {
513 586 ▶ if ($isgrep) {
Branch: 1 branch
BranchTrueFalseTotal
if ($isgrep) { } 311 275 586
514 311 ▶ if ($extended_info) {
Branch: 1 branch
BranchTrueFalseTotal
if ($extended_info) { } 3 308 311
515 3 ▶ if ( $code->( $_, $extended ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$code($_, $extended)) 2 1 3
516 2 push @filelines, $_;
517 }
518 } else {
519 308 ▶ if ( $code->($_) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$code($_)) 258 50 308
520 258 push @filelines, $_;
521 }
522 }
523 } else {
524 # We are doing a map
525 275 ▶ if ($extended_info) {
Branch: 1 branch
BranchTrueFalseTotal
if ($extended_info) { } 3 272 275
526 3 push @filelines, $code->( $_, $extended );
527 } else {
528 272 push @filelines, $code->($_);
529 }
530 }
531 }
532  
533 # If we're reading multi-parts, do we need to end the read?
534 592 ▶ if ( ( $end > 0 ) && ( tell($fh) > $end ) ) { last; }
Branch: 1 branch
BranchTrueFalseTotal
if ($end > 0 and tell $fh > $end) 27 565 592
Condition100%3/3
$end > 0 and tell $fh > $end
!ll&&!rl&&r
32324227
MC/DC100%2/2
$end > 0 and tell $fh > $end
$end > 0 tell $fh > $end
Truth table100%3/3
A and B
A= $end > 0B= tell $fh > $end
ABresult
0X0
100
111
535 }
536 119 push @mapped_lines, \@filelines;
537  
538 119 close $fh;
539 }
540  
541 98 return \@mapped_lines;
542 }
543  
544 # Internal function to facilitate reading a file in chunks.
545 #
546 # If parts == 1, this basically just opens the file (and returns -1 for
547 # end, to be discussed later)
548 #
549 # If parts > 1, then this divides the file (by byte count) into that
550 # many parts, and then seeks to the first character at the start of a
551 # new line in that part (lines are attributed to the part in which they
552 # end).
553 #
554 # It also returns an end position - no line starting *after* the end
555 # position is in the relevant chunk.
556 #
557 # part_number is zero indexed.
558 #
559 # For part_number >= 1, the first valid character is actually start + 1
560 # If a line actually starts at the first position, we treat it as
561 # part of the previous chunk.
562 #
563 # If no lines would start in a given chunk, this seeks to the end of the
564 # file (so it gives an EOF on the first read)
565 338 ▶sub _open_and_seek {
Subroutine _open_and_seek: called CC=15 SCAR=29.2
566 0 ▶ if ( scalar(@_) != 4 ) { confess 'invalid call' }
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ != 4) 0 338 338
567 338 my ( $self, $file, $parts, $part_number ) = @_;
568  
569 0 ▶ if ( !defined($parts) ) { $parts = 1; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $parts) 0 338 338
570 0 ▶ if ( !defined($part_number) ) { $part_number = 0; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $part_number) 0 338 338
571  
572 338 ▶ if ( $parts <= $part_number ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parts <= $part_number) 0 338 338
573 0 confess("Part Number must be less than number of parts");
574 }
575 338 ▶ if ( $parts <= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parts <= 0) 0 338 338
576 0 confess("Number of parts must be > 0");
577 }
578 338 ▶ if ( $part_number < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($part_number < 0) 0 338 338
579 0 confess("Part Number must be greater or equal to 0");
580 }
581  
582 338 my $fh = $self->_open($file);
583 0 ▶ if ( !defined($fh) ) { return ( $fh, 0 ); }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fh) 0 338 338
584  
585 # If this is a single part request, we are done here.
586 # We use -1, not size, because it's possible the read is from a
587 # terminal or pipe or something else that can grow.
588 338 ▶ if ( $parts == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parts == 0) 0 338 338
589 0 return ( $fh, -1 );
590 }
591  
592 # This is a request for part of a multi-part document. How big is
593 # it?
594 338 seek( $fh, 0, Fcntl::SEEK_END );
595 338 my $size = tell($fh);
596  
597 # Special case - more threads than needed.
598 338 ▶ if ( $parts > $size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parts > $size) 21 317 338
599 21 ▶ if ( $part_number > $size ) { return ( $fh, -1 ) }
Branch: 1 branch
BranchTrueFalseTotal
if ($part_number > $size) 9 12 21
600  
601 # We want each part to be one byte, basically. Not fractiosn of
602 # a byte.
603 12 $parts = $size;
604 }
605  
606 # We have a zero byte file, special case
607 329 ▶ if ( $parts == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parts == 0) 12 317 329
608 12 return ( $fh, -1 );
609 }
610  
611 # Figure out start and end size
612 317 my $start = int( $part_number * ( $size / $parts ) );
613 317 my $end = int( $start + ( $size / $parts ) );
614  
615 # Seek to start position
616 317 seek( $fh, $start, Fcntl::SEEK_SET );
617  
618 # Read and discard junk to the end of line.
619 # But ONLY for parts other than the first one. We basically assume
620 # all parts > 1 are starting mid-line.
621 317 ▶ if ( $part_number > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($part_number > 0) 54 263 317
622 54 scalar(<$fh>);
623 }
624  
625 # Special case - allow file to have grown since first read to end
626 317 ▶ if ( ( $parts - 1 ) == $part_number ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parts - 1 == $part_number) 263 54 317
627 263 return ( $fh, -1 );
628 }
629  
630 # Another special case... If we're already past the end, seek to
631 # the end.
632 54 ▶ if ( tell($fh) > $end ) {
Branch: 1 branch
BranchTrueFalseTotal
if (tell $fh > $end) 2 52 54
633 2 seek( $fh, 0, Fcntl::SEEK_END );
634 }
635  
636 # We return the file at this position.
637 54 return ( $fh, $end );
638 }
639  
640 362 ▶sub _open {
Subroutine _open: called CC=7 SCAR=22.4
641 0 ▶ if ( scalar(@_) != 2 ) { confess 'invalid call'; }
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ != 2) 0 362 362
642 362 my ( $self, $file ) = @_;
643  
644 362 ▶ if ( ( !-r $file ) && $self->{skip_unreadable} ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not -r $file and $self->{'skip_unreadable'}) { } 1 361 362
elsif (not -e _) { } 0 361 361
elsif (not -r _) { } 0 361 361
Condition66%2/3
not -r $file and $self->{"skip_unreadable"}
!ll&&!rl&&r
36101
MC/DC50%1/2
not -r $file and $self->{"skip_unreadable"}
not -r $file $self->{"skip_unreadable"}
Truth table66%2/3
not(A) and B
A= not -r $fileB= $self->{"skip_unreadable"}
ABresult
0X0
100
111
645 1 return; # We don't give an error if skip_unreadable
646 } elsif ( !-e _ ) { # _ is file handle from last stat() call
647 0 confess("File does not exist: $file");
648 } elsif ( !-r _ ) {
649 0 confess("File is unreadable: $file");
650 }
651 361 ▶ open my $fh, '<', $file or die $!;
Branch: 1 branch
BranchTrueFalseTotal
&File::ByLine::Object::open(my $fh, "<", $file) or die $! 361 0 361
652  
653 361 return $fh;
654 }
655  
656 506 ▶sub _require_parallel {
Subroutine _require_parallel: called CC=4 SCAR=15.9
657 0 ▶ if ( scalar(@_) != 1 ) { confess 'invalid call'; }
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ != 1) 0 506 506
658 506 my $self = shift;
659  
660 require Parallel::WorkUnit
661 506 ▶ or die("You must install Parallel::WorkUnit to use the parallel_* methods");
Branch: 1 branch
BranchTrueFalseTotal
require Parallel::WorkUnit or die "You must install Parallel::WorkUnit to use the parallel_* methods" 506 0 506
662  
663 506 ▶ if ( $Parallel::WorkUnit::VERSION < 2.181850 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Parallel::WorkUnit::VERSION < "2.18185") 0 506 506
664 0 die( "Parallel::WorkUnit version 2.181850 or newer required. You have "
665 . $Parallel::WorkUnit::Version );
666 }
667  
668 506 return;
669 }
670  
671 # Validate something is code like
672 #
673 # Borrowed/modified from Params::Util (written by Adam Kennedy)
674 110 ▶sub _codelike {
Subroutine _codelike: called CC=6 SCAR=18.0
675 0 ▶ if ( scalar(@_) != 1 ) { confess 'invalid call' }
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ != 1) 0 110 110
676 110 my $thing = shift;
677  
678 110 ▶ if ( defined( reftype($thing) ) && ( reftype($thing) eq 'CODE' ) ) { return 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined builtin::reftype($thing) and builtin::reftype($thing) eq "CODE") 106 4 110
Condition100%3/3
defined builtin::reftype($thing) and builtin::reftype($thing) eq "CODE"
!ll&&!rl&&r
13106
MC/DC100%2/2
defined builtin::reftype($thing) and builtin::reftype($thing) eq "CODE"
defined builtin::reftype($thing) builtin::reftype($thing) eq "CODE"
Truth table100%3/3
A and B
A= defined builtin::reftype($thing)B= builtin::reftype($thing) eq "CODE"
ABresult
0X0
100
111
679 4 ▶ if ( blessed($thing) && overload::Method( $thing, '&{}' ) ) { return 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($thing) and overload::Method($thing, "&{}")) 1 3 4
Condition100%3/3
builtin::blessed($thing) and overload::Method($thing, "&{}")
!ll&&!rl&&r
211
MC/DC100%2/2
builtin::blessed($thing) and overload::Method($thing, "&{}")
builtin::blessed($thing) overload::Method($thing, "&{}")
Truth table100%3/3
A and B
A= builtin::blessed($thing)B= overload::Method($thing, "&{}")
ABresult
0X0
100
111
680  
681 3 return;
682 }
683  
684 619 ▶sub _listlike {
Subroutine _listlike: called CC=5 SCAR=18.1
685 0 ▶ if ( scalar(@_) != 1 ) { confess 'invalid call' }
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ != 1) 0 619 619
686 619 my $thing = shift;
687  
688 619 ▶ if ( reftype($thing) ) { return 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::reftype($thing)) 150 469 619
689 0 ▶ if ( defined( blessed($thing) ) && overload::Method( $thing, '[]' ) ) { return 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined builtin::blessed($thing) and overload::Method($thing, "[]")) 0 469 469
Condition33%1/3
defined builtin::blessed($thing) and overload::Method($thing, "[]")
!ll&&!rl&&r
46900
MC/DC0%0/2
defined builtin::blessed($thing) and overload::Method($thing, "[]")
defined builtin::blessed($thing) overload::Method($thing, "[]")
Truth table33%1/3
A and B
A= defined builtin::blessed($thing)B= overload::Method($thing, "[]")
ABresult
0X0
100
111
690  
691 469 return;
692 }
693  
694 # Takes a hashref, key, and default value
695 # If the hashref item exists, returns the corresponding value. If the hashref
696 # item does not exist, returns the default value.
697 0 ▶sub _option_helper {
Subroutine _option_helper: not called CC=3 SCAR=24.8
698 0 ▶ if ( scalar(@_) != 3 ) { confess 'invalid call' }
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ != 3) 0 0 0
699 0 my ( $hash, $key, $default ) = @_;
700  
701 0 ▶ if ( exists( $hash->{$key} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $hash->{$key}) { } 0 0 0
702 0 return $hash->{$key};
703 } else {
704 0 return $default;
705 }
706 }
707  
708 705 ▶sub _is_number {
Subroutine _is_number: called CC=3 SCAR=11.2
709 0 ▶ if ( scalar(@_) != 1 ) { confess 'invalid call' }
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ != 1) 0 705 705
710 705 my $val = shift;
711  
712 705 ▶ if ( !defined($val) ) { return; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val) 1 704 705
713  
714 704 return $val =~ /
715 \A # Start of string
716 [0-9]+ # ASCII digit
717 (?: \. 0+)? # Optional .0 or .000 or .00000 etc
718 \z # End of string
719 /sx;
720 }
721  
722 # Returns an extended info object
723 248 ▶sub _extended {
Subroutine _extended: called CC=2 SCAR=7.6
724 0 ▶ if ( scalar(@_) != 3 ) { confess 'invalid call' }
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ != 3) 0 248 248
725 248 my ( $self, $filename, $process_number ) = @_;
726  
727 return {
728 248 filename => $filename,
729 object => $self,
730 process_number => $process_number,
731 };
732 }
733  
734 # Executes the header_handler function when required, or skipps headers.
735 #
736 # This returns TRUE if there is a header to process. FALSE otherwise
737 #
738 # This takes several parameters:
739 # $self - This is an object method of course.
740 # $filename = The filename being processed
741 # $line - The line to process
742 # $part - Which "part" is calling this (we always return FALSE and
743 # refuse to process the header if $part > 0)
744 # $fileno - Which file number are we on (start at zero)
745 #
746 # If header_skip is FALSE and header_handler is unset, this ALWAYS
747 # returns false.
748 #
749 # This should never be called except for the first line of a file
750 301 ▶sub _handle_header {
Subroutine _handle_header: called CC=9 SCAR=22.0
751 0 ▶ if ( scalar(@_) != 5 ) { confess 'invalid call' }
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ != 5) 0 301 301
752 301 my ( $self, $filename, $line, $part, $fileno ) = @_;
753  
754 301 ▶ if ($part) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if ($part) 35 266 301
755  
756 266 ▶ if ( ( !$self->header_skip() ) && ( !defined( $self->header_handler() ) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->header_skip or defined $self->header_handler) 99 167 266
Condition100%3/3
$self->header_skip or defined $self->header_handler
l!l&&r!l&&!r
516299
MC/DC100%2/2
$self->header_skip or defined $self->header_handler
$self->header_skip defined $self->header_handler
Truth table100%3/3
A or B
A= $self->header_skipB= defined $self->header_handler
ABresult
1X1
011
000
757 99 return;
758 }
759  
760 167 ▶ if ( $fileno && ( !$self->header_all_files() ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fileno and not $self->header_all_files) 46 121 167
Condition100%3/3
$fileno and not $self->header_all_files
!ll&&!rl&&r
120146
MC/DC100%2/2
$fileno and not $self->header_all_files
$fileno not $self->header_all_files
Truth table100%3/3
A and not(B)
A= $filenoB= not $self->header_all_files
ABresult
0X0
100
111
761 46 return;
762 }
763  
764 # We have a header to process.
765 121 ▶ if ( defined( $self->header_handler() ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->header_handler) 116 5 121
766 116 local $_ = $line;
767  
768 116 ▶ if ( $self->{extended_info} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'extended_info'}) { } 59 57 116
769 59 my $extended = $self->_extended( $filename, $part );
770 59 $self->{header_handler}( $line, $extended );
771 } else {
772 57 $self->{header_handler}($line);
773 }
774 }
775 121 return 1;
776 }
777  
778  
779 79 1;
780  
781 __END__
↑ Summary ByLine.pm »