File Coverage

blib/lib/Sort/Sub/by_num_then_ascii.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_num_then_ascii;
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   20 use 5.010;
  1         3  
9 1     1   5 use strict;
  1         2  
  1         20  
10 1     1   4 use warnings;
  1         2  
  1         151  
11              
12             sub meta {
13             return {
14 0     0 0 0 v => 1,
15             summary => 'Sort numbers (sorted numerically) before non-numbers (sorted asciibetically)',
16             };
17             }
18             sub gen_sorter {
19 3     3 0 8 my ($is_reverse, $is_ci) = @_;
20              
21 3         10 my $re_is_num = qr/\A
22             [+-]?
23             (?:\d+|\d*(?:\.\d*)?)
24             (?:[Ee][+-]?\d+)?
25             \z/x;
26              
27             sub {
28 1     1   8 no strict 'refs';
  1         2  
  1         213  
29              
30 36     36   54 my $caller = caller();
31 36 50       60 my $a = @_ ? $_[0] : ${"$caller\::a"};
  0         0  
32 36 50       56 my $b = @_ ? $_[1] : ${"$caller\::b"};
  0         0  
33              
34 36         43 my $cmp = 0;
35 36 100       158 if ($a =~ $re_is_num) {
36 17 100       101 if ($b =~ $re_is_num) {
37 7         20 $cmp = $a <=> $b;
38             } else {
39 10         15 $cmp = -1;
40             }
41             } else {
42 19 100       66 if ($b =~ $re_is_num) {
43 4         7 $cmp = 1;
44             } else {
45 15 100       32 $cmp = $is_ci ?
46             lc($a) cmp lc($b) : $a cmp $b;
47             }
48             }
49 36 100       88 $is_reverse ? -1*$cmp : $cmp;
50 3         18 };
51             }
52              
53             1;
54             # ABSTRACT: Sort numbers (sorted numerically) before non-numbers (sorted asciibetically)
55              
56             __END__