script/psort

filter: Statementstmt 92.0% 139 / 151 Branchbran 73.2% 63 / 86 Conditioncond 55.5% 5 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 100% 20 / 20 Podpod n/a totaltotal 83.4% 227 / 272 CC 13 SCAR 16.4
CC
13
Cov
84%
add_psort
14.3
__ANON__
4.1
__ANON__
2.1
CRAP 5.1
↑ Summary lib/App/psort.pm »
1 #!/usr/local/bin/perl -w
2 # -*- perl -*-
3  
4 #
5 # Author: Slaven Rezic
6 #
7 # Copyright (C) 2009,2011,2013,2015,2016,2018,2019,2022,2023 Slaven Rezic. All rights reserved.
8 # This program is free software; you can redistribute it and/or
9 # modify it under the same terms as Perl itself.
10 #
11 # Mail: slaven@rezic.de
12 # WWW: http://www.rezic.de/eserte/
13 #
14  
15 98 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 98 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 98 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 101 $VERSION = "0.12";
20  
21  
22 12 ▶sub _has_capture_groups {
Subroutine _has_capture_groups: called CC=1 SCAR=0.0
23 12 my $qr = shift;
24 12 $qr =~ /(?<!\\)\([^(?:)]/;
25 }
26  
27 101 ▶return 1 if caller;
Branch: 1 branch
BranchTrueFalseTotal
if caller 0 98 98
28  
29 101 my $sort_type = 'alpha';
30 98 my $reverse;
31 my $check;
32 98 my $unique;
33 98 my $ignore_leading_blanks;
34 98 my $ignore_case;
35 98 my $ignore_nonprinting;
36 98 my $perlscript;
37 98 my $rx;
38 98 my @modules;
39 98 my $no_warnings;
40 98 my $cmp_perlscript;
41  
42 98 Getopt::Long::Configure("bundling");
43 GetOptions(
44 18 ▶ "n|numeric-sort" => sub { $sort_type = 'numeric' },
Subroutine __ANON__: called CC=1 SCAR=0.0
45 6 ▶ "N|natural-sort" => sub { $sort_type = 'sort_naturally' },
Subroutine __ANON__: called CC=1 SCAR=0.0
46 6 ▶ "V|version-sort" => sub { $sort_type = 'version' },
Subroutine __ANON__: called CC=1 SCAR=0.0
47 "C|compare-function=s" => \$cmp_perlscript,
48 "b|ignore-leading-blanks" => \$ignore_leading_blanks,
49 "f|ignore-case" => \$ignore_case,
50 "i|ignore-nonprinting" => \$ignore_nonprinting,
51 "r|reverse" => \$reverse,
52 "c|check" => \$check,
53 "u|unique" => \$unique,
54 "e|field-function=s" => \$perlscript,
55 "rx=s" => \$rx,
56 'M|module=s@' => \@modules,
57 3 ▶ 'm=s@' => sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
58 3 ▶ push @modules, $_[1] =~ /=/ ? $_[1] : "$_[1]=";
Branch: 1 branch
BranchTrueFalseTotal
$_[1] =~ /=/ ? : 3 0 3
59 },
60 "X|no-warnings" => \$no_warnings,
61 2 ▶ "v|version" => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
62 2 print "psort version $VERSION\n";
63 2 exit 0;
64 },
65 98 ▶ ) or die <<EOF;
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("n|numeric-sort", sub { $sort_type = "numeric"; } , "N|natural-sort", sub { $sort_type = "sort_naturally"; } , "V|version-sort", sub { $sort_type = "version"; } , "C|compare-function=s", \$cmp_perlscript, "b|ignore-leading-blanks", \$ignore_leading_blanks, "f|ignore-case", \$ignore_case, "i|ignore-nonprinting", \$ignore_nonprinting, "r|reverse", \$reverse, "c|check", \$check, "u|unique", \$unique, "e|field-function=s", \$perlscript, "rx=s", \$rx, "M|module=s\@", \@modules, "m=s\@", sub { push @modules, $_[1] =~ /=/ ? $_[1] : "$_[1]="; } , "X|no-warnings", \$no_warnings, "v|version", sub { print "psort version $main::VERSION\n"; exit 0; } ) or die "usage: $0 [OPTION]... [FILE]...\n\nComparison functions:\n-C: compare using any perl code (\$a and \$b are defined)\n-n: compare numerically\n-N: compare using Sort::Naturally\n-V: compare versions\n\nSort key selection:\n-b: ignore leading blanks\n-e: perl oneliner, should return the value to be compared as last value\n--rx: capture sort value by regexp\n-f: ignore case\n-i: ignore non-printing characters\n\nMisc:\n-c: check only\n-u: unique\n-r: reverse sorting\n-M: load perl modules\n-m: load perl modules (without importing)\n-v: print version\n" 96 0 96
66 usage: $0 [OPTION]... [FILE]...
67  
68 Comparison functions:
69 -C: compare using any perl code (\$a and \$b are defined)
70 -n: compare numerically
71 -N: compare using Sort::Naturally
72 -V: compare versions
73  
74 Sort key selection:
75 -b: ignore leading blanks
76 -e: perl oneliner, should return the value to be compared as last value
77 --rx: capture sort value by regexp
78 -f: ignore case
79 -i: ignore non-printing characters
80  
81 Misc:
82 -c: check only
83 -u: unique
84 -r: reverse sorting
85 -M: load perl modules
86 -m: load perl modules (without importing)
87 -v: print version
88 EOF
89  
90 96 for my $module_spec (@modules) {
91 9 my($module,$imports) = split /=/, $module_spec, 2;
92 9 eval qq{require $module};
93 9 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 9 9
94 9 my @imports;
95 9 ▶ if (defined $imports) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $imports) { } 6 3 9
96 6 ▶ if (length $imports) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $imports) { } 6 0 6
97 6 @imports = split /,/, $imports;
98 6 $module->import(@imports);
99 } else {
100 # don't import anything
101 }
102 } else {
103 3 $module->import; # default imports
104 }
105 }
106  
107 96 ▶if ($cmp_perlscript) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmp_perlscript) 15 81 96
108 15 $sort_type = eval "no strict; sub { $cmp_perlscript }";
109 15 ▶ die "Cannot compile 'cmp' code: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 15 15
110 }
111  
112 96 my @data;
113 my $cb;
114 96 ▶defined $perlscript and defined $rx
Branch: 1 branch
BranchTrueFalseTotal
defined $perlscript and defined $rx and die "Cannot define -e/--field-function and --rx together\n" 0 96 96
Condition66%2/3
defined $perlscript and defined $rx
!ll&&!rl&&r
8790
MC/DC0%0/2
defined $perlscript and defined $rx
defined $perlscript defined $rx
Truth table66%2/3
A and B
A= defined $perlscriptB= defined $rx
ABresult
0X0
100
111
115 and die "Cannot define -e/--field-function and --rx together\n";
116 96 ▶if (defined $perlscript) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $perlscript) { } 9 87 96
elsif ($rx) { } 12 75 87
117 9 $cb = eval "no strict; sub { $perlscript }";
118 9 ▶ die "Cannot compile code: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 9 9
119 } elsif ($rx) {
120 12 my $qr = eval { qr{$rx} };
121 12 ▶ die "Cannot compile regular expression '$rx': $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 12 12
122 12 ▶ if (!_has_capture_groups($qr)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_has_capture_groups($qr)) 3 9 12
123 3 $qr = eval { qr{($rx)} };
124 3 ▶ die $@ if $@; # should not happen
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 3 3
125 }
126 12 ▶ if ($rx =~ m{^/(.*)/$}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rx =~ m[^/(.*)/$]) { } 3 9 12
127 3 my $rx_without_slashes = $1;
128 3 my $warned_about_slashes;
129 9 ▶ $cb = sub {
Subroutine __ANON__: called CC=3 SCAR=14.2
130 9 $_ =~ $qr;
131 9 ▶ if (!$warned_about_slashes && !defined $1) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($warned_about_slashes or defined $1) 0 9 9
Condition33%1/3
$warned_about_slashes or defined $1
l!l&&r!l&&!r
090
MC/DC0%0/2
$warned_about_slashes or defined $1
$warned_about_slashes defined $1
Truth table33%1/3
A or B
A= $warned_about_slashesB= defined $1
ABresult
1X1
011
000
132 0 warn "No regexp match. Maybe you want to omit the slashes, i.e. use --rx '$rx_without_slashes'?\n";
133 0 $warned_about_slashes = 1;
134 }
135 9 $1;
136 3 };
137 } else {
138 9 ▶ $cb = sub { $_ =~ $qr; $1 };
Subroutine __ANON__: called CC=1 SCAR=0.0
139 }
140 } else {
141 75 ▶ $cb = sub { $_ };
Subroutine __ANON__: called CC=1 SCAR=0.0
142 }
143  
144 96 ▶if (@ARGV) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ARGV) { } 96 0 96
145 96 for my $file (@ARGV) {
146 96 add_psort($file);
147 }
148 } else {
149 0 add_psort(\*STDIN);
150 }
151  
152 96 my $sort_preamble = q{no warnings 'uninitialized';};
153 96 my $sort_creator;
154 96 ▶if ($sort_type eq 'numeric') {
Branch: 5 branches
BranchTrueFalseTotal
if ($sort_type eq 'numeric') { } 18 78 96
elsif ($sort_type eq 'alpha') { } 51 27 78
elsif ($sort_type eq 'sort_naturally') { } 6 21 27
elsif ($sort_type eq 'version') { } 6 15 21
elsif (UNIVERSAL::isa($sort_type, 'CODE')) { } 15 0 15
155 18 $sort_preamble = q{no warnings 'numeric', 'uninitialized';};
156 18 ▶ $sort_creator = sub { $_[0] . ' <=> ' . $_[1] };
Subroutine __ANON__: called CC=1 SCAR=0.0
157 } elsif ($sort_type eq 'alpha') {
158 51 ▶ $sort_creator = sub { $_[0] . ' cmp ' . $_[1] };
Subroutine __ANON__: called CC=1 SCAR=0.0
159 } elsif ($sort_type eq 'sort_naturally') {
160 6 require Sort::Naturally;
161 6 ▶ $sort_creator = sub { 'Sort::Naturally::ncmp(' . $_[0] . ', ' . $_[1] . ')' };
Subroutine __ANON__: called CC=1 SCAR=0.0
162 } elsif ($sort_type eq 'version') {
163 6 require CPAN::Version;
164 6 ▶ $sort_creator = sub { 'CPAN::Version->vcmp(' . $_[0] . ', ' . $_[1] . ')' };
Subroutine __ANON__: called CC=1 SCAR=0.0
165 } elsif (UNIVERSAL::isa($sort_type, 'CODE')) {
166 15 ▶ $sort_creator = sub { 'local($a, $b) = (' . $_[0] . ', ' . $_[1] . '); $sort_type->()' };
Subroutine __ANON__: called CC=1 SCAR=0.0
167 } else {
168 0 die "Unhandled sort type '$sort_type'";
169 }
170  
171 96 ▶if ($check) {
Branch: 1 branch
BranchTrueFalseTotal
if ($check) { } 64 32 96
172 64 ▶ exit 0 if !@data;
Branch: 1 branch
BranchTrueFalseTotal
unless @data 2 62 64
173 62 my $code = $sort_preamble;
174 62 $code .= 'sub { ';
175 62 ▶ if ($reverse) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reverse) { } 8 54 62
176 8 $code .= $sort_creator->('$_[1]->[1]', '$_[0]->[1]');
177 } else {
178 54 $code .= $sort_creator->('$_[0]->[1]', '$_[1]->[1]');
179 }
180 62 $code .= '}';
181 62 ▶ my $check_sub = eval $code;
Subroutine BEGIN: called CC=1 SCAR=0.0
182 62 ▶ die "Error while evaluating '$code': $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 62 62
183 62 ▶ for(my $i=1; $i<=$#data; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#data) 151 38 189
184 151 ▶ if ($unique && $data[$i-1][0] eq $data[$i][0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unique and $data[$i - 1][0] eq $data[$i][0]) 0 151 151
Condition66%2/3
$unique and $data[$i - 1][0] eq $data[$i][0]
!ll&&!rl&&r
139120
MC/DC0%0/2
$unique and $data[$i - 1][0] eq $data[$i][0]
$unique $data[$i - 1][0] eq $data[$i][0]
Truth table66%2/3
A and B
A= $uniqueB= $data[$i - 1][0] eq $data[$i][0]
ABresult
0X0
100
111
185 0 exit 1;
186 }
187 151 ▶ if ($check_sub->($data[$i-1], $data[$i]) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$check_sub($data[$i - 1], $data[$i]) > 0) 24 127 151
188 24 exit 1;
189 }
190 }
191 38 exit 0;
192 } else {
193 32 my $code = $sort_preamble;
194 32 $code .= '@data = ';
195 32 ▶ if ($unique) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unique) 3 29 32
196 3 $code .= 'do { my $last_line; grep { my $keep = !defined $last_line || $_->[0] ne $last_line; $last_line = $_->[0]; $keep } ';
197 }
198 32 $code .= 'sort { ';
199 32 ▶ if ($reverse) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reverse) { } 4 28 32
200 4 $code .= $sort_creator->('$b->[1]', '$a->[1]');
201 } else {
202 28 $code .= $sort_creator->('$a->[1]', '$b->[1]');
203 }
204 32 $code .= ' } @data';
205 32 ▶ if ($unique) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unique) 3 29 32
206 3 $code .= ' }';
207 }
208 32 ▶ eval $code;
Subroutine BEGIN: called CC=1 SCAR=0.0
209 32 ▶ die "Error while evaluating '$code': $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 32 32
210  
211 32 for (@data) {
212 140 print $_->[0];
213 }
214 }
215  
216 96 ▶sub add_psort {
Subroutine add_psort: called CC=10 SCAR=26.6
217 96 my($file_or_fh) = @_;
218 96 my($fh, $diag);
219 96 ▶ if (ref $file_or_fh eq 'GLOB') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file_or_fh eq 'GLOB') { } 0 96 96
220 0 $fh = $file_or_fh;
221 0 ▶ if ($file_or_fh == \*STDIN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file_or_fh == \*STDIN) { } 0 0 0
222 0 $diag = 'standard input';
223 } else {
224 0 $diag = 'provided file handle'; # currently cannot happen
225 }
226 } else {
227 96 my $file = $file_or_fh;
228 96 $diag = $file;
229 96 ▶ open $fh, '<', $file or die "Can't open $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $file or die "Can't open ${file}: $!" 96 0 96
230 }
231  
232 96 ▶ while(<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 424 96 520
233 424 my $line = $_;
234 424 my $res = $cb->($_); # force scalar context
235 424 ▶ if (!defined $res) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $res) 0 424 424
236 0 ▶ if (!$no_warnings) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($no_warnings) 0 0 0
237 0 warn "Uninitialized value returned in psort eval or regexp at $diag line $.\n";
238 }
239 0 $res = '';
240 }
241 424 ▶ $res = uc $res if $ignore_case;
Branch: 1 branch
BranchTrueFalseTotal
if $ignore_case 183 241 424
242 424 ▶ $res =~ s{^\s+}{} if $ignore_leading_blanks;
Branch: 1 branch
BranchTrueFalseTotal
if $ignore_leading_blanks 9 415 424
243 424 ▶ $res =~ s{[[:^print:]]}{}g if $ignore_nonprinting;
Branch: 1 branch
BranchTrueFalseTotal
if $ignore_nonprinting 6 418 424
244 424 $res =~ s{\r?\n$}{}; # remove newline at end
245 424 push @data, [$line, $res];
246 }
247 }
248  
249 __END__
↑ Summary lib/App/psort.pm »