Sort/Sub/by_count.pm

filter: Statementstmt 90.2% 37 / 41 Branchbran 50.0% 12 / 24 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 87.5% 7 / 8 Podpod 0.0% 0 / 2 totaltotal 71.2% 57 / 80 CC 14 SCAR 18.7
CC
14
Cov
74%
__ANON__
10.1
_pattern_to_re
7.5
gen_sorter
2.1
CRAP 6.5
« Sort/Sub.pm ↑ Summary Sort/Sub/by_ascii_then_num.pm »
1 package Sort::Sub::by_count;
2  
3 2 ▶use 5.010001;
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 number of occurrences of pattern in string',
16 };
17 }
18  
19 1 ▶sub _pattern_to_re {
Subroutine _pattern_to_re: called CC=6 SCAR=20.2
20 1 my $args = shift;
21  
22 1 my $re;
23 1 ▶ my $pattern = $args->{pattern}; defined $pattern or die "Please specify pattern";
Branch: 1 branch
BranchTrueFalseTotal
defined $pattern or die "Please specify pattern" 1 0 1
24 1 ▶ if ($args->{fixed_string}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{'fixed_string'}) { } 0 1 1
25 0 ▶ $re = $args->{ignore_case} ? qr/\Q$pattern/i : qr/\Q$pattern/;
Branch: 1 branch
BranchTrueFalseTotal
$args->{'ignore_case'} ? : 0 0 0
26 } else {
27 1 ▶ eval { $re = $args->{ignore_case} ? qr/$pattern/i : qr/$pattern/ };
Branch: 1 branch
BranchTrueFalseTotal
$args->{'ignore_case'} ? : 0 1 1
28 1 ▶ die "Invalid pattern: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
29 }
30  
31 1 $re;
32 }
33  
34 1 ▶sub gen_sorter {
Subroutine gen_sorter: called CC=2 SCAR=7.4
Pod undocumented
35 1 my ($is_reverse, $is_ci, $args) = @_;
36  
37 1 ▶ die __PACKAGE__.": Please specify 'pattern'" unless defined $args->{pattern};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"pattern"} 0 1 1
38  
39 1 my $re = _pattern_to_re($args);
40  
41 4 ▶ sub {
Subroutine __ANON__: called CC=8 SCAR=23.2
42 2 ▶ no strict 'refs'; ## no critic: TestingAndDebugging::ProhibitNoStrict
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 4 my $caller = caller();
45 0 ▶ my $a = @_ ? $_[0] : ${"$caller\::a"};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 4 0 4
46 0 ▶ my $b = @_ ? $_[1] : ${"$caller\::b"};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 4 0 4
47  
48 4 ▶ my $count_a = 0; $count_a++ while $a =~ /$re/g;
Branch: 1 branch
BranchTrueFalseTotal
$a =~ /$re/g and do { ++$count_a } 10 4 14
49 4 ▶ my $count_b = 0; $count_b++ while $b =~ /$re/g;
Branch: 1 branch
BranchTrueFalseTotal
$b =~ /$re/g and do { ++$count_b } 9 4 13
50  
51 4 ▶ ($is_reverse ? -1 : 1)*(
Branch: 2 branches
BranchTrueFalseTotal
$is_reverse ? : 0 4 4
$is_ci ? : 0 0 0
Condition33%1/3
$count_a <=> $count_b || ($is_ci ? lc $a cmp lc $b : $a cmp $b)
l!l&&r!l&&!r
400
MC/DC0%0/2
$count_a <=> $count_b || ($is_ci ? lc $a cmp lc $b : $a cmp $b)
$count_a <=> $count_b ($is_ci ? lc $a cmp lc $b : $a cmp $b)
Truth table33%1/3
A || B
A= $count_a <=> $count_bB= ($is_ci ? lc $a cmp lc $b : $a cmp $b)
ABresult
1X1
011
000
52 ($count_a <=> $count_b) ||
53 ($is_ci ? lc($a) cmp lc($b) : $a cmp $b)
54 );
55 1 };
56 }
57  
58 2 1;
59 # ABSTRACT: Sort by number of occurrences of pattern in string
60  
61 __END__
« Sort/Sub.pm ↑ Summary Sort/Sub/by_ascii_then_num.pm »