| 1 | package Geo::UK::Postcode::Regex; | ||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||
| 3 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 4 | 7 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||
| 6 | 7 | ▶ | use Geo::UK::Postcode::Regex::Hash; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||
| 8 | 7 | ▶ | use base 'Exporter'; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 9 | 7 | our @EXPORT_OK = qw( is_valid_pc is_strict_pc is_lax_pc %REGEXES ); | |||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||
| 11 | 7 | our $VERSION = '0.015'; | |||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||
| 13 | =encoding utf-8 | ||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||
| 15 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||
| 17 | Geo::UK::Postcode::Regex - regular expressions for handling British postcodes | ||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||
| 19 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||
| 21 | See L<Geo::UK::Postcode::Regex::Simple> for an alternative interface. | ||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||
| 23 | use Geo::UK::Postcode::Regex; | ||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||
| 25 | ## REGULAR EXPRESSIONS | ||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||
| 27 | my $lax_re = Geo::UK::Postcode::Regex->regex; | ||||||||||||||||||||||||||||||||||||||||
| 28 | my $strict_re = Geo::UK::Postcode::Regex->regex_strict; | ||||||||||||||||||||||||||||||||||||||||
| 29 | my $valid_re = Geo::UK::Postcode::Regex->valid_regex; | ||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||
| 31 | # matching only | ||||||||||||||||||||||||||||||||||||||||
| 32 | if ( $foo =~ $lax_re ) {...} | ||||||||||||||||||||||||||||||||||||||||
| 33 | if ( $foo =~ $strict_re ) {...} | ||||||||||||||||||||||||||||||||||||||||
| 34 | if ( $foo =~ $valid_re ) {...} | ||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||
| 36 | # matching and using components - see also parse() | ||||||||||||||||||||||||||||||||||||||||
| 37 | if ( $foo =~ $lax_re ) { | ||||||||||||||||||||||||||||||||||||||||
| 38 | my ( $area, $district, $sector, $unit ) = ( $1, $2, $3, $4 ); | ||||||||||||||||||||||||||||||||||||||||
| 39 | my $subdistrict = $district =~ s/([A-Z])$// ? $1 : undef; | ||||||||||||||||||||||||||||||||||||||||
| 40 | ... | ||||||||||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||||||||||
| 42 | if ( $foo =~ $strict_re ) { | ||||||||||||||||||||||||||||||||||||||||
| 43 | my ( $area, $district, $sector, $unit ) = ( $1, $2, $3, $4 ); | ||||||||||||||||||||||||||||||||||||||||
| 44 | my $subdistrict = $district =~ s/([A-Z])$// ? $1 : undef; | ||||||||||||||||||||||||||||||||||||||||
| 45 | ... | ||||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||||
| 47 | if ( $foo =~ $valid_re ) { | ||||||||||||||||||||||||||||||||||||||||
| 48 | my ( $outcode, $sector, $unit ) = ( $1, $2, $3 ); | ||||||||||||||||||||||||||||||||||||||||
| 49 | ... | ||||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||
| 53 | ## VALIDATION METHODS | ||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||
| 55 | use Geo::UK::Postcode::Regex qw( is_valid_pc is_strict_pc is_lax_pc ); | ||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||
| 57 | if (is_valid_pc("GE0 1UK")) { | ||||||||||||||||||||||||||||||||||||||||
| 58 | ... | ||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||
| 60 | if (is_strict_pc("GE0 1UK")) { | ||||||||||||||||||||||||||||||||||||||||
| 61 | ... | ||||||||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||||||
| 63 | if (is_lax_pc("GE0 1UK")) { | ||||||||||||||||||||||||||||||||||||||||
| 64 | ... | ||||||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||
| 68 | ## PARSING | ||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||
| 70 | my $parsed = Geo::UK::Postcode::Regex->parse("WC1H 9EB"); | ||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||
| 72 | # returns: | ||||||||||||||||||||||||||||||||||||||||
| 73 | # { area => 'WC', | ||||||||||||||||||||||||||||||||||||||||
| 74 | # district => '1', | ||||||||||||||||||||||||||||||||||||||||
| 75 | # subdistrict => 'H', | ||||||||||||||||||||||||||||||||||||||||
| 76 | # sector => '9', | ||||||||||||||||||||||||||||||||||||||||
| 77 | # unit => 'EB', | ||||||||||||||||||||||||||||||||||||||||
| 78 | # outcode => 'WC1H', | ||||||||||||||||||||||||||||||||||||||||
| 79 | # incode => '9EB', | ||||||||||||||||||||||||||||||||||||||||
| 80 | # valid => 1, | ||||||||||||||||||||||||||||||||||||||||
| 81 | # strict => 1, | ||||||||||||||||||||||||||||||||||||||||
| 82 | # partial => 0, | ||||||||||||||||||||||||||||||||||||||||
| 83 | # non_geographical => 0, | ||||||||||||||||||||||||||||||||||||||||
| 84 | # bfpo => 0, | ||||||||||||||||||||||||||||||||||||||||
| 85 | # } | ||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||
| 87 | # strict parsing (only valid characters): | ||||||||||||||||||||||||||||||||||||||||
| 88 | ...->parse( $pc, { strict => 1 } ) | ||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||
| 90 | # valid outcodes only | ||||||||||||||||||||||||||||||||||||||||
| 91 | ...->parse( $pc, { valid => 1 } ) | ||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||
| 93 | # match partial postcodes, e.g. 'WC1H', 'WC1H 9' - see below | ||||||||||||||||||||||||||||||||||||||||
| 94 | ...->parse( $pc, { partial => 1 } ) | ||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||
| 97 | ## PARSING PARTIAL POSTCODES | ||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||
| 99 | # outcode (district) only | ||||||||||||||||||||||||||||||||||||||||
| 100 | my $parsed = Geo::UK::Postcode::Regex->parse( "AB10", { partial => 1 } ); | ||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||
| 102 | # returns: | ||||||||||||||||||||||||||||||||||||||||
| 103 | # { area => 'AB', | ||||||||||||||||||||||||||||||||||||||||
| 104 | # district => '10', | ||||||||||||||||||||||||||||||||||||||||
| 105 | # subdistrict => undef, | ||||||||||||||||||||||||||||||||||||||||
| 106 | # sector => undef, | ||||||||||||||||||||||||||||||||||||||||
| 107 | # unit => undef, | ||||||||||||||||||||||||||||||||||||||||
| 108 | # outcode => 'AB10', | ||||||||||||||||||||||||||||||||||||||||
| 109 | # incode => undef, | ||||||||||||||||||||||||||||||||||||||||
| 110 | # valid => 1, | ||||||||||||||||||||||||||||||||||||||||
| 111 | # strict => 1, | ||||||||||||||||||||||||||||||||||||||||
| 112 | # partial => 1, | ||||||||||||||||||||||||||||||||||||||||
| 113 | # non_geographical => 0, | ||||||||||||||||||||||||||||||||||||||||
| 114 | # bfpo => 0, | ||||||||||||||||||||||||||||||||||||||||
| 115 | # } | ||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||
| 117 | # sector only | ||||||||||||||||||||||||||||||||||||||||
| 118 | my $parsed = Geo::UK::Postcode::Regex->parse( "AB10 1", { partial => 1 } ); | ||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||
| 120 | # returns: | ||||||||||||||||||||||||||||||||||||||||
| 121 | # { area => 'AB', | ||||||||||||||||||||||||||||||||||||||||
| 122 | # district => '10', | ||||||||||||||||||||||||||||||||||||||||
| 123 | # subdistrict => undef, | ||||||||||||||||||||||||||||||||||||||||
| 124 | # sector => 1, | ||||||||||||||||||||||||||||||||||||||||
| 125 | # unit => undef, | ||||||||||||||||||||||||||||||||||||||||
| 126 | # outcode => 'AB10', | ||||||||||||||||||||||||||||||||||||||||
| 127 | # incode => '1', | ||||||||||||||||||||||||||||||||||||||||
| 128 | # valid => 1, | ||||||||||||||||||||||||||||||||||||||||
| 129 | # strict => 1, | ||||||||||||||||||||||||||||||||||||||||
| 130 | # partial => 1, | ||||||||||||||||||||||||||||||||||||||||
| 131 | # non_geographical => 0, | ||||||||||||||||||||||||||||||||||||||||
| 132 | # bfpo => 0, | ||||||||||||||||||||||||||||||||||||||||
| 133 | # } | ||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||
| 136 | ## EXTRACT OUTCODE FROM POSTCODE | ||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||
| 138 | my $outcode = Geo::UK::Postcode::Regex->outcode("AB101AA"); # returns 'AB10' | ||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||
| 140 | my $outcode = Geo::UK::Postcode::Regex->outcode( $postcode, { valid => 1 } ) | ||||||||||||||||||||||||||||||||||||||||
| 141 | or die "Invalid postcode"; | ||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||
| 144 | ## EXTRACT POSTCODES FROM TEXT | ||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||
| 146 | # \%options as per parse, excluding partial | ||||||||||||||||||||||||||||||||||||||||
| 147 | my @extracted = Geo::UK::Postcode::Regex->extract( $text, \%options ); | ||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||
| 150 | ## POSTTOWNS | ||||||||||||||||||||||||||||||||||||||||
| 151 | my @posttowns = Geo::UK::Postcode::Regex->outcode_to_posttowns($outcode); | ||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||
| 154 | ## OUTCODES | ||||||||||||||||||||||||||||||||||||||||
| 155 | my @outcodes = Geo::UK::Postcode::Regex->posttown_to_outcodes($posttown); | ||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||
| 158 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||
| 160 | Parsing UK postcodes with regular expressions. This package has been | ||||||||||||||||||||||||||||||||||||||||
| 161 | separated from L<Geo::UK::Postcode> so it can be installed and used with fewer | ||||||||||||||||||||||||||||||||||||||||
| 162 | dependencies. | ||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||
| 164 | Can handle partial postcodes (just the outcode or sector) and can test | ||||||||||||||||||||||||||||||||||||||||
| 165 | against valid characters and currently valid outcodes. | ||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||
| 167 | Also can determine the posttown(s) from a postcode. | ||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||
| 169 | Districts and post town information taken from: | ||||||||||||||||||||||||||||||||||||||||
| 170 | L<https://en.wikipedia.org/wiki/Postcode_districts> | ||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||
| 172 | =head1 IMPORTANT CHANGES FOR VERSION 0.014 | ||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||
| 174 | Please note that various bugfixes have changed the following: | ||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||
| 176 | =over | ||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||
| 178 | =item * | ||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||
| 180 | Unanchored regular expressions no longer match valid postcodes within invalid | ||||||||||||||||||||||||||||||||||||||||
| 181 | ones. | ||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||
| 183 | =item * | ||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||
| 185 | Unanchored regular expressions in partial mode now can match a valid or strict | ||||||||||||||||||||||||||||||||||||||||
| 186 | outcode with an invalid incode. | ||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||
| 188 | =back | ||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||
| 190 | Please get in touch if you have any questions. | ||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||
| 192 | See L<Geo::UK::Postcode::Regex::Simple> for other changes affecting the Simple | ||||||||||||||||||||||||||||||||||||||||
| 193 | interface. | ||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||
| 195 | =head1 NOTES AND LIMITATIONS | ||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||
| 197 | When parsing a partial postcode, whitespace may be required to separate the | ||||||||||||||||||||||||||||||||||||||||
| 198 | outcode from the sector. | ||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||
| 200 | For example the sector 'B1 1' cannot be distinguished from the district 'B11' | ||||||||||||||||||||||||||||||||||||||||
| 201 | without whitespace. This is not a problem when parsing full postcodes. | ||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||
| 203 | =cut | ||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||
| 205 | ## REGULAR EXPRESSIONS | ||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||
| 207 | 7 | my $AREA1 = 'ABCDEFGHIJKLMNOPRSTUWYZ'; # [^QVX] | |||||||||||||||||||||||||||||||||||||||
| 208 | 7 | my $AREA2 = 'ABCDEFGHKLMNOPQRSTUVWXY'; # [^IJZ] | |||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||
| 210 | 7 | my $SUBDISTRICT1 = 'ABCDEFGHJKPSTUW'; # for single letter areas | |||||||||||||||||||||||||||||||||||||||
| 211 | 7 | my $SUBDISTRICT2 = 'ABEHMNPRVWXY'; # for two letter areas | |||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||
| 213 | 7 | my $UNIT1 = 'ABDEFGHJLNPQRSTUWXYZ'; # [^CIKMOV] | |||||||||||||||||||||||||||||||||||||||
| 214 | 7 | my $UNIT2 = 'ABDEFGHJLNPQRSTUWXYZ'; # [^CIKMOV] | |||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||
| 216 | 7 | our %COMPONENTS = ( | |||||||||||||||||||||||||||||||||||||||
| 217 | strict => { | ||||||||||||||||||||||||||||||||||||||||
| 218 | area => "[$AREA1][$AREA2]?", | ||||||||||||||||||||||||||||||||||||||||
| 219 | district => qq% (?: | ||||||||||||||||||||||||||||||||||||||||
| 220 | [0-9][0-9]? | ||||||||||||||||||||||||||||||||||||||||
| 221 | | (?<![A-Z]{2}) [0-9][$SUBDISTRICT1]? | ||||||||||||||||||||||||||||||||||||||||
| 222 | | (?<=[A-Z]{2}) [0-9][$SUBDISTRICT2] | ||||||||||||||||||||||||||||||||||||||||
| 223 | ) %, | ||||||||||||||||||||||||||||||||||||||||
| 224 | sector => '[0-9]', | ||||||||||||||||||||||||||||||||||||||||
| 225 | unit => "[$UNIT1][$UNIT2]", | ||||||||||||||||||||||||||||||||||||||||
| 226 | blank => '', | ||||||||||||||||||||||||||||||||||||||||
| 227 | }, | ||||||||||||||||||||||||||||||||||||||||
| 228 | lax => { | ||||||||||||||||||||||||||||||||||||||||
| 229 | area => '[A-Z]{1,2}', | ||||||||||||||||||||||||||||||||||||||||
| 230 | district => '[0-9](?:[0-9]|[A-Z])?', | ||||||||||||||||||||||||||||||||||||||||
| 231 | sector => '[0-9]', | ||||||||||||||||||||||||||||||||||||||||
| 232 | unit => '[A-Z]{2}', | ||||||||||||||||||||||||||||||||||||||||
| 233 | }, | ||||||||||||||||||||||||||||||||||||||||
| 234 | ); | ||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||
| 236 | 7 | my %BASE_REGEXES = ( | |||||||||||||||||||||||||||||||||||||||
| 237 | full => ' %s %s \s* %s %s ', | ||||||||||||||||||||||||||||||||||||||||
| 238 | partial => ' %s %s (?: \s* %s (?:%s)? ) ? ', | ||||||||||||||||||||||||||||||||||||||||
| 239 | ); | ||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||
| 241 | 7 | my ( %POSTTOWNS, %OUTCODES ); | |||||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||||||
| 243 | tie our %REGEXES, 'Geo::UK::Postcode::Regex::Hash', _fetch => sub { | ||||||||||||||||||||||||||||||||||||||||
| 244 | 78 | ▶ | my ($key) = @_; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=11 SCAR=24.0
| |||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||
| 246 | 78 | ▶ | _outcode_data() if $key =~ m/valid/ && !%OUTCODES; | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$key =~ /valid/ and not %OUTCODES
MC/DC100%2/2
$key =~ /valid/ and not %OUTCODES
$key =~ /valid/
not %OUTCODES
Truth table100%3/3
A and not(B)
A= $key =~ /valid/B= not %OUTCODES
| |||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||
| 248 | 78 | ▶ | my $type = $key =~ m/lax/ ? 'lax' : 'strict'; | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||||||||||
| 250 | 78 | my $components = $Geo::UK::Postcode::Regex::COMPONENTS{$type}; | |||||||||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||||||||||
| 252 | my @comps | ||||||||||||||||||||||||||||||||||||||||
| 253 | = $key =~ m/valid/ | ||||||||||||||||||||||||||||||||||||||||
| 254 | 23 | ? @{$components}{qw( outcodes blank sector unit )} | |||||||||||||||||||||||||||||||||||||||
| 255 | 78 | ▶ | : @{$components}{qw( area district sector unit )}; | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||
| 257 | 78 | ▶ | @comps = map { $_ ? "($_)" : $_ } @comps if $key =~ m/captures/; | ||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||
| 259 | 78 | ▶ | my $size = $key =~ m/partial/ ? 'partial' : 'full'; | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||||||
| 261 | 78 | my $re = sprintf( $BASE_REGEXES{$size}, @comps ); | |||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||
| 263 | 78 | ▶ | if ( $key =~ m/anchored/ ) { | ||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||
| 264 | 42 | $re = '^' . $re . '$'; | |||||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||||||
| 266 | } elsif ( $key =~ m/extract/ ) { | ||||||||||||||||||||||||||||||||||||||||
| 267 | 12 | $re = '(?:[^0-9A-Z]|\b) (' . $re . ') (?:[^0-9A-Z]|\b)'; | |||||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||||||
| 269 | } else { | ||||||||||||||||||||||||||||||||||||||||
| 270 | 24 | $re = '(?:[^0-9A-Z]|\b) ' . $re . ' (?:[^0-9A-Z]|\b)'; | |||||||||||||||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||
| 273 | 78 | ▶ | return $key =~ m/case-insensitive/ ? qr/$re/ix : qr/$re/x; | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 274 | 7 | }; | |||||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||||||
| 276 | ## OUTCODE AND POSTTOWN DATA | ||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||
| 278 | sub _outcode_data { | ||||||||||||||||||||||||||||||||||||||||
| 279 | 6 | ▶ | my %area_districts; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
_outcode_data: called CC=10 SCAR=23.0
| |||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||
| 281 | # Get outcodes from __DATA__ | ||||||||||||||||||||||||||||||||||||||||
| 282 | 6 | ▶ | while ( my $line = <DATA> ) { | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 283 | 17940 | ▶ | next unless $line =~ m/\w/; | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 284 | 17934 | chomp $line; | |||||||||||||||||||||||||||||||||||||||
| 285 | 17934 | my ( $outcode, $non_geographical, @posttowns ) = split /,/, $line; | |||||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||||||
| 287 | 17934 | push @{ $POSTTOWNS{$_} }, $outcode foreach @posttowns; | |||||||||||||||||||||||||||||||||||||||
| 288 | 17934 | $OUTCODES{$outcode} = { | |||||||||||||||||||||||||||||||||||||||
| 289 | posttowns => \@posttowns, | ||||||||||||||||||||||||||||||||||||||||
| 290 | non_geographical => $non_geographical, | ||||||||||||||||||||||||||||||||||||||||
| 291 | }; | ||||||||||||||||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||||||||
| 294 | # Add in BX non-geographical outcodes | ||||||||||||||||||||||||||||||||||||||||
| 295 | 6 | foreach ( 1 .. 99 ) { | |||||||||||||||||||||||||||||||||||||||
| 296 | 594 | $OUTCODES{ 'BX' . $_ } = { | |||||||||||||||||||||||||||||||||||||||
| 297 | posttowns => [], | ||||||||||||||||||||||||||||||||||||||||
| 298 | non_geographical => 1, | ||||||||||||||||||||||||||||||||||||||||
| 299 | }; | ||||||||||||||||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||
| 302 | 6 | foreach my $outcode ( sort keys %OUTCODES ) { | |||||||||||||||||||||||||||||||||||||||
| 303 | my ( $area, $district ) | ||||||||||||||||||||||||||||||||||||||||
| 304 | = $outcode =~ $REGEXES{strict_partial_anchored_captures} | ||||||||||||||||||||||||||||||||||||||||
| 305 | 18528 | ▶ | or next; | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||||||
| 307 | 18528 | ▶ | $district = " $district" if length $district < 2; | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||
| 309 | 18528 | push @{ $area_districts{$area}->{ substr( $district, 0, 1 ) } }, | |||||||||||||||||||||||||||||||||||||||
| 310 | substr( $district, 1, 1 ); | ||||||||||||||||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||||||||
| 313 | $Geo::UK::Postcode::Regex::COMPONENTS{strict}->{outcodes} = '(?: ' . join( | ||||||||||||||||||||||||||||||||||||||||
| 314 | "|\n", | ||||||||||||||||||||||||||||||||||||||||
| 315 | map { | ||||||||||||||||||||||||||||||||||||||||
| 316 | 6 | my $area = $_; | |||||||||||||||||||||||||||||||||||||||
| 317 | sprintf( | ||||||||||||||||||||||||||||||||||||||||
| 318 | "%s (?:%s)", # | ||||||||||||||||||||||||||||||||||||||||
| 319 | $area, | ||||||||||||||||||||||||||||||||||||||||
| 320 | join( | ||||||||||||||||||||||||||||||||||||||||
| 321 | ' | ', | ||||||||||||||||||||||||||||||||||||||||
| 322 | map { | ||||||||||||||||||||||||||||||||||||||||
| 323 | sprintf( "%s[%s]", | ||||||||||||||||||||||||||||||||||||||||
| 324 | 2982 | $_, join( '', @{ $area_districts{$area}->{$_} } ) ) | |||||||||||||||||||||||||||||||||||||||
| 325 | } # | ||||||||||||||||||||||||||||||||||||||||
| 326 | 4188 | ▶ | sort { $a eq ' ' ? 1 : $b eq ' ' ? -1 : $a <=> $b } | ||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||
| 327 | 756 | keys %{ $area_districts{$area} } | |||||||||||||||||||||||||||||||||||||||
| 328 | ) | ||||||||||||||||||||||||||||||||||||||||
| 329 | ) | ||||||||||||||||||||||||||||||||||||||||
| 330 | } sort keys %area_districts | ||||||||||||||||||||||||||||||||||||||||
| 331 | ) . ' )'; | ||||||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||||
| 333 | } | ||||||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||||||
| 335 | =head1 VALIDATION METHODS | ||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||
| 337 | The following methods are for validating postcodes to various degrees. | ||||||||||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||||||||||
| 339 | L<Geo::UK::Postcode::Regex::Simple> may provide a more convenient way of using | ||||||||||||||||||||||||||||||||||||||||
| 340 | and customising these. | ||||||||||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||||||||||
| 342 | =head2 regex, strict_regex, valid_regex | ||||||||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||||||||
| 344 | Return regular expressions to parse postcodes and capture the constituent | ||||||||||||||||||||||||||||||||||||||||
| 345 | parts: area, district, sector and unit (or outcode, sector and unit in the | ||||||||||||||||||||||||||||||||||||||||
| 346 | case of C<valid_regex>). | ||||||||||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||||||||||
| 348 | C<strict_regex> checks that the postcode only contains valid characters | ||||||||||||||||||||||||||||||||||||||||
| 349 | according to the postcode specifications. | ||||||||||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||||||||||
| 351 | C<valid_regex> checks that the outcode currently exists. | ||||||||||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||||||||
| 353 | =head2 regex_partial, strict_regex_partial, valid_regex_partial | ||||||||||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||||||||||
| 355 | As above, but matches on partial postcodes of just the outcode | ||||||||||||||||||||||||||||||||||||||||
| 356 | or sector | ||||||||||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||||||||||
| 358 | =cut | ||||||||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||||||||
| 360 | 1 | ▶ | sub valid_regex_partial { $REGEXES{valid_partial_anchored_captures} } | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
valid_regex_partial: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 361 | 1 | ▶ | sub strict_regex_partial { $REGEXES{strict_partial_anchored_captures} } | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
strict_regex_partial: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 362 | 1 | ▶ | sub regex_partial { $REGEXES{lax_partial_anchored_captures} } | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
regex_partial: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 363 | 1 | ▶ | sub valid_regex { $REGEXES{valid_anchored_captures} } | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
valid_regex: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 364 | 1 | ▶ | sub strict_regex { $REGEXES{strict_anchored_captures} } | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
strict_regex: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 365 | 1 | ▶ | sub regex { $REGEXES{lax_anchored_captures} } | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
regex: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||||||||||
| 368 | =head2 is_valid_pc, is_strict_pc, is_lax_pc | ||||||||||||||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||||||||||||||
| 370 | if (is_valid_pc( "AB1 2CD" ) ) { ... } | ||||||||||||||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||||||||||||||
| 372 | Alternative way to access the regexes. | ||||||||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||||||||
| 374 | =cut | ||||||||||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||||||||||
| 376 | sub is_valid_pc { | ||||||||||||||||||||||||||||||||||||||||
| 377 | 61 | ▶ | my $pc = @_ > 1 ? $_[1] : $_[0]; # back-compat: can call as class method | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_valid_pc: called CC=3 SCAR=11.1
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 378 | 61 | ▶ | return $pc =~ $REGEXES{valid_anchored} ? 1 : 0 | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 379 | } | ||||||||||||||||||||||||||||||||||||||||
| 380 | sub is_strict_pc { | ||||||||||||||||||||||||||||||||||||||||
| 381 | 61 | ▶ | my $pc = @_ > 1 ? $_[1] : $_[0]; # back-compat: can call as class method | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_strict_pc: called CC=3 SCAR=11.1
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 382 | 61 | ▶ | return $pc =~ $REGEXES{strict_anchored} ? 1 : 0 | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 383 | } | ||||||||||||||||||||||||||||||||||||||||
| 384 | sub is_lax_pc { | ||||||||||||||||||||||||||||||||||||||||
| 385 | 61 | ▶ | my $pc = @_ > 1 ? $_[1] : $_[0]; # back-compat: can call as class method | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_lax_pc: called CC=3 SCAR=11.1
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 386 | 61 | ▶ | return $pc =~ $REGEXES{lax_anchored} ? 1 : 0 | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 387 | } | ||||||||||||||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||||||||||||||
| 389 | =head1 PARSING METHODS | ||||||||||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||||||||||
| 391 | The following methods are for parsing postcodes or strings containing postcodes. | ||||||||||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||||||||||
| 393 | =head2 PARSING_OPTIONS | ||||||||||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||||||||||
| 395 | The parsing methods can take the following options, passed via a hashref: | ||||||||||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||||||||||
| 397 | =over | ||||||||||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||||||||||
| 399 | =item strict | ||||||||||||||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||||||||||||||
| 401 | Postcodes must not contain invalid characters according to the postcode | ||||||||||||||||||||||||||||||||||||||||
| 402 | specification. For example a 'Q' may not appear as the first character. | ||||||||||||||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||||||||||||||
| 404 | =item valid | ||||||||||||||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||||||||||||||
| 406 | Postcodes must contain an outcode (area + district) that currently exists, in | ||||||||||||||||||||||||||||||||||||||||
| 407 | addition to conforming to the C<strict> definition. | ||||||||||||||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||||||||||||||
| 409 | Returns false if string is not a currently existing outcode. | ||||||||||||||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||||||||||||||
| 411 | =item partial | ||||||||||||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||||||||||||
| 413 | Allows partial postcodes to be matched. In practice this means either an outcode | ||||||||||||||||||||||||||||||||||||||||
| 414 | ( area and district ) or an outcode together with the sector. | ||||||||||||||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||||||||||||||
| 416 | =back | ||||||||||||||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||||||||||||||
| 418 | =head2 extract | ||||||||||||||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||||||||||||||
| 420 | my @extracted = Geo::UK::Postcode::Regex->extract( $string, \%options ); | ||||||||||||||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||||||||||||||
| 422 | Returns a list of full postcodes extracted from a string. | ||||||||||||||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||||||||||||||
| 424 | =cut | ||||||||||||||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||||||||||||||
| 426 | # TODO need to/can do partial? | ||||||||||||||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||||||||||||||
| 428 | sub extract { | ||||||||||||||||||||||||||||||||||||||||
| 429 | 3606 | ▶ | my ( $class, $string, $options ) = @_; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
extract: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||||||||||||||
| 431 | 3606 | ▶ | _outcode_data() unless %OUTCODES; | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||||||||||||||
| 433 | my $key | ||||||||||||||||||||||||||||||||||||||||
| 434 | = $options->{valid} ? 'valid' | ||||||||||||||||||||||||||||||||||||||||
| 435 | 3606 | ▶ | : $options->{strict} ? 'strict' | ||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||
| 436 | : 'lax'; | ||||||||||||||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||||||||||||||
| 438 | 3606 | ▶ | $key .= '_case-insensitive' if $options->{'case-insensitive'}; | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 439 | 3606 | $key .= '_extract'; | |||||||||||||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||||||||||||||
| 441 | 3606 | my @extracted = $string =~ m/$REGEXES{$key}/g; | |||||||||||||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||||||||||||||
| 443 | 3606 | return map {uc} @extracted; | |||||||||||||||||||||||||||||||||||||||
| 444 | } | ||||||||||||||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||||||||||||||
| 446 | =head2 parse | ||||||||||||||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||||||||||||||
| 448 | my $parsed = Geo::UK::Postcode::Regex->parse( $pc, \%options ); | ||||||||||||||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||||||||||||||
| 450 | Returns hashref of the constituent parts - see SYNOPSIS. Missing parts will be | ||||||||||||||||||||||||||||||||||||||||
| 451 | set as undefined. | ||||||||||||||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||||||||||||||
| 453 | =cut | ||||||||||||||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||||||||||||||
| 455 | sub parse { | ||||||||||||||||||||||||||||||||||||||||
| 456 | 4036 | ▶ | my ( $class, $string, $options ) = @_; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse: called CC=21 SCAR=30.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||||||||||||||
| 458 | 4036 | ▶ | $options ||= {}; | ||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$options ||= {}
MC/DC0%0/1
$options ||= {}
$options
Truth table50%1/2
A
A= $options
| |||||||||||||||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||||||||||||||
| 460 | 4036 | ▶ | $string = uc $string if $options->{'case-insensitive'}; | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||||||||||||||
| 462 | my $re | ||||||||||||||||||||||||||||||||||||||||
| 463 | = $options->{partial} | ||||||||||||||||||||||||||||||||||||||||
| 464 | 4036 | ▶ | ? 'partial_anchored_captures' | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 465 | : 'anchored_captures'; | ||||||||||||||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||||||||||||||
| 467 | 4036 | my ( $area, $district, $sector, $unit ) = $string =~ $REGEXES{"strict_$re"}; | |||||||||||||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||||||||||||||
| 469 | 4036 | ▶ | my $strict = $area ? 1 : 0; # matched strict? | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||||||||||||||
| 471 | 4036 | ▶ | unless ($strict) { | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 472 | 1635 | ▶ | return if $options->{strict}; | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||||||||||||||
| 474 | # try lax regex | ||||||||||||||||||||||||||||||||||||||||
| 475 | 565 | ▶ | ( $area, $district, $sector, $unit ) = $string =~ $REGEXES{"lax_$re"} | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 476 | or return; | ||||||||||||||||||||||||||||||||||||||||
| 477 | } | ||||||||||||||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||||||||||||||
| 479 | 2559 | ▶ | return unless $unit || $options->{partial}; | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$unit or $options->{"partial"}
MC/DC0%0/2
$unit or $options->{"partial"}
$unit
$options->{"partial"}
Truth table66%2/3
A or B
A= $unitB= $options->{"partial"}
| |||||||||||||||||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||||||||||||||
| 481 | 2559 | ▶ | return unless defined $district; | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||||||||||||||
| 483 | 2559 | my $outcode = $area . $district; | |||||||||||||||||||||||||||||||||||||||
| 484 | 2559 | my $outcode_data = $class->outcodes_lookup->{$outcode}; | |||||||||||||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||||||||||||||
| 486 | 2559 | ▶ | return if $options->{valid} && !$outcode_data; | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$options->{"valid"} and not $outcode_data
MC/DC100%2/2
$options->{"valid"} and not $outcode_data
$options->{"valid"}
not $outcode_data
Truth table100%3/3
A and not(B)
A= $options->{"valid"}B= not $outcode_data
| |||||||||||||||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||||||||||||||
| 488 | 2336 | ▶ | my $subdistrict = $district =~ s/([A-Z])$// ? $1 : undef; | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||||||||||||||
| 490 | 2336 | ▶ | my $incode = $unit ? "$sector$unit" : $sector ? $sector : undef; | ||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||||||||||||||
| 492 | return { | ||||||||||||||||||||||||||||||||||||||||
| 493 | area => $area, | ||||||||||||||||||||||||||||||||||||||||
| 494 | district => $district, | ||||||||||||||||||||||||||||||||||||||||
| 495 | subdistrict => $subdistrict, | ||||||||||||||||||||||||||||||||||||||||
| 496 | sector => $sector, | ||||||||||||||||||||||||||||||||||||||||
| 497 | unit => $unit, | ||||||||||||||||||||||||||||||||||||||||
| 498 | outcode => $outcode, | ||||||||||||||||||||||||||||||||||||||||
| 499 | incode => $incode, | ||||||||||||||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||||||||||||||
| 501 | strict => $strict, | ||||||||||||||||||||||||||||||||||||||||
| 502 | partial => $unit ? 0 : 1, | ||||||||||||||||||||||||||||||||||||||||
| 503 | valid => $outcode_data && $strict ? 1 : 0, | ||||||||||||||||||||||||||||||||||||||||
| 504 | |||||||||||||||||||||||||||||||||||||||||
| 505 | 2336 | ▶ | $outcode_data->{non_geographical} ? ( non_geographical => 1 ) : (), | ||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
Condition100%3/3
$outcode_data and $strict
MC/DC100%2/2
$outcode_data and $strict
$outcode_data
$strict
Truth table100%3/3
A and B
A= $outcode_dataB= $strict
| |||||||||||||||||||||||||||||||||||||||||
| 506 | $outcode eq "BF1" ? ( bfpo => 1 ) : (), | ||||||||||||||||||||||||||||||||||||||||
| 507 | }; | ||||||||||||||||||||||||||||||||||||||||
| 508 | } | ||||||||||||||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||||||||||||||
| 510 | =head2 outcode | ||||||||||||||||||||||||||||||||||||||||
| 511 | |||||||||||||||||||||||||||||||||||||||||
| 512 | my $outcode = Geo::UK::Postcode::Regex->outcode( $pc, \%options ); | ||||||||||||||||||||||||||||||||||||||||
| 513 | |||||||||||||||||||||||||||||||||||||||||
| 514 | Extract the outcode (area and district) from a postcode string. Will work on | ||||||||||||||||||||||||||||||||||||||||
| 515 | full or partial postcodes. | ||||||||||||||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||||||||||||||
| 517 | =cut | ||||||||||||||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||||||||||||||
| 519 | sub outcode { | ||||||||||||||||||||||||||||||||||||||||
| 520 | 18 | ▶ | my ( $class, $string, $options ) = @_; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
outcode: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||||||||||||||
| 522 | 18 | ▶ | my $parsed = $class->parse( $string, { partial => 1, %{ $options || {} } } ) | ||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||
| 523 | or return; | ||||||||||||||||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||||||||||||||
| 525 | 14 | return $parsed->{outcode}; | |||||||||||||||||||||||||||||||||||||||
| 526 | } | ||||||||||||||||||||||||||||||||||||||||
| 527 | |||||||||||||||||||||||||||||||||||||||||
| 528 | =head1 LOOKUP METHODS | ||||||||||||||||||||||||||||||||||||||||
| 529 | |||||||||||||||||||||||||||||||||||||||||
| 530 | =head2 outcode_to_posttowns | ||||||||||||||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||||||||||||||
| 532 | my ( $posttown1, $posttown2, ... ) | ||||||||||||||||||||||||||||||||||||||||
| 533 | = Geo::UK::Postcode::Regex->outcode_to_posttowns($outcode); | ||||||||||||||||||||||||||||||||||||||||
| 534 | |||||||||||||||||||||||||||||||||||||||||
| 535 | Returns posttown(s) for supplied outcode. | ||||||||||||||||||||||||||||||||||||||||
| 536 | |||||||||||||||||||||||||||||||||||||||||
| 537 | Note - most outcodes will only have one posttown, but some are shared between | ||||||||||||||||||||||||||||||||||||||||
| 538 | two posttowns. | ||||||||||||||||||||||||||||||||||||||||
| 539 | |||||||||||||||||||||||||||||||||||||||||
| 540 | =cut | ||||||||||||||||||||||||||||||||||||||||
| 541 | |||||||||||||||||||||||||||||||||||||||||
| 542 | sub outcode_to_posttowns { | ||||||||||||||||||||||||||||||||||||||||
| 543 | 2 | ▶ | my ( $class, $outcode ) = @_; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
outcode_to_posttowns: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 544 | |||||||||||||||||||||||||||||||||||||||||
| 545 | 2 | my $data = $class->outcodes_lookup->{$outcode}; | |||||||||||||||||||||||||||||||||||||||
| 546 | |||||||||||||||||||||||||||||||||||||||||
| 547 | 2 | ▶ | return @{ $data ? $data->{posttowns} : [] }; | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 548 | } | ||||||||||||||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||||||||||||||
| 550 | =head2 posttown_to_outcodes | ||||||||||||||||||||||||||||||||||||||||
| 551 | |||||||||||||||||||||||||||||||||||||||||
| 552 | my @outcodes = Geo::UK::Postcode::Regex->posttown_to_outcodes($posttown); | ||||||||||||||||||||||||||||||||||||||||
| 553 | |||||||||||||||||||||||||||||||||||||||||
| 554 | Returns the outcodes covered by a posttown. Note some outcodes are shared | ||||||||||||||||||||||||||||||||||||||||
| 555 | between posttowns. | ||||||||||||||||||||||||||||||||||||||||
| 556 | |||||||||||||||||||||||||||||||||||||||||
| 557 | =cut | ||||||||||||||||||||||||||||||||||||||||
| 558 | |||||||||||||||||||||||||||||||||||||||||
| 559 | sub posttown_to_outcodes { | ||||||||||||||||||||||||||||||||||||||||
| 560 | 1 | ▶ | my ( $class, $posttown ) = @_; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
posttown_to_outcodes: called CC=3 SCAR=11.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 561 | |||||||||||||||||||||||||||||||||||||||||
| 562 | 1 | ▶ | return @{ $class->posttowns_lookup->{ $posttown || '' } || [] }; | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$posttown || ''
MC/DC0%0/1
$posttown || ''
$posttown
Truth table50%1/2
A
A= $posttown
| |||||||||||||||||||||||||||||||||||||||||
| 563 | } | ||||||||||||||||||||||||||||||||||||||||
| 564 | |||||||||||||||||||||||||||||||||||||||||
| 565 | =head2 outcodes_lookup | ||||||||||||||||||||||||||||||||||||||||
| 566 | |||||||||||||||||||||||||||||||||||||||||
| 567 | my %outcodes = %{ Geo::UK::Postcode::Regex->outcodes_lookup }; | ||||||||||||||||||||||||||||||||||||||||
| 568 | print "valid outcode" if $outcodes{$outcode}; | ||||||||||||||||||||||||||||||||||||||||
| 569 | my @posttowns = @{ $outcodes{$outcode} }; | ||||||||||||||||||||||||||||||||||||||||
| 570 | |||||||||||||||||||||||||||||||||||||||||
| 571 | Hashref of outcodes to posttown(s); | ||||||||||||||||||||||||||||||||||||||||
| 572 | |||||||||||||||||||||||||||||||||||||||||
| 573 | =head2 posttowns_lookup | ||||||||||||||||||||||||||||||||||||||||
| 574 | |||||||||||||||||||||||||||||||||||||||||
| 575 | my %posttowns = %{ Geo::UK::Postcode::Regex->posttowns_lookup }; | ||||||||||||||||||||||||||||||||||||||||
| 576 | print "valid posttown" if $posttowns{$posttown}; | ||||||||||||||||||||||||||||||||||||||||
| 577 | my @outcodes = @{ $[posttowns{$posttown} }; | ||||||||||||||||||||||||||||||||||||||||
| 578 | |||||||||||||||||||||||||||||||||||||||||
| 579 | Hashref of posttown to outcode(s); | ||||||||||||||||||||||||||||||||||||||||
| 580 | |||||||||||||||||||||||||||||||||||||||||
| 581 | =cut | ||||||||||||||||||||||||||||||||||||||||
| 582 | |||||||||||||||||||||||||||||||||||||||||
| 583 | sub outcodes_lookup { | ||||||||||||||||||||||||||||||||||||||||
| 584 | 2561 | ▶ | my $class = shift; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
outcodes_lookup: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 585 | |||||||||||||||||||||||||||||||||||||||||
| 586 | 2561 | ▶ | _outcode_data() unless %OUTCODES; | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 587 | |||||||||||||||||||||||||||||||||||||||||
| 588 | 2561 | return \%OUTCODES; | |||||||||||||||||||||||||||||||||||||||
| 589 | } | ||||||||||||||||||||||||||||||||||||||||
| 590 | |||||||||||||||||||||||||||||||||||||||||
| 591 | sub posttowns_lookup { | ||||||||||||||||||||||||||||||||||||||||
| 592 | 1 | ▶ | my $class = shift; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
posttowns_lookup: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 593 | |||||||||||||||||||||||||||||||||||||||||
| 594 | 1 | ▶ | _outcode_data() unless %POSTTOWNS; | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 595 | |||||||||||||||||||||||||||||||||||||||||
| 596 | 1 | return \%POSTTOWNS; | |||||||||||||||||||||||||||||||||||||||
| 597 | } | ||||||||||||||||||||||||||||||||||||||||
| 598 | |||||||||||||||||||||||||||||||||||||||||
| 599 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||
| 600 | |||||||||||||||||||||||||||||||||||||||||
| 601 | =over | ||||||||||||||||||||||||||||||||||||||||
| 602 | |||||||||||||||||||||||||||||||||||||||||
| 603 | =item * | ||||||||||||||||||||||||||||||||||||||||
| 604 | |||||||||||||||||||||||||||||||||||||||||
| 605 | L<Geo::UK::Postcode> | ||||||||||||||||||||||||||||||||||||||||
| 606 | |||||||||||||||||||||||||||||||||||||||||
| 607 | =item * | ||||||||||||||||||||||||||||||||||||||||
| 608 | |||||||||||||||||||||||||||||||||||||||||
| 609 | L<Geo::Address::Mail::UK> | ||||||||||||||||||||||||||||||||||||||||
| 610 | |||||||||||||||||||||||||||||||||||||||||
| 611 | =item * | ||||||||||||||||||||||||||||||||||||||||
| 612 | |||||||||||||||||||||||||||||||||||||||||
| 613 | L<Geo::Postcode> | ||||||||||||||||||||||||||||||||||||||||
| 614 | |||||||||||||||||||||||||||||||||||||||||
| 615 | =item * | ||||||||||||||||||||||||||||||||||||||||
| 616 | |||||||||||||||||||||||||||||||||||||||||
| 617 | L<Data::Validation::Constraints::Postcode> | ||||||||||||||||||||||||||||||||||||||||
| 618 | |||||||||||||||||||||||||||||||||||||||||
| 619 | =item * | ||||||||||||||||||||||||||||||||||||||||
| 620 | |||||||||||||||||||||||||||||||||||||||||
| 621 | L<CGI::Untaint::uk_postcode> | ||||||||||||||||||||||||||||||||||||||||
| 622 | |||||||||||||||||||||||||||||||||||||||||
| 623 | =item * | ||||||||||||||||||||||||||||||||||||||||
| 624 | |||||||||||||||||||||||||||||||||||||||||
| 625 | L<Form::Validator::UKPostcode> | ||||||||||||||||||||||||||||||||||||||||
| 626 | |||||||||||||||||||||||||||||||||||||||||
| 627 | =back | ||||||||||||||||||||||||||||||||||||||||
| 628 | |||||||||||||||||||||||||||||||||||||||||
| 629 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||||||||
| 630 | |||||||||||||||||||||||||||||||||||||||||
| 631 | =head2 Bugs / Feature Requests | ||||||||||||||||||||||||||||||||||||||||
| 632 | |||||||||||||||||||||||||||||||||||||||||
| 633 | Please report any bugs or feature requests through the issue tracker | ||||||||||||||||||||||||||||||||||||||||
| 634 | at L<https://github.com/mjemmeson/geo-uk-postcode-regex/issues>. | ||||||||||||||||||||||||||||||||||||||||
| 635 | You will be notified automatically of any progress on your issue. | ||||||||||||||||||||||||||||||||||||||||
| 636 | |||||||||||||||||||||||||||||||||||||||||
| 637 | =head2 Source Code | ||||||||||||||||||||||||||||||||||||||||
| 638 | |||||||||||||||||||||||||||||||||||||||||
| 639 | This is open source software. The code repository is available for | ||||||||||||||||||||||||||||||||||||||||
| 640 | public review and contribution under the terms of the license. | ||||||||||||||||||||||||||||||||||||||||
| 641 | |||||||||||||||||||||||||||||||||||||||||
| 642 | L<https://github.com/mjemmeson/geo-uk-postcode-regex> | ||||||||||||||||||||||||||||||||||||||||
| 643 | |||||||||||||||||||||||||||||||||||||||||
| 644 | git clone git://github.com/mjemmeson/geo-uk-postcode-regex.git | ||||||||||||||||||||||||||||||||||||||||
| 645 | |||||||||||||||||||||||||||||||||||||||||
| 646 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||
| 647 | |||||||||||||||||||||||||||||||||||||||||
| 648 | Michael Jemmeson E<lt>mjemmeson@cpan.orgE<gt> | ||||||||||||||||||||||||||||||||||||||||
| 649 | |||||||||||||||||||||||||||||||||||||||||
| 650 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||||||||||||||
| 651 | |||||||||||||||||||||||||||||||||||||||||
| 652 | Copyright 2015- Michael Jemmeson | ||||||||||||||||||||||||||||||||||||||||
| 653 | |||||||||||||||||||||||||||||||||||||||||
| 654 | =head1 LICENSE | ||||||||||||||||||||||||||||||||||||||||
| 655 | |||||||||||||||||||||||||||||||||||||||||
| 656 | This library is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||||||||||||||
| 657 | it under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||
| 658 | |||||||||||||||||||||||||||||||||||||||||
| 659 | =cut | ||||||||||||||||||||||||||||||||||||||||
| 660 | |||||||||||||||||||||||||||||||||||||||||
| 661 | 7 | 1; | |||||||||||||||||||||||||||||||||||||||
| 662 | |||||||||||||||||||||||||||||||||||||||||
| 663 | __DATA__ | ||||||||||||||||||||||||||||||||||||||||