blib/lib/File/Sort.pm

filter: Statementstmt 67.8% 203 / 299 Branchbran 46.4% 106 / 228 Conditioncond 27.1% 16 / 59 MC/DCmcdc 3.1% 1 / 32 Subroutinesub 82.6% 19 / 23 Podpod 0.0% 0 / 2 totaltotal 53.6% 345 / 643 CC 146 SCAR 49.2
CC
146
Cov
55%
_sort_file
233.2
_make_sort_sub
200.1
sort_file
123.0
CRAP 137.1
↑ Summary
1 package File::Sort;
2 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Fcntl qw(O_RDONLY O_WRONLY O_CREAT O_TRUNC);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Symbol qw(gensym);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use locale;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use vars qw($VERSION *sortsub *sort1 *sort2 *map1 *map2 %fh);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 require Exporter;
10 1 ▶use vars qw(@ISA @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 @ISA = 'Exporter';
12 1 @EXPORT_OK = 'sort_file';
13 1 $VERSION = '1.01';
14  
15 7 ▶sub sort_file {
Subroutine sort_file: called CC=13 SCAR=48.1
Pod undocumented
16 7 my @args = @_;
17 7 ▶ if (ref $args[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args[0]) { } 7 0 7
18  
19 # fix pos to look like k
20 7 ▶ if (exists $args[0]{'pos'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args[0]{"pos"}) 0 7 7
21 0 my @argv;
22 0 my $pos = $args[0]{'pos'};
23  
24 0 ▶ if (!ref $pos) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $pos) 0 0 0
25 0 $pos = [$pos];
26 }
27  
28 0 ▶ if (!exists $args[0]{'k'}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not exists $args[0]{'k'}) { } 0 0 0
elsif (not ref $args[0]{'k'}) { } 0 0 0
29 0 $args[0]{'k'} = [];
30 } elsif (!ref $args[0]{'k'}) {
31 0 $args[0]{'k'} = [$args[0]{'k'}];
32 }
33  
34 0 for (@$pos) {
35 0 my $n;
36 0 ▶ if ( /^\+(\d+)(?:\.(\d+))?([bdfinr]+)?
Branch: 1 branch
BranchTrueFalseTotal
if (/^\+(\d+)(?:\.(\d+))?([bdfinr]+)? (?:\s+\-(\d+)(?:\.(\d+))?([bdfinr]+)?)?$/lx) 0 0 0
37 (?:\s+\-(\d+)(?:\.(\d+))?([bdfinr]+)?)?$/x) {
38 0 $n = $1 + 1;
39 0 ▶ $n .= '.' . ($2 + 1) if defined $2;
Branch: 1 branch
BranchTrueFalseTotal
if defined $2 0 0 0
40 0 ▶ $n .= $3 if $3;
Branch: 1 branch
BranchTrueFalseTotal
if $3 0 0 0
41  
42 0 ▶ if (defined $4) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $4) 0 0 0
43 0 ▶ $n .= "," . (defined $5 ? ($4 + 1) . ".$5" : $4);
Branch: 1 branch
BranchTrueFalseTotal
defined $5 ? : 0 0 0
44 0 ▶ $n .= $6 if $6;
Branch: 1 branch
BranchTrueFalseTotal
if $6 0 0 0
45 }
46 0 push @{$args[0]{'k'}}, $n;
47 }
48 }
49  
50 }
51 7 _sort_file(@args);
52 } else {
53 0 _sort_file({I => $args[0], o => $args[1]});
54 }
55 }
56  
57 7 ▶sub _sort_file {
Subroutine _sort_file: called CC=42 SCAR=54.5
58 7 local $\; # don't mess up our prints
59 7 my($opts, @fh, @recs) = shift;
60  
61 # record separator, default to \n
62 7 ▶ local $/ = $opts->{R} ? $opts->{R} : "\n";
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'R'} ? : 0 7 7
63  
64 # get input files into anon array if not already
65 7 ▶ $opts->{I} = [$opts->{I}] unless ref $opts->{I};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $opts->{"I"} 6 1 7
66  
67 7 ▶ usage() unless @{$opts->{I}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$opts->{"I"};} 0 7 7
68  
69 # "K" == "no k", for later
70 7 ▶ $opts->{K} = $opts->{k} ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'k'} ? : 2 5 7
71 7 ▶ $opts->{k} = $opts->{k} ? [$opts->{k}] : [] if !ref $opts->{k};
Branch: 2 branches
BranchTrueFalseTotal
unless ref $opts->{"k"} 7 0 7
$opts->{'k'} ? : 2 5 7
72  
73 # set output and other defaults
74 7 ▶ $opts->{o} = !$opts->{o} ? '' : $opts->{o};
Branch: 1 branch
BranchTrueFalseTotal
!$opts->{'o'} ? : 0 7 7
75 7 ▶ $opts->{'y'} ||= $ENV{MAX_SORT_RECORDS} || 200000; # default max records
Condition60%3/5
$opts->{"y"} ||= $ENV{"MAX_SORT_RECORDS"} || 200000
l!l&&r!l&&!r
250
$ENV{'MAX_SORT_RECORDS'} || 200000
l!l
05
MC/DC0%0/2
$opts->{"y"} ||= $ENV{"MAX_SORT_RECORDS"} || 200000
$opts->{"y"} $ENV{'MAX_SORT_RECORDS'}
Truth table33%1/3
A ||= B
A= $opts->{"y"}B= $ENV{'MAX_SORT_RECORDS'}
ABresult
1X1
011
000
76 7 ▶ $opts->{F} ||= $ENV{MAX_SORT_FILES} || 40; # default max files
Condition40%2/5
$opts->{"F"} ||= $ENV{"MAX_SORT_FILES"} || 40
l!l&&r!l&&!r
070
$ENV{'MAX_SORT_FILES'} || 40
l!l
07
MC/DC0%0/2
$opts->{"F"} ||= $ENV{"MAX_SORT_FILES"} || 40
$opts->{"F"} $ENV{'MAX_SORT_FILES'}
Truth table0%0/3
A ||= B
A= $opts->{"F"}B= $ENV{'MAX_SORT_FILES'}
ABresult
1X1
011
000
77  
78  
79 # see big ol' mess below
80 7 _make_sort_sub($opts);
81  
82 # only check to see if file is sorted
83 7 ▶ if ($opts->{c}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opts->{'c'}) { } 0 7 7
elsif ($opts->{'m'}) { } 0 7 7
84 0 local *F;
85 0 my $last;
86  
87 0 ▶ if ($opts->{I}[0] eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'I'}[0] eq '-') { } 0 0 0
88 0 ▶ open(F, $opts->{I}[0])
Branch: 1 branch
BranchTrueFalseTotal
open F, $opts->{"I"}[0] or die qq[Can't open `$opts->{"I"}[0]' for reading: $!] 0 0 0
89 or die "Can't open `$opts->{I}[0]' for reading: $!";
90 } else {
91 0 ▶ sysopen(F, $opts->{I}[0], O_RDONLY)
Branch: 1 branch
BranchTrueFalseTotal
sysopen F, $opts->{"I"}[0], 0 or die qq[Can't open `$opts->{"I"}[0]' for reading: $!] 0 0 0
92 or die "Can't open `$opts->{I}[0]' for reading: $!";
93 }
94  
95 0 ▶ while (defined(my $rec = <F>)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $rec = readline F)) 0 0 0
96 # fail if -u and keys are not unique (assume sorted)
97 0 ▶ if ($opts->{u} && $last) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"u"} and $last) 0 0 0
Condition0%0/3
$opts->{"u"} and $last
!ll&&!rl&&r
000
MC/DC0%0/2
$opts->{"u"} and $last
$opts->{"u"} $last
Truth table0%0/3
A and B
A= $opts->{"u"}B= $last
ABresult
0X0
100
111
98 0 ▶ return 0 unless _are_uniq($opts->{K}, $last, $rec);
Branch: 1 branch
BranchTrueFalseTotal
unless _are_uniq($opts->{"K"}, $last, $rec) 0 0 0
99 }
100  
101 # fail if records not in proper sort order
102 0 ▶ if ($last) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last) 0 0 0
103 0 my @foo;
104 0 ▶ if ($opts->{K}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'K'}) { } 0 0 0
105 0 local $^W;
106 0 @foo = sort sort1 ($rec, $last);
107 } else {
108 0 local $^W;
109 0 @foo = map {$_->[0]} sort sortsub
110 map &map1, ($rec, $last);
111 }
112 0 ▶ return 0 if $foo[0] ne $last || $foo[1] ne $rec;
Branch: 1 branch
BranchTrueFalseTotal
if $foo[0] ne $last or $foo[1] ne $rec 0 0 0
Condition0%0/3
$foo[0] ne $last or $foo[1] ne $rec
l!l&&r!l&&!r
000
MC/DC0%0/2
$foo[0] ne $last or $foo[1] ne $rec
$foo[0] ne $last $foo[1] ne $rec
Truth table0%0/3
A or B
A= $foo[0] ne $lastB= $foo[1] ne $rec
ABresult
1X1
011
000
113 }
114  
115 # save value of last record
116 0 $last = $rec;
117 }
118  
119 # success, yay
120 0 return 1;
121  
122 # if merging sorted files
123 } elsif ($opts->{'m'}) {
124  
125 0 foreach my $filein (@{$opts->{I}}) {
126  
127 # just open files and get array of handles
128 0 my $sym = gensym();
129  
130 0 ▶ sysopen($sym, $filein, O_RDONLY)
Branch: 1 branch
BranchTrueFalseTotal
sysopen $sym, $filein, 0 or die "Can't open `${filein}' for reading: $!" 0 0 0
131 or die "Can't open `$filein' for reading: $!";
132  
133 0 push @fh, $sym;
134 }
135
136 # ooo, get ready, get ready
137 } else {
138  
139 # once for each input file
140 7 foreach my $filein (@{$opts->{I}}) {
141 8 local *F;
142 8 my $count = 0;
143  
144 8 ▶ _debug("Sorting file $filein ...\n") if $opts->{D};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"D"} 0 8 8
145  
146 8 ▶ if ($filein eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($filein eq '-') { } 0 8 8
147 0 ▶ open(F, $filein)
Branch: 1 branch
BranchTrueFalseTotal
open F, $filein or die "Can't open `${filein}' for reading: $!" 0 0 0
148 or die "Can't open `$filein' for reading: $!";
149 } else {
150 8 ▶ sysopen(F, $filein, O_RDONLY)
Branch: 1 branch
BranchTrueFalseTotal
sysopen F, $filein, 0 or die "Can't open `${filein}' for reading: $!" 8 0 8
151 or die "Can't open `$filein' for reading: $!";
152 }
153  
154 8 ▶ while (defined(my $rec = <F>)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $rec = readline F)) 4724 8 4732
155 4724 push @recs, $rec;
156 4724 $count++; # keep track of number of records
157  
158 4724 ▶ if ($count >= $opts->{'y'}) { # don't go over record limit
Branch: 1 branch
BranchTrueFalseTotal
if ($count >= $opts->{"y"}) 100 4624 4724
159  
160 _debug("$count records reached in `$filein'\n")
161 100 ▶ if $opts->{D};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"D"} 0 100 100
162  
163 # save to temp file, add new fh to array
164 100 push @fh, _write_temp(\@recs, $opts);
165  
166 # reset record count and record array
167 100 ($count, @recs) = (0);
168  
169 # do a merge now if at file limit
170 100 ▶ if (@fh >= $opts->{F}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fh >= $opts->{"F"}) 2 98 100
171  
172 # get filehandle and restart array with it
173 2 @fh = (_merge_files($opts, \@fh, [], _get_temp()));
174  
175 2 ▶ _debug("\nCreating temp files ...\n") if $opts->{D};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"D"} 0 2 2
176 }
177 }
178 }
179  
180 8 close F;
181 }
182  
183 # records leftover, didn't reach record limit
184 7 ▶ if (@recs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@recs) 5 2 7
185 5 ▶ _debug("\nSorting leftover records ...\n") if $opts->{D};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"D"} 0 5 5
186 5 _check_last(\@recs);
187 5 ▶ if ($opts->{K}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'K'}) { } 3 2 5
188 3 local $^W;
189 3 @recs = sort sort1 @recs;
190 } else {
191 2 local $^W;
192 2 @recs = map {$_->[0]} sort sortsub map &map1, @recs;
193 }
194 }
195 }
196  
197 # do the merge thang, uh huh, do the merge thang
198 7 my $close = _merge_files($opts, \@fh, \@recs, $opts->{o});
199 7 ▶ close $close unless fileno($close) == fileno('STDOUT'); # don't close STDOUT
Branch: 1 branch
BranchTrueFalseTotal
unless fileno $close == fileno STDOUT 7 0 7
200  
201 7 ▶ _debug("\nDone!\n\n") if $opts->{D};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"D"} 0 7 7
202 7 return 1; # yay
203 }
204  
205 # take optional arrayref of handles of sorted files,
206 # plus optional arrayref of sorted scalars
207 9 ▶sub _merge_files {
Subroutine _merge_files: called CC=16 SCAR=30.0
208 # we need the options, filehandles, and output file
209 9 my($opts, $fh, $recs, $file) = @_;
210 9 my($uniq, $first, $o, %oth);
211  
212 # arbitrarily named keys, store handles as values
213 9 %oth = map {($o++ => $_)} @$fh;
214  
215 # match handle key in %oth to next record of the handle
216 %fh = map {
217 9 my $fh = $oth{$_};
218 102 ($_ => scalar <$fh>);
219 } keys %oth;
220  
221 # extra records, special X "handle"
222 9 ▶ $fh{X} = shift @$recs if @$recs;
Branch: 1 branch
BranchTrueFalseTotal
if @$recs 5 4 9
223  
224 9 ▶ _debug("\nCreating sorted $file ...\n") if $opts->{D};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"D"} 0 9 9
225  
226 # output to STDOUT if no output file provided
227 9 ▶ if ($file eq '') {
Branch: 2 branches
BranchTrueFalseTotal
if ($file eq '') { } 0 9 9
elsif (not ref $file) { } 7 2 9
228 0 $file = \*STDOUT;
229  
230 # if output file is a path, not a reference to a file, open
231 # file and get a reference to it
232 } elsif (!ref $file) {
233 7 my $tfh = gensym();
234 7 ▶ sysopen($tfh, $file, O_WRONLY|O_CREAT|O_TRUNC)
Branch: 1 branch
BranchTrueFalseTotal
sysopen $tfh, $file, 577 or die "Can't open `${file}' for writing: $!" 7 0 7
235 or die "Can't open `$file' for writing: $!";
236 7 $file = $tfh;
237 }
238  
239 9 my $oldfh = select $file;
240 9 $| = 0; # just in case, use the buffer, you knob
241  
242 9 ▶ while (keys %fh) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %File::Sort::fh) 301 0 301
243 # don't bother sorting keys if only one key remains!
244 301 ▶ if (!$opts->{u} && keys %fh == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $opts->{"u"} and keys %File::Sort::fh == 1) 9 292 301
Condition66%2/3
not $opts->{"u"} and keys %File::Sort::fh == 1
!ll&&!rl&&r
02929
MC/DC50%1/2
not $opts->{"u"} and keys %File::Sort::fh == 1
not $opts->{"u"} keys %File::Sort::fh == 1
Truth table66%2/3
not(A) and B
A= not $opts->{"u"}B= keys %File::Sort::fh == 1
ABresult
0X0
100
111
245 9 ($first) = keys %fh;
246 9 my $curr = $oth{$first};
247 9 ▶ my @left = $first eq 'X' ? @$recs : <$curr>;
Branch: 1 branch
BranchTrueFalseTotal
$first eq 'X' ? : 5 4 9
248 9 print $fh{$first}, @left;
249 9 delete $fh{$first};
250 9 last;
251 }
252  
253 {
254 # $first is arbitrary number assigned to first fh in sort
255 292 ▶ if ($opts->{K}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'K'}) { } 292 0 292
256 292 local $^W;
257 292 ($first) = (sort sort2 keys %fh);
258 } else {
259 0 local $^W;
260 0 ($first) = (map {$_->[0]} sort sortsub
261 map &map2, keys %fh);
262 }
263 }
264  
265 # don't print if -u and not unique
266 292 ▶ if ($opts->{u}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'u'}) { } 0 292 292
267 print $fh{$first} if
268 0 ▶ (!$uniq || _are_uniq($opts->{K}, $uniq, $fh{$first}));
Branch: 1 branch
BranchTrueFalseTotal
if not $uniq or _are_uniq($opts->{"K"}, $uniq, $File::Sort::fh{$first}) 0 0 0
Condition0%0/3
not $uniq or _are_uniq($opts->{"K"}, $uniq, $File::Sort::fh{$first})
l!l&&r!l&&!r
000
MC/DC0%0/2
not $uniq or _are_uniq($opts->{"K"}, $uniq, $File::Sort::fh{$first})
not $uniq _are_uniq($opts->{"K"}, $uniq, $File::Sort::fh{$first})
Truth table0%0/3
not(A) or B
A= not $uniqB= _are_uniq($opts->{"K"}, $uniq, $File::Sort::fh{$first})
ABresult
1X1
011
000
269 0 $uniq = $fh{$first};
270 } else {
271 292 print $fh{$first};
272 }
273  
274 # get current filehandle
275 292 my $curr = $oth{$first};
276  
277 # use @$recs, not filehandles, if key is X
278 292 ▶ my $rec = $first eq 'X' ? shift @$recs : scalar <$curr>;
Branch: 1 branch
BranchTrueFalseTotal
$first eq 'X' ? : 0 292 292
279  
280 292 ▶ if (defined $rec) { # bring up next record for this filehandle
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rec) { } 194 98 292
281 194 $fh{$first} = $rec;
282  
283 } else { # we don't need you anymore
284 98 delete $fh{$first};
285 }
286 }
287  
288 9 seek $file, 0, 0; # might need to read back from it
289 9 select $oldfh;
290 9 return $file;
291 }
292  
293 105 ▶sub _check_last {
Subroutine _check_last: called CC=2 SCAR=6.9
294 # add new record separator if not one there
295 105 ▶ ${$_[0]}[-1] .= $/ if (${$_[0]}[-1] !~ m|$/$|);
Branch: 1 branch
BranchTrueFalseTotal
if not do { $_[0] }->[-1] =~ m[$/$]l 4 101 105
296 }
297  
298 100 ▶sub _write_temp {
Subroutine _write_temp: called CC=4 SCAR=15.4
299 100 my($recs, $opts) = @_;
300 100 ▶ my $temp = _get_temp() or die "Can't get temp file: $!";
Branch: 1 branch
BranchTrueFalseTotal
my $temp = _get_temp() or die "Can't get temp file: $!" 100 0 100
301  
302 100 _check_last($recs);
303  
304 100 ▶ _debug("New tempfile: $temp\n") if $opts->{D};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"D"} 0 100 100
305  
306 100 ▶ if ($opts->{K}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'K'}) { } 100 0 100
307 100 local $^W;
308 100 print $temp sort sort1 @{$recs};
309 } else {
310 0 local $^W;
311 0 print $temp map {$_->[0]} sort sortsub map &map1, @{$recs};
312 }
313  
314 100 seek $temp, 0, 0; # might need to read back from it
315 100 return $temp;
316 }
317  
318 2 ▶sub _parse_keydef {
Subroutine _parse_keydef: called CC=19 SCAR=40.7
319 2 my($k, $topts) = @_;
320  
321 # gurgle
322 2 $k =~ /^(\d+)(?:\.(\d+))?([bdfinr]+)?
323 (?:,(\d+)(?:\.(\d+))?([bdfinr]+)?)?$/x;
324  
325 # set defaults at zero or undef
326 2 ▶ my %opts = (
Branch: 1 branch
BranchTrueFalseTotal
defined $4 ? : 0 2 2
Condition50%5/10
$1 || 0
l!l
20
$2 || 0
l!l
02
$3 || ''
l!l
02
$5 || 0
l!l
02
$6 || ''
l!l
02
MC/DC0%0/1
$1 || 0
$1
MC/DC0%0/1
$2 || 0
$2
MC/DC0%0/1
$3 || ''
$3
MC/DC0%0/1
$5 || 0
$5
MC/DC0%0/1
$6 || ''
$6
Truth table50%1/2
A
A= $1
Aresult
11
00
Truth table50%1/2
A
A= $2
Aresult
11
00
Truth table50%1/2
A
A= $3
Aresult
11
00
Truth table50%1/2
A
A= $5
Aresult
11
00
Truth table50%1/2
A
A= $6
Aresult
11
00
327 %$topts, # get other options
328 ksf => $1 || 0, # start field
329 ksc => $2 || 0, # start field char start
330 kst => $3 || '', # start field type
331 kff => (defined $4 ? $4 : undef), # end field
332 kfc => $5 || 0, # end field char end
333 kft => $6 || '', # end field type
334 );
335  
336 # their idea of 1 is not ours
337 2 for (qw(ksf ksc kff)) { # kfc stays same
338 6 ▶ $opts{$_}-- if $opts{$_};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{$_} 2 4 6
339 }
340  
341 # if nothing in kst or kft, use other flags possibly passed
342 2 ▶ if (!$opts{kst} && !$opts{kft}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $opts{'kst'} || $opts{'kft'}) { } 2 0 2
Condition33%1/3
$opts{'kst'} || $opts{'kft'}
l!l&&r!l&&!r
002
MC/DC0%0/2
$opts{'kst'} || $opts{'kft'}
$opts{'kst'} $opts{'kft'}
Truth table33%1/3
A || B
A= $opts{'kst'}B= $opts{'kft'}
ABresult
1X1
011
000
343 2 foreach (qw(b d f i n r)) {
344 12 ▶ $opts{kst} .= $_ if $topts->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if $topts->{$_} 3 9 12
345 12 ▶ $opts{kft} .= $_ if $topts->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if $topts->{$_} 3 9 12
346 }
347  
348 # except for b, flags on one apply to the other
349 } else {
350 0 foreach (qw(d f i n r)) {
351 0 ▶ $opts{kst} .= $_ if ($opts{kst} =~ /$_/ || $opts{kft} =~ /$_/);
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"kst"} =~ /$_/l or $opts{"kft"} =~ /$_/l 0 0 0
Condition0%0/3
$opts{"kst"} =~ /$_/l or $opts{"kft"} =~ /$_/l
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{"kst"} =~ /$_/l or $opts{"kft"} =~ /$_/l
$opts{"kst"} =~ /$_/l $opts{"kft"} =~ /$_/l
Truth table0%0/3
A or B
A= $opts{"kst"} =~ /$_/lB= $opts{"kft"} =~ /$_/l
ABresult
1X1
011
000
352 0 ▶ $opts{kft} .= $_ if ($opts{kst} =~ /$_/ || $opts{kft} =~ /$_/);
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"kst"} =~ /$_/l or $opts{"kft"} =~ /$_/l 0 0 0
Condition0%0/3
$opts{"kst"} =~ /$_/l or $opts{"kft"} =~ /$_/l
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{"kst"} =~ /$_/l or $opts{"kft"} =~ /$_/l
$opts{"kst"} =~ /$_/l $opts{"kft"} =~ /$_/l
Truth table0%0/3
A or B
A= $opts{"kst"} =~ /$_/lB= $opts{"kft"} =~ /$_/l
ABresult
1X1
011
000
353 }
354 }
355  
356 2 return \%opts;
357 }
358  
359 7 ▶sub _make_sort_sub {
Subroutine _make_sort_sub: called CC=50 SCAR=53.0
360 7 my($topts, @sortsub, @mapsub, @sort1, @sort2) = shift;
361  
362 # if no keydefs set
363 7 ▶ if ($topts->{K}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($topts->{"K"}) 5 2 7
364 5 $topts->{kst} = '';
365 5 foreach (qw(b d f i n r)) {
366 30 ▶ $topts->{kst} .= $_ if $topts->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if $topts->{$_} 4 26 30
367 }
368  
369 # more complex stuff, act like we had -k defined
370 5 ▶ if ($topts->{kst} =~ /[bdfi]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($topts->{"kst"} =~ /[bdfi]/l) 0 5 5
371 0 $topts->{K} = 0;
372 0 $topts->{k} = ['K']; # special K ;-)
373 }
374 }
375  
376 # if no keydefs set
377 7 ▶ if ($topts->{K}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($topts->{'K'}) { } 5 2 7
378 5 ▶ _debug("No keydef set\n") if $topts->{D};
Branch: 1 branch
BranchTrueFalseTotal
if $topts->{"D"} 0 5 5
379  
380 # defaults for main sort sub components
381 5 my($cmp, $aa, $bb, $fa, $fb) = qw(cmp $a $b $fh{$a} $fh{$b});
382  
383 # reverse sense
384 5 ▶ ($bb, $aa, $fb, $fa) = ($aa, $bb, $fa, $fb) if $topts->{r};
Branch: 1 branch
BranchTrueFalseTotal
if $topts->{"r"} 2 3 5
385  
386 # do numeric sort
387 5 ▶ $cmp = '<=>' if $topts->{n};
Branch: 1 branch
BranchTrueFalseTotal
if $topts->{"n"} 2 3 5
388  
389 # add finished expression to array
390 5 my $sort1 = "sub { $aa $cmp $bb }\n";
391 5 my $sort2 = "sub { $fa $cmp $fb }\n";
392  
393 5 ▶ _debug("$sort1\n$sort2\n") if $topts->{D};
Branch: 1 branch
BranchTrueFalseTotal
if $topts->{"D"} 0 5 5
394  
395 {
396 5 local $^W;
397 5 ▶ *sort1 = eval $sort1;
Subroutine __ANON__: called CC=1 SCAR=0.0
398 5 ▶ die "Can't create sort sub: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 5 5
399 5 ▶ *sort2 = eval $sort2;
Subroutine __ANON__: called CC=1 SCAR=0.0
400 5 ▶ die "Can't create sort sub: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 5 5
401 }
402  
403 } else {
404  
405 # get text separator or use whitespace
406 $topts->{t} =
407 defined $topts->{X} ? $topts->{X} :
408 2 ▶ defined $topts->{t} ? quotemeta($topts->{t}) :
Branch: 2 branches
BranchTrueFalseTotal
defined $topts->{'X'} ? : 0 2 2
defined $topts->{'t'} ? : 2 0 2
409 '\s+';
410 2 ▶ $topts->{t} =~ s|/|\\/|g if defined $topts->{X};
Branch: 1 branch
BranchTrueFalseTotal
if defined $topts->{"X"} 0 2 2
411  
412 2 foreach my $k (@{$topts->{k}}) {
413 2 my($opts, @fil) = ($topts);
414
415 # defaults for main sort sub components
416 2 my($cmp, $ab_, $fab_, $aa, $bb) = qw(cmp $_ $fh{$_} $a $b);
417  
418 # skip stuff if special K
419 2 ▶ $opts = $k eq 'K' ? $topts : _parse_keydef($k, $topts);
Branch: 1 branch
BranchTrueFalseTotal
$k eq 'K' ? : 0 2 2
420  
421 2 ▶ if ($k ne 'K') {
Branch: 1 branch
BranchTrueFalseTotal
if ($k ne 'K') { } 2 0 2
422 my($tmp1, $tmp2) = ("\$tmp[$opts->{ksf}]",
423 2 ▶ ($opts->{kff} ? "\$tmp[$opts->{kff}]" : ''));
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'kff'} ? : 0 2 2
424  
425 # skip leading spaces
426 2 ▶ if ($opts->{kst} =~ /b/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"kst"} =~ /b/l) 0 2 2
427 0 $tmp1 = "($tmp1 =~ /(\\S.*)/)[0]";
428 }
429  
430 2 ▶ if ($opts->{kft} =~ /b/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"kft"} =~ /b/l) 0 2 2
431 0 $tmp2 = "($tmp2 =~ /(\\S.*)/)[0]";
432 }
433  
434 # simpler if one field, goody for us
435 2 ▶ if (! defined $opts->{kff} || $opts->{ksf} == $opts->{kff}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $opts->{'kff'} or $opts->{'ksf'} == $opts->{'kff'}) { } 2 0 2
Condition33%1/3
not defined $opts->{"kff"} or $opts->{"ksf"} == $opts->{"kff"}
l!l&&r!l&&!r
200
MC/DC0%0/2
not defined $opts->{"kff"} or $opts->{"ksf"} == $opts->{"kff"}
not defined $opts->{"kff"} $opts->{"ksf"} == $opts->{"kff"}
Truth table33%1/3
not(A) or B
A= not defined $opts->{"kff"}B= $opts->{"ksf"} == $opts->{"kff"}
ABresult
1X1
011
000
436  
437 # simpler if chars are both 0, wicked pissah
438 2 ▶ if ($opts->{ksc} == 0 &&
Branch: 2 branches
BranchTrueFalseTotal
if ($opts->{'ksc'} == 0 and !$opts->{'kfc'} || $opts->{'kfc'} == 0) { } 2 0 2
elsif (not $opts->{'kfc'}) { } 0 0 0
Condition33%2/6
$opts->{"ksc"} == 0 and !$opts->{"kfc"} || $opts->{"kfc"} == 0
!ll&&!rl&&r
002
not $opts->{"kfc"} or $opts->{"kfc"} == 0
l!l&&r!l&&!r
200
MC/DC0%0/3
$opts->{"ksc"} == 0 and !$opts->{"kfc"} || $opts->{"kfc"} == 0
$opts->{"ksc"} == 0 not $opts->{"kfc"} $opts->{"kfc"} == 0
Truth table25%1/4
A and not(B) or C
A= $opts->{"ksc"} == 0B= not $opts->{"kfc"}C= $opts->{"kfc"} == 0
ABCresult
0XX0
1000
11X1
1011
439 (!$opts->{kfc} || $opts->{kfc} == 0)) {
440 2 @fil = "\$tmp[$opts->{ksf}]";
441  
442 # hmmmmm
443 } elsif (!$opts->{kfc}) {
444 0 @fil = "substr($tmp1, $opts->{ksc})";
445  
446 # getting out of hand now
447 } else {
448 @fil = "substr($tmp1, $opts->{ksc}, ".
449 0 ($opts->{kfc} - $opts->{ksc}) . ')';
450 }
451  
452 # try again, shall we?
453 } else {
454  
455 # if spans two fields, but chars are both 0
456 # and neither has -b, alrighty
457 0 ▶ if ($opts->{kfc} == 0 && $opts->{ksc} == 0 &&
Branch: 2 branches
BranchTrueFalseTotal
if ($opts->{'kfc'} == 0 and $opts->{'ksc'} == 0 and not $opts->{'kst'} =~ /b/l and not $opts->{'kft'} =~ /b/l) { } 0 0 0
elsif ($opts->{'kff'} - $opts->{'ksf'} == 1) { } 0 0 0
Condition0%0/9
$opts->{"kfc"} == 0 and $opts->{"ksc"} == 0 and not $opts->{"kst"} =~ /b/l and not $opts->{"kft"} =~ /b/l
!ll&&!rl&&r
000
$opts->{"kfc"} == 0 and $opts->{"ksc"} == 0 and not $opts->{"kst"} =~ /b/l
!ll&&!rl&&r
000
$opts->{"kfc"} == 0 and $opts->{"ksc"} == 0
!ll&&!rl&&r
000
MC/DC0%0/4
$opts->{"kfc"} == 0 and $opts->{"ksc"} == 0 and not $opts->{"kst"} =~ /b/l and not $opts->{"kft"} =~ /b/l
$opts->{"kfc"} == 0 $opts->{"ksc"} == 0 not $opts->{"kst"} =~ /b/l not $opts->{"kft"} =~ /b/l
Truth table0%0/5
A and B and not(C) and not(D)
A= $opts->{"kfc"} == 0B= $opts->{"ksc"} == 0C= not $opts->{"kst"} =~ /b/lD= not $opts->{"kft"} =~ /b/l
ABCDresult
0XXX0
10XX0
110X0
11100
11111
458 $opts->{kst} !~ /b/ && $opts->{kft} !~ /b/) {
459 0 @fil = "join(''," .
460 "\@tmp[$opts->{ksf} .. $opts->{kff}])";
461  
462 # if only one field away
463 } elsif (($opts->{kff} - $opts->{ksf}) == 1) {
464 0 @fil = "join('', substr($tmp1, $opts->{ksc}), " .
465 "substr($tmp2, 0, $opts->{kfc}))";
466  
467 # fine, have it your way! hurt me! love me!
468 } else {
469 @fil = "join('', substr($tmp1, $opts->{ksc}), " .
470 "\@tmp[" . ($opts->{ksf} + 1) . " .. " .
471 0 ($opts->{kff} - 1) . "], " .
472 "substr($tmp2, 0, $opts->{kfc}))";
473 }
474 }
475 } else {
476 0 ▶ @fil = $opts->{kst} =~ /b/ ?
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'kst'} =~ /b/l ? : 0 0 0
477 "(\$tmp[0] =~ /(\\S.*)/)[0]" : "\$tmp[0]";
478 }
479  
480 # fold to upper case
481 2 ▶ if ($opts->{kst} =~ /f/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"kst"} =~ /f/l) 0 2 2
482 0 $fil[0] = "uc($fil[0])";
483 }
484  
485 # only alphanumerics and whitespace, override -i
486 2 ▶ if ($opts->{kst} =~ /d/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opts->{'kst'} =~ /d/l) { } 0 2 2
elsif ($opts->{'kst'} =~ /i/l) { } 0 2 2
487 0 $topts->{DD}++;
488 0 push @fil, "\$tmp =~ s/[^\\w\\s]+//g", '"$tmp"';
489  
490 # only printable characters
491 } elsif ($opts->{kst} =~ /i/) {
492 0 require POSIX;
493 0 $fil[0] = "join '', grep {POSIX::isprint \$_} " .
494 "split //,\n$fil[0]";
495 }
496  
497 2 ▶ $fil[0] = "\$tmp = $fil[0]" if $opts->{kst} =~ /d/;
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"kst"} =~ /d/l 0 2 2
498  
499  
500 # reverse sense
501 2 ▶ ($bb, $aa) = ($aa, $bb) if ($opts->{kst} =~ /r/);
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"kst"} =~ /r/l 1 1 2
502  
503 # do numeric sort
504 2 ▶ $cmp = '<=>' if ($opts->{kst} =~ /n/);
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"kst"} =~ /n/l 2 0 2
505  
506 # add finished expressions to arrays
507 2 my $n = @sortsub + 2;
508 2 push @sortsub, sprintf "%s->[$n] %s %s->[$n]",
509 $aa, $cmp, $bb;
510  
511 2 ▶ if (@fil > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fil > 1) { } 0 2 2
512 push @mapsub, " (\n" .
513 0 join(",\n", map {s/^/ /mg; $_} @fil),
514 "\n )[-1],\n ";
515 } else {
516 2 push @mapsub, " " . $fil[0] . ",\n ";
517 }
518 }
519  
520 # if not -u
521 2 ▶ if (! $topts->{u} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($topts->{"u"}) 2 0 2
522 # do straight compare if all else is equal
523 push @sortsub, sprintf "%s->[1] %s %s->[1]",
524 2 ▶ $topts->{r} ? qw($b cmp $a) : qw($a cmp $b);
Branch: 1 branch
BranchTrueFalseTotal
$topts->{'r'} ? : 1 1 2
525 }
526  
527 2 my(%maps, $sortsub, $mapsub) = (map1 => '$_', map2 => '$fh{$_}');
528  
529 2 $sortsub = "sub {\n " . join(" || \n ", @sortsub) . "\n}\n";
530  
531 2 for my $m (keys %maps) {
532 4 my $k = $maps{$m};
533 $maps{$m} = sprintf "sub {\n my \@tmp = %s;\n",
534 4 ▶ $topts->{k}[0] eq 'K' ? $k : "split(/$topts->{t}/, $k)";
Branch: 1 branch
BranchTrueFalseTotal
$topts->{'k'}[0] eq 'K' ? : 0 4 4
535  
536 4 ▶ $maps{$m} .= " my \$tmp;\n" if $topts->{DD};
Branch: 1 branch
BranchTrueFalseTotal
if $topts->{"DD"} 0 4 4
537 4 $maps{$m} .= "\n [\$_, $k";
538 4 ▶ $maps{$m} .= ",\n " . join('', @mapsub) if @mapsub;
Branch: 1 branch
BranchTrueFalseTotal
if @mapsub 4 0 4
539 4 $maps{$m} .= "]\n}\n";
540 }
541  
542 2 ▶ _debug("$sortsub\n$maps{map1}\n$maps{map2}\n") if $topts->{D};
Branch: 1 branch
BranchTrueFalseTotal
if $topts->{"D"} 0 2 2
543  
544 {
545 2 local $^W;
546 2 ▶ *sortsub = eval $sortsub;
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$File::Sort::b->[2] <=> $File::Sort::a->[2] or $File::Sort::b->[1] cmp $File::Sort::a->[1] 198 0 198
547 2 ▶ die "Can't create sort sub: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 2 2
548 2 ▶ *map1 = eval $maps{map1};
Subroutine __ANON__: called CC=1 SCAR=0.0
549 2 ▶ die "Can't create sort sub: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 2 2
550 0 ▶ *map2 = eval $maps{map2};
Subroutine __ANON__: not called CC=1 SCAR=2.6
551 2 ▶ die "Can't create sort sub: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 2 2
552 }
553 }
554 }
555  
556  
557 102 ▶sub _get_temp { # nice and simple
Subroutine _get_temp: called CC=1 SCAR=0.0
558 102 require IO::File;
559 102 IO::File->new_tmpfile;
560 }
561  
562 0 ▶sub _are_uniq {
Subroutine _are_uniq: not called CC=2 SCAR=17.9
563 0 my $nok = shift;
564 0 local $^W;
565  
566 0 ▶ if ($nok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nok) { } 0 0 0
567 0 ($a, $b) = @_;
568 0 return &sort1;
569 } else {
570 0 ($a, $b) = map &map1, @_;
571 0 return &sortsub;
572 }
573 }
574  
575 0 ▶sub _debug {
Subroutine _debug: not called CC=1 SCAR=6.9
576 0 print STDERR @_;
577 }
578  
579 0 ▶sub usage {
Subroutine usage: not called CC=5 SCAR=34.0
Pod undocumented
580 0 local $/ = "\n"; # in case changed
581 0 my $u;
582  
583 0 seek DATA, 0, 0;
584 0 ▶ while (<DATA>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline DATA)) 0 0 0
585 0 ▶ last if m/^=head1 SYNOPSIS$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^=head1 SYNOPSIS$/l 0 0 0
586 }
587  
588 0 ▶ while (<DATA>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline DATA)) 0 0 0
589 0 ▶ last if m/^=/;
Branch: 1 branch
BranchTrueFalseTotal
if /^=/l 0 0 0
590 0 $u .= $_;
591 }
592  
593 0 $u =~ s/\n//;
594
595 0 die "Usage:$u";
596  
597 }
598  
599 __END__
↑ Summary