blib/lib/Mail/AddressSort.pm

filter: Statementstmt 7.4% 8 / 107 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 16.6% 2 / 12 Podpod 100% 7 / 7 totaltotal 10.2% 17 / 166 CC 22 SCAR 43.4
CC
22
Cov
6%
batches
182.0
_printBatch
20.0
printBatches
12.0
CRAP 76.9
↑ Summary
1 # Mail::AddressSort
2 # Sorts an array of email addresses
3 # for the purpose of expediting delivery of multiple recipients
4  
5 #
6 # Package Definition
7 #
8  
9 package Mail::AddressSort;
10  
11 #
12 # Compiler Directives
13 #
14  
15 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 #
18 # Global Variables
19 #
20  
21 1 ▶use vars qw/$VERSION/;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 1 $VERSION=1.0;
24  
25 #
26 # Subroutines
27 #
28  
29 # Constructor method
30 sub new
31 0 ▶{
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
32 0 my ($class)=shift;
33 0 my ($object);
34 0 $object={};
35 0 bless ($object);
36 0 return($object);
37 }
38  
39 # Add addresses to the list
40 sub input
41 0 ▶{
Subroutine input: not called CC=2 SCAR=17.9
Pod documented
42 0 my ($self)=shift;
43 0 my ($addr,$parsed);
44 0 ▶while (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
45 {
46 0 ($addr)=shift;
47 0 $parsed=_sortFmt($addr);
48 0 $self->{list}->{$parsed}=$addr;
49 }
50 0 return();
51 }
52  
53 # Return all addresses in one sorted array
54 sub sorted
55 0 ▶{
Subroutine sorted: not called CC=2 SCAR=17.9
Pod documented
56 0 my ($self)=shift;
57 0 my ($key,@array);
58 0 foreach $key (sort(keys(%{$self->{list}})))
59 {
60 0 push(@array,$self->{list}->{$key});
61 }
62 0 return(@array);
63 }
64  
65 # Return a count of the number of addresses in the list
66 sub count
67 0 ▶{
Subroutine count: not called CC=1 SCAR=6.9
Pod documented
68 0 my ($self)=shift;
69 0 return scalar(keys(%{$self->{list}}));
70 }
71  
72 # Return all addresses in an array of arrays limited by a
73 # maximun number of addresses per array and also by domain if necessary
74  
75 sub batches
76 0 ▶{
Subroutine batches: not called CC=13 SCAR=52.0
Pod documented
77 0 my ($self)=shift;
78 0 my ($arg,$max,$hostSplit,$count,$batchNum,$host,$lastHost,$address
79 ,$out,@list,$batches);
80 0 ▶while (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
81 {
82 0 ($arg)=shift;
83 0 ▶ if ($arg eq "-maxRecipients")
Branch: 2 branches
BranchTrueFalseTotal
if ($arg eq '-maxRecipients') { } 0 0 0
elsif ($arg eq '-byHost') { } 0 0 0
84 {
85 0 $max=shift;
86 0 print("MaxRecp passed as $max\n");
87 } elsif ($arg eq "-byHost")
88 {
89 0 $hostSplit=1;
90 }
91 }
92 0 ▶if ($max <=0)
Branch: 1 branch
BranchTrueFalseTotal
if ($max <= 0) 0 0 0
93 {
94 0 my ($num)=$self->count();
95 0 ▶ if ($num < 100)
Branch: 1 branch
BranchTrueFalseTotal
if ($num < 100) { } 0 0 0
96 {
97 0 $max=int($num/5);
98 0 ▶ $max++ if ($max ==0);
Branch: 1 branch
BranchTrueFalseTotal
if $max == 0 0 0 0
99 } else {
100 0 $max=100;
101 }
102 }
103 0 print("MaxRecp set to $max\n");
104 0 $max++;
105 0 $batches=[];
106 0 $count=1;
107 0 $batchNum=1;
108 0 foreach $address (sort(keys(%{$self->{list}})))
109 {
110 0 $out=$self->{list}->{$address};
111 0 ($host)=split(/\@/,$address);
112 0 ▶ $lastHost=$host unless ($lastHost);
Branch: 1 branch
BranchTrueFalseTotal
unless $lastHost 0 0 0
113 0 ▶ if ($count == $max || ($host ne $lastHost && $hostSplit))
Branch: 1 branch
BranchTrueFalseTotal
if ($count == $max or $host ne $lastHost and $hostSplit) 0 0 0
Condition0%0/6
$count == $max or $host ne $lastHost and $hostSplit
l!l&&r!l&&!r
000
$host ne $lastHost and $hostSplit
!ll&&!rl&&r
000
MC/DC0%0/3
$count == $max or $host ne $lastHost and $hostSplit
$count == $max $host ne $lastHost $hostSplit
Truth table0%0/4
A or B and C
A= $count == $maxB= $host ne $lastHostC= $hostSplit
ABCresult
1XX1
0111
00X0
0100
114 {
115 0 push(@{$batches},[ @list ]);
116 0 (@list)=();
117 0 $batchNum++;
118 0 $count=1;
119 }
120 0 push(@list,$out);
121 0 $count++;
122 0 $lastHost=$host;
123 }
124 0 ▶if (@list)
Branch: 1 branch
BranchTrueFalseTotal
if (@list) 0 0 0
125 {
126 0 push(@{$batches},[ @list ]);
127 }
128 0 return($batches);
129 }
130  
131 sub printBatches
132 0 ▶{
Subroutine printBatches: not called CC=3 SCAR=24.8
Pod documented
133 0 my ($self,$batch,$number)=@_;
134 0 my ($sub,$count,$size,$addr);
135 0 ▶if ($number)
Branch: 1 branch
BranchTrueFalseTotal
if ($number) { } 0 0 0
136 {
137 0 _printBatch($batch,$number);
138 } else {
139 0 $count=scalar(@{$batch});
140 0 ▶ for ($sub=1;$sub<=$count;$sub++)
Branch: 1 branch
BranchTrueFalseTotal
if ($sub <= $count) 0 0 0
141 {
142 0 _printBatch($batch,$sub);
143 }
144 }
145 0 return;
146 }
147  
148 sub printHostCount
149 0 ▶{
Subroutine printHostCount: not called CC=2 SCAR=17.9
Pod documented
150 0 my ($self)=shift;
151 0 my (%report,$host,$domain);
152 0 (%report)=$self->_hostCount();
153 0 foreach $host (sort(keys(%report)))
154 {
155 0 $domain=join(".",reverse(split(/\./,$host)));
156 0 printf("%5d %-40s\n",$report{$host},$domain);
157 }
158 0 return;
159 }
160  
161 #
162 # Hidden Subroutines
163 #
164  
165 sub _sortFmt
166 0 ▶{
Subroutine _sortFmt: not called CC=1 SCAR=6.9
167 0 my ($addr)=shift;
168 0 my ($parsed,$user,$domain,@dp,@rdp);
169 0 ($user,$domain)=split(/\@/,$addr);
170 0 ($domain)=lc($domain);
171 0 (@dp)=split(/\./,$domain);
172 0 $parsed=join(".",reverse(@dp))."\@".$user;
173 0 return($parsed);
174 }
175  
176 sub _printBatch
177 0 ▶{
Subroutine _printBatch: not called CC=4 SCAR=30.0
178 0 my ($batch,$number)=@_;
179 0 my ($count,$size,$address);
180 0 $count=$#$batch;
181 0 ▶return undef if ($number < 1 || $ number > $count);
Branch: 1 branch
BranchTrueFalseTotal
if $number < 1 or $number > $count 0 0 0
Condition0%0/3
$number < 1 or $number > $count
l!l&&r!l&&!r
000
MC/DC0%0/2
$number < 1 or $number > $count
$number < 1 $number > $count
Truth table0%0/3
A or B
A= $number < 1B= $number > $count
ABresult
1X1
011
000
182 0 $size=scalar(@{$batch->[$number-1]});
183 0 print("Batch $number of $count ($size entries)\n");
184 0 foreach $address (@{$batch->[$number-1]})
185 {
186 0 print("\t$address\n");
187 }
188 0 return;
189 }
190  
191 sub _hostCount
192 0 ▶{
Subroutine _hostCount: not called CC=2 SCAR=17.9
193 0 my ($self)=shift;
194 0 my ($entry,$domain,%count);
195 0 foreach $entry (keys(%{$self->{list}}))
196 {
197 0 ($domain)=split(/\@/,$entry);
198 0 $count{$domain}++;
199 }
200 0 return(%count);
201 }
202  
203 #
204 # Exit Area
205 #
206 1 1;
207  
208 __END__
↑ Summary