Sort/Sub/by_perl_function.pm

filter: Statementstmt 83.7% 31 / 37 Branchbran 62.5% 10 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 85.7% 6 / 7 Podpod 0.0% 0 / 2 totaltotal 75.8% 47 / 62 CC 9 SCAR 14.2
CC
9
Cov
77%
__ANON__
6.4
gen_sorter
4.4
meta
2.0
CRAP 4.1
« Sort/Sub/by_num_then_ascii.pm ↑ Summary Sort/Sub/numerically_no_warning.pm »
1 package Sort::Sub::by_perl_function;
2  
3 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
8 2 our $DATE = '2024-07-17'; # DATE
9 2 our $DIST = 'Sort-Sub'; # DIST
10 2 our $VERSION = '0.121'; # VERSION
11  
12 0 ▶sub meta {
Subroutine meta: not called CC=1 SCAR=6.9
Pod undocumented
13 return {
14 0 v => 1,
15 summary => 'Sort by Perl function',
16 args => {
17 function => {
18 schema => 'perl::funcname*',
19 req => 1,
20 },
21 numeric => {
22 summary => "Compare using Perl's <=> instead of cmp",
23 schema => 'bool*',
24 default => 0,
25 },
26 },
27 };
28 }
29 3 ▶sub gen_sorter {
Subroutine gen_sorter: called CC=4 SCAR=14.7
Pod undocumented
30 3 my ($is_reverse, $is_ci, $args) = @_;
31  
32 3 my $function = $args->{function};
33 3 ▶ die "Please supply sorter argument 'function'"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $function 0 3 3
34 unless defined $function;
35  
36 3 ▶ if ($function =~ /(.+)::(.+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($function =~ /(.+)::(.+)/) 0 3 3
37 # qualified with a package name, load associated module
38 0 my $mod = $1;
39 0 (my $mod_pm = "$mod.pm") =~ s!::!/!g;
40 0 require $mod_pm;
41 }
42  
43 3 my $code_str = "sub { $function\(\$_[0]) }";
44 3 my $code_call_func = eval $code_str; ## no critic: BuiltinFunctions::ProhibitStringyEval
45 3 ▶ die "Can't compile $code_str: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 3 3
46  
47 8 ▶ sub {
Subroutine __ANON__: called CC=6 SCAR=18.6
48 2 ▶ no strict 'refs'; ## no critic: TestingAndDebugging::ProhibitNoStrict
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 8 my $caller = caller();
51 0 ▶ my $a = @_ ? $_[0] : ${"$caller\::a"};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 8 0 8
52 0 ▶ my $b = @_ ? $_[1] : ${"$caller\::b"};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 8 0 8
53  
54 8 my $res_a = $code_call_func->($a);
55 8 my $res_b = $code_call_func->($b);
56  
57 8 ▶ my $cmp = $args->{numeric} ? $res_a <=> $res_b :
Branch: 2 branches
BranchTrueFalseTotal
$args->{'numeric'} ? : 0 8 8
$is_ci ? : 5 3 8
58 $is_ci ? lc($res_a) cmp lc($res_b) : $res_a cmp $res_b;
59 8 ▶ $is_reverse ? -1*$cmp : $cmp;
Branch: 1 branch
BranchTrueFalseTotal
$is_reverse ? : 2 6 8
60 3 };
61 }
62  
63 2 1;
64 # ABSTRACT: Sort by Perl function
65  
66 __END__
« Sort/Sub/by_num_then_ascii.pm ↑ Summary Sort/Sub/numerically_no_warning.pm »