File Coverage

blib/lib/Sort/Sub/by_ascii_then_num.pm
Criterion Covered Total %
statement 26 29 89.6
branch 12 14 85.7
condition n/a
subroutine 6 7 85.7
pod 0 2 0.0
total 44 52 84.6


line stmt bran cond sub pod time code
1             package Sort::Sub::by_ascii_then_num;
2              
3             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
4             our $DATE = '2020-02-28'; # DATE
5             our $DIST = 'Sort-Sub'; # DIST
6             our $VERSION = '0.118'; # VERSION
7              
8 1     1   21 use 5.010;
  1         3  
9 1     1   5 use strict;
  1         2  
  1         20  
10 1     1   5 use warnings;
  1         2  
  1         150  
11              
12             sub meta {
13             return {
14 0     0 0 0 v => 1,
15             summary => 'Sort non-numbers (sorted asciibetically) before numbers (sorted numerically)',
16             };
17             }
18              
19             sub gen_sorter {
20 3     3 0 7 my ($is_reverse, $is_ci) = @_;
21              
22 3         11 my $re_is_num = qr/\A
23             [+-]?
24             (?:\d+|\d*(?:\.\d*)?)
25             (?:[Ee][+-]?\d+)?
26             \z/x;
27              
28             sub {
29 1     1   8 no strict 'refs';
  1         2  
  1         231  
30              
31 34     34   48 my $caller = caller();
32 34 50       63 my $a = @_ ? $_[0] : ${"$caller\::a"};
  0         0  
33 34 50       50 my $b = @_ ? $_[1] : ${"$caller\::b"};
  0         0  
34              
35 34         43 my $cmp = 0;
36 34 100       154 if ($a =~ $re_is_num) {
37 16 100       64 if ($b =~ $re_is_num) {
38 7         20 $cmp = $a <=> $b;
39             } else {
40 9         15 $cmp = 1;
41             }
42             } else {
43 18 100       59 if ($b =~ $re_is_num) {
44 3         7 $cmp = -1;
45             } else {
46 15 100       35 $cmp = $is_ci ?
47             lc($a) cmp lc($b) : $a cmp $b;
48             }
49             }
50 34 100       86 $is_reverse ? -1*$cmp : $cmp;
51 3         21 };
52             }
53              
54             1;
55             # ABSTRACT: Sort non-numbers (sorted asciibetically) before numbers (sorted numerically)
56              
57             __END__