File Coverage

blib/lib/Sort/Sub/last_num_in_text.pm
Criterion Covered Total %
statement 33 37 89.1
branch 13 20 65.0
condition 6 9 66.6
subroutine 6 6 100.0
pod 0 1 0.0
total 58 73 79.4


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