blib/lib/Sort/ArrayOfArrays.pm

filter: Statementstmt 73.1% 79 / 108 Branchbran 46.0% 35 / 76 Conditioncond 36.8% 14 / 38 MC/DCmcdc 8.3% 2 / 24 Subroutinesub 100% 7 / 7 Podpod 0.0% 0 / 4 totaltotal 53.3% 137 / 257 CC 54 SCAR 52.3
CC
54
Cov
58%
make_sort_code_text
262.5
sort_it
13.0
make_sort_code_ref
4.9
CRAP 186.1
↑ Summary
1 #!/usr/bin/perl -w
2  
3 package Sort::ArrayOfArrays;
4  
5 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use vars qw(@ISA @EXPORT_OK $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 @ISA = qw(Exporter);
9 6 @EXPORT_OK = qw(sort_it);
10 6 $VERSION = '1.00';
11  
12 5 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
13 5 my $type = shift;
14 5 ▶ my @PASSED_ARGS = (ref $_[0] eq 'HASH') ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 5 0 5
15 5 my @DEFAULT_ARGS = (
16 header_row => 0,
17 results => [],
18 sort_column => '',
19 sort_code => [],
20 );
21 5 my %ARGS = (@DEFAULT_ARGS, @PASSED_ARGS);
22 5 my $self = bless \%ARGS, $type;
23 5 return $self;
24 }
25  
26 5 ▶sub sort_it {
Subroutine sort_it: called CC=9 SCAR=25.6
Pod undocumented
27 5 my $self = shift;
28  
29 5 ▶ unless(ref $self eq __PACKAGE__) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self eq __PACKAGE__) 0 5 5
30 0 $self = __PACKAGE__->new({
31 results => $self,
32 sort_column => $_[0],
33 sort_code => $_[1],
34 header_row => $_[2],
35 });
36 }
37  
38 5 ▶ die "\$self->{results} is required" unless($self->{results});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"results"} 0 5 5
39 5 ▶ die "\$self->{results} needs to be an array of arrays" unless(ref $self->{results} eq 'ARRAY' && $self->{results}->[0] && ref $self->{results}->[0] eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->{"results"} eq "ARRAY" and $self->{"results"}[0] and ref $self->{"results"}[0] eq "ARRAY" 0 5 5
Condition33%2/6
ref $self->{"results"} eq "ARRAY" and $self->{"results"}[0] and ref $self->{"results"}[0] eq "ARRAY"
!ll&&!rl&&r
005
ref $self->{"results"} eq "ARRAY" and $self->{"results"}[0]
!ll&&!rl&&r
005
MC/DC0%0/3
ref $self->{"results"} eq "ARRAY" and $self->{"results"}[0] and ref $self->{"results"}[0] eq "ARRAY"
ref $self->{"results"} eq "ARRAY" $self->{"results"}[0] ref $self->{"results"}[0] eq "ARRAY"
Truth table25%1/4
A and B and C
A= ref $self->{"results"} eq "ARRAY"B= $self->{"results"}[0]C= ref $self->{"results"}[0] eq "ARRAY"
ABCresult
0XX0
10X0
1100
1111
40 5 ▶ if($self->{header_row}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"header_row"}) 0 5 5
41 0 $self->{zero_row} = shift @{$self->{results}};
42 }
43  
44 5 $self->{rows} = (@{$self->{results}} - 1);
45  
46 5 $self->make_sort_code_ref;
47  
48 5 my $sort_method = $self->{sort_code_ref};
49 5 my @temp = (0 .. $self->{rows});
50 5 @temp = sort $sort_method @temp;
51  
52 5 my $return = [];
53  
54 5 ▶ for(my $i=0;$i<@temp;$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @temp) 15 5 20
55 15 $return->[$i] = $self->{results}->[$temp[$i]];
56 }
57  
58 5 ▶ if($self->{header_row}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"header_row"}) 0 5 5
59 0 unshift @{$return}, $self->{zero_row};
60 0 delete $self->{zero_row};
61 }
62 5 $self->{results} = $return;
63 5 return $return;
64 }
65  
66 5 ▶sub make_sort_code_text {
Subroutine make_sort_code_text: called CC=42 SCAR=55.7
Pod undocumented
67 5 my $self = shift;
68 5 ▶ return if($self->{sort_code_text});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"sort_code_text"} 0 5 5
69  
70 5 my $total_sorts = 0;
71 5 foreach my $this_sort_column (split /\s*,\s*/, $self->{sort_column}) {
72 7 $total_sorts++;
73 7 my $col = abs($this_sort_column);
74 7 ▶ if($self->{sort_code} && ref $self->{sort_code}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"sort_code"} and ref $self->{"sort_code"}) 7 0 7
Condition33%1/3
$self->{"sort_code"} and ref $self->{"sort_code"}
!ll&&!rl&&r
007
MC/DC0%0/2
$self->{"sort_code"} and ref $self->{"sort_code"}
$self->{"sort_code"} ref $self->{"sort_code"}
Truth table33%1/3
A and B
A= $self->{"sort_code"}B= ref $self->{"sort_code"}
ABresult
0X0
100
111
75 7 ▶ if(ref $self->{sort_code} eq 'HASH' && $self->{sort_code}->{$col}) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $self->{'sort_code'} eq 'HASH' and $self->{'sort_code'}{$col}) { } 0 7 7
elsif (ref $self->{'sort_code'} eq 'ARRAY' and $self->{'sort_code'}[$col]) { } 0 7 7
Condition33%1/3
ref $self->{"sort_code"} eq "HASH" and $self->{"sort_code"}{$col}
!ll&&!rl&&r
700
MC/DC0%0/2
ref $self->{"sort_code"} eq "HASH" and $self->{"sort_code"}{$col}
ref $self->{"sort_code"} eq "HASH" $self->{"sort_code"}{$col}
Truth table33%1/3
A and B
A= ref $self->{"sort_code"} eq "HASH"B= $self->{"sort_code"}{$col}
ABresult
0X0
100
111
76 0 ▶ if(ref $self->{sort_code}->{$col}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"sort_code"}{$col}) 0 0 0
77 0 ▶ die "any ref for \$self->{sort_code}->{$col} needs to be an CODE ref" unless(ref $self->{sort_code}->{$col} eq 'CODE');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->{"sort_code"}{$col} eq "CODE" 0 0 0
78 0 $self->{sort_code_ref} = $self->{sort_code}->{$col};
79 0 return;
80 }
81 ▶ } elsif(ref $self->{sort_code} eq 'ARRAY' && $self->{sort_code}->[$col]) {
Condition33%1/3
ref $self->{"sort_code"} eq "ARRAY" and $self->{"sort_code"}[$col]
!ll&&!rl&&r
070
MC/DC0%0/2
ref $self->{"sort_code"} eq "ARRAY" and $self->{"sort_code"}[$col]
ref $self->{"sort_code"} eq "ARRAY" $self->{"sort_code"}[$col]
Truth table33%1/3
A and B
A= ref $self->{"sort_code"} eq "ARRAY"B= $self->{"sort_code"}[$col]
ABresult
0X0
100
111
82 0 ▶ if(ref $self->{sort_code}->[$col]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"sort_code"}[$col]) 0 0 0
83 0 ▶ die "any ref for \$self->{sort_code}->[$col] needs to be an CODE ref" unless(ref $self->{sort_code}->[$col] eq 'CODE');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->{"sort_code"}[$col] eq "CODE" 0 0 0
84 0 $self->{sort_code_ref} = $self->{sort_code}->[$col];
85 0 return;
86 }
87 }
88 }
89 }
90  
91 5 $self->{sort_code_text} = "sub {\n";#"}"
92 5 my $this_sort = 0;
93 5 foreach my $this_sort_column (split /\s*,\s*/, $self->{sort_column}) {
94 7 $this_sort++;
95 7 my $this_sort_method;
96 7 ▶ if($self->{sort_code} && ref $self->{sort_code}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"sort_code"} and ref $self->{"sort_code"}) 7 0 7
Condition33%1/3
$self->{"sort_code"} and ref $self->{"sort_code"}
!ll&&!rl&&r
007
MC/DC0%0/2
$self->{"sort_code"} and ref $self->{"sort_code"}
$self->{"sort_code"} ref $self->{"sort_code"}
Truth table33%1/3
A and B
A= $self->{"sort_code"}B= ref $self->{"sort_code"}
ABresult
0X0
100
111
97 7 ▶ if(ref $self->{sort_code} eq 'HASH' && $self->{sort_code}->{$this_sort_column}) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $self->{'sort_code'} eq 'HASH' and $self->{'sort_code'}{$this_sort_column}) { } 0 7 7
elsif (ref $self->{'sort_code'} eq 'ARRAY' and $self->{'sort_code'}[$this_sort_column]) { } 0 7 7
Condition33%1/3
ref $self->{"sort_code"} eq "HASH" and $self->{"sort_code"}{$this_sort_column}
!ll&&!rl&&r
700
MC/DC0%0/2
ref $self->{"sort_code"} eq "HASH" and $self->{"sort_code"}{$this_sort_column}
ref $self->{"sort_code"} eq "HASH" $self->{"sort_code"}{$this_sort_column}
Truth table33%1/3
A and B
A= ref $self->{"sort_code"} eq "HASH"B= $self->{"sort_code"}{$this_sort_column}
ABresult
0X0
100
111
98 0 $this_sort_method = $self->{sort_code}->{$this_sort_column};
99 ▶ } elsif(ref $self->{sort_code} eq 'ARRAY' && $self->{sort_code}->[$this_sort_column]) {
Condition33%1/3
ref $self->{"sort_code"} eq "ARRAY" and $self->{"sort_code"}[$this_sort_column]
!ll&&!rl&&r
070
MC/DC0%0/2
ref $self->{"sort_code"} eq "ARRAY" and $self->{"sort_code"}[$this_sort_column]
ref $self->{"sort_code"} eq "ARRAY" $self->{"sort_code"}[$this_sort_column]
Truth table33%1/3
A and B
A= ref $self->{"sort_code"} eq "ARRAY"B= $self->{"sort_code"}[$this_sort_column]
ABresult
0X0
100
111
100 0 $this_sort_method = $self->{sort_code}->[$this_sort_column];
101 }
102 }
103 7 my $this_toggle = $this_sort_column =~ s/^\-(\d+)$/$1/;
104  
105 7 ▶ unless($this_sort_method) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($this_sort_method) 7 0 7
106 7 $this_sort_method = 'aa';
107 7 ▶ for(my $i=0;$i<=$self->{rows};$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $self->{"rows"}) 7 0 7
108 7 ▶ if($self->{results}->[$i]->[$this_sort_column] =~ /[^0-9.\-+ ]/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'results'}[$i][$this_sort_column] =~ /[^0-9.\-+ ]/) { } 6 1 7
elsif ($self->{'results'}[$i][$this_sort_column] =~ /^[0-9.\-+]+$/) { } 1 0 1
109 6 last;
110 } elsif($self->{results}->[$i]->[$this_sort_column] =~ /^[0-9.\-+]+$/) {
111 1 ▶ next if($self->{results}->[$i]->[$this_sort_column] =~ /^\.+$/);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"results"}[$i][$this_sort_column] =~ /^\.+$/ 0 1 1
112 1 $this_sort_method = 'na';
113 1 last;
114 }
115 }
116 }
117  
118 7 ▶ if($total_sorts == $this_sort) {
Branch: 1 branch
BranchTrueFalseTotal
if ($total_sorts == $this_sort) 5 2 7
119 5 ▶ if($this_toggle && $this_sort_method =~ /^.a$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($this_toggle and $this_sort_method =~ /^.a$/) { } 4 1 5
elsif ($this_toggle and $this_sort_method =~ /^.d$/) { } 0 2 2
Condition66%2/3
$this_toggle and $this_sort_method =~ /^.a$/
!ll&&!rl&&r
104
MC/DC50%1/2
$this_toggle and $this_sort_method =~ /^.a$/
$this_toggle $this_sort_method =~ /^.a$/
Truth table66%2/3
A and B
A= $this_toggleB= $this_sort_method =~ /^.a$/
ABresult
0X0
100
111
120 4 $this_sort_method =~ s/^(.)a$/$1d/i;
121 ▶ } elsif($this_toggle && $this_sort_method =~ /^.d$/) {
Condition33%1/3
$this_toggle and $this_sort_method =~ /^.d$/
!ll&&!rl&&r
100
MC/DC0%0/2
$this_toggle and $this_sort_method =~ /^.d$/
$this_toggle $this_sort_method =~ /^.d$/
Truth table33%1/3
A and B
A= $this_toggleB= $this_sort_method =~ /^.d$/
ABresult
0X0
100
111
122 0 $this_sort_method =~ s/^(.)d$/$1a/i;
123 }
124 }
125  
126 # I just change stuff of type link to stuff of type regex, with the regex below
127 7 ▶ if ($this_sort_method =~ s/^l(.)$/r$1/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this_sort_method =~ s/^l(.)$/r$1/i) 0 7 7
128 0 ▶ die "need regex for sort_method on column $this_sort_column" unless(exists $self->{sort_method_regex}->{$this_sort_column});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"sort_method_regex"}{$this_sort_column} 0 0 0
129 0 $self->{sort_method_regex}->{$this_sort_column} = qr@<a\s+href[^>]+?>(.+?)</a>@i;
130 }
131  
132 7 my $symbol;
133 7 my $special_sort_method = 0;
134 7 ▶ if($this_sort_method =~ /^a.$/i) {
Branch: 4 branches
BranchTrueFalseTotal
if ($this_sort_method =~ /^a.$/i) { } 6 1 7
elsif ($this_sort_method =~ /^n.$/i) { } 1 0 1
elsif ($this_sort_method =~ /^t.$/i) { } 0 0 0
elsif ($this_sort_method =~ /^r.$/i) { } 0 0 0
135 6 $symbol = 'cmp';
136 } elsif ($this_sort_method =~ /^n.$/i) {
137 1 $symbol = '<=>';
138 } elsif ($this_sort_method =~ /^t.$/i) {
139 0 $special_sort_method = 1;
140 } elsif ($this_sort_method =~ /^r.$/i) {
141 0 ▶ die "need regex for sort_method on column $this_sort_column" unless($self->{sort_method_regex}->{$this_sort_column});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"sort_method_regex"}{$this_sort_column} 0 0 0
142 0 ▶ $symbol = $self->{sort_method_symbol}->{$this_sort_column} || 'cmp';
Condition0%0/2
$self->{'sort_method_symbol'}{$this_sort_column} || 'cmp'
l!l
00
MC/DC0%0/1
$self->{'sort_method_symbol'}{$this_sort_column} || 'cmp'
$self->{'sort_method_symbol'}{$this_sort_column}
Truth table0%0/2
A
A= $self->{'sort_method_symbol'}{$this_sort_column}
Aresult
11
00
143 0 $self->{sort_code_text} .= <<SORT_METHOD;
144 my (\$_a) = \$self->{results}->[\$a]->[$this_sort_column] =~ /$self->{sort_method_regex}->{$this_sort_column}/;
145 my (\$_b) = \$self->{results}->[\$b]->[$this_sort_column] =~ /$self->{sort_method_regex}->{$this_sort_column}/;
146 SORT_METHOD
147  
148 0 ▶ if($self->{sort_code}->[$this_sort_column] =~ /^.d$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'sort_code'}[$this_sort_column] =~ /^.d$/i) { } 0 0 0
149 0 $self->{sort_code_text} = "{\n $self->{sort_code_text}\n \$_b $symbol \$_a\n}";
150 } else {
151 0 $self->{sort_code_text} = "{\n $self->{sort_code_text}\n \$_a $symbol \$_b\n}";
152 }
153 0 $special_sort_method = 1;
154 } else {
155 0 die "unknown sort method $this_sort_method";
156 }
157  
158 7 ▶ if(!$special_sort_method && $this_sort_method =~ /^.a$/i) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $special_sort_method and $this_sort_method =~ /^.a$/i) { } 3 4 7
elsif ($this_sort_method =~ /^.d$/i) { } 4 0 4
Condition66%2/3
not $special_sort_method and $this_sort_method =~ /^.a$/i
!ll&&!rl&&r
043
MC/DC50%1/2
not $special_sort_method and $this_sort_method =~ /^.a$/i
not $special_sort_method $this_sort_method =~ /^.a$/i
Truth table66%2/3
not(A) and B
A= not $special_sort_methodB= $this_sort_method =~ /^.a$/i
ABresult
0X0
100
111
159 3 $self->{sort_code_text} .= ' $self->{results}->[$a]->[' . "$this_sort_column" .'] ' . "$symbol" . ' $self->{results}->[$b]->[' . "$this_sort_column" . "] ||\n";
160 } elsif ($this_sort_method =~ /^.d$/i) {
161 4 $self->{sort_code_text} .= ' $self->{results}->[$b]->[' . "$this_sort_column" .'] ' . "$symbol" . ' $self->{results}->[$a]->[' . "$this_sort_column" . "] ||\n";
162 }
163 }
164 # sadly, this line is to unbug vi {
165 5 $self->{sort_code_text} =~ s/\s*\|\|\s*$/\n\}/;
166 }
167  
168 5 ▶sub make_sort_code_ref {
Subroutine make_sort_code_ref: called CC=4 SCAR=15.9
Pod undocumented
169 5 my $self = shift;
170 5 ▶ return if($self->{sort_code_ref} && ref $self->{sort_code_ref} eq 'CODE');
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"sort_code_ref"} and ref $self->{"sort_code_ref"} eq "CODE" 0 5 5
Condition33%1/3
$self->{"sort_code_ref"} and ref $self->{"sort_code_ref"} eq "CODE"
!ll&&!rl&&r
500
MC/DC0%0/2
$self->{"sort_code_ref"} and ref $self->{"sort_code_ref"} eq "CODE"
$self->{"sort_code_ref"} ref $self->{"sort_code_ref"} eq "CODE"
Truth table33%1/3
A and B
A= $self->{"sort_code_ref"}B= ref $self->{"sort_code_ref"} eq "CODE"
ABresult
0X0
100
111
171 5 $self->make_sort_code_text;
172 5 $self->{sort_code_ref} = eval $self->{sort_code_text};
173 5 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 5 5
174 0 die "$self->{sort_code_text}\ndid not lead to a valid CODE ref";
175 }
176 }
177  
178 6 1;
179  
180 __END__
↑ Summary