| 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
| |||||||||||||||||||||||||||||||||
| 24 | 1 | ▶ | if ($args->{fixed_string}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 25 | 0 | ▶ | $re = $args->{ignore_case} ? qr/\Q$pattern/i : qr/\Q$pattern/; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 26 | } else { | ||||||||||||||||||||||||||||||||
| 27 | 1 | ▶ | eval { $re = $args->{ignore_case} ? qr/$pattern/i : qr/$pattern/ }; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 28 | 1 | ▶ | die "Invalid pattern: $@" if $@; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 46 | 0 | ▶ | my $b = @_ ? $_[1] : ${"$caller\::b"}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||
| 48 | 4 | ▶ | my $count_a = 0; $count_a++ while $a =~ /$re/g; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 49 | 4 | ▶ | my $count_b = 0; $count_b++ while $b =~ /$re/g; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||
| 51 | 4 | ▶ | ($is_reverse ? -1 : 1)*( | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
$count_a <=> $count_b || ($is_ci ? lc $a cmp lc $b : $a cmp $b)
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)
| |||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||