| 1 | package Geo::Address::Parser::Country; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 6 | ▶ | use Carp qw(croak carp); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 6 | ▶ | use Locale::Object::Country; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 6 | ▶ | use Object::Configure; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 6 | ▶ | use Params::Get; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 6 | ▶ | use Params::Validate::Strict qw(validate_strict); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 6 | ▶ | use Return::Set qw(set_return); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 6 | our $VERSION = '0.02'; | |||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | # Direct component-to-country mappings, keyed on lowercase component. | ||||||||||||||||||||||||||||||||||||||||||
| 16 | # Values are either a plain country string, or a hashref with | ||||||||||||||||||||||||||||||||||||||||||
| 17 | # 'country' and optional 'warning' keys. | ||||||||||||||||||||||||||||||||||||||||||
| 18 | 6 | my %DIRECT = ( | |||||||||||||||||||||||||||||||||||||||||
| 19 | 'england' => 'United Kingdom', | ||||||||||||||||||||||||||||||||||||||||||
| 20 | 'scotland' => 'United Kingdom', | ||||||||||||||||||||||||||||||||||||||||||
| 21 | 'wales' => 'United Kingdom', | ||||||||||||||||||||||||||||||||||||||||||
| 22 | 'isle of man' => 'United Kingdom', | ||||||||||||||||||||||||||||||||||||||||||
| 23 | 'northern ireland' => 'United Kingdom', | ||||||||||||||||||||||||||||||||||||||||||
| 24 | 'uk' => 'United Kingdom', | ||||||||||||||||||||||||||||||||||||||||||
| 25 | 'england, uk' => 'United Kingdom', | ||||||||||||||||||||||||||||||||||||||||||
| 26 | 'england uk' => 'United Kingdom', | ||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | # Malformed but seen in real data | ||||||||||||||||||||||||||||||||||||||||||
| 29 | 'scot' => { | ||||||||||||||||||||||||||||||||||||||||||
| 30 | country => 'United Kingdom', | ||||||||||||||||||||||||||||||||||||||||||
| 31 | warning => "country should be 'Scotland' not 'Scot'", | ||||||||||||||||||||||||||||||||||||||||||
| 32 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | # US variants | ||||||||||||||||||||||||||||||||||||||||||
| 35 | 'usa' => 'United States', | ||||||||||||||||||||||||||||||||||||||||||
| 36 | 'us' => 'United States', | ||||||||||||||||||||||||||||||||||||||||||
| 37 | 'u.s.a.' => 'United States', | ||||||||||||||||||||||||||||||||||||||||||
| 38 | 'united states of america' => 'United States', | ||||||||||||||||||||||||||||||||||||||||||
| 39 | 'united states' => 'United States', | ||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | # German historical names | ||||||||||||||||||||||||||||||||||||||||||
| 42 | 'preussen' => 'Germany', | ||||||||||||||||||||||||||||||||||||||||||
| 43 | "preu\x{00DF}en" => 'Germany', | ||||||||||||||||||||||||||||||||||||||||||
| 44 | 'deutschland' => 'Germany', | ||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | # Dutch variants | ||||||||||||||||||||||||||||||||||||||||||
| 47 | 'holland' => 'Netherlands', | ||||||||||||||||||||||||||||||||||||||||||
| 48 | 'the netherlands' => 'Netherlands', | ||||||||||||||||||||||||||||||||||||||||||
| 49 | # 'nl' => { # Not a common abbreviation and clashes with Newfoundland in Canada | ||||||||||||||||||||||||||||||||||||||||||
| 50 | # country => 'Netherlands', | ||||||||||||||||||||||||||||||||||||||||||
| 51 | # warning => 'assuming country is Netherlands', | ||||||||||||||||||||||||||||||||||||||||||
| 52 | # }, | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | # Slovenian historical name | ||||||||||||||||||||||||||||||||||||||||||
| 55 | 'slovenija' => 'Slovenia', | ||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | # Canadian provinces/territories missing their country | ||||||||||||||||||||||||||||||||||||||||||
| 58 | 'nova scotia' => { | ||||||||||||||||||||||||||||||||||||||||||
| 59 | country => 'Canada', | ||||||||||||||||||||||||||||||||||||||||||
| 60 | warning => "country 'Canada' missing from record", | ||||||||||||||||||||||||||||||||||||||||||
| 61 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 62 | 'newfoundland' => { | ||||||||||||||||||||||||||||||||||||||||||
| 63 | country => 'Canada', | ||||||||||||||||||||||||||||||||||||||||||
| 64 | warning => "country 'Canada' missing from record", | ||||||||||||||||||||||||||||||||||||||||||
| 65 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 66 | 'nfld' => { | ||||||||||||||||||||||||||||||||||||||||||
| 67 | country => 'Canada', | ||||||||||||||||||||||||||||||||||||||||||
| 68 | warning => "country 'Canada' missing from record", | ||||||||||||||||||||||||||||||||||||||||||
| 69 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 70 | 'ns' => { | ||||||||||||||||||||||||||||||||||||||||||
| 71 | country => 'Canada', | ||||||||||||||||||||||||||||||||||||||||||
| 72 | warning => "country 'Canada' missing from record", | ||||||||||||||||||||||||||||||||||||||||||
| 73 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 74 | 'can.' => { | ||||||||||||||||||||||||||||||||||||||||||
| 75 | country => 'Canada', | ||||||||||||||||||||||||||||||||||||||||||
| 76 | warning => "country 'Canada' missing from record", | ||||||||||||||||||||||||||||||||||||||||||
| 77 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 78 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | # Schema for new() arguments, used by Params::Validate::Strict | ||||||||||||||||||||||||||||||||||||||||||
| 81 | 6 | my $NEW_SCHEMA = { | |||||||||||||||||||||||||||||||||||||||||
| 82 | us => { type => 'object' }, | ||||||||||||||||||||||||||||||||||||||||||
| 83 | ca_en => { type => 'object' }, | ||||||||||||||||||||||||||||||||||||||||||
| 84 | ca_fr => { type => 'object' }, | ||||||||||||||||||||||||||||||||||||||||||
| 85 | au => { type => 'object' }, | ||||||||||||||||||||||||||||||||||||||||||
| 86 | geonames => { | ||||||||||||||||||||||||||||||||||||||||||
| 87 | type => 'object', | ||||||||||||||||||||||||||||||||||||||||||
| 88 | can => 'search', | ||||||||||||||||||||||||||||||||||||||||||
| 89 | optional => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 90 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 91 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | # Schema for resolve() arguments | ||||||||||||||||||||||||||||||||||||||||||
| 94 | 6 | my $RESOLVE_SCHEMA = { | |||||||||||||||||||||||||||||||||||||||||
| 95 | component => { type => 'string', min => 1 }, | ||||||||||||||||||||||||||||||||||||||||||
| 96 | place => { type => 'string', min => 1 }, | ||||||||||||||||||||||||||||||||||||||||||
| 97 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | # Schema for the resolve() return value | ||||||||||||||||||||||||||||||||||||||||||
| 100 | 6 | my $RESOLVE_RETURN_SCHEMA = { | |||||||||||||||||||||||||||||||||||||||||
| 101 | type => 'hashref', | ||||||||||||||||||||||||||||||||||||||||||
| 102 | schema => { | ||||||||||||||||||||||||||||||||||||||||||
| 103 | country => { type => 'string', optional => 1 }, | ||||||||||||||||||||||||||||||||||||||||||
| 104 | place => { type => 'string', min => 1 }, | ||||||||||||||||||||||||||||||||||||||||||
| 105 | warnings => { type => 'arrayref' }, | ||||||||||||||||||||||||||||||||||||||||||
| 106 | unknown => { type => 'boolean' }, | ||||||||||||||||||||||||||||||||||||||||||
| 107 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 108 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | Geo::Address::Parser::Country - Resolve a place string component to a | ||||||||||||||||||||||||||||||||||||||||||
| 113 | canonical country name | ||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | Version 0.02 | ||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | use Geo::Address::Parser::Country; | ||||||||||||||||||||||||||||||||||||||||||
| 122 | use Locale::US; | ||||||||||||||||||||||||||||||||||||||||||
| 123 | use Locale::CA; | ||||||||||||||||||||||||||||||||||||||||||
| 124 | use Locale::AU; | ||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | my $resolver = Geo::Address::Parser::Country->new({ | ||||||||||||||||||||||||||||||||||||||||||
| 127 | us => Locale::US->new(), | ||||||||||||||||||||||||||||||||||||||||||
| 128 | ca_en => Locale::CA->new(lang => 'en'), | ||||||||||||||||||||||||||||||||||||||||||
| 129 | ca_fr => Locale::CA->new(lang => 'fr'), | ||||||||||||||||||||||||||||||||||||||||||
| 130 | au => Locale::AU->new(), | ||||||||||||||||||||||||||||||||||||||||||
| 131 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | my $result = $resolver->resolve( | ||||||||||||||||||||||||||||||||||||||||||
| 134 | component => 'England', | ||||||||||||||||||||||||||||||||||||||||||
| 135 | place => 'Ramsgate, Kent, England', | ||||||||||||||||||||||||||||||||||||||||||
| 136 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | # $result->{country} eq 'United Kingdom' | ||||||||||||||||||||||||||||||||||||||||||
| 139 | # $result->{place} eq 'Ramsgate, Kent, England' | ||||||||||||||||||||||||||||||||||||||||||
| 140 | # $result->{warnings} is [] | ||||||||||||||||||||||||||||||||||||||||||
| 141 | # $result->{unknown} is 0 | ||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | Resolves the last comma-separated component of a place string into a | ||||||||||||||||||||||||||||||||||||||||||
| 146 | canonical country name. Handles common variants, abbreviations, and | ||||||||||||||||||||||||||||||||||||||||||
| 147 | historical names found in genealogy data and other poorly-normalised | ||||||||||||||||||||||||||||||||||||||||||
| 148 | address sources. | ||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||
| 150 | Designed specifically to tolerate poor-quality data from software | ||||||||||||||||||||||||||||||||||||||||||
| 151 | imports where place strings may be inconsistent, abbreviated, or use | ||||||||||||||||||||||||||||||||||||||||||
| 152 | historical country names no longer in common use. | ||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||
| 154 | Resolution proceeds through the following steps in order: | ||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||
| 156 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||
| 158 | =item 1. Direct lookup table (covers historical names, abbreviations, | ||||||||||||||||||||||||||||||||||||||||||
| 159 | common variants) | ||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||
| 161 | =item 2. US state code or name via Locale::US | ||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||
| 163 | =item 3. Canadian province code or name via Locale::CA (English and French) | ||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||
| 165 | =item 4. Australian state code or name via Locale::AU | ||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||
| 167 | =item 5. Locale::Object::Country by name | ||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||
| 169 | =item 6. Geo::GeoNames search (optional, only if object provided at | ||||||||||||||||||||||||||||||||||||||||||
| 170 | construction) | ||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||
| 172 | =item 7. Unknown - returns with C<unknown =E<gt> 1> | ||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||
| 174 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||
| 176 | =head1 TODO | ||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||
| 178 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||
| 180 | =item * Add C<normalise_place()> to handle missing commas before | ||||||||||||||||||||||||||||||||||||||||||
| 181 | country and state names in raw uncleaned input strings. Poor data | ||||||||||||||||||||||||||||||||||||||||||
| 182 | import means strings like C<"Houston TX USA"> or | ||||||||||||||||||||||||||||||||||||||||||
| 183 | C<"Some Place England"> need comma insertion before component | ||||||||||||||||||||||||||||||||||||||||||
| 184 | extraction can work correctly. This should be implemented before | ||||||||||||||||||||||||||||||||||||||||||
| 185 | relying on C<resolve()> for raw uncleaned input. | ||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||
| 187 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||
| 189 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||
| 191 | =head2 new | ||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||
| 193 | =head3 Purpose | ||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||
| 195 | Constructs a new resolver object. The locale objects are used for | ||||||||||||||||||||||||||||||||||||||||||
| 196 | state and province lookups and are retained for the lifetime of the | ||||||||||||||||||||||||||||||||||||||||||
| 197 | object. | ||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||
| 199 | =head3 API Specification | ||||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||
| 201 | =head4 Input | ||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||
| 203 | { | ||||||||||||||||||||||||||||||||||||||||||
| 204 | us => { type => 'object', can => 'new' }, # Locale::US instance | ||||||||||||||||||||||||||||||||||||||||||
| 205 | ca_en => { type => 'object', can => 'new' }, # Locale::CA English instance | ||||||||||||||||||||||||||||||||||||||||||
| 206 | ca_fr => { type => 'object', can => 'new' }, # Locale::CA French instance | ||||||||||||||||||||||||||||||||||||||||||
| 207 | au => { type => 'object', can => 'new' }, # Locale::AU instance | ||||||||||||||||||||||||||||||||||||||||||
| 208 | geonames => { # Optional Geo::GeoNames instance | ||||||||||||||||||||||||||||||||||||||||||
| 209 | type => 'object', | ||||||||||||||||||||||||||||||||||||||||||
| 210 | can => 'search', | ||||||||||||||||||||||||||||||||||||||||||
| 211 | optional => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 212 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||
| 215 | =head4 Output | ||||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||
| 217 | { type => 'object', isa => 'Geo::Address::Parser::Country' } | ||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||
| 219 | =head3 Arguments | ||||||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||||||
| 221 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||
| 223 | =item * C<us> - A L<Locale::US> instance. Required. | ||||||||||||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||||||||||||
| 225 | =item * C<ca_en> - A L<Locale::CA> instance with C<lang =E<gt> 'en'>. Required. | ||||||||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||||||||
| 227 | =item * C<ca_fr> - A L<Locale::CA> instance with C<lang =E<gt> 'fr'>. Required. | ||||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||
| 229 | =item * C<au> - A L<Locale::AU> instance. Required. | ||||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||
| 231 | =item * C<geonames> - An optional L<Geo::GeoNames> instance used as a | ||||||||||||||||||||||||||||||||||||||||||
| 232 | last-resort fallback when all other resolution methods fail. | ||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||
| 234 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||
| 236 | =head3 Returns | ||||||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||||||
| 238 | A blessed C<Geo::Address::Parser::Country> object. | ||||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||
| 240 | =head3 Side Effects | ||||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||
| 242 | None. | ||||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||
| 244 | =head3 Notes | ||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||
| 246 | The locale objects are stored by reference and shared for all calls to | ||||||||||||||||||||||||||||||||||||||||||
| 247 | C<resolve()>. Constructing them once and reusing the resolver object | ||||||||||||||||||||||||||||||||||||||||||
| 248 | is more efficient than constructing a new resolver for each lookup. | ||||||||||||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||||||||||||
| 250 | =head3 Example | ||||||||||||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||||||||||||
| 252 | my $resolver = Geo::Address::Parser::Country->new({ | ||||||||||||||||||||||||||||||||||||||||||
| 253 | us => Locale::US->new(), | ||||||||||||||||||||||||||||||||||||||||||
| 254 | ca_en => Locale::CA->new(lang => 'en'), | ||||||||||||||||||||||||||||||||||||||||||
| 255 | ca_fr => Locale::CA->new(lang => 'fr'), | ||||||||||||||||||||||||||||||||||||||||||
| 256 | au => Locale::AU->new(), | ||||||||||||||||||||||||||||||||||||||||||
| 257 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||
| 259 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||||||||
| 261 | sub new { | ||||||||||||||||||||||||||||||||||||||||||
| 262 | 179 | ▶ | my $class = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||||||||||
| 264 | # Accept both hashref and flat list via Params::Get | ||||||||||||||||||||||||||||||||||||||||||
| 265 | 179 | my $args = Params::Get::get_params(undef, @_); | |||||||||||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||||||||||||
| 267 | # Validate all constructor arguments strictly | ||||||||||||||||||||||||||||||||||||||||||
| 268 | 179 | validate_strict({ | |||||||||||||||||||||||||||||||||||||||||
| 269 | description => 'Geo::Address::Parser::Country::new', | ||||||||||||||||||||||||||||||||||||||||||
| 270 | input => $args, | ||||||||||||||||||||||||||||||||||||||||||
| 271 | schema => $NEW_SCHEMA, | ||||||||||||||||||||||||||||||||||||||||||
| 272 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||||||
| 274 | 174 | $args = Object::Configure::configure($class, $args); | |||||||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||||||||
| 276 | # Build and return the blessed object | ||||||||||||||||||||||||||||||||||||||||||
| 277 | return bless { | ||||||||||||||||||||||||||||||||||||||||||
| 278 | us => $args->{us}, | ||||||||||||||||||||||||||||||||||||||||||
| 279 | ca_en => $args->{ca_en}, | ||||||||||||||||||||||||||||||||||||||||||
| 280 | ca_fr => $args->{ca_fr}, | ||||||||||||||||||||||||||||||||||||||||||
| 281 | au => $args->{au}, | ||||||||||||||||||||||||||||||||||||||||||
| 282 | geonames => $args->{geonames}, | ||||||||||||||||||||||||||||||||||||||||||
| 283 | 174 | }, $class; | |||||||||||||||||||||||||||||||||||||||||
| 284 | } | ||||||||||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||||||||||
| 286 | =head2 resolve | ||||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||
| 288 | =head3 Purpose | ||||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||
| 290 | Resolves the last comma-separated component of a place string to a | ||||||||||||||||||||||||||||||||||||||||||
| 291 | canonical country name, and returns the (possibly modified) place | ||||||||||||||||||||||||||||||||||||||||||
| 292 | string alongside any warnings generated during resolution. | ||||||||||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||||||||||
| 294 | =head3 API Specification | ||||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||
| 296 | =head4 Input | ||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||
| 298 | { | ||||||||||||||||||||||||||||||||||||||||||
| 299 | component => { type => 'string', min => 1 }, | ||||||||||||||||||||||||||||||||||||||||||
| 300 | place => { type => 'string', min => 1 }, | ||||||||||||||||||||||||||||||||||||||||||
| 301 | } | ||||||||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||||||||
| 303 | =head4 Output | ||||||||||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||||||||
| 305 | { | ||||||||||||||||||||||||||||||||||||||||||
| 306 | type => 'hashref', | ||||||||||||||||||||||||||||||||||||||||||
| 307 | schema => { | ||||||||||||||||||||||||||||||||||||||||||
| 308 | country => { type => 'string', optional => 1 }, | ||||||||||||||||||||||||||||||||||||||||||
| 309 | place => { type => 'string', min => 1 }, | ||||||||||||||||||||||||||||||||||||||||||
| 310 | warnings => { type => 'arrayref' }, | ||||||||||||||||||||||||||||||||||||||||||
| 311 | unknown => { type => 'boolean' }, | ||||||||||||||||||||||||||||||||||||||||||
| 312 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||||||||||
| 315 | =head3 Arguments | ||||||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||||||
| 317 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||||||||
| 319 | =item * C<component> - The last comma-separated component of the place | ||||||||||||||||||||||||||||||||||||||||||
| 320 | string, e.g. C<"England">, C<"TX">, C<"NSW">. Required. | ||||||||||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||||||||||
| 322 | =item * C<place> - The full place string, e.g. | ||||||||||||||||||||||||||||||||||||||||||
| 323 | C<"Ramsgate, Kent, England">. May be modified by appending a country | ||||||||||||||||||||||||||||||||||||||||||
| 324 | suffix where needed. Required. | ||||||||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||||||||
| 326 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||
| 328 | =head3 Returns | ||||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||
| 330 | A hashref containing: | ||||||||||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||||||||||
| 332 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||||||||||
| 334 | =item * C<country> - The canonical country name as a string, e.g. | ||||||||||||||||||||||||||||||||||||||||||
| 335 | C<"United Kingdom">. C<undef> if resolution failed. | ||||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||||
| 337 | =item * C<place> - The full place string, possibly with a country | ||||||||||||||||||||||||||||||||||||||||||
| 338 | suffix appended (e.g. C<", USA">). Always returned even if unmodified. | ||||||||||||||||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||||||||||||||||
| 340 | =item * C<warnings> - An arrayref of warning strings generated during | ||||||||||||||||||||||||||||||||||||||||||
| 341 | resolution. May be empty. The caller is responsible for acting on | ||||||||||||||||||||||||||||||||||||||||||
| 342 | these, e.g. by passing them to a C<complain()> function. | ||||||||||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||||||||||
| 344 | =item * C<unknown> - A boolean. True if the country could not be | ||||||||||||||||||||||||||||||||||||||||||
| 345 | resolved by any method. | ||||||||||||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||||||||||||
| 347 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||||||||||||
| 349 | =head3 Side Effects | ||||||||||||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||||||||||||
| 351 | None. All warnings are returned to the caller rather than emitted | ||||||||||||||||||||||||||||||||||||||||||
| 352 | directly. | ||||||||||||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||||||||||||
| 354 | =head3 Notes | ||||||||||||||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||||||||||||
| 356 | Resolution order is: direct lookup, US state, Canadian province, | ||||||||||||||||||||||||||||||||||||||||||
| 357 | Australian state, Locale::Object::Country, GeoNames (if available). | ||||||||||||||||||||||||||||||||||||||||||
| 358 | The first successful match wins. | ||||||||||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||||||||||
| 360 | When a US state, Canadian province, or Australian state is recognised, | ||||||||||||||||||||||||||||||||||||||||||
| 361 | the appropriate country string (C<", USA">, C<", Canada">, | ||||||||||||||||||||||||||||||||||||||||||
| 362 | C<", Australia">) is appended to C<place> if not already present. | ||||||||||||||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||||||||||||||
| 364 | =head3 Example | ||||||||||||||||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||||||||||||||||
| 366 | my $result = $resolver->resolve( | ||||||||||||||||||||||||||||||||||||||||||
| 367 | component => 'TX', | ||||||||||||||||||||||||||||||||||||||||||
| 368 | place => 'Houston, TX', | ||||||||||||||||||||||||||||||||||||||||||
| 369 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||||||||||||||||
| 371 | # $result->{country} eq 'United States' | ||||||||||||||||||||||||||||||||||||||||||
| 372 | # $result->{place} eq 'Houston, TX, USA' | ||||||||||||||||||||||||||||||||||||||||||
| 373 | # $result->{warnings}[0] eq 'TX: assuming country is United States' | ||||||||||||||||||||||||||||||||||||||||||
| 374 | # $result->{unknown} is 0 | ||||||||||||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||||||||||||
| 376 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||||||||||
| 378 | sub resolve { | ||||||||||||||||||||||||||||||||||||||||||
| 379 | 1331 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
resolve: called CC=18 SCAR=28.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||||||||||||
| 381 | # Accept both hashref and flat list | ||||||||||||||||||||||||||||||||||||||||||
| 382 | 1331 | my $args = Params::Get::get_params(undef, @_); | |||||||||||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||||||||||||
| 384 | # Validate input arguments | ||||||||||||||||||||||||||||||||||||||||||
| 385 | 1331 | validate_strict({ | |||||||||||||||||||||||||||||||||||||||||
| 386 | description => 'Geo::Address::Parser::Country::resolve', | ||||||||||||||||||||||||||||||||||||||||||
| 387 | input => $args, | ||||||||||||||||||||||||||||||||||||||||||
| 388 | schema => $RESOLVE_SCHEMA, | ||||||||||||||||||||||||||||||||||||||||||
| 389 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||||||||||||
| 391 | 1331 | my $component = $args->{component}; | |||||||||||||||||||||||||||||||||||||||||
| 392 | 1331 | my $place = $args->{place}; | |||||||||||||||||||||||||||||||||||||||||
| 393 | 1331 | my $lc = lc($component); | |||||||||||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||||||||||||
| 395 | # Accumulate warnings to return to caller rather than emitting them | ||||||||||||||||||||||||||||||||||||||||||
| 396 | 1331 | my @warnings; | |||||||||||||||||||||||||||||||||||||||||
| 397 | my $country; | ||||||||||||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||||||||||||
| 399 | # Step 1: check the direct lookup table first, handles historical | ||||||||||||||||||||||||||||||||||||||||||
| 400 | # names, common abbreviations and malformed but real-world input | ||||||||||||||||||||||||||||||||||||||||||
| 401 | 1331 | ▶ | if(my $match = $DIRECT{$lc}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 9 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 402 | 286 | ▶ | if(ref($match) eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 403 | 21 | $country = $match->{country}; | |||||||||||||||||||||||||||||||||||||||||
| 404 | 21 | ▶ | push @warnings, $match->{warning} if $match->{warning}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 405 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 406 | 265 | $country = $match; | |||||||||||||||||||||||||||||||||||||||||
| 407 | } | ||||||||||||||||||||||||||||||||||||||||||
| 408 | } | ||||||||||||||||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||||||||||||||||
| 410 | # Step 2: two-letter US state code, e.g. "TX" | ||||||||||||||||||||||||||||||||||||||||||
| 411 | ▶ | elsif($component =~ /^[A-Z]{2}$/i | |||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$component =~ /^[A-Z]{2}$/i and $self->{"us"}{"code2state"}{uc $component}
MC/DC100%2/2
$component =~ /^[A-Z]{2}$/i and $self->{"us"}{"code2state"}{uc $component}
$component =~ /^[A-Z]{2}$/i
$self->{"us"}{"code2state"}{uc $component}
Truth table100%3/3
A and B
A= $component =~ /^[A-Z]{2}$/iB= $self->{"us"}{"code2state"}{uc $component}
| |||||||||||||||||||||||||||||||||||||||||||
| 412 | && $self->{us}{code2state}{uc($component)}) { | ||||||||||||||||||||||||||||||||||||||||||
| 413 | 649 | $country = 'United States'; | |||||||||||||||||||||||||||||||||||||||||
| 414 | 649 | push @warnings, "$component: assuming country is United States"; | |||||||||||||||||||||||||||||||||||||||||
| 415 | 649 | $place = $self->_append_country($place, 'USA'); | |||||||||||||||||||||||||||||||||||||||||
| 416 | } | ||||||||||||||||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||||||||||||||||
| 418 | # Step 3: US state full name, e.g. "Texas" | ||||||||||||||||||||||||||||||||||||||||||
| 419 | elsif($self->{us}{state2code}{uc($component)}) { | ||||||||||||||||||||||||||||||||||||||||||
| 420 | 4 | $country = 'United States'; | |||||||||||||||||||||||||||||||||||||||||
| 421 | 4 | push @warnings, "$component: assuming country is United States"; | |||||||||||||||||||||||||||||||||||||||||
| 422 | 4 | $place = $self->_append_country($place, 'USA'); | |||||||||||||||||||||||||||||||||||||||||
| 423 | } | ||||||||||||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||||||||||||
| 425 | # Step 4: Canadian province code in English or French | ||||||||||||||||||||||||||||||||||||||||||
| 426 | ▶ | elsif($component =~ /^[A-Z]{2}$/i | |||||||||||||||||||||||||||||||||||||||||
|
Condition100%6/6
$component =~ /^[A-Z]{2}$/i and $self->{"ca_en"}{"code2province"}{uc $component} || $self->{"ca_fr"}{"code2province"}{uc $component}
$self->{"ca_en"}{"code2province"}{uc $component} or $self->{"ca_fr"}{"code2province"}{uc $component}
MC/DC100%3/3
$component =~ /^[A-Z]{2}$/i and $self->{"ca_en"}{"code2province"}{uc $component} || $self->{"ca_fr"}{"code2province"}{uc $component}
$component =~ /^[A-Z]{2}$/i
$self->{"ca_en"}{"code2province"}{uc $component}
$self->{"ca_fr"}{"code2province"}{uc $component}
Truth table100%4/4
A and B or C
A= $component =~ /^[A-Z]{2}$/iB= $self->{"ca_en"}{"code2province"}{uc $component}C= $self->{"ca_fr"}{"code2province"}{uc $component}
| |||||||||||||||||||||||||||||||||||||||||||
| 427 | && ($self->{ca_en}{code2province}{uc($component)} | ||||||||||||||||||||||||||||||||||||||||||
| 428 | || $self->{ca_fr}{code2province}{uc($component)})) { | ||||||||||||||||||||||||||||||||||||||||||
| 429 | 91 | $country = 'Canada'; | |||||||||||||||||||||||||||||||||||||||||
| 430 | 91 | push @warnings, "$component: assuming country is Canada"; | |||||||||||||||||||||||||||||||||||||||||
| 431 | 91 | $place = $self->_append_country($place, 'Canada'); | |||||||||||||||||||||||||||||||||||||||||
| 432 | } | ||||||||||||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||||||||||||
| 434 | # Step 5: Canadian province full name in English or French | ||||||||||||||||||||||||||||||||||||||||||
| 435 | ▶ | elsif($self->{ca_en}{province2code}{uc($component)} | |||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$self->{"ca_en"}{"province2code"}{uc $component} or $self->{"ca_fr"}{"province2code"}{uc $component}
MC/DC100%2/2
$self->{"ca_en"}{"province2code"}{uc $component} or $self->{"ca_fr"}{"province2code"}{uc $component}
$self->{"ca_en"}{"province2code"}{uc $component}
$self->{"ca_fr"}{"province2code"}{uc $component}
Truth table100%3/3
A or B
A= $self->{"ca_en"}{"province2code"}{uc $component}B= $self->{"ca_fr"}{"province2code"}{uc $component}
| |||||||||||||||||||||||||||||||||||||||||||
| 436 | || $self->{ca_fr}{province2code}{uc($component)}) { | ||||||||||||||||||||||||||||||||||||||||||
| 437 | 6 | $country = 'Canada'; | |||||||||||||||||||||||||||||||||||||||||
| 438 | 6 | push @warnings, "$component: assuming country is Canada"; | |||||||||||||||||||||||||||||||||||||||||
| 439 | 6 | $place = $self->_append_country($place, 'Canada'); | |||||||||||||||||||||||||||||||||||||||||
| 440 | } | ||||||||||||||||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||||||||||||||||
| 442 | # Step 6: Australian state code, e.g. "NSW", "VIC" | ||||||||||||||||||||||||||||||||||||||||||
| 443 | ▶ | elsif($component =~ /^[A-Z]{2,3}$/i | |||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$component =~ /^[A-Z]{2,3}$/i and $self->{"au"}{"code2state"}{$component}
MC/DC100%2/2
$component =~ /^[A-Z]{2,3}$/i and $self->{"au"}{"code2state"}{$component}
$component =~ /^[A-Z]{2,3}$/i
$self->{"au"}{"code2state"}{$component}
Truth table100%3/3
A and B
A= $component =~ /^[A-Z]{2,3}$/iB= $self->{"au"}{"code2state"}{$component}
| |||||||||||||||||||||||||||||||||||||||||||
| 444 | && $self->{au}{code2state}{$component}) { | ||||||||||||||||||||||||||||||||||||||||||
| 445 | 80 | $country = 'Australia'; | |||||||||||||||||||||||||||||||||||||||||
| 446 | 80 | push @warnings, "$component: assuming country is Australia"; | |||||||||||||||||||||||||||||||||||||||||
| 447 | 80 | $place = $self->_append_country($place, 'Australia'); | |||||||||||||||||||||||||||||||||||||||||
| 448 | } | ||||||||||||||||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||||||||||||||||
| 450 | # Step 7: Australian state full name | ||||||||||||||||||||||||||||||||||||||||||
| 451 | elsif($self->{au}{state2code}{uc($component)}) { | ||||||||||||||||||||||||||||||||||||||||||
| 452 | 3 | $country = 'Australia'; | |||||||||||||||||||||||||||||||||||||||||
| 453 | 3 | push @warnings, "$component: assuming country is Australia"; | |||||||||||||||||||||||||||||||||||||||||
| 454 | 3 | $place = $self->_append_country($place, 'Australia'); | |||||||||||||||||||||||||||||||||||||||||
| 455 | } | ||||||||||||||||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||||||||||||||||
| 457 | # Step 8: fall back to Locale::Object::Country by name | ||||||||||||||||||||||||||||||||||||||||||
| 458 | elsif(my $loc = Locale::Object::Country->new(name => $component)) { | ||||||||||||||||||||||||||||||||||||||||||
| 459 | 101 | $country = $loc->name(); | |||||||||||||||||||||||||||||||||||||||||
| 460 | } | ||||||||||||||||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||||||||||||||||
| 462 | # Step 9: optional GeoNames fallback for anything still unresolved | ||||||||||||||||||||||||||||||||||||||||||
| 463 | elsif($self->{geonames}) { | ||||||||||||||||||||||||||||||||||||||||||
| 464 | 18 | $country = $self->_geonames_lookup( | |||||||||||||||||||||||||||||||||||||||||
| 465 | $place, $component, \@warnings | ||||||||||||||||||||||||||||||||||||||||||
| 466 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 467 | } | ||||||||||||||||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||||||||||||||||
| 469 | # Build and validate the return structure before handing back | ||||||||||||||||||||||||||||||||||||||||||
| 470 | 1330 | ▶ | my $result = { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 471 | country => $country, | ||||||||||||||||||||||||||||||||||||||||||
| 472 | place => $place, | ||||||||||||||||||||||||||||||||||||||||||
| 473 | warnings => \@warnings, | ||||||||||||||||||||||||||||||||||||||||||
| 474 | unknown => defined($country) ? 0 : 1, | ||||||||||||||||||||||||||||||||||||||||||
| 475 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||||||||||||||||
| 477 | 1330 | return set_return($result, $RESOLVE_RETURN_SCHEMA); | |||||||||||||||||||||||||||||||||||||||||
| 478 | } | ||||||||||||||||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||||||||||||||||
| 480 | # _append_country | ||||||||||||||||||||||||||||||||||||||||||
| 481 | # | ||||||||||||||||||||||||||||||||||||||||||
| 482 | # Purpose: | ||||||||||||||||||||||||||||||||||||||||||
| 483 | # Appends a country suffix to a place string if not already present. | ||||||||||||||||||||||||||||||||||||||||||
| 484 | # | ||||||||||||||||||||||||||||||||||||||||||
| 485 | # Entry criteria: | ||||||||||||||||||||||||||||||||||||||||||
| 486 | # $self - blessed object | ||||||||||||||||||||||||||||||||||||||||||
| 487 | # $place - non-empty place string | ||||||||||||||||||||||||||||||||||||||||||
| 488 | # $suffix - country string to append, e.g. 'USA' | ||||||||||||||||||||||||||||||||||||||||||
| 489 | # | ||||||||||||||||||||||||||||||||||||||||||
| 490 | # Exit status: | ||||||||||||||||||||||||||||||||||||||||||
| 491 | # Returns the (possibly modified) place string. | ||||||||||||||||||||||||||||||||||||||||||
| 492 | # | ||||||||||||||||||||||||||||||||||||||||||
| 493 | # Side effects: | ||||||||||||||||||||||||||||||||||||||||||
| 494 | # None. | ||||||||||||||||||||||||||||||||||||||||||
| 495 | # | ||||||||||||||||||||||||||||||||||||||||||
| 496 | # Notes: | ||||||||||||||||||||||||||||||||||||||||||
| 497 | # Uses a case-insensitive check to avoid double-appending. | ||||||||||||||||||||||||||||||||||||||||||
| 498 | # E.g. 'Houston, TX' becomes 'Houston, TX, USA'. | ||||||||||||||||||||||||||||||||||||||||||
| 499 | # 'Houston, TX, USA' is returned unchanged. | ||||||||||||||||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||||||||||||||||
| 501 | sub _append_country { | ||||||||||||||||||||||||||||||||||||||||||
| 502 | 843 | ▶ | my ($self, $place, $suffix) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_append_country: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||||||||||||||||
| 504 | # Return unchanged if suffix already present at end of string | ||||||||||||||||||||||||||||||||||||||||||
| 505 | 843 | ▶ | return $place if $place =~ /,\s*\Q$suffix\E\s*$/i; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 506 | |||||||||||||||||||||||||||||||||||||||||||
| 507 | 831 | return "$place, $suffix"; | |||||||||||||||||||||||||||||||||||||||||
| 508 | } | ||||||||||||||||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||||||||||||||||
| 510 | # _geonames_lookup | ||||||||||||||||||||||||||||||||||||||||||
| 511 | # | ||||||||||||||||||||||||||||||||||||||||||
| 512 | # Purpose: | ||||||||||||||||||||||||||||||||||||||||||
| 513 | # Uses the optional Geo::GeoNames object to search for a country | ||||||||||||||||||||||||||||||||||||||||||
| 514 | # name when all other resolution methods have failed. | ||||||||||||||||||||||||||||||||||||||||||
| 515 | # | ||||||||||||||||||||||||||||||||||||||||||
| 516 | # Entry criteria: | ||||||||||||||||||||||||||||||||||||||||||
| 517 | # $self - blessed object with {geonames} set | ||||||||||||||||||||||||||||||||||||||||||
| 518 | # $place - full place string, used as the search query for | ||||||||||||||||||||||||||||||||||||||||||
| 519 | # maximum context | ||||||||||||||||||||||||||||||||||||||||||
| 520 | # $component - original component string, used in warning text | ||||||||||||||||||||||||||||||||||||||||||
| 521 | # $warnings - arrayref to push warnings onto | ||||||||||||||||||||||||||||||||||||||||||
| 522 | # | ||||||||||||||||||||||||||||||||||||||||||
| 523 | # Exit status: | ||||||||||||||||||||||||||||||||||||||||||
| 524 | # Returns the country name string on success, undef on failure. | ||||||||||||||||||||||||||||||||||||||||||
| 525 | # | ||||||||||||||||||||||||||||||||||||||||||
| 526 | # Side effects: | ||||||||||||||||||||||||||||||||||||||||||
| 527 | # Pushes a warning onto $warnings if a country is found via GeoNames. | ||||||||||||||||||||||||||||||||||||||||||
| 528 | # | ||||||||||||||||||||||||||||||||||||||||||
| 529 | # Notes: | ||||||||||||||||||||||||||||||||||||||||||
| 530 | # We search on the full place string rather than just the component | ||||||||||||||||||||||||||||||||||||||||||
| 531 | # to give GeoNames maximum context and improve result accuracy. | ||||||||||||||||||||||||||||||||||||||||||
| 532 | # The first result is used. | ||||||||||||||||||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||||||||||||||||
| 534 | sub _geonames_lookup { | ||||||||||||||||||||||||||||||||||||||||||
| 535 | 22 | ▶ | my ($self, $place, $component, $warnings) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_geonames_lookup: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||
| 536 | |||||||||||||||||||||||||||||||||||||||||||
| 537 | my $result = $self->{geonames}->search( | ||||||||||||||||||||||||||||||||||||||||||
| 538 | 22 | q => $place, | |||||||||||||||||||||||||||||||||||||||||
| 539 | style => 'FULL', | ||||||||||||||||||||||||||||||||||||||||||
| 540 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 541 | |||||||||||||||||||||||||||||||||||||||||||
| 542 | # Normalise to the first element if an arrayref was returned. | ||||||||||||||||||||||||||||||||||||||||||
| 543 | # Guard against a bare scalar (e.g. an error string) being returned. | ||||||||||||||||||||||||||||||||||||||||||
| 544 | 21 | ▶ | if(ref($result) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 545 | 18 | $result = $result->[0]; | |||||||||||||||||||||||||||||||||||||||||
| 546 | } elsif(!ref($result)) { | ||||||||||||||||||||||||||||||||||||||||||
| 547 | 2 | return; # Bug fix: scalar return, not a hashref | |||||||||||||||||||||||||||||||||||||||||
| 548 | } | ||||||||||||||||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||||||||||||||||
| 550 | # Must be a plain hashref at this point | ||||||||||||||||||||||||||||||||||||||||||
| 551 | 19 | ▶ | return unless ref($result) eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 552 | |||||||||||||||||||||||||||||||||||||||||||
| 553 | # Extract country name; must be a defined, non-ref, non-empty string | ||||||||||||||||||||||||||||||||||||||||||
| 554 | 12 | my $country = $result->{countryName}; | |||||||||||||||||||||||||||||||||||||||||
| 555 | 12 | ▶ | return unless defined($country) # Bug fix: undef countryName | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
defined $country and not ref $country and length $country
defined $country and not ref $country
MC/DC100%3/3
defined $country and not ref $country and length $country
defined $country
not ref $country
length $country
Truth table100%4/4
A and not(B) and C
A= defined $countryB= not ref $countryC= length $country
| |||||||||||||||||||||||||||||||||||||||||||
| 556 | && !ref($country) # Bug fix: hashref countryName | ||||||||||||||||||||||||||||||||||||||||||
| 557 | && length($country); # Bug fix: empty-string countryName | ||||||||||||||||||||||||||||||||||||||||||
| 558 | |||||||||||||||||||||||||||||||||||||||||||
| 559 | 7 | push @{$warnings}, "$component: assuming country is $country"; | |||||||||||||||||||||||||||||||||||||||||
| 560 | 7 | return $country; | |||||||||||||||||||||||||||||||||||||||||
| 561 | } | ||||||||||||||||||||||||||||||||||||||||||
| 562 | |||||||||||||||||||||||||||||||||||||||||||
| 563 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 564 | |||||||||||||||||||||||||||||||||||||||||||
| 565 | Nigel Horne C<< <njh@nigelhorne.com> >> | ||||||||||||||||||||||||||||||||||||||||||
| 566 | |||||||||||||||||||||||||||||||||||||||||||
| 567 | =head1 REPOSITORY | ||||||||||||||||||||||||||||||||||||||||||
| 568 | |||||||||||||||||||||||||||||||||||||||||||
| 569 | L<https://github.com/nigelhorne/Geo-Address-Parser-Country> | ||||||||||||||||||||||||||||||||||||||||||
| 570 | |||||||||||||||||||||||||||||||||||||||||||
| 571 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||||||||||
| 572 | |||||||||||||||||||||||||||||||||||||||||||
| 573 | This module is provided as-is without any warranty. | ||||||||||||||||||||||||||||||||||||||||||
| 574 | |||||||||||||||||||||||||||||||||||||||||||
| 575 | Please report any bugs or feature requests to C<bug-geo-address-parser at rt.cpan.org>, | ||||||||||||||||||||||||||||||||||||||||||
| 576 | or through the web interface at | ||||||||||||||||||||||||||||||||||||||||||
| 577 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Geo-Address-Parser-Country>. | ||||||||||||||||||||||||||||||||||||||||||
| 578 | I will be notified, and then you'll | ||||||||||||||||||||||||||||||||||||||||||
| 579 | automatically be notified of progress on your bug as I make changes. | ||||||||||||||||||||||||||||||||||||||||||
| 580 | |||||||||||||||||||||||||||||||||||||||||||
| 581 | =head1 BUGS | ||||||||||||||||||||||||||||||||||||||||||
| 582 | |||||||||||||||||||||||||||||||||||||||||||
| 583 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 584 | |||||||||||||||||||||||||||||||||||||||||||
| 585 | =item * The direct lookup table contains C<nl> as an abbreviation for the | ||||||||||||||||||||||||||||||||||||||||||
| 586 | Netherlands. This conflicts with C<NL>, the ISO 3166-2 code for the Canadian | ||||||||||||||||||||||||||||||||||||||||||
| 587 | province of Newfoundland and Labrador. Because the direct table is consulted | ||||||||||||||||||||||||||||||||||||||||||
| 588 | before the C<Locale::CA> province-code path, passing C<component =E<gt> 'NL'> | ||||||||||||||||||||||||||||||||||||||||||
| 589 | currently resolves to C<Netherlands> rather than C<Canada>. The workaround | ||||||||||||||||||||||||||||||||||||||||||
| 590 | is to pass the full province name (C<Newfoundland and Labrador>) or to ensure | ||||||||||||||||||||||||||||||||||||||||||
| 591 | the place string includes an explicit C<Canada> suffix before calling | ||||||||||||||||||||||||||||||||||||||||||
| 592 | C<resolve()>. | ||||||||||||||||||||||||||||||||||||||||||
| 593 | |||||||||||||||||||||||||||||||||||||||||||
| 594 | =item * C<Geo::GeoNames> generates its query methods via C<AUTOLOAD>, so | ||||||||||||||||||||||||||||||||||||||||||
| 595 | C<can('search')> returns false at the Perl level even though | ||||||||||||||||||||||||||||||||||||||||||
| 596 | C<$geonames-E<gt>search(...)> works correctly at runtime. The constructor | ||||||||||||||||||||||||||||||||||||||||||
| 597 | schema currently validates the optional C<geonames> argument with | ||||||||||||||||||||||||||||||||||||||||||
| 598 | C<can =E<gt> 'search'>, which rejects a real C<Geo::GeoNames> object. | ||||||||||||||||||||||||||||||||||||||||||
| 599 | Until this is resolved, pass a wrapper object that defines C<search> as a | ||||||||||||||||||||||||||||||||||||||||||
| 600 | named method, or subclass C<Geo::GeoNames> and add a stub: | ||||||||||||||||||||||||||||||||||||||||||
| 601 | |||||||||||||||||||||||||||||||||||||||||||
| 602 | package My::GeoNames; | ||||||||||||||||||||||||||||||||||||||||||
| 603 | use parent 'Geo::GeoNames'; | ||||||||||||||||||||||||||||||||||||||||||
| 604 | sub search { my $self = shift; $self->SUPER::search(@_) } | ||||||||||||||||||||||||||||||||||||||||||
| 605 | |||||||||||||||||||||||||||||||||||||||||||
| 606 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 607 | |||||||||||||||||||||||||||||||||||||||||||
| 608 | Please report additional bugs via the GitHub issue tracker: | ||||||||||||||||||||||||||||||||||||||||||
| 609 | L<https://github.com/nigelhorne/Geo-Address-Parser-Country/issues> | ||||||||||||||||||||||||||||||||||||||||||
| 610 | |||||||||||||||||||||||||||||||||||||||||||
| 611 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 612 | |||||||||||||||||||||||||||||||||||||||||||
| 613 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 614 | |||||||||||||||||||||||||||||||||||||||||||
| 615 | =item * L<Geo::Address::Parser> | ||||||||||||||||||||||||||||||||||||||||||
| 616 | |||||||||||||||||||||||||||||||||||||||||||
| 617 | =item * L<Locale::US> | ||||||||||||||||||||||||||||||||||||||||||
| 618 | |||||||||||||||||||||||||||||||||||||||||||
| 619 | =item * L<Locale::CA> | ||||||||||||||||||||||||||||||||||||||||||
| 620 | |||||||||||||||||||||||||||||||||||||||||||
| 621 | =item * L<Locale::AU> | ||||||||||||||||||||||||||||||||||||||||||
| 622 | |||||||||||||||||||||||||||||||||||||||||||
| 623 | =item * L<Locale::Object::Country> | ||||||||||||||||||||||||||||||||||||||||||
| 624 | |||||||||||||||||||||||||||||||||||||||||||
| 625 | =item * L<Geo::GeoNames> | ||||||||||||||||||||||||||||||||||||||||||
| 626 | |||||||||||||||||||||||||||||||||||||||||||
| 627 | =item * L<Params::Get> | ||||||||||||||||||||||||||||||||||||||||||
| 628 | |||||||||||||||||||||||||||||||||||||||||||
| 629 | =item * L<Params::Validate::Strict> | ||||||||||||||||||||||||||||||||||||||||||
| 630 | |||||||||||||||||||||||||||||||||||||||||||
| 631 | =item * L<Return::Set> | ||||||||||||||||||||||||||||||||||||||||||
| 632 | |||||||||||||||||||||||||||||||||||||||||||
| 633 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 634 | |||||||||||||||||||||||||||||||||||||||||||
| 635 | =head1 LICENCE AND COPYRIGHT | ||||||||||||||||||||||||||||||||||||||||||
| 636 | |||||||||||||||||||||||||||||||||||||||||||
| 637 | Copyright 2026 Nigel Horne. | ||||||||||||||||||||||||||||||||||||||||||
| 638 | |||||||||||||||||||||||||||||||||||||||||||
| 639 | Usage is subject to licence terms. | ||||||||||||||||||||||||||||||||||||||||||
| 640 | |||||||||||||||||||||||||||||||||||||||||||
| 641 | The licence terms of this software are as follows: | ||||||||||||||||||||||||||||||||||||||||||
| 642 | |||||||||||||||||||||||||||||||||||||||||||
| 643 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 644 | |||||||||||||||||||||||||||||||||||||||||||
| 645 | =item * Personal single user, single computer use: GPL2 | ||||||||||||||||||||||||||||||||||||||||||
| 646 | |||||||||||||||||||||||||||||||||||||||||||
| 647 | =item * All other users (including Commercial, Charity, Educational, Government) | ||||||||||||||||||||||||||||||||||||||||||
| 648 | must apply in writing for a licence for use from Nigel Horne at the | ||||||||||||||||||||||||||||||||||||||||||
| 649 | above e-mail. | ||||||||||||||||||||||||||||||||||||||||||
| 650 | |||||||||||||||||||||||||||||||||||||||||||
| 651 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 652 | |||||||||||||||||||||||||||||||||||||||||||
| 653 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 654 | |||||||||||||||||||||||||||||||||||||||||||
| 655 | 6 | 1; | |||||||||||||||||||||||||||||||||||||||||