Postcodes.pm

filter: Statementstmt 42.3% 140 / 331 Branchbran 22.2% 53 / 238 Conditioncond 48.7% 19 / 39 MC/DCmcdc 37.5% 9 / 24 Subroutinesub 53.8% 21 / 39 Podpod 100% 31 / 31 totaltotal 38.8% 273 / 702 CC 139 SCAR 76.7
CC
139
Cov
35%
_selection
4519.9
_selection_loop
42.0
_verify_selectionlist
24.9
CRAP 2152.3
↑ Summary Postcodes/Update.pm »
1 package Geo::Postcodes;
2  
3 #################################################################################
4 # #
5 # This file is written by Arne Sommer - perl@bbop.org #
6 # #
7 #################################################################################
8  
9 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 4 our $VERSION = '0.32';
13  
14 ## Which methods are available ##################################################
15  
16 4 my @valid_fields = qw(postcode location borough county type type_verbose owner
17 address); # Used by the 'get_fields' procedure.
18  
19 4 my %valid_fields;
20  
21 4 foreach (@valid_fields)
22 {
23 32 $valid_fields{$_} = 1; # Used by 'is_field' for easy lookup.
24 }
25  
26 ## Type Description #############################################################
27  
28 4 my %typedesc;
29  
30 4 $typedesc{BX} = "Post Office box";
31 4 $typedesc{ST} = "Street address";
32 4 $typedesc{SX} = "Service box";
33 4 $typedesc{IO} = "Individual owner";
34 4 $typedesc{STBX} = "Street Address and Post Office box";
35 4 $typedesc{MU} = "Multiple usage";
36 4 $typedesc{PP} = "Porto Paye receiver";
37  
38 4 $typedesc{PN} = "Place name";
39  
40 ## OO Methods ###################################################################
41  
42 4 our %postcode_of;
43 4 our %location_of;
44 4 our %borough_of;
45 4 our %county_of;
46 4 our %type_of;
47 4 our %owner_of;
48 4 our %address_of;
49  
50 sub new
51 0 ▶{
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
52 0 my $class = shift;
53 0 my $postcode = shift;
54 0 my $self = shift; # Allow for subclassing.
55  
56 0 ▶ return unless valid($postcode);
Branch: 1 branch
BranchTrueFalseTotal
unless valid($postcode) 0 0 0
57  
58 0 ▶ unless ($self)
Branch: 1 branch
BranchTrueFalseTotal
unless ($self) 0 0 0
59 {
60 0 $self = bless \(my $dummy), $class;
61 }
62  
63 0 $postcode_of {$self} = $postcode;
64 0 $location_of {$self} = location_of ($postcode);
65 0 $borough_of {$self} = borough_of ($postcode);
66 0 $county_of {$self} = county_of ($postcode);
67 0 $type_of {$self} = type_of ($postcode);
68 0 $owner_of {$self} = owner_of ($postcode);
69 0 $address_of {$self} = address_of ($postcode);
70 0 return $self;
71 }
72  
73 sub DESTROY
74 0 ▶{
Subroutine DESTROY: not called CC=1 SCAR=6.9
75 0 my $object_id = $_[0];
76  
77 0 delete $postcode_of {$object_id};
78 0 delete $location_of {$object_id};
79 0 delete $borough_of {$object_id};
80 0 delete $county_of {$object_id};
81 0 delete $type_of {$object_id};
82 0 delete $owner_of {$object_id};
83 0 delete $address_of {$object_id};
84 }
85  
86 sub postcode
87 0 ▶{
Subroutine postcode: not called CC=3 SCAR=24.8
Pod documented
88 0 my $self = shift;
89 0 ▶ return unless defined $self;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self 0 0 0
90 0 ▶ return $postcode_of{$self} if exists $postcode_of{$self};
Branch: 1 branch
BranchTrueFalseTotal
if exists $Geo::Postcodes::postcode_of{$self} 0 0 0
91 0 return;
92 }
93  
94 sub location
95 0 ▶{
Subroutine location: not called CC=3 SCAR=24.8
Pod documented
96 0 my $self = shift;
97 0 ▶ return unless defined $self;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self 0 0 0
98 0 ▶ return $location_of{$self} if exists $location_of{$self};
Branch: 1 branch
BranchTrueFalseTotal
if exists $Geo::Postcodes::location_of{$self} 0 0 0
99 0 return;
100 }
101  
102 sub borough
103 0 ▶{
Subroutine borough: not called CC=3 SCAR=24.8
Pod documented
104 0 my $self = shift;
105 0 ▶ return unless defined $self;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self 0 0 0
106 0 ▶ return $borough_of{$self} if exists $borough_of{$self};
Branch: 1 branch
BranchTrueFalseTotal
if exists $Geo::Postcodes::borough_of{$self} 0 0 0
107 0 return;
108 }
109  
110 sub county
111 0 ▶{
Subroutine county: not called CC=3 SCAR=24.8
Pod documented
112 0 my $self = shift;
113 0 ▶ return unless defined $self;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self 0 0 0
114 0 ▶ return $county_of{$self} if exists $county_of{$self};
Branch: 1 branch
BranchTrueFalseTotal
if exists $Geo::Postcodes::county_of{$self} 0 0 0
115 0 return;
116 }
117  
118 sub type
119 0 ▶{
Subroutine type: not called CC=3 SCAR=24.8
Pod documented
120 0 my $self = shift;
121 0 ▶ return unless defined $self;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self 0 0 0
122 0 ▶ return $type_of{$self} if exists $type_of{$self};
Branch: 1 branch
BranchTrueFalseTotal
if exists $Geo::Postcodes::type_of{$self} 0 0 0
123 0 return;
124 }
125  
126 sub type_verbose
127 0 ▶{
Subroutine type_verbose: not called CC=4 SCAR=30.0
Pod documented
128 0 my $self = shift;
129 0 ▶ return unless defined $self;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self 0 0 0
130 0 ▶ return unless exists $type_of{$self};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $Geo::Postcodes::type_of{$self} 0 0 0
131 0 ▶ return unless exists $typedesc{$type_of{$self}};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $typedesc{$Geo::Postcodes::type_of{$self}} 0 0 0
132 0 return $typedesc{$type_of{$self}};
133 }
134  
135 sub owner
136 0 ▶{
Subroutine owner: not called CC=3 SCAR=24.8
Pod documented
137 0 my $self = shift;
138 0 ▶ return unless defined $self;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self 0 0 0
139 0 ▶ return $owner_of{$self} if exists $owner_of{$self};
Branch: 1 branch
BranchTrueFalseTotal
if exists $Geo::Postcodes::owner_of{$self} 0 0 0
140 0 return;
141 }
142  
143 sub address
144 0 ▶{
Subroutine address: not called CC=3 SCAR=24.8
Pod documented
145 0 my $self = shift;
146 0 ▶ return unless defined $self;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self 0 0 0
147 0 ▶ return $address_of{$self} if exists $address_of{$self};
Branch: 1 branch
BranchTrueFalseTotal
if exists $Geo::Postcodes::address_of{$self} 0 0 0
148 0 return;
149 }
150  
151 #################################################################################
152  
153 sub get_postcodes ## Return all the postcodes, unsorted.
154 1 ▶{
Subroutine get_postcodes: called CC=1 SCAR=0.0
Pod documented
155 1 return;
156 }
157  
158 sub get_fields ## Get a list of legal fields for the class/object.
159 1 ▶{
Subroutine get_fields: called CC=1 SCAR=0.0
Pod documented
160 1 return @valid_fields;
161 }
162  
163 sub is_field ## Is the specified field legal? Can be called as
164 46 ▶{ ## a procedure, or as a method.
Subroutine is_field: called CC=3 SCAR=11.0
Pod documented
165 46 my $field = shift;
166 46 ▶ $field = shift if $field =~ /Geo::Postcodes/; # Called on an object.
Branch: 1 branch
BranchTrueFalseTotal
if $field =~ /Geo::Postcodes/ 45 1 46
167  
168 46 ▶ return 1 if $valid_fields{$field};
Branch: 1 branch
BranchTrueFalseTotal
if $valid_fields{$field} 35 11 46
169 11 return 0;
170 }
171  
172 ## Global Procedures - Stub Version, Override in your subclass #################
173  
174 sub legal # Is it a legal code, i.e. something that follows the syntax rule.
175 4 ▶{
Subroutine legal: called CC=1 SCAR=0.0
Pod documented
176 4 return 0;
177 }
178  
179 sub valid # Is the code in actual use.
180 4 ▶{
Subroutine valid: called CC=1 SCAR=0.0
Pod documented
181 4 return 0;
182 }
183  
184 sub postcode_of
185 0 ▶{
Subroutine postcode_of: not called CC=1 SCAR=6.9
Pod documented
186 0 return;
187 }
188  
189 sub location_of
190 2 ▶{
Subroutine location_of: called CC=1 SCAR=0.0
Pod documented
191 2 return;
192 }
193  
194 sub borough_of
195 2 ▶{
Subroutine borough_of: called CC=1 SCAR=0.0
Pod documented
196 2 return;
197 }
198  
199 sub county_of
200 2 ▶{
Subroutine county_of: called CC=1 SCAR=0.0
Pod documented
201 2 return;
202 }
203  
204 sub type_of
205 2 ▶{
Subroutine type_of: called CC=1 SCAR=0.0
Pod documented
206 2 return;
207 }
208  
209 sub type_verbose_of
210 0 ▶{
Subroutine type_verbose_of: not called CC=1 SCAR=6.9
Pod documented
211 0 return;
212 }
213  
214 sub owner_of
215 2 ▶{
Subroutine owner_of: called CC=1 SCAR=0.0
Pod documented
216 2 return;
217 }
218  
219 sub address_of
220 2 ▶{
Subroutine address_of: called CC=1 SCAR=0.0
Pod documented
221 2 return;
222 }
223  
224 sub get_types
225 0 ▶{
Subroutine get_types: not called CC=1 SCAR=6.9
Pod documented
226 0 return keys %typedesc;
227 }
228  
229 sub type2verbose
230 0 ▶{
Subroutine type2verbose: not called CC=3 SCAR=24.8
Pod documented
231 0 my $type = shift;
232 0 ▶ return unless $type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 0 0 0
233 0 ▶ return unless exists $typedesc{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $typedesc{$type} 0 0 0
234 0 return $typedesc{$type};
235 }
236  
237 4 my %legal_mode;
238 4 $legal_mode{'and'} = $legal_mode{'and not'} = 1;
239 4 $legal_mode{'nand'} = $legal_mode{'nand not'} = 1;
240 4 $legal_mode{'nor'} = $legal_mode{'nor not'} = 1;
241 4 $legal_mode{'or'} = $legal_mode{'or not'} = 1;
242 4 $legal_mode{'xnor'} = $legal_mode{'xnor not'} = 1;
243 4 $legal_mode{'xor'} = $legal_mode{'xor not'} = 1;
244  
245 4 my %legal_initial_mode;
246 4 $legal_initial_mode{'all'} = $legal_initial_mode{'none'} = 1;
247 4 $legal_initial_mode{'not'} = $legal_initial_mode{'one'} = 1;
248  
249 sub is_legal_selectionmode
250 12 ▶{
Subroutine is_legal_selectionmode: called CC=2 SCAR=6.9
Pod documented
251 12 my $mode = shift;
252 12 ▶ return 1 if $legal_mode{$mode};
Branch: 1 branch
BranchTrueFalseTotal
if $legal_mode{$mode} 6 6 12
253 6 return 0;
254 }
255  
256 sub is_legal_initial_selectionmode
257 60 ▶{
Subroutine is_legal_initial_selectionmode: called CC=3 SCAR=11.0
Pod documented
258 60 my $mode = shift;
259 60 ▶ return 1 if $legal_initial_mode{$mode} or $legal_mode{$mode};
Branch: 1 branch
BranchTrueFalseTotal
if $legal_initial_mode{$mode} or $legal_mode{$mode} 48 12 60
Condition100%3/3
$legal_initial_mode{$mode} or $legal_mode{$mode}
l!l&&r!l&&!r
113712
MC/DC100%2/2
$legal_initial_mode{$mode} or $legal_mode{$mode}
$legal_initial_mode{$mode} $legal_mode{$mode}
Truth table100%3/3
A or B
A= $legal_initial_mode{$mode}B= $legal_mode{$mode}
ABresult
1X1
011
000
260 12 return 0;
261 }
262  
263 sub get_selectionmodes
264 0 ▶{
Subroutine get_selectionmodes: not called CC=1 SCAR=6.9
Pod documented
265 0 return sort keys %legal_mode;
266 }
267  
268 sub get_initial_selectionmodes
269 0 ▶{
Subroutine get_initial_selectionmodes: not called CC=1 SCAR=6.9
Pod documented
270 0 return sort (keys %legal_mode, keys %legal_initial_mode);
271 }
272  
273 sub verify_selectionlist
274 53 ▶{
Subroutine verify_selectionlist: called CC=1 SCAR=0.0
Pod documented
275 53 return Geo::Postcodes::_verify_selectionlist('Geo::Postcodes', @_);
276 # Black magic.
277 }
278  
279 sub _verify_selectionlist
280 53 ▶{
Subroutine _verify_selectionlist: called CC=23 SCAR=32.1
281 53 my $caller_class = shift;
282 53 my @args = @_; # A list of selection arguments to verify
283  
284 53 my $status = 1; # Return value
285 53 my @out = ();
286 53 my @verbose = ();
287  
288 53 ▶ return (0, "No arguments") unless @args;
Branch: 1 branch
BranchTrueFalseTotal
unless @args 0 53 53
289  
290 53 ▶ if (is_legal_initial_selectionmode($args[0]))
Branch: 1 branch
BranchTrueFalseTotal
if (is_legal_initial_selectionmode($args[0])) 41 12 53
291 {
292 41 my $mode = shift @args;
293  
294 41 ▶ if (@args and $args[0] eq "not" and is_legal_initial_selectionmode("$mode $args[0]"))
Branch: 1 branch
BranchTrueFalseTotal
if (@args and $args[0] eq "not" and is_legal_initial_selectionmode("$mode $args[0]")) 5 36 41
Condition83%5/6
@args and $args[0] eq "not" and is_legal_initial_selectionmode("$mode $args[0]")
!ll&&!rl&&r
3605
@args and $args[0] eq "not"
!ll&&!rl&&r
16205
MC/DC66%2/3
@args and $args[0] eq "not" and is_legal_initial_selectionmode("$mode $args[0]")
@args $args[0] eq "not" is_legal_initial_selectionmode("$mode $args[0]")
Truth table75%3/4
A and B and C
A= @argsB= $args[0] eq "not"C= is_legal_initial_selectionmode("$mode $args[0]")
ABCresult
0XX0
10X0
1100
1111
295 {
296 5 $mode = "$mode $args[0]";
297 5 shift @args;
298 }
299  
300 41 push @out, $mode;
301 41 push @verbose, "Mode: '$mode' - ok";
302  
303 41 ▶ return (1, @out) if $mode eq "all" or $mode eq "none";
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "all" or $mode eq "none" 4 37 41
Condition100%3/3
$mode eq "all" or $mode eq "none"
l!l&&r!l&&!r
2237
MC/DC100%2/2
$mode eq "all" or $mode eq "none"
$mode eq "all" $mode eq "none"
Truth table100%3/3
A or B
A= $mode eq "all"B= $mode eq "none"
ABresult
1X1
011
000
304 37 ▶ return (1, @out) if $mode eq "one" and @args == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "one" and @args == 0 1 36 37
Condition100%3/3
$mode eq "one" and @args == 0
!ll&&!rl&&r
3331
MC/DC100%2/2
$mode eq "one" and @args == 0
$mode eq "one" @args == 0
Truth table100%3/3
A and B
A= $mode eq "one"B= @args == 0
ABresult
0X0
100
111
305 # This one can both be used alone, or followed by more.
306  
307 36 ▶ return (0, @verbose, "Missing method/value pair - not ok") unless @args >= 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @args >= 2 14 22 36
308 # Missing method/value pair.
309 }
310  
311 ## Done with the first one
312  
313 34 ▶ while (@args)
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 45 34 79
314 {
315 45 my $argument = shift(@args);
316  
317 45 ▶ if ($caller_class->is_field($argument))
Branch: 3 branches
BranchTrueFalseTotal
if ($caller_class->is_field($argument)) { } 34 11 45
elsif (is_legal_selectionmode($argument)) { } 5 6 11
elsif ($argument eq 'procedure') { } 4 2 6
318 {
319 34 push @out, $argument;
320 34 push @verbose, "Field: '$argument' - ok";
321  
322 34 ▶ if (@args)
Branch: 1 branch
BranchTrueFalseTotal
if (@args) { } 34 0 34
323 {
324 34 $argument = shift(@args);
325 34 push @out, $argument;
326 34 push @verbose, "String: '$argument' - ok";
327 }
328 else
329 {
330 0 push @verbose, "Missing string - not ok"; # The last element was a method.
331 0 $status = 0;
332 0 @args = (); # Terminate the loop
333 }
334 }
335 elsif (is_legal_selectionmode($argument))
336 {
337 5 ▶ if (@args and $args[0] eq "not" and is_legal_selectionmode("$argument $args[0]"))
Branch: 1 branch
BranchTrueFalseTotal
if (@args and $args[0] eq "not" and is_legal_selectionmode("$argument $args[0]")) 1 4 5
Condition66%4/6
@args and $args[0] eq "not" and is_legal_selectionmode("$argument $args[0]")
!ll&&!rl&&r
401
@args and $args[0] eq "not"
!ll&&!rl&&r
041
MC/DC33%1/3
@args and $args[0] eq "not" and is_legal_selectionmode("$argument $args[0]")
@args $args[0] eq "not" is_legal_selectionmode("$argument $args[0]")
Truth table50%2/4
A and B and C
A= @argsB= $args[0] eq "not"C= is_legal_selectionmode("$argument $args[0]")
ABCresult
0XX0
10X0
1100
1111
338 {
339 1 $argument = "$argument $args[0]";
340 1 shift @args;
341 }
342 5 push @out, $argument;
343 5 push @verbose, "Mode: '$argument' - ok";
344  
345 5 ▶ unless (@args >= 2) # Missing method/value pair
Branch: 1 branch
BranchTrueFalseTotal
unless (@args >= 2) 0 5 5
346 {
347 0 push @verbose, "Missing method/value pair - not ok";
348 0 $status = 0;
349 0 @args = (); # Terminate the loop
350 }
351 }
352 elsif ($argument eq 'procedure')
353 {
354 4 push @out, $argument;
355 4 push @verbose, "Field: 'procedure' - ok";
356  
357 4 my $procedure = shift(@args);
358 4 ▶ if (ref $procedure eq "CODE")
Branch: 1 branch
BranchTrueFalseTotal
if (ref $procedure eq 'CODE') { } 4 0 4
359 {
360 4 ▶ if (_valid_procedure_pointer($procedure))
Branch: 1 branch
BranchTrueFalseTotal
if (_valid_procedure_pointer($procedure)) { } 2 2 4
361 {
362 2 push @out, $procedure;
363 2 push @verbose, "Procedure pointer: '$procedure' - ok";
364 }
365 else
366 {
367 2 push @verbose, "No such procedure: '$procedure' - not ok";
368 2 $status = 0;
369 2 @args = (); # Terminate the loop
370 }
371 }
372 else
373 {
374 0 push @verbose, "Not a procedure pointer: '$procedure' - not ok";
375 0 $status = 0;
376 0 @args = (); # Terminate the loop
377 }
378 }
379 else
380 {
381 2 push @verbose, "Illegal argument: '$argument' - not ok";
382 2 $status = 0;
383 2 @args = (); # Terminate the loop
384 }
385 }
386  
387 34 ▶ return (1, @out) if $status; # Return a modified argument list on success.
Branch: 1 branch
BranchTrueFalseTotal
if $status 30 4 34
388  
389 4 return (0, @verbose); # Return a list of diagnostic meddages on failure.
390 }
391  
392 sub selection_loop
393 0 ▶{
Subroutine selection_loop: not called CC=1 SCAR=6.9
Pod documented
394 0 return Geo::Postcodes::_selection_loop('Geo::Postcodes', @_);
395 # Black magic.
396 }
397  
398 sub _selection_loop
399 0 ▶{
Subroutine _selection_loop: not called CC=6 SCAR=37.4
400 0 my $caller_class = shift;
401  
402 0 my $objects_requested = 0; # Not object oriented.
403  
404 0 ▶ if ($_[0] eq $caller_class)
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] eq $caller_class) 0 0 0
405 {
406 0 $objects_requested = 1;
407 0 shift;
408 }
409  
410 0 my $procedure_pointer = shift;
411  
412 0 ▶ return 0 unless $procedure_pointer;
Branch: 1 branch
BranchTrueFalseTotal
unless $procedure_pointer 0 0 0
413  
414 0 my @selection_clauses = @_;
415 0 my @postcodes = _selection($caller_class, @selection_clauses);
416  
417 0 ▶ return 0 unless @postcodes;
Branch: 1 branch
BranchTrueFalseTotal
unless @postcodes 0 0 0
418  
419 0 foreach (@postcodes)
420 {
421 0 ▶ &$procedure_pointer($objects_requested ? $caller_class->new($_) : $_);
Branch: 1 branch
BranchTrueFalseTotal
$objects_requested ? : 0 0 0
422 }
423 0 return 1;
424 }
425  
426  
427 #################################################################################
428 # #
429 # Returns a list of postcodes if called as a procedure; #
430 # Geo::Postcodes::XX::selection(...) #
431 # Returns a list of objects if called as a method; #
432 # Geo::Postcodes::XX->selection(...) #
433 # #
434 # Note that 'or' and 'not' are not written efficient, as they recompile the #
435 # regular expression(s) for every postcode. #
436 # #
437 #################################################################################
438  
439 sub selection
440 1 ▶{
Subroutine selection: called CC=1 SCAR=0.0
Pod documented
441 1 return Geo::Postcodes::_selection('Geo::Postcodes', @_);
442 # Black magic.
443 }
444  
445 sub _selection
446 1 ▶{
Subroutine _selection: called CC=84 SCAR=84.2
447 1 my $caller_class = shift;
448  
449 1 my $objects_requested = 0; # Not object oriented.
450  
451 1 ▶ if ($_[0] eq $caller_class)
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] eq $caller_class) 0 1 1
452 {
453 0 $objects_requested = 1;
454 0 shift;
455 }
456  
457 1 ▶ if ($_[0] eq 'all')
Branch: 2 branches
BranchTrueFalseTotal
if ($_[0] eq 'all') { } 0 1 1
elsif ($_[0] eq 'none') { } 0 1 1
458 {
459 0 my @all = sort &{&_proc_pointer($caller_class . '::get_postcodes')}();
460 # Get all the postcodes.
461  
462 0 ▶ return @all unless $objects_requested;
Branch: 1 branch
BranchTrueFalseTotal
unless $objects_requested 0 0 0
463  
464 0 my @out_objects;
465  
466 0 foreach my $postcode (@all)
467 {
468 0 push(@out_objects, $caller_class->new($postcode));
469 }
470  
471 0 return @out_objects;
472 }
473  
474 elsif ($_[0] eq 'none')
475 {
476 0 return; # Absolutely nothing.
477 }
478  
479 1 my $limit = 0; # Set to one if we have requested only one postcode.
480 1 ▶ if ($_[0] eq "one")
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] eq "one") 0 1 1
481 {
482 0 $limit = 1;
483 0 shift; # Get rid of the mode.
484 }
485  
486 1 my $mode = "and";
487 # The mode defaults to 'and' unless specified.
488  
489 1 my %out = ();
490  
491 ## The first set of method/value ##############################################
492  
493 1 my @all = &{&_proc_pointer($caller_class . '::get_postcodes')}();
494 # Get all the postcodes.
495  
496 1 my($field, $current_field, $value, $current_value);
497  
498 1 ▶ if (@_) # As 'one' can be without additional arguments.
Branch: 2 branches
BranchTrueFalseTotal
if (@_) { } 1 0 1
elsif ($limit) { } 0 0 0
499 {
500 1 ▶ if (is_legal_initial_selectionmode($_[0]))
Branch: 1 branch
BranchTrueFalseTotal
if (is_legal_initial_selectionmode($_[0])) 1 0 1
501 {
502 1 ▶ if ($_[1] eq "not" and is_legal_initial_selectionmode("$_[0] $_[1]"))
Branch: 1 branch
BranchTrueFalseTotal
if ($_[1] eq 'not' and is_legal_initial_selectionmode("$_[0] $_[1]")) { } 0 1 1
Condition33%1/3
$_[1] eq "not" and is_legal_initial_selectionmode("$_[0] $_[1]")
!ll&&!rl&&r
100
MC/DC0%0/2
$_[1] eq "not" and is_legal_initial_selectionmode("$_[0] $_[1]")
$_[1] eq "not" is_legal_initial_selectionmode("$_[0] $_[1]")
Truth table33%1/3
A and B
A= $_[1] eq "not"B= is_legal_initial_selectionmode("$_[0] $_[1]")
ABresult
0X0
100
111
503 {
504 0 $mode = shift; $mode .= " "; $mode .= shift;
505 }
506 else
507 {
508 1 ▶ $mode = shift if is_legal_initial_selectionmode($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if is_legal_initial_selectionmode($_[0]) 1 0 1
509 }
510 }
511  
512 1 $field = shift;
513  
514 1 ▶ if ($field eq 'procedure')
Branch: 1 branch
BranchTrueFalseTotal
if ($field eq 'procedure') { } 0 1 1
515 {
516 0 my $procedure = shift;
517 0 ▶ return unless _valid_procedure_pointer($procedure);
Branch: 1 branch
BranchTrueFalseTotal
unless _valid_procedure_pointer($procedure) 0 0 0
518  
519 0 my $match;
520  
521 0 foreach my $postcode (@all)
522 {
523 0 eval { $match = $procedure->($_); };
524 0 ▶ return if $@; # Return if the procedure was uncallable.
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
525  
526 0 ▶ if ($mode =~ /not/) { $out{$postcode}++ unless $match; }
Branch: 2 branches
BranchTrueFalseTotal
if ($mode =~ /not/) { } 0 0 0
unless $match 0 0 0
527 0 ▶ else { $out{$postcode}++ if $match; }
Branch: 1 branch
BranchTrueFalseTotal
if $match 0 0 0
528 }
529 }
530 else
531 {
532 1 ▶ return unless &{&_proc_pointer($caller_class . '::is_field')}($field);
Branch: 1 branch
BranchTrueFalseTotal
unless &{&_proc_pointer($caller_class . '::is_field');}($field) 0 1 1
533 # Return if the specified method is undefined for the class.
534 # As and 'and' with a list with one undefined item gives an empty list.
535  
536 1 my $current_field = &_proc_pointer($caller_class . '::' . $field .'_of');
537  
538 1 $value = shift; $value =~ s/%/\.\*/g;
539 1 ▶ return unless $value;
Branch: 1 branch
BranchTrueFalseTotal
unless $value 0 1 1
540 # A validity check is impossible, so this is the next best thing.
541  
542 1 foreach my $postcode (@all)
543 {
544 0 $current_value = $current_field->($postcode);
545 # Call the procedure with the current postcode as argument
546  
547 0 ▶ next unless $current_value;
Branch: 1 branch
BranchTrueFalseTotal
unless $current_value 0 0 0
548 # Skip postcodes without this field.
549  
550 0 my $match = $current_value =~ m{^$value$}i; ## Case insensitive
551  
552 0 ▶ if ($mode =~ /not/) { $out{$postcode}++ unless $match; }
Branch: 2 branches
BranchTrueFalseTotal
if ($mode =~ /not/) { } 0 0 0
unless $match 0 0 0
553 0 ▶ else { $out{$postcode}++ if $match; }
Branch: 1 branch
BranchTrueFalseTotal
if $match 0 0 0
554 }
555 }
556  
557 1 ▶ $mode = 'and' if $mode eq 'not';
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "not" 0 1 1
558 }
559  
560 elsif ($limit) # just one argument; 'one'.
561 {
562 0 map { $out{$_} = 1 } @all
563 }
564  
565 1 ▶ while (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 1 1
566 {
567 0 ▶ if (is_legal_selectionmode($_[0]))
Branch: 1 branch
BranchTrueFalseTotal
if (is_legal_selectionmode($_[0])) 0 0 0
568 {
569 0 ▶ if ($_[1] eq "not" and is_legal_selectionmode("$_[0] $_[1]"))
Branch: 1 branch
BranchTrueFalseTotal
if ($_[1] eq 'not' and is_legal_selectionmode("$_[0] $_[1]")) { } 0 0 0
Condition0%0/3
$_[1] eq "not" and is_legal_selectionmode("$_[0] $_[1]")
!ll&&!rl&&r
000
MC/DC0%0/2
$_[1] eq "not" and is_legal_selectionmode("$_[0] $_[1]")
$_[1] eq "not" is_legal_selectionmode("$_[0] $_[1]")
Truth table0%0/3
A and B
A= $_[1] eq "not"B= is_legal_selectionmode("$_[0] $_[1]")
ABresult
0X0
100
111
570 {
571 0 $mode = shift; $mode .= " "; $mode .= shift;
572 }
573 else
574 {
575 0 ▶ $mode = shift if is_legal_selectionmode($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if is_legal_selectionmode($_[0]) 0 0 0
576 }
577 }
578  
579 # Use the one already on hand, if none is given.
580  
581 0 my $is_procedure = 0;
582 0 my $procedure;
583  
584 0 $field = shift;
585  
586 0 ▶ if ($field eq 'procedure')
Branch: 1 branch
BranchTrueFalseTotal
if ($field eq 'procedure') { } 0 0 0
587 {
588 0 $is_procedure = 1;
589 0 $procedure = shift;
590 0 ▶ return unless _valid_procedure_pointer($procedure);
Branch: 1 branch
BranchTrueFalseTotal
unless _valid_procedure_pointer($procedure) 0 0 0
591 }
592 else
593 {
594 0 ▶ return unless &{&_proc_pointer($caller_class . '::is_field')}($field);
Branch: 1 branch
BranchTrueFalseTotal
unless &{&_proc_pointer($caller_class . '::is_field');}($field) 0 0 0
595 # Return if the specified method is undefined for the class.
596 # As an 'and' with a list with one undefined item gives an empty list.
597  
598 0 $current_field = &_proc_pointer($caller_class . '::' . $field .'_of');
599  
600 0 $value = shift;
601 0 $value =~ s/%/\.\*/g;
602 0 ▶ return unless $value;
Branch: 1 branch
BranchTrueFalseTotal
unless $value 0 0 0
603 # A validity check is impossible, so this is the next best thing.
604 }
605  
606 0 ▶ foreach my $postcode ($mode =~ /and/ ? (keys %out) : @all)
Branch: 1 branch
BranchTrueFalseTotal
$mode =~ /and/ ? : 0 0 0
607 {
608 # We start with the result from the previous iteration if the mode
609 # is one of the 'and'-family. Otherwise it is one of the 'or'-family,
610 # and we have to start from scratch (@all).
611  
612 0 my $match;
613  
614 0 ▶ if ($procedure)
Branch: 1 branch
BranchTrueFalseTotal
if ($procedure) { } 0 0 0
615 {
616 0 eval { $match = $procedure->($postcode); };
617 0 ▶ return if $@; # Return if the procedure was uncallable.
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
618 }
619 else
620 {
621 0 $current_value = $current_field->($postcode);
622 # Call the procedure with the current postcode as argument
623  
624 0 ▶ next unless $current_value;
Branch: 1 branch
BranchTrueFalseTotal
unless $current_value 0 0 0
625 # Skip postcodes without this field.
626  
627 0 $match = $current_value =~ m{^$value$}i; ## Case insensitive
628 }
629  
630 0 ▶ if ($mode eq "and")
Branch: 12 branches
BranchTrueFalseTotal
if ($mode eq 'and') { } 0 0 0
elsif ($mode eq 'and not') { } 0 0 0
elsif ($mode eq 'nand') { } 0 0 0
elsif ($mode eq 'nand not') { } 0 0 0
elsif ($mode eq 'or') { } 0 0 0
elsif ($mode eq 'or not') { } 0 0 0
elsif ($mode eq 'nor') { } 0 0 0
elsif ($mode eq 'nor not') { } 0 0 0
elsif ($mode eq 'xor') { } 0 0 0
elsif ($mode eq 'xor not') { } 0 0 0
elsif ($mode eq 'xnor') { } 0 0 0
elsif ($mode eq 'xnor not') { } 0 0 0
631 {
632 0 ▶ delete $out{$postcode} unless $match;
Branch: 1 branch
BranchTrueFalseTotal
unless $match 0 0 0
633 }
634 elsif ($mode eq "and not")
635 {
636 0 ▶ delete $out{$postcode} if $match;
Branch: 1 branch
BranchTrueFalseTotal
if $match 0 0 0
637 }
638  
639 elsif ($mode eq "nand")
640 {
641 0 ▶ if ($match and $out{$postcode}) { delete $out{$postcode} if $out{$postcode}; }
Branch: 2 branches
BranchTrueFalseTotal
if ($match and $out{$postcode}) { } 0 0 0
if $out{$postcode} 0 0 0
Condition0%0/3
$match and $out{$postcode}
!ll&&!rl&&r
000
MC/DC0%0/2
$match and $out{$postcode}
$match $out{$postcode}
Truth table0%0/3
A and B
A= $matchB= $out{$postcode}
ABresult
0X0
100
111
642 0 else { $out{$postcode}++; }
643 }
644 elsif ($mode eq "nand not")
645 {
646 0 ▶ if (!$match and $out{$postcode}) { delete $out{$postcode} if $out{$postcode}; }
Branch: 2 branches
BranchTrueFalseTotal
if (not $match and $out{$postcode}) { } 0 0 0
if $out{$postcode} 0 0 0
Condition0%0/3
not $match and $out{$postcode}
!ll&&!rl&&r
000
MC/DC0%0/2
not $match and $out{$postcode}
not $match $out{$postcode}
Truth table0%0/3
not(A) and B
A= not $matchB= $out{$postcode}
ABresult
0X0
100
111
647 0 else { $out{$postcode}++; }
648 }
649  
650 elsif ($mode eq "or")
651 {
652 0 ▶ $out{$postcode}++ if $match;
Branch: 1 branch
BranchTrueFalseTotal
if $match 0 0 0
653 }
654 elsif ($mode eq "or not")
655 {
656 0 ▶ $out{$postcode}++ unless $match;
Branch: 1 branch
BranchTrueFalseTotal
unless $match 0 0 0
657 }
658 elsif ($mode eq "nor")
659 {
660 0 ▶ if (!$match and !$out{$postcode}) { $out{$postcode}++; }
Branch: 1 branch
BranchTrueFalseTotal
if (not $match || $out{$postcode}) { } 0 0 0
Condition0%0/3
$match || $out{$postcode}
l!l&&r!l&&!r
000
MC/DC0%0/2
$match || $out{$postcode}
$match $out{$postcode}
Truth table0%0/3
A || B
A= $matchB= $out{$postcode}
ABresult
1X1
011
000
661 0 ▶ else { delete $out{$postcode} if $out{$postcode}; }
Branch: 1 branch
BranchTrueFalseTotal
if $out{$postcode} 0 0 0
662 }
663 elsif ($mode eq "nor not")
664 {
665 0 ▶ if ($match and !$out{$postcode}) { $out{$postcode}++; }
Branch: 1 branch
BranchTrueFalseTotal
if ($match and not $out{$postcode}) { } 0 0 0
Condition0%0/3
$match and not $out{$postcode}
!ll&&!rl&&r
000
MC/DC0%0/2
$match and not $out{$postcode}
$match not $out{$postcode}
Truth table0%0/3
A and not(B)
A= $matchB= not $out{$postcode}
ABresult
0X0
100
111
666 0 ▶ else { delete $out{$postcode} if $out{$postcode}; }
Branch: 1 branch
BranchTrueFalseTotal
if $out{$postcode} 0 0 0
667 }
668 elsif ($mode eq "xor")
669 {
670 0 ▶ if ($match)
Branch: 1 branch
BranchTrueFalseTotal
if ($match) 0 0 0
671 {
672 0 ▶ if ($out{$postcode}) { delete $out{$postcode}; }
Branch: 1 branch
BranchTrueFalseTotal
if ($out{$postcode}) { } 0 0 0
673 0 else { $out{$postcode}++; }
674 }
675 }
676 elsif ($mode eq "xor not")
677 {
678 0 ▶ unless ($match)
Branch: 1 branch
BranchTrueFalseTotal
unless ($match) 0 0 0
679 {
680 0 ▶ if ($out{$postcode}) { delete $out{$postcode}; }
Branch: 1 branch
BranchTrueFalseTotal
if ($out{$postcode}) { } 0 0 0
681 0 else { $out{$postcode}++; }
682 }
683 }
684  
685 elsif ($mode eq "xnor")
686 {
687 0 ▶ my $boolean = $out{$postcode} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$out{$postcode} ? : 0 0 0
688 0 ▶ if ($match == $boolean)
Branch: 1 branch
BranchTrueFalseTotal
if ($match == $boolean) { } 0 0 0
689 {
690 0 $out{$postcode}++;
691 }
692 else
693 {
694 0 ▶ delete $out{$postcode} if $out{$postcode};
Branch: 1 branch
BranchTrueFalseTotal
if $out{$postcode} 0 0 0
695 }
696 }
697 elsif ($mode eq "xnor not")
698 {
699 0 ▶ my $boolean = $out{$postcode} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$out{$postcode} ? : 0 0 0
700 0 ▶ if ($match != $boolean)
Branch: 1 branch
BranchTrueFalseTotal
if ($match != $boolean) { } 0 0 0
701 {
702 0 $out{$postcode}++;
703 }
704 else
705 {
706 0 ▶ delete $out{$postcode} if $out{$postcode};
Branch: 1 branch
BranchTrueFalseTotal
if $out{$postcode} 0 0 0
707 }
708 }
709 }
710 }
711  
712 ###############################################################################
713  
714 1 ▶ return unless %out;
Branch: 1 branch
BranchTrueFalseTotal
unless %out 1 0 1
715 # Return nothing if we have an empty list (or rather, hash).
716  
717 0 my @out;
718  
719 0 ▶ if ($limit) # The caller has requested just one postcode, #
Branch: 1 branch
BranchTrueFalseTotal
if ($limit) { } 0 0 0
720 { # and will get exactly that if any matches #
721 0 my @list = keys %out; # were found. The returned postcode is chosen #
722 0 @out = $list[rand(@list)]; # by random. #
723 }
724 else
725 {
726 0 @out = sort keys %out;
727 # This will give an ordered list, as opposed to a semi random order. This #
728 # is essential when comparing lists of postcodes, as the test scripts do. #
729 }
730  
731 ###############################################################################
732  
733 0 ▶ return @out unless $objects_requested;
Branch: 1 branch
BranchTrueFalseTotal
unless $objects_requested 0 0 0
734  
735 0 my @out_objects;
736  
737 0 foreach my $postcode (@out)
738 {
739 0 push(@out_objects, $caller_class->new($postcode));
740 }
741  
742 0 return @out_objects;
743 }
744  
745  
746 sub _proc_pointer
747 3 ▶{
Subroutine _proc_pointer: called CC=1 SCAR=0.0
748 3 my $procedure_name = shift;
749 3 return \&{$procedure_name};
750 }
751  
752 sub _valid_procedure_pointer
753 4 ▶{
Subroutine _valid_procedure_pointer: called CC=3 SCAR=11.0
754 4 my $ptr = shift;
755 4 ▶ return 0 if ref $ptr ne "CODE";
Branch: 1 branch
BranchTrueFalseTotal
if ref $ptr ne "CODE" 0 4 4
756 4 ▶ return 1 if defined(&$ptr);
Branch: 1 branch
BranchTrueFalseTotal
if defined &$ptr 2 2 4
757 2 return 0;
758 }
759  
760 4 1;
761 __END__
↑ Summary Postcodes/Update.pm »