blib/lib/File/MergeSort.pm

filter: Statementstmt 97.2% 105 / 108 Branchbran 80.9% 34 / 42 Conditioncond 79.1% 19 / 24 MC/DCmcdc 50.0% 7 / 14 Subroutinesub 100% 13 / 13 Podpod 100% 3 / 3 totaltotal 88.7% 181 / 204 CC 33 SCAR 17.2
CC
33
Cov
91%
new
9.1
next_line
7.0
_open_file
6.3
CRAP 5.6
↑ Summary
1 package File::MergeSort;
2  
3 4 ▶use 5.006; # 5.6.0
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 our $VERSION = '1.23';
11  
12 4 my $have_io_zlib;
13  
14 4 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.0
15 4 eval { require IO::Zlib; };
16 4 ▶ unless ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 4 0 4
17 4 require IO::Zlib;
18 4 $have_io_zlib++;
19 }
20 }
21  
22 ### PRIVATE METHODS
23  
24 15 ▶sub _open_file {
Subroutine _open_file: called CC=6 SCAR=18.5
25 15 my $self = shift;
26 15 ▶ my $file = shift || croak 'No filename specified';
Condition50%1/2
shift() || croak('No filename specified')
l!l
150
MC/DC0%0/1
shift() || croak('No filename specified')
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
27  
28 15 my $fh;
29  
30 15 ▶ if ( $file =~ /[.](z|gz)$/ ) { # Files matching .z or .gz
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /[.](z|gz)$/) { } 5 10 15
31 5 ▶ if ( $have_io_zlib ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($have_io_zlib) { } 5 0 5
32 5 ▶ $fh = IO::Zlib->new( $file, 'rb' ) or croak "Failed to open file $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
$fh = "IO::Zlib"->new($file, "rb") or croak("Failed to open file ${file}: $!") 5 0 5
33 } else {
34 0 croak 'IO::Zlib not available, cannot handle compressed files. Stopping';
35 }
36 } else {
37 10 ▶ $fh = IO::File->new( $file, '<' ) or croak "Failed to open file $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
$fh = "IO::File"->new($file, "<") or croak("Failed to open file ${file}: $!") 9 1 10
38 }
39  
40 14 return $fh;
41 }
42  
43 # Yes, I'm really closing filehandles, just trying to be consistent
44 # with the _open_file counterpart.
45 14 ▶sub _close_file {
Subroutine _close_file: called CC=2 SCAR=7.0
46 14 my $self = shift;
47 14 my $fh = shift;
48  
49 14 ▶ $fh->close() or croak "Problems closing filehandle: $!";
Branch: 1 branch
BranchTrueFalseTotal
$fh->close or croak("Problems closing filehandle: $!") 14 0 14
50  
51 14 return 1;
52 }
53  
54 82 ▶sub _get_line {
Subroutine _get_line: called CC=5 SCAR=16.1
55 82 my $self = shift;
56 82 ▶ my $fh = shift || croak 'No filehandle supplied';
Condition50%1/2
shift() || croak('No filehandle supplied')
l!l
820
MC/DC0%0/1
shift() || croak('No filehandle supplied')
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
57  
58 82 my $line;
59  
60 82 ▶ if ( $self->{'skip_empty'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'skip_empty'}) { } 19 63 82
61 19 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
not defined $line && $line =~ /^$/ or do { do { $line = readline $fh } } 19 4 23
Condition100%3/3
defined $line && $line =~ /^$/
!ll&&!rl&&r
2174
MC/DC100%2/2
defined $line && $line =~ /^$/
defined $line $line =~ /^$/
Truth table100%3/3
A && B
A= defined $lineB= $line =~ /^$/
ABresult
0X0
100
111
62 23 $line = <$fh>;
63 } until ( ! defined $line || $line !~ /^$/ );
64 } else {
65 63 $line = <$fh>;
66 }
67 82 return $line;
68 }
69  
70 # Given a line of input and a code reference that extracts a value
71 # from the line, return an index value that can be used to compare the
72 # lines.
73 68 ▶sub _get_index {
Subroutine _get_index: called CC=3 SCAR=11.6
74 68 my $self = shift;
75 68 ▶ my $line = shift || croak 'No line supplied';
Condition50%1/2
shift() || croak('No line supplied')
l!l
680
MC/DC0%0/1
shift() || croak('No line supplied')
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
76  
77 68 my $code_ref = $self->{'index'};
78 68 my $index = $code_ref->( $line );
79  
80 68 ▶ if ( $index ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index) { } 68 0 68
81 68 return $index;
82 } else {
83 0 croak 'Unable to return an index. Stopping';
84 }
85 }
86  
87 ### PUBLIC METHODS
88  
89 12 ▶sub new {
Subroutine new: called CC=9 SCAR=22.0
Pod documented
90 12 my $class = shift;
91 12 my $files_ref = shift; # ref to array of files.
92 12 my $index_ref = shift; # ref to sub that will extract index value from line
93 12 my $opts_ref = shift; # ref to hash of options, optional.
94  
95 12 ▶ unless ( ref $files_ref eq 'ARRAY' && @{ $files_ref } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $files_ref eq "ARRAY" and @{$files_ref;}) 4 8 12
Condition100%3/3
ref $files_ref eq "ARRAY" and @{$files_ref;}
!ll&&!rl&&r
318
MC/DC100%2/2
ref $files_ref eq "ARRAY" and @{$files_ref;}
ref $files_ref eq "ARRAY" @{$files_ref;}
Truth table100%3/3
A and B
A= ref $files_ref eq "ARRAY"B= @{$files_ref;}
ABresult
0X0
100
111
96 4 croak 'Array reference of input files required';
97 }
98  
99 8 ▶ unless ( ref $index_ref eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $index_ref eq "CODE") 1 7 8
100 1 croak 'Code reference required for merge key extraction';
101 }
102  
103 7 ▶ if ( $opts_ref && ref $opts_ref ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts_ref and ref $opts_ref ne "HASH") 0 7 7
Condition66%2/3
$opts_ref and ref $opts_ref ne "HASH"
!ll&&!rl&&r
610
MC/DC0%0/2
$opts_ref and ref $opts_ref ne "HASH"
$opts_ref ref $opts_ref ne "HASH"
Truth table66%2/3
A and B
A= $opts_refB= ref $opts_ref ne "HASH"
ABresult
0X0
100
111
104 0 croak 'Options should be supplied as a hash reference';
105 }
106  
107 my $self = { index => $index_ref,
108 stack => [],
109 7 ▶ skip_empty => $opts_ref->{'skip_empty_lines'} ? 1 : 0,
Branch: 1 branch
BranchTrueFalseTotal
$opts_ref->{'skip_empty_lines'} ? : 1 6 7
110 };
111  
112 7 bless $self, $class;
113  
114 7 my @files;
115 7 my $i = 0;
116 7 foreach my $file ( @{ $files_ref } ) {
117 15 my $fh = $self->_open_file( $file );
118 14 my $l = $self->_get_line( $fh );
119 14 my $idx = $self->_get_index( $l );
120  
121 14 my $f = { 'fh' => $fh,
122 'line' => $l,
123 'index' => $idx,
124 'pref' => $i++, # preference: take the records from the files in the order specified.
125 };
126  
127 14 push @files, $f;
128 }
129  
130 # Now that the first records are complete for each file, sort them
131 # by merge key then file order. Create a sorted array of hashrefs
132 # based on the index values of each file.
133 6 $self->{'stack'} = [ sort { $a->{'index'} cmp $b->{'index'}
134 9 ▶ || $a->{'pref'} <=> $b->{'pref'}
Condition66%2/3
$File::MergeSort::a->{'index'} cmp $File::MergeSort::b->{'index'} || $File::MergeSort::a->{'pref'} <=> $File::MergeSort::b->{'pref'}
l!l&&r!l&&!r
270
MC/DC0%0/2
$File::MergeSort::a->{'index'} cmp $File::MergeSort::b->{'index'} || $File::MergeSort::a->{'pref'} <=> $File::MergeSort::b->{'pref'}
$File::MergeSort::a->{'index'} cmp $File::MergeSort::b->{'index'} $File::MergeSort::a->{'pref'} <=> $File::MergeSort::b->{'pref'}
Truth table66%2/3
A || B
A= $File::MergeSort::a->{'index'} cmp $File::MergeSort::b->{'index'}B= $File::MergeSort::a->{'pref'} <=> $File::MergeSort::b->{'pref'}
ABresult
1X1
011
000
135 } @files ];
136  
137 6 return $self;
138 }
139  
140 74 ▶sub next_line {
Subroutine next_line: called CC=7 SCAR=19.5
Pod documented
141 74 my $self = shift;
142  
143 74 my $pick = shift @{ $self->{'stack'} };
144 74 ▶ return unless $pick;
Branch: 1 branch
BranchTrueFalseTotal
unless $pick 6 68 74
145  
146 68 my $line = $pick->{'line'};
147  
148 # Re-populate invalidated data in the shifted structure, before
149 # reinserting into stack.
150 68 my $nextline = $self->_get_line( $pick->{'fh'} );
151  
152 68 ▶ if ( $nextline ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nextline) { } 54 14 68
153 54 $pick->{'line'} = $nextline;
154 54 $pick->{'index'} = $self->_get_index( $nextline );
155 } else {
156 # File exhausted, close and return last line, no need to
157 # proceed onto juggling stack.
158 14 $self->_close_file( $pick->{'fh'} );
159 14 return $line;
160 }
161  
162 # Re-organise the 'stack' structure to insert the newly fetched
163 # data into the correct position for the next call. Since it
164 # begins as a sorted array, and we only need to insert one element
165 # in the appropriate position in the array, we can abandon the
166 # loop as soon as we hit the right spot.
167 # There may be room for optimisation here. Algorithms and/or tips
168 # welcome.
169  
170 # Scan the array for the point where:
171  
172 # * The index of the element to insert is the less than than the
173 # element in the array
174  
175 # ...or...
176  
177 # * The index of the element to insert is the same as that in the
178 # array, but the preference of the element to insert is lower -
179 # this is so that data is consistently fed in from the source
180 # files in the order specified in the constuctor.
181  
182 # Previous behaviour can be had with last if $_->{'index'} ge $pick->{'index'};
183 54 my $i = 0;
184  
185 54 foreach ( @{ $self->{'stack'} } ) {
186 56 ▶ if ( $_->{'index'} gt $pick->{'index'}
Branch: 1 branch
BranchTrueFalseTotal
if ($_->{"index"} gt $pick->{"index"} or $_->{"index"} eq $pick->{"index"} and $pick->{"pref"} <= $_->{"pref"}) 27 29 56
Condition100%6/6
$_->{"index"} gt $pick->{"index"} or $_->{"index"} eq $pick->{"index"} and $pick->{"pref"} <= $_->{"pref"}
l!l&&r!l&&!r
151229
$_->{"index"} eq $pick->{"index"} and $pick->{"pref"} <= $_->{"pref"}
!ll&&!rl&&r
161312
MC/DC100%3/3
$_->{"index"} gt $pick->{"index"} or $_->{"index"} eq $pick->{"index"} and $pick->{"pref"} <= $_->{"pref"}
$_->{"index"} gt $pick->{"index"} $_->{"index"} eq $pick->{"index"} $pick->{"pref"} <= $_->{"pref"}
Truth table100%4/4
A or B and C
A= $_->{"index"} gt $pick->{"index"}B= $_->{"index"} eq $pick->{"index"}C= $pick->{"pref"} <= $_->{"pref"}
ABCresult
1XX1
0111
00X0
0100
187 || ( $_->{'index'} eq $pick->{'index'} && $pick->{'pref'} <= $_->{'pref'} )
188 ) {
189 27 last;
190 }
191 29 $i++;
192 }
193  
194 # And stuff the fresh data in the appropriate place.
195 54 splice @{ $self->{'stack'} }, $i, 0, $pick;
196  
197 54 return $line;
198 }
199  
200 # Dump the contents of the file to either STDOUT (default).
201 4 ▶sub dump {
Subroutine dump: called CC=6 SCAR=18.0
Pod documented
202 4 my $self = shift;
203 4 my $file = shift; # optional
204  
205 4 my $lines = 0;
206  
207 4 ▶ if ( $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file) { } 2 2 4
208 2 ▶ open my $fh, '>', $file or croak "Unable to create output file $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $file or croak("Unable to create output file ${file}: $!") 2 0 2
209  
210 2 ▶ while ( my $line = $self->next_line() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = $self->next_line) 18 2 20
211 18 print $fh $line;
212 18 $lines++;
213 }
214  
215 2 ▶ close $fh or croak "Problems closing output file $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or croak("Problems closing output file ${file}: $!") 2 0 2
216 } else {
217 2 ▶ while ( my $line = $self->next_line() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = $self->next_line) 23 2 25
218 23 print $line;
219 23 $lines++;
220 }
221 }
222  
223 4 return $lines;
224 }
225  
226 4 1;
227  
228 __END__
↑ Summary