Sort/Sub.pm

filter: Statementstmt 70.3% 57 / 81 Branchbran 41.6% 10 / 24 Conditioncond 100% 4 / 4 MC/DCmcdc 100% 2 / 2 Subroutinesub 70.0% 7 / 10 Podpod 100% 1 / 1 totaltotal 66.3% 81 / 122 CC 15 SCAR 20.4
CC
15
Cov
65%
__ANON__
20.0
__ANON__
12.0
get_sorter
6.0
CRAP 7.7
« Sort/Sub/by_last_num_in_text.pm ↑ Summary Sort/Sub/by_count.pm »
1 package Sort::Sub;
2  
3 19 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 19 ▶use strict 'subs', 'vars';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 19 ▶use Log::ger;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 19 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
9 19 our $DATE = '2024-07-17'; # DATE
10 19 our $DIST = 'Sort-Sub'; # DIST
11 19 our $VERSION = '0.121'; # VERSION
12  
13 19 our $re_spec = qr/\A(\$)?(\w+)(?:<(\w*)>)?\z/;
14  
15 our %argsopt_sortsub = (
16 sort_sub => {
17 summary => 'Name of a Sort::Sub::* module (without the prefix)',
18 schema => ['sortsub::spec*'],
19 },
20 sort_args => {
21 'x.name.is_plural' => 1,
22 'x.name.singular' => 'sort_arg',
23 summary => 'Arguments to pass to the Sort::Sub::* routine',
24 schema => ['array*', of=>'str*'],
25 0 ▶ element_completion => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
26 0 my %cargs = @_;
27  
28 # do we have the routine already? if yes, extract the metadata
29 0 my $rname = $cargs{args}{sort_sub};
30 0 ▶ return [] unless defined $rname;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rname 0 0 0
31  
32 0 my $mod = "Sort::Sub::$rname";
33 0 (my $mod_pm = "$mod.pm") =~ s!::!/!g;
34 0 eval { require $mod_pm };
35 0 ▶ return {message=>"Cannot load $mod: $@"} if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
36 0 my $meta;
37 0 eval { $meta = $mod->meta };
38 0 ▶ return [] unless $meta;
Branch: 1 branch
BranchTrueFalseTotal
unless $meta 0 0 0
39  
40 0 require Complete::Sequence;
41 return Complete::Sequence::complete_sequence(
42 word => $cargs{word},
43 sequence => [
44 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
45 0 ▶ [$meta->{args} ? keys(%{ $meta->{args} }) : ()];
Branch: 1 branch
BranchTrueFalseTotal
$meta->{'args'} ? : 0 0 0
46 },
47 '=',
48 0 ▶ sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
49 0 my $stash = shift;
50 0 my $argname = $stash->{completed_item_words}[0];
51 0 ▶ return [] unless defined $argname;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $argname 0 0 0
52  
53 0 my $argspec = $meta->{args}{$argname};
54 0 ▶ return [] unless $argspec->{schema};
Branch: 1 branch
BranchTrueFalseTotal
unless $argspec->{"schema"} 0 0 0
55  
56 0 require Complete::Sah;
57 0 require Complete::Util;
58 Complete::Util::arrayify_answer(
59 Complete::Sah::complete_from_schema(
60 word => $stash->{cur_word},
61 schema => $argspec->{schema},
62 )
63 0 );
64  
65 },
66 0 ],
67 );
68 },
69 },
70 19 );
71  
72 43 ▶sub get_sorter {
Subroutine get_sorter: called CC=6 SCAR=17.9
Pod documented
73 43 my ($spec, $args, $with_meta) = @_;
74  
75 43 ▶ my ($is_var, $name, $opts) = $spec =~ $re_spec
Branch: 1 branch
BranchTrueFalseTotal
() = $spec =~ /$Sort::Sub::re_spec/ or die "Invalid sorter spec '${spec}', please use: " . '[$]NAME [ <OPTS> ]' 43 0 43
76 or die "Invalid sorter spec '$spec', please use: ".
77 '[$]NAME [ <OPTS> ]';
78 43 my $modpm = "Sort/Sub/$name.pm";
79 43 require $modpm;
80 43 ▶ $opts //= "";
Condition100%2/2
$opts //= ""
l!l
2716
MC/DC100%1/1
$opts //= ""
$opts
Truth table100%2/2
A
A= $opts
Aresult
11
00
81 43 my $is_reverse = $opts =~ /r/;
82 43 my $is_ci = $opts =~ /i/;
83 43 my $gen_sorter = \&{"Sort::Sub::$name\::gen_sorter"};
84 43 ▶ my $sorter = $gen_sorter->($is_reverse, $is_ci, $args // {});
Condition100%2/2
$args // {}
l!l
421
MC/DC100%1/1
$args // {}
$args
Truth table100%2/2
A
A= $args
Aresult
11
00
85 43 ▶ if ($with_meta) {
Branch: 1 branch
BranchTrueFalseTotal
if ($with_meta) { } 1 42 43
86 1 my $meta = {};
87 1 eval { $meta = &{"Sort::Sub::$name\::meta"}() };
88 1 ▶ warn if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
89 1 return ($sorter, $meta);
90 } else {
91 42 return $sorter;
92 }
93 }
94  
95 41 ▶sub import {
Subroutine import: called CC=4 SCAR=13.9
96 41 my $class = shift;
97 41 my $caller = caller;
98  
99 41 my $i = -1;
100 41 while (1) {
101 82 $i++;
102 82 ▶ last if $i >= @_;
Branch: 1 branch
BranchTrueFalseTotal
if $i >= @_ 41 41 82
103 41 my $import = $_[$i];
104 41 my $args = {};
105 41 ▶ if (ref $_[$i+1] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[$i + 1] eq "HASH") 13 28 41
106 13 $args = $_[$i+1];
107 13 $i++;
108 }
109 41 my $sorter = get_sorter($import, $args);
110 41 my ($is_var, $name) = $import =~ $re_spec; # XXX double matching
111 41 ▶ if ($is_var) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_var) { } 1 40 41
112 1 ${"$caller\::$name"} = \&$sorter;
113 } else {
114 19 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
115 40 *{"$caller\::$name"} = \&$sorter;
116 }
117 }
118 }
119  
120 19 1;
121 # ABSTRACT: Collection of sort subroutines
122  
123 __END__
« Sort/Sub/by_last_num_in_text.pm ↑ Summary Sort/Sub/by_count.pm »