blib/lib/Geo/UK/Postcode.pm

filter: Statementstmt 100% 47 / 47 Branchbran 95.0% 19 / 20 Conditioncond 95.6% 22 / 23 MC/DCmcdc 63.6% 7 / 11 Subroutinesub 100% 25 / 25 Podpod 94.1% 16 / 17 totaltotal 95.1% 136 / 143 CC 21 SCAR 10.7
CC
21
Cov
98%
pc_sort
8.0
__ANON__
3.0
incode
3.0
CRAP 2.9
↑ Summary
1 package Geo::UK::Postcode;
2  
3 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use MooX::Aliases;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Geo::UK::Postcode::Regex;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use overload '""' => "as_string";
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 our $VERSION = '0.010';
12  
13 2 our @EXPORT_OK = qw/ pc_sort /;
14  
15 =encoding utf-8
16  
17 =head1 NAME
18  
19 Geo::UK::Postcode - Object and class methods for working with British postcodes.
20  
21 =head1 SYNOPSIS
22  
23 # See Geo::UK::Postcode::Regex for parsing/matching postcodes
24  
25 use Geo::UK::Postcode;
26  
27 my $pc = Geo::UK::Postcode->new("wc1h9eb");
28  
29 $pc->raw; # wc1h9eb - as entered
30 $pc->as_string; # WC1H 9EB - output in correct format
31 "$pc"; # stringifies, same output as '->as_string'
32 $pc->fixed_format; # 8 characters, the incode always last three
33  
34 $pc->area; # WC
35 $pc->district; # 1
36 $pc->subdistrict; # H
37 $pc->sector; # 9
38 $pc->unit; # EB
39  
40 $pc->outcode; # WC1H
41 $pc->incode; # 9EB
42  
43 $pc->strict; # true if matches strict regex
44 $pc->valid; # true if matches strict regex and has a valid outcode
45 $pc->partial; # true if postcode is for a district or sector only
46  
47 $pc->non_geographical; # true if outcode is known to be
48 # non-geographical
49  
50 $pc->bfpo; # true if postcode is for a BFPO address
51  
52 my @posttowns = $pc->posttowns; # list of one or more 'post towns'
53 # associated with this postcode
54  
55 # Sort Postcode objects:
56 use Geo::UK::Postcode qw/ pc_sort /;
57  
58 my @sorted_pcs = sort pc_sort @unsorted_pcs;
59  
60 =head1 DESCRIPTION
61  
62 An object to represent a British postcode.
63  
64 For matching and parsing postcodes in a non-OO manner without the L<Moo>
65 dependency (for form validation, for example), see L<Geo::UK::Postcode::Regex>
66 or L<Geo::UK::Postcode::Regex::Simple>.
67  
68 For geo-location (finding latitude and longitude) see
69 L</"GEO-LOCATING POSTCODES">.
70  
71 =head1 ATTRIBUTES
72  
73 =head2 raw
74  
75 The exact string that the object was constructed from, without formatting.
76  
77 =cut
78  
79 has raw => (
80 is => 'ro',
81 114 ▶ isa => sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
82 114 ▶ die "Empty or invalid value passed to 'raw'" unless $_[0] && !ref $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] and not ref $_[0] 1 113 114
Condition66%2/3
$_[0] and not ref $_[0]
!ll&&!rl&&r
10113
MC/DC50%1/2
$_[0] and not ref $_[0]
$_[0] not ref $_[0]
Truth table66%2/3
A and not(B)
A= $_[0]B= not ref $_[0]
ABresult
0X0
100
111
83 },
84 2 );
85  
86 =for Pod::Coverage BUILDARGS BUILD components
87  
88 =cut
89  
90 # private - hashref to hold parsed components of postcode
91 has components => (
92 is => 'rwp',
93 114 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
94 2 );
95  
96 114 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
97 114 my ( $orig, $class, $args ) = @_;
98  
99 114 ▶ return $class->$orig( #
Branch: 1 branch
BranchTrueFalseTotal
ref $args ? : 0 114 114
100 ref $args ? $args : { raw => $args }
101 );
102 2 };
103  
104 113 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=6.9
Pod undocumented
105 113 my ($self) = @_;
106  
107 113 my $pc = uc $self->raw;
108  
109 113 ▶ my $parsed = Geo::UK::Postcode::Regex->parse( $pc, { partial => 1 } )
Branch: 1 branch
BranchTrueFalseTotal
my $parsed = "Geo::UK::Postcode::Regex"->parse($pc, {"partial", 1}) or die sprintf("Unable to parse '%s' as a postcode", $self->raw) 109 4 113
110 or die sprintf( "Unable to parse '%s' as a postcode", $self->raw );
111  
112 109 $self->_set_components($parsed);
113 }
114  
115 =head1 METHODS
116  
117 =head2 raw
118  
119  
120 =head2 as_string
121  
122 $pc->as_string;
123  
124 # or:
125  
126 "$pc";
127  
128 Stringification of postcode object, returns postcode with a single space
129 between outcode and incode.
130  
131 =cut
132  
133 249 ▶sub as_string { $_[0]->outcode . ( $_[0]->incode ? ' ' . $_[0]->incode : '' ) }
Subroutine as_string: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->incode ? : 221 28 249
134  
135 =head2 fixed_format
136  
137 my $fixed_format = $postcode->fixed_format;
138  
139 Returns the full postcode in a fixed length (8 character) format, with extra
140 padding spaces inserted as necessary.
141  
142 =cut
143  
144 73 ▶sub fixed_format {
Subroutine fixed_format: called CC=1 SCAR=0.0
Pod documented
145 73 sprintf( "%-4s %-3s", $_[0]->outcode, $_[0]->incode );
146 }
147  
148 =head2 area, district, subdistrict, sector, unit
149  
150 Return the corresponding part of the postcode, undef if not present.
151  
152 =cut
153  
154 921 ▶sub area { shift->components->{area} }
Subroutine area: called CC=1 SCAR=0.0
Pod documented
155 745 ▶sub district { shift->components->{district} }
Subroutine district: called CC=1 SCAR=0.0
Pod documented
156 719 ▶sub subdistrict { shift->components->{subdistrict} }
Subroutine subdistrict: called CC=1 SCAR=0.0
Pod documented
157 784 ▶sub sector { shift->components->{sector} }
Subroutine sector: called CC=1 SCAR=0.0
Pod documented
158 784 ▶sub unit { shift->components->{unit} }
Subroutine unit: called CC=1 SCAR=0.0
Pod documented
159  
160 =head2 outcode
161  
162 The first half of the postcode, before the space - comprises of the area and
163 district.
164  
165 =head2 incode
166  
167 The second half of the postcode, after the space - comprises of the sector
168 and unit. Returns an empty string if not present.
169  
170 =cut
171  
172 614 ▶sub outcode {
Subroutine outcode: called CC=2 SCAR=6.9
Pod documented
173 614 ▶ $_[0]->area . $_[0]->district . ( $_[0]->subdistrict || '' );
Condition100%2/2
$_[0]->subdistrict || ''
l!l
143471
MC/DC100%1/1
$_[0]->subdistrict || ''
$_[0]->subdistrict
Truth table100%2/2
A
A= $_[0]->subdistrict
Aresult
11
00
174 }
175  
176 711 ▶sub incode {
Subroutine incode: called CC=3 SCAR=11.0
Pod documented
177 711 ▶ ( $_[0]->sector // '' ) . ( $_[0]->unit || '' );
Condition100%4/4
$_[0]->sector // ""
l!l
65754
$_[0]->unit || ''
l!l
595116
MC/DC100%1/1
$_[0]->sector // ""
$_[0]->sector
MC/DC100%1/1
$_[0]->unit || ''
$_[0]->unit
Truth table100%2/2
A
A= $_[0]->sector
Aresult
11
00
Truth table100%2/2
A
A= $_[0]->unit
Aresult
11
00
178 }
179  
180 =head2 outward, inward
181  
182 Aliases for C<outcode> and C<incode>.
183  
184 =cut
185  
186 2 alias outward => 'outcode';
187 2 alias inward => 'incode';
188  
189 =head2 valid
190  
191 if ($pc->valid) {
192 ...
193 }
194  
195 Returns true if postcode has valid outcode and matches strict regex.
196  
197 =head2 partial
198  
199 if ($pc->partial) {
200 ...
201 }
202  
203 Returns true if postcode is not a full postcode, either a postcode district
204 ( e . g . AB10 )
205 or postcode sector (e.g. AB10 1).
206  
207 =head2 strict
208  
209 if ($pc->strict) {
210 ...
211 }
212  
213 Returns true if postcode matches strict regex, meaning all characters are valid
214 ( although postcode might not exist ) .
215  
216 =cut
217  
218 73 ▶sub valid {
Subroutine valid: called CC=2 SCAR=6.9
Pod documented
219 73 ▶ $_[0]->components->{valid} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->components->{'valid'} ? : 41 32 73
220 }
221  
222 73 ▶sub partial {
Subroutine partial: called CC=2 SCAR=6.9
Pod documented
223 73 ▶ $_[0]->components->{partial} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->components->{'partial'} ? : 13 60 73
224 }
225  
226 73 ▶sub strict {
Subroutine strict: called CC=2 SCAR=6.9
Pod documented
227 73 ▶ $_[0]->components->{strict} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->components->{'strict'} ? : 60 13 73
228 }
229  
230 =head2 non_geographical
231  
232 if ($pc->non_geographical) {
233 ...
234 }
235  
236 Returns true if the outcode is known to be non-geographical. Note that
237 geographical outcodes may have non-geographical postcodes within them.
238  
239 (Non-geographical postcodes are used for PO Boxes, or organisations
240 receiving large amounts of post).
241  
242 =cut
243  
244 73 ▶sub non_geographical {
Subroutine non_geographical: called CC=2 SCAR=6.9
Pod documented
245 73 ▶ $_[0]->components->{non_geographical} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->components->{'non_geographical'} ? : 18 55 73
246 }
247  
248 =head2 bfpo
249  
250 if ($pc->bfpo) {
251 ...
252 }
253  
254 Returns true if postcode is mapped to a BFPO number (British Forces Post
255 Office).
256  
257 =cut
258  
259 73 ▶sub bfpo {
Subroutine bfpo: called CC=2 SCAR=6.9
Pod documented
260 73 ▶ $_[0]->outcode eq 'BF1' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->outcode eq 'BF1' ? : 6 67 73
261 }
262  
263 =head2 posttowns
264  
265 my (@posttowns) = $postcode->posttowns;
266  
267 Returns list of one or more 'post towns' that this postcode is assigned to.
268  
269 Post towns are rarely used today, and are no longer required in a postal address
270 but are included with the postcode data, so provided here.
271  
272 =cut
273  
274 73 ▶sub posttowns {
Subroutine posttowns: called CC=1 SCAR=0.0
Pod documented
275 73 Geo::UK::Postcode::Regex->outcode_to_posttowns( $_[0]->outcode );
276 }
277  
278 =head1 EXPORTABLE
279  
280 =head2 pc_sort
281  
282 my @sorted_pcs = sort pc_sort @unsorted_pcs;
283  
284 Exportable sort function, sorts postcode objects in a useful manner. The
285 sort is in the following order: area, district, subdistrict, sector, unit
286 (ascending alphabetical or numerical order as appropriate).
287  
288 =cut
289  
290 117 ▶sub pc_sort($$) {
Subroutine pc_sort: called CC=8 SCAR=20.8
Pod documented
291 117 ▶ $_[0]->area cmp $_[1]->area
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->area cmp $_[1]->area or $_[0]->district <=> $_[1]->district or ($_[0]->subdistrict || "") cmp ($_[1]->subdistrict || "") or ($_[0]->incode || "") cmp ($_[1]->incode || "") 106 11 117
Condition100%14/14
$_[0]->area cmp $_[1]->area or $_[0]->district <=> $_[1]->district or ($_[0]->subdistrict || "") cmp ($_[1]->subdistrict || "")
l!l&&r!l&&!r
101511
$_[0]->area cmp $_[1]->area or $_[0]->district <=> $_[1]->district
l!l&&r!l&&!r
881316
$_[0]->subdistrict || ''
l!l
412
$_[1]->subdistrict || ''
l!l
511
$_[0]->incode || ''
l!l
74
$_[1]->incode || ''
l!l
101
MC/DC0%0/3
$_[0]->area cmp $_[1]->area or $_[0]->district <=> $_[1]->district or ($_[0]->subdistrict || "") cmp ($_[1]->subdistrict || "")
$_[0]->area cmp $_[1]->area $_[0]->district <=> $_[1]->district $_[0]->subdistrict
MC/DC100%1/1
$_[1]->subdistrict || ''
$_[1]->subdistrict
MC/DC100%1/1
$_[0]->incode || ''
$_[0]->incode
MC/DC100%1/1
$_[1]->incode || ''
$_[1]->incode
Truth table0%0/4
A or B or C
A= $_[0]->area cmp $_[1]->areaB= $_[0]->district <=> $_[1]->districtC= $_[0]->subdistrict
ABCresult
1XX1
01X1
0011
0000
Truth table100%2/2
A
A= $_[1]->subdistrict
Aresult
11
00
Truth table100%2/2
A
A= $_[0]->incode
Aresult
11
00
Truth table100%2/2
A
A= $_[1]->incode
Aresult
11
00
292 || $_[0]->district <=> $_[1]->district
293 || ( $_[0]->subdistrict || '' ) cmp( $_[1]->subdistrict || '' )
294 || ( $_[0]->incode || '' ) cmp( $_[1]->incode || '' );
295 }
296  
297 2 1;
298  
299 __END__
↑ Summary