Sort/Sub/by_num_then_ascii.pm

filter: Statementstmt 91.1% 31 / 34 Branchbran 85.7% 12 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 85.7% 6 / 7 Podpod 0.0% 0 / 2 totaltotal 85.9% 49 / 57 CC 8 SCAR 16.4
CC
8
Cov
90%
__ANON__
8.1
meta
2.0
gen_sorter
1.0
CRAP 5.2
« Sort/Sub/by_ascii_then_num.pm ↑ Summary Sort/Sub/by_perl_function.pm »
1 package Sort::Sub::by_num_then_ascii;
2  
3 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
8 2 our $DATE = '2024-07-17'; # DATE
9 2 our $DIST = 'Sort-Sub'; # DIST
10 2 our $VERSION = '0.121'; # VERSION
11  
12 0 ▶sub meta {
Subroutine meta: not called CC=1 SCAR=6.9
Pod undocumented
13 return {
14 0 v => 1,
15 summary => 'Sort numbers (sorted numerically) before non-numbers (sorted asciibetically)',
16 };
17 }
18 3 ▶sub gen_sorter {
Subroutine gen_sorter: called CC=1 SCAR=0.0
Pod undocumented
19 3 my ($is_reverse, $is_ci) = @_;
20  
21 3 my $re_is_num = qr/\A
22 [+-]?
23 (?:\d+|\d*(?:\.\d*)?)
24 (?:[Ee][+-]?\d+)?
25 \z/x;
26  
27 36 ▶ sub {
Subroutine __ANON__: called CC=8 SCAR=21.0
28 2 ▶ no strict 'refs'; ## no critic: TestingAndDebugging::ProhibitNoStrict
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 36 my $caller = caller();
31 0 ▶ my $a = @_ ? $_[0] : ${"$caller\::a"};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 36 0 36
32 0 ▶ my $b = @_ ? $_[1] : ${"$caller\::b"};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 36 0 36
33  
34 36 my $cmp = 0;
35 36 ▶ if ($a =~ $re_is_num) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a =~ /$re_is_num/) { } 17 19 36
36 17 ▶ if ($b =~ $re_is_num) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b =~ /$re_is_num/) { } 7 10 17
37 7 $cmp = $a <=> $b;
38 } else {
39 10 $cmp = -1;
40 }
41 } else {
42 19 ▶ if ($b =~ $re_is_num) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b =~ /$re_is_num/) { } 4 15 19
43 4 $cmp = 1;
44 } else {
45 15 ▶ $cmp = $is_ci ?
Branch: 1 branch
BranchTrueFalseTotal
$is_ci ? : 9 6 15
46 lc($a) cmp lc($b) : $a cmp $b;
47 }
48 }
49 36 ▶ $is_reverse ? -1*$cmp : $cmp;
Branch: 1 branch
BranchTrueFalseTotal
$is_reverse ? : 10 26 36
50 3 };
51 }
52  
53 2 1;
54 # ABSTRACT: Sort numbers (sorted numerically) before non-numbers (sorted asciibetically)
55  
56 __END__
« Sort/Sub/by_ascii_then_num.pm ↑ Summary Sort/Sub/by_perl_function.pm »