File Coverage

blib/lib/Sort/Sub/by_last_num_in_text.pm
Criterion Covered Total %
statement 32 38 84.2
branch 13 20 65.0
condition 6 9 66.6
subroutine 6 7 85.7
pod 0 2 0.0
total 57 76 75.0


line stmt bran cond sub pod time code
1             package Sort::Sub::by_last_num_in_text;
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   19 use 5.010001;
  1         4  
9 1     1   5 use strict;
  1         2  
  1         21  
10 1     1   4 use warnings;
  1         2  
  1         69  
11              
12             sub meta {
13             return {
14 0     0 0 0 v => 1,
15             summary => 'Sort by last number found in text or (if no number is found) ascibetically',
16             };
17             }
18              
19             sub gen_sorter {
20 1     1 0 3 my ($is_reverse, $is_ci) = @_;
21              
22             sub {
23 1     1   6 no strict 'refs';
  1         1  
  1         295  
24              
25 9     9   13 my $caller = caller();
26 9 50       17 my $a = @_ ? $_[0] : ${"$caller\::a"};
  0         0  
27 9 50       17 my $b = @_ ? $_[1] : ${"$caller\::b"};
  0         0  
28              
29 9         18 my $cmp;
30              
31             my @nums;
32 9 100       0 my $num_a; @nums = $a =~ /(\d+)/g; $num_a = $nums[-1] if @nums;
  9         26  
  9         21  
33 9 100       12 my $num_b; @nums = $b =~ /(\d+)/g; $num_b = $nums[-1] if @nums;
  9         28  
  9         21  
34              
35             {
36 9 100 100     10 if (defined $num_a && defined $num_b) {
  9 100 66     41  
    50 33        
37 6         11 $cmp = $num_a <=> $num_b;
38 6 50       11 last if $cmp;
39             } elsif (defined $num_a && !defined $num_b) {
40 1         1 $cmp = -1;
41 1         2 last;
42             } elsif (!defined $num_a && defined $num_b) {
43 2         4 $cmp = 1;
44 2         2 last;
45             }
46              
47 0 0       0 if ($is_ci) {
48 0         0 $cmp = lc($a) cmp lc($b);
49             } else {
50 0         0 $cmp = $a cmp $b;
51             }
52             }
53              
54 9 50       29 $is_reverse ? -1*$cmp : $cmp;
55 1         7 };
56             }
57              
58             1;
59             # ABSTRACT: Sort by last number found in text or (if no number is found) ascibetically
60              
61             __END__