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