blib/lib/List/OrderBy.pm

filter: Statementstmt 73.6% 53 / 72 Branchbran 75.0% 3 / 4 Conditioncond 50.0% 3 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 44.0% 11 / 25 Podpod 100% 8 / 8 totaltotal 66.3% 79 / 119 CC 5 SCAR 6.6
CC
5
Cov
72%
new
3.4
get
3.0
order_by_desc
2.0
CRAP 1.9
↑ Summary
1 package List::OrderBy;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
5
6 3 ▶use vars qw{ $VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS };
Subroutine BEGIN: called CC=1 SCAR=0.0
7
8 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 @ISA = qw(Exporter);
10 3 %EXPORT_TAGS = ( 'all' => [ qw(
11 order_by then_by
12 order_cmp_by then_cmp_by
13 order_by_desc then_by_desc
14 order_cmp_by_desc then_cmp_by_desc
15 ) ] );
16
17 3 @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
18 3 @EXPORT = @EXPORT_OK;
19 3 $VERSION = '0.2';
20 };
21
22 1 ▶sub order_by(&;@) {
Subroutine order_by: called CC=1 SCAR=0.0
Pod documented
23 1 ▶ List::OrderBy::Container->new(sub { $_[0] <=> $_[1] }, @_)->get();
Subroutine __ANON__: called CC=1 SCAR=0.0
24 }
25
26 0 ▶sub order_by_desc(&;@) {
Subroutine order_by_desc: not called CC=1 SCAR=6.9
Pod documented
27 0 ▶ List::OrderBy::Container->new(sub { $_[1] <=> $_[0] }, @_)->get();
Subroutine __ANON__: not called CC=1 SCAR=6.9
28 }
29
30 0 ▶sub order_cmp_by_desc(&;@) {
Subroutine order_cmp_by_desc: not called CC=1 SCAR=6.9
Pod documented
31 0 ▶ List::OrderBy::Container->new(sub { $_[1] cmp $_[0] }, @_)->get();
Subroutine __ANON__: not called CC=1 SCAR=6.9
32 }
33
34 0 ▶sub order_cmp_by(&;@) {
Subroutine order_cmp_by: not called CC=1 SCAR=6.9
Pod documented
35 0 ▶ List::OrderBy::Container->new(sub { $_[0] cmp $_[1] }, @_)->get();
Subroutine __ANON__: not called CC=1 SCAR=6.9
36 }
37
38 0 ▶sub then_by(&;@) {
Subroutine then_by: not called CC=1 SCAR=6.9
Pod documented
39 0 ▶ List::OrderBy::Container->new(sub { $_[0] <=> $_[1] }, @_)
Subroutine __ANON__: not called CC=1 SCAR=6.9
40 }
41
42 0 ▶sub then_by_desc(&;@) {
Subroutine then_by_desc: not called CC=1 SCAR=6.9
Pod documented
43 0 ▶ List::OrderBy::Container->new(sub { $_[1] <=> $_[0] }, @_)
Subroutine __ANON__: not called CC=1 SCAR=6.9
44 }
45
46 0 ▶sub then_cmp_by(&;@) {
Subroutine then_cmp_by: not called CC=1 SCAR=6.9
Pod documented
47 0 ▶ List::OrderBy::Container->new(sub { $_[0] cmp $_[1] }, @_)
Subroutine __ANON__: not called CC=1 SCAR=6.9
48 }
49
50 0 ▶sub then_cmp_by_desc(&;@) {
Subroutine then_cmp_by_desc: not called CC=1 SCAR=6.9
Pod documented
51 0 ▶ List::OrderBy::Container->new(sub { $_[1] cmp $_[0] }, @_)
Subroutine __ANON__: not called CC=1 SCAR=6.9
52 }
53
54 package List::OrderBy::Container;
55 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
57
58 1 ▶sub new {
Subroutine new: called CC=3 SCAR=12.3
59 1 my $class = shift;
60 1 my $key_comparer = shift;
61 1 my $key_extractor = shift;
62 1 my @list = @_;
63 1 my $self = bless { }, $class;
64
65 # Chained then_by calls are merged into a single object
66 1 ▶ if (@list and UNIVERSAL::isa($list[0], __PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
if (@list and UNIVERSAL::isa($list[0], __PACKAGE__)) { } 0 1 1
Condition33%1/3
@list and UNIVERSAL::isa($list[0], __PACKAGE__)
!ll&&!rl&&r
010
MC/DC0%0/2
@list and UNIVERSAL::isa($list[0], __PACKAGE__)
@list UNIVERSAL::isa($list[0], __PACKAGE__)
Truth table33%1/3
A and B
A= @listB= UNIVERSAL::isa($list[0], __PACKAGE__)
ABresult
0X0
100
111
67
68 # Copy reference to the list and the existing key extractors
69 0 $self->{key_extractors} = [ @{ $list[0]->{key_extractors} } ];
70 0 $self->{key_comparers} = [ @{ $list[0]->{key_comparers} } ];
71 0 $self->{list} = $list[0]->{list};
72
73 } else {
74 1 $self->{list} = \@list;
75 }
76
77 # A sequence `order_by { ... } then_by { ... }` is evaluated from
78 # the right to the left, and to make the first element the first
79 # extractor to be applied, elements are unshifted into the list.
80
81 1 unshift @{ $self->{key_extractors} }, $key_extractor;
82 1 unshift @{ $self->{key_comparers} }, $key_comparer;
83
84 1 $self;
85 }
86
87 1 ▶sub get {
Subroutine get: called CC=3 SCAR=11.0
88 1 my $merged = shift;
89
90 # Extract all keys
91 my @keys = map {
92 1 my $code = $_;
93
94 # When a sub is used as key extractor instead of a code block,
95 # authors would expect the data passed in as argument, so this
96 # does both, pass through $_ and pass through the parameter.
97
98 1 [ map { scalar $code->($_); } @{ $merged->{list} } ]
99
100 1 } @{ $merged->{key_extractors} };
101
102 my @sorted_indices = sort {
103 3 my $compare = 0;
104 3 ▶ for (my $ix = 0; !$compare and $ix <= $#keys; ++$ix) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $compare and $ix <= $#keys) 3 3 6
Condition66%2/3
not $compare and $ix <= $#keys
!ll&&!rl&&r
303
MC/DC50%1/2
not $compare and $ix <= $#keys
not $compare $ix <= $#keys
Truth table66%2/3
not(A) and B
A= not $compareB= $ix <= $#keys
ABresult
0X0
100
111
105 3 $compare = $merged->{key_comparers}[$ix]
106 ->($keys[$ix]->[$a], $keys[$ix]->[$b]);
107 }
108 $compare;
109 1 } 0 .. $#{ $merged->{list} };
110
111 1 return map { $merged->{list}[$_] } @sorted_indices;
112 }
113
114 3 1;
115
116 __END__
↑ Summary