File Coverage

blib/lib/Sort/Sub.pm
Criterion Covered Total %
statement 46 46 100.0
branch 10 12 83.3
condition 4 4 100.0
subroutine 6 6 100.0
pod 1 1 100.0
total 67 69 97.1


line stmt bran cond sub pod time code
1             package Sort::Sub;
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 16     16   690 use 5.010001;
  16         51  
9 16     16   81 use strict 'subs', 'vars';
  16         25  
  16         457  
10 16     16   95 use warnings;
  16         65  
  16         7803  
11              
12             our $re_spec = qr/\A(\$)?(\w+)(?:<(\w*)>)?\z/;
13              
14             our %argsopt_sortsub = (
15             sort_sub => {
16             summary => 'Name of a Sort::Sub::* module (without the prefix)',
17             schema => ['sortsub::spec*'],
18             },
19             sort_args => {
20             summary => 'Arguments to pass to the Sort::Sub::* routine',
21             schema => ['hash*', of=>'str*'],
22             },
23             );
24              
25             sub get_sorter {
26 43     43 1 1067 my ($spec, $args, $with_meta) = @_;
27              
28 43 50       453 my ($is_var, $name, $opts) = $spec =~ $re_spec
29             or die "Invalid sorter spec '$spec', please use: ".
30             '[$]NAME [ ]';
31 43         7724 require "Sort/Sub/$name.pm";
32 43   100     194 $opts //= "";
33 43         116 my $is_reverse = $opts =~ /r/;
34 43         89 my $is_ci = $opts =~ /i/;
35 43         58 my $gen_sorter = \&{"Sort::Sub::$name\::gen_sorter"};
  43         166  
36 43   100     180 my $sorter = $gen_sorter->($is_reverse, $is_ci, $args // {});
37 43 100       117 if ($with_meta) {
38 1         2 my $meta = {};
39 1         3 eval { $meta = &{"Sort::Sub::$name\::meta"}() };
  1         1  
  1         5  
40 1 50       3 warn if $@;
41 1         5 return ($sorter, $meta);
42             } else {
43 42         111 return $sorter;
44             }
45             }
46              
47             sub import {
48 41     41   887 my $class = shift;
49 41         79 my $caller = caller;
50              
51 41         64 my $i = -1;
52 41         59 while (1) {
53 82         119 $i++;
54 82 100       244 last if $i >= @_;
55 41         77 my $import = $_[$i];
56 41         66 my $args = {};
57 41 100       128 if (ref $_[$i+1] eq 'HASH') {
58 13         23 $args = $_[$i+1];
59 13         16 $i++;
60             }
61 41         98 my $sorter = get_sorter($import, $args);
62 41         328 my ($is_var, $name) = $import =~ $re_spec; # XXX double matching
63 41 100       105 if ($is_var) {
64 1         3 ${"$caller\::$name"} = \&$sorter;
  1         4  
65             } else {
66 16     16   141 no warnings 'redefine';
  16         56  
  16         1587  
67 40         77 *{"$caller\::$name"} = \&$sorter;
  40         353  
68             }
69             }
70             }
71              
72             1;
73             # ABSTRACT: Collection of sort subroutines
74              
75             __END__