File Coverage

blib/lib/Sort/Sub/num_in_text.pm
Criterion Covered Total %
statement 31 34 91.1
branch 13 20 65.0
condition 6 9 66.6
subroutine 6 6 100.0
pod 0 1 0.0
total 56 70 80.0


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