Sort/Sub/by_perl_code.pm

filter: Statementstmt 94.4% 51 / 54 Branchbran 66.6% 8 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 92.8% 13 / 14 Podpod 0.0% 0 / 2 totaltotal 87.8% 72 / 82 CC 7 SCAR 8.4
CC
7
Cov
89%
__ANON__
4.2
gen_sorter
4.0
meta
2.0
CRAP 2.3
« Sort/Sub/record_by_reverse_order.pm ↑ Summary Sort/Sub/naturally.pm »
1 package Sort::Sub::by_perl_code;
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 2 ▶use Log::ger;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
9 2 our $DATE = '2024-07-17'; # DATE
10 2 our $DIST = 'Sort-Sub'; # DIST
11 2 our $VERSION = '0.121'; # VERSION
12  
13 0 ▶sub meta {
Subroutine meta: not called CC=1 SCAR=6.9
Pod undocumented
14 return {
15 0 v => 1,
16 summary => 'Sort by Perl code',
17 args => {
18 code => {
19 summary => 'Either compiled code or string code excluding the "sub {" and "}" enclosure',
20 description => <<'_',
21  
22 Code should accept two arguments (the operands to be compared) and is expected
23 to return -1, 0, -1 like the builtin `cmp` operator.
24  
25 _
26 schema => 'str*',
27 req => 1,
28 },
29 },
30 };
31 }
32 6 ▶sub gen_sorter {
Subroutine gen_sorter: called CC=4 SCAR=13.9
Pod undocumented
33 6 my ($is_reverse, $is_ci, $args) = @_;
34  
35 6 my $code = $args->{code};
36 6 ▶ die "Please supply sorter argument 'code'"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $code 0 6 6
37 unless defined $code;
38  
39 6 ▶ if (ref $code ne 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $code ne "CODE") 3 3 6
40 3 $code = "no strict; no warnings; sub { $code }";
41 3 log_trace "Compiling sort code: $code";
42 3 ▶ $code = eval $code; ## no critic: BuiltinFunctions::ProhibitStringyEval
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
43 3 ▶ die "Can't compile $code: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 3 3
44 }
45  
46 16 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=14.5
47 2 ▶ no strict 'refs'; ## no critic: TestingAndDebugging::ProhibitNoStrict
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 16 my $caller = caller();
50 0 ▶ my $a = @_ ? $_[0] : ${"$caller\::a"};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 16 0 16
51 0 ▶ my $b = @_ ? $_[1] : ${"$caller\::b"};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 16 0 16
52  
53 16 my $cmp = $code->($a, $b);
54 16 ▶ $is_reverse ? -1*$cmp : $cmp;
Branch: 1 branch
BranchTrueFalseTotal
$is_reverse ? : 4 12 16
55 6 };
56 }
57  
58 2 1;
59 # ABSTRACT: Sort by Perl code
60  
61 __END__
« Sort/Sub/record_by_reverse_order.pm ↑ Summary Sort/Sub/naturally.pm »