| 1 | package Number::Phone::Country; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 36 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 36 | ▶ | use Number::Phone::Country::Data; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | # *_codes are global so we can mock in some tests | ||||||||||||||||||||||||||||
| 7 | 36 | ▶ | use vars qw($VERSION %idd_codes %prefix_codes %number_translations); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 36 | $VERSION = '2.00'; | |||||||||||||||||||||||||||
| 9 | 36 | my $use_uk = 0; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 154 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 12 | 154 | shift; | |||||||||||||||||||||||||||
| 13 | 154 | foreach my $param (@_) { | |||||||||||||||||||||||||||
| 14 | 4 | ▶ | if(lc($param) eq 'uk') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 15 | 2 | $use_uk = 1; | |||||||||||||||||||||||||||
| 16 | } else { | ||||||||||||||||||||||||||||
| 17 | 2 | die("Unknown param to ".__PACKAGE__." '$param' at ".join(' line ', (caller())[1,2])."\n"); | |||||||||||||||||||||||||||
| 18 | } | ||||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | our %NANP_areas = ( | ||||||||||||||||||||||||||||
| 23 | CA => do { | ||||||||||||||||||||||||||||
| 24 | # see http://www.cnac.ca/co_codes/co_code_status.htm | ||||||||||||||||||||||||||||
| 25 | # checked on 2026-09-04 | ||||||||||||||||||||||||||||
| 26 | # next check due 2027-03-01 (semi-annually) | ||||||||||||||||||||||||||||
| 27 | 36 | my $canada = join('|', qw( | |||||||||||||||||||||||||||
| 28 | 204 226 236 249 250 257 263 273 289 | ||||||||||||||||||||||||||||
| 29 | 306 343 354 365 367 368 382 | ||||||||||||||||||||||||||||
| 30 | 403 416 418 428 431 437 438 450 468 474 | ||||||||||||||||||||||||||||
| 31 | 506 514 519 548 579 581 584 587 | ||||||||||||||||||||||||||||
| 32 | 604 613 639 647 672 683 | ||||||||||||||||||||||||||||
| 33 | 705 709 742 753 778 780 782 | ||||||||||||||||||||||||||||
| 34 | 807 819 825 867 873 879 | ||||||||||||||||||||||||||||
| 35 | 902 905 942 | ||||||||||||||||||||||||||||
| 36 | )); | ||||||||||||||||||||||||||||
| 37 | # handful of non-geographic country-specific codes ... | ||||||||||||||||||||||||||||
| 38 | # see https://en.wikipedia.org/wiki/Area_code_600 | ||||||||||||||||||||||||||||
| 39 | # checked on 2025-03-23 | ||||||||||||||||||||||||||||
| 40 | # next check due 2027-01-01 (every 2 years) | ||||||||||||||||||||||||||||
| 41 | 36 | $canada = join('|', $canada, 600, 622, 633, 644, 655, 677, 688); | |||||||||||||||||||||||||||
| 42 | }, | ||||||||||||||||||||||||||||
| 43 | 36 | US => do { | |||||||||||||||||||||||||||
| 44 | # see https://en.wikipedia.org/wiki/List_of_North_American_Numbering_Plan_area_codes#United_States | ||||||||||||||||||||||||||||
| 45 | # (or https://www.allareacodes.com/area_code_listings_by_state.htm | ||||||||||||||||||||||||||||
| 46 | # but the latter doesn't contain some overlays that are about to come into service) | ||||||||||||||||||||||||||||
| 47 | # NB for Hyder, Alaska, it shares three COs with Stewart, BC, and we can't tell which number is in which country, | ||||||||||||||||||||||||||||
| 48 | # so those prefixes aren't listed here | ||||||||||||||||||||||||||||
| 49 | # checked on 2026-09-04 | ||||||||||||||||||||||||||||
| 50 | # next check due 2027-03-01 (semi-annually) | ||||||||||||||||||||||||||||
| 51 | 36 | my $usa = join('|', qw( | |||||||||||||||||||||||||||
| 52 | 205 251 256 334 483 659 938 | ||||||||||||||||||||||||||||
| 53 | 907 | ||||||||||||||||||||||||||||
| 54 | 480 520 602 623 928 | ||||||||||||||||||||||||||||
| 55 | 327 479 501 870 | ||||||||||||||||||||||||||||
| 56 | 209 213 279 310 323 341 350 357 369 408 415 424 442 510 530 559 562 619 626 628 650 657 661 669 707 714 738 747 760 805 818 820 831 837 840 858 909 916 925 949 951 | ||||||||||||||||||||||||||||
| 57 | 303 719 720 748 970 983 | ||||||||||||||||||||||||||||
| 58 | 203 475 860 959 | ||||||||||||||||||||||||||||
| 59 | 302 | ||||||||||||||||||||||||||||
| 60 | 202 771 | ||||||||||||||||||||||||||||
| 61 | 239 305 321 324 352 386 407 448 561 645 656 689 727 728 754 772 786 813 850 863 904 941 954 | ||||||||||||||||||||||||||||
| 62 | 229 404 470 478 678 706 762 770 912 943 | ||||||||||||||||||||||||||||
| 63 | 808 | ||||||||||||||||||||||||||||
| 64 | 208 986 | ||||||||||||||||||||||||||||
| 65 | 217 224 309 312 331 447 464 618 630 708 730 773 779 815 847 861 872 | ||||||||||||||||||||||||||||
| 66 | 219 260 317 463 574 765 812 930 | ||||||||||||||||||||||||||||
| 67 | 319 515 563 641 712 | ||||||||||||||||||||||||||||
| 68 | 316 620 785 913 | ||||||||||||||||||||||||||||
| 69 | 270 364 502 606 859 | ||||||||||||||||||||||||||||
| 70 | 225 318 337 457 504 985 | ||||||||||||||||||||||||||||
| 71 | 207 | ||||||||||||||||||||||||||||
| 72 | 227 240 301 410 443 667 | ||||||||||||||||||||||||||||
| 73 | 339 351 413 508 617 774 781 857 978 | ||||||||||||||||||||||||||||
| 74 | 231 248 269 313 517 586 616 679 734 810 906 947 989 | ||||||||||||||||||||||||||||
| 75 | 218 320 507 612 651 763 924 952 | ||||||||||||||||||||||||||||
| 76 | 228 471 601 662 769 | ||||||||||||||||||||||||||||
| 77 | 235 314 417 557 573 636 660 816 975 | ||||||||||||||||||||||||||||
| 78 | 406 | ||||||||||||||||||||||||||||
| 79 | 308 402 531 | ||||||||||||||||||||||||||||
| 80 | 702 725 775 | ||||||||||||||||||||||||||||
| 81 | 603 | ||||||||||||||||||||||||||||
| 82 | 201 551 609 640 732 848 856 862 908 973 | ||||||||||||||||||||||||||||
| 83 | 505 575 | ||||||||||||||||||||||||||||
| 84 | 212 315 329 332 347 363 516 518 585 607 624 631 646 680 716 718 838 845 914 917 929 934 | ||||||||||||||||||||||||||||
| 85 | 252 336 472 704 743 828 910 919 980 984 | ||||||||||||||||||||||||||||
| 86 | 701 | ||||||||||||||||||||||||||||
| 87 | 216 220 234 283 326 330 380 419 436 440 513 567 614 740 937 | ||||||||||||||||||||||||||||
| 88 | 405 539 572 580 918 | ||||||||||||||||||||||||||||
| 89 | 458 503 541 971 | ||||||||||||||||||||||||||||
| 90 | 215 223 267 272 412 445 484 570 582 610 717 724 814 835 878 | ||||||||||||||||||||||||||||
| 91 | 401 | ||||||||||||||||||||||||||||
| 92 | 803 821 839 843 854 864 | ||||||||||||||||||||||||||||
| 93 | 605 | ||||||||||||||||||||||||||||
| 94 | 423 615 629 729 731 865 901 931 | ||||||||||||||||||||||||||||
| 95 | 210 214 254 281 325 346 361 409 430 432 469 512 621 682 713 726 737 806 817 830 832 903 915 936 940 945 956 972 979 | ||||||||||||||||||||||||||||
| 96 | 385 435 801 | ||||||||||||||||||||||||||||
| 97 | 802 | ||||||||||||||||||||||||||||
| 98 | 276 434 540 571 686 703 757 804 826 948 | ||||||||||||||||||||||||||||
| 99 | 206 253 360 425 509 564 | ||||||||||||||||||||||||||||
| 100 | 304 681 | ||||||||||||||||||||||||||||
| 101 | 262 274 353 414 534 608 715 920 | ||||||||||||||||||||||||||||
| 102 | 307 | ||||||||||||||||||||||||||||
| 103 | )); | ||||||||||||||||||||||||||||
| 104 | # handful of non-geographic country-specific codes ... | ||||||||||||||||||||||||||||
| 105 | # see https://en.wikipedia.org/wiki/Area_code_710 | ||||||||||||||||||||||||||||
| 106 | # checked on 2025-09-13 | ||||||||||||||||||||||||||||
| 107 | # next check due 2027-09-01 (every 2 years) | ||||||||||||||||||||||||||||
| 108 | 36 | $usa = join('|', $usa, 710); | |||||||||||||||||||||||||||
| 109 | }, | ||||||||||||||||||||||||||||
| 110 | # see https://en.wikipedia.org/wiki/North_American_Numbering_Plan#Countries_and_territories | ||||||||||||||||||||||||||||
| 111 | # checked on 2025-12-10 | ||||||||||||||||||||||||||||
| 112 | # next check due 2026-12-01 (annually) | ||||||||||||||||||||||||||||
| 113 | AS => '684', # American Samoa | ||||||||||||||||||||||||||||
| 114 | AI => '264', # Anguilla | ||||||||||||||||||||||||||||
| 115 | AG => '268', # Antigua and Barbude | ||||||||||||||||||||||||||||
| 116 | BS => '242', # Bahamas | ||||||||||||||||||||||||||||
| 117 | BB => '246', # Barbados | ||||||||||||||||||||||||||||
| 118 | BM => '441', # Bermuda | ||||||||||||||||||||||||||||
| 119 | VG => '284', # British Virgin Islands | ||||||||||||||||||||||||||||
| 120 | KY => '345', # Cayman Islands | ||||||||||||||||||||||||||||
| 121 | DM => '767', # Dominica | ||||||||||||||||||||||||||||
| 122 | DO => '809|829|849', # Dominican Republic | ||||||||||||||||||||||||||||
| 123 | GD => '473', # Grenada | ||||||||||||||||||||||||||||
| 124 | GU => '671', # Guam | ||||||||||||||||||||||||||||
| 125 | JM => '876|658', # Jamaica | ||||||||||||||||||||||||||||
| 126 | MS => '664', # Montserrat | ||||||||||||||||||||||||||||
| 127 | MP => '670', # Northern Mariana Islands | ||||||||||||||||||||||||||||
| 128 | PR => '787|939', # Puerto Rico | ||||||||||||||||||||||||||||
| 129 | KN => '869', # Saint Kitts and Nevis | ||||||||||||||||||||||||||||
| 130 | LC => '758', # Saint Lucia | ||||||||||||||||||||||||||||
| 131 | VC => '784', # Saint Vincent and the Grenadines | ||||||||||||||||||||||||||||
| 132 | SX => '721', # Sint Maarten | ||||||||||||||||||||||||||||
| 133 | TT => '868', # Trinidad and Tobago | ||||||||||||||||||||||||||||
| 134 | TC => '649', # Turks and Caicos Islands | ||||||||||||||||||||||||||||
| 135 | VI => '340', # US Virgin Islands | ||||||||||||||||||||||||||||
| 136 | ); | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | # private sub, returns list of NANP areas for the given ISO country code | ||||||||||||||||||||||||||||
| 139 | 0 | ▶ | sub _NANP_area_codes { | ||||||||||||||||||||||||||
|
Subroutine
_NANP_area_codes: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 140 | # uncoverable subroutine - only used in build scripts | ||||||||||||||||||||||||||||
| 141 | # uncoverable statement | ||||||||||||||||||||||||||||
| 142 | 0 | return split('\|', $NANP_areas{shift()}); | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | # private sub, returns list of NANP areas that *aren't* in the US or Canada | ||||||||||||||||||||||||||||
| 146 | 1 | ▶ | sub _non_US_CA_area_codes { | ||||||||||||||||||||||||||
|
Subroutine
_non_US_CA_area_codes: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 147 | # uncoverable subroutine - only used in build scripts | ||||||||||||||||||||||||||||
| 148 | return sort split('\|', join('|', | ||||||||||||||||||||||||||||
| 149 | 1 | @NANP_areas{grep { $_ !~ /^(US|CA)$/ } keys %NANP_areas} | |||||||||||||||||||||||||||
| 150 | )); | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 6666 | ▶ | sub phone2country { | ||||||||||||||||||||||||||
|
Subroutine
phone2country: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 155 | 6666 | return (phone2country_and_idd(@_))[0]; | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 6675 | ▶ | sub phone2country_and_idd { | ||||||||||||||||||||||||||
|
Subroutine
phone2country_and_idd: called CC=15 SCAR=27.1
Pod
documented
| |||||||||||||||||||||||||||||
| 159 | 6675 | my ($phone, $ignore_translations) = @_; | |||||||||||||||||||||||||||
| 160 | 6675 | $phone =~ s/[^\+?\d+]//g; | |||||||||||||||||||||||||||
| 161 | 6675 | ▶ | $phone = '+1'.$phone unless(substr($phone, 0, 1) =~ /[1+]/); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | 6675 | $phone =~ s/\D//g; | |||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | # deal with NANP insanity | ||||||||||||||||||||||||||||
| 165 | 6675 | ▶ | if($phone =~ m!^1(\d{3})\d{7}$!) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | 2403 | my $area = $1; | |||||||||||||||||||||||||||
| 167 | 2403 | foreach my $country (keys %NANP_areas) { | |||||||||||||||||||||||||||
| 168 | 40326 | ▶ | if($area =~ /^($NANP_areas{$country})$/x) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | 1686 | return ($country, 1); | |||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | 717 | return ('NANP', 1); | |||||||||||||||||||||||||||
| 173 | } else { | ||||||||||||||||||||||||||||
| 174 | 4272 | my @prefixes = map { substr($phone, 0, $_) } reverse 1..length($phone); | |||||||||||||||||||||||||||
| 175 | 4272 | foreach my $idd (@prefixes) { | |||||||||||||||||||||||||||
| 176 | 36728 | ▶ | if(exists $idd_codes{$idd}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 177 | next if( | ||||||||||||||||||||||||||||
| 178 | $ignore_translations && | ||||||||||||||||||||||||||||
| 179 | 4285 | ▶ | exists($number_translations{$idd}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$ignore_translations and exists $Number::Phone::Country::number_translations{$idd}
MC/DC100%2/2
$ignore_translations and exists $Number::Phone::Country::number_translations{$idd}
$ignore_translations
exists $Number::Phone::Country::number_translations{$idd}
Truth table100%3/3
A and B
A= $ignore_translationsB= exists $Number::Phone::Country::number_translations{$idd}
| |||||||||||||||||||||||||||||
| 180 | ); | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 4263 | my $country = $idd_codes{$idd}; | |||||||||||||||||||||||||||
| 183 | 4263 | ▶ | if(ref($country) eq 'ARRAY'){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | 800 | foreach my $country_code (@$country) { | |||||||||||||||||||||||||||
| 185 | 1077 | my $class = "Number\::Phone\::StubCountry\::" . $country_code; | |||||||||||||||||||||||||||
| 186 | 1077 | eval "require $class"; | |||||||||||||||||||||||||||
| 187 | 1077 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 188 | 16 | my $error = $@; | |||||||||||||||||||||||||||
| 189 | } elsif($class->new('+' . $phone)) { | ||||||||||||||||||||||||||||
| 190 | return ( | ||||||||||||||||||||||||||||
| 191 | 714 | ▶ | (($country_code eq 'GB' && $use_uk) ? 'UK' : $country_code), | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$country_code eq "GB" and $use_uk
MC/DC100%2/2
$country_code eq "GB" and $use_uk
$country_code eq "GB"
$use_uk
Truth table100%3/3
A and B
A= $country_code eq "GB"B= $use_uk
| |||||||||||||||||||||||||||||
| 192 | $idd | ||||||||||||||||||||||||||||
| 193 | ); | ||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||
| 196 | 86 | $country = @$country[0]; | |||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | 3549 | $country =~ s/.*:://; | |||||||||||||||||||||||||||
| 199 | 3549 | return ($country, $idd); | |||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | 9 | return; | |||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | 4195 | ▶ | sub country_code { | ||||||||||||||||||||||||||
|
Subroutine
country_code: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 207 | 4195 | my $country = shift; | |||||||||||||||||||||||||||
| 208 | 4195 | ▶ | $country = uc($country) if($country =~ /^[a-z]{2}$/i); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | 4195 | ▶ | my $data = $prefix_codes{$country} or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | 4194 | return $$data[0]; | |||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | 278 | ▶ | sub idd_code { | ||||||||||||||||||||||||||
|
Subroutine
idd_code: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 216 | 278 | my $country = uc shift; | |||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | 278 | ▶ | my $data = $prefix_codes{$country} or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | 277 | return $$data[1]; | |||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | 239 | ▶ | sub idd_regex { | ||||||||||||||||||||||||||
|
Subroutine
idd_regex: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 224 | 239 | my $country = uc shift; | |||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | 239 | ▶ | my $data = $prefix_codes{$country} or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | 239 | return $$data[3]; | |||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | 240 | ▶ | sub ndd_code { | ||||||||||||||||||||||||||
|
Subroutine
ndd_code: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 232 | 240 | my $country = uc shift; | |||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | 240 | ▶ | my $data = $prefix_codes{$country} or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | 239 | return $$data[2]; | |||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | # Translate numbers that exist in one country's number plan but end up in | ||||||||||||||||||||||||||||
| 240 | # another country, e.g. +353 48 for Northern Ireland, +39 0549 for SM) | ||||||||||||||||||||||||||||
| 241 | # to their canonical form for the target country (e.g. +35348 -> +4428) | ||||||||||||||||||||||||||||
| 242 | 41 | ▶ | sub canonicalize_number { | ||||||||||||||||||||||||||
|
Subroutine
canonicalize_number: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 243 | 41 | my $number = shift; | |||||||||||||||||||||||||||
| 244 | 41 | my ($digits) = $number =~ /^\+(\d+)/; | |||||||||||||||||||||||||||
| 245 | 41 | ▶ | return $number unless defined $digits; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 246 | 41 | foreach my $translated_prefix (sort { length($b) <=> length($a) } keys %number_translations) { | |||||||||||||||||||||||||||
| 247 | 76 | ▶ | if(substr($digits, 0, length($translated_prefix)) eq $translated_prefix) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 248 | 19 | (my $translated = $number) =~ s/^\+$translated_prefix/+$number_translations{$translated_prefix}/; | |||||||||||||||||||||||||||
| 249 | 19 | return $translated; | |||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||
| 252 | 22 | return $number; | |||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | 36 | 1; | |||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | =head1 NAME | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | Number::Phone::Country - Lookup country of phone number | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | use Number::Phone::Country; | ||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | #returns 'CA', '1' for Canada | ||||||||||||||||||||||||||||
| 266 | my ($iso_country_code, $idd) = Number::Phone::Country::phone2country_and_idd("1 (604) 111-1111"); | ||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | or | ||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | use Number::Phone::Country qw(uk); | ||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | my ($iso_country_code, $idd) = Number::Phone::Country::phone2country_and_idd(...); | ||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | or | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | #returns '49' | ||||||||||||||||||||||||||||
| 277 | my $idd = Number::Phone::Country::country_code("DE"); | ||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | =head1 INCOMPATIBLE CHANGES | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | =head2 from version 2.00 onwards | ||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | As of version 2.00 the C<phone2country> function is no longer exported. It has | ||||||||||||||||||||||||||||
| 284 | been deprecated since version 0.5, which was released in 2004. Use of the | ||||||||||||||||||||||||||||
| 285 | C<noexport> flag will now result in warnings. The first release after August | ||||||||||||||||||||||||||||
| 286 | 2026 will upgrade those to be fatal errors. At the same time warnings about | ||||||||||||||||||||||||||||
| 287 | any other unknown params will also be upgraded to be fatal. | ||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | This module looks up up the country based on a telephone number. | ||||||||||||||||||||||||||||
| 292 | It uses the International Direct Dialing (IDD) prefix, and | ||||||||||||||||||||||||||||
| 293 | lookups North American numbers using the Area Code, in accordance | ||||||||||||||||||||||||||||
| 294 | with the North America Numbering Plan (NANP). It can also, given a | ||||||||||||||||||||||||||||
| 295 | country, tell you the country code, and the prefixes you need to dial | ||||||||||||||||||||||||||||
| 296 | when in that country to call outside your local area or to call another | ||||||||||||||||||||||||||||
| 297 | country. | ||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | Be aware that the ISO code for the United Kingdom is GB, not UK. If | ||||||||||||||||||||||||||||
| 300 | you would prefer UK, pass the C<uk> flag as demonstrated in the L</SYNOPSIS>. | ||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | I have put in number ranges for Kosovo, which does not yet have an ISO country | ||||||||||||||||||||||||||||
| 303 | code. I have used XK, as that is the de facto standard as used by numerous | ||||||||||||||||||||||||||||
| 304 | international bodies such as the European Commission and the IMF. I previously | ||||||||||||||||||||||||||||
| 305 | used KOS, as used by the UN Development Programme. This may change again in | ||||||||||||||||||||||||||||
| 306 | the future. | ||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | The following functions are available: | ||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | =over 4 | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | =item country_code($country) | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | Returns the international dialing prefix for this country - eg, for the UK | ||||||||||||||||||||||||||||
| 317 | it returns 44, and for Canada it returns 1. | ||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||
| 319 | =item idd_code($country) | ||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | Returns one, of possibly multiple, International Direct Dialing prefixes for | ||||||||||||||||||||||||||||
| 322 | the given ISO Alpha-2 country code. Returns nothing if the country code is not | ||||||||||||||||||||||||||||
| 323 | recognised or not supported. | ||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | The IDD prefix is needed to make a call B<from a country> to another country. | ||||||||||||||||||||||||||||
| 326 | For example, when calling the UK from the US, the caller must first dial the | ||||||||||||||||||||||||||||
| 327 | IDD prefix 011 to setup an international call, followed by the country calling | ||||||||||||||||||||||||||||
| 328 | code for the UK (44), followed by the UK national number. | ||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | Many telephone systems allow the caller to dial a plus sign prefix | ||||||||||||||||||||||||||||
| 331 | (+) in place of the IDD, in which case the system replaces the plus sign with | ||||||||||||||||||||||||||||
| 332 | the correct IDD prefix for the caller's country. | ||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | Some countries have more than one IDD code, allowing the caller to route their | ||||||||||||||||||||||||||||
| 335 | calls through specific networks. C<idd_code> only returns one code. See | ||||||||||||||||||||||||||||
| 336 | C<idd_regex> if you need to match a number against known IDD prefixes for a | ||||||||||||||||||||||||||||
| 337 | given country. | ||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | =item idd_regex($country) | ||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | Returns a regular expression that matches against known International Direct | ||||||||||||||||||||||||||||
| 342 | Dialing prefixes for the given ISO Alpha-2 country code. Returns nothing if | ||||||||||||||||||||||||||||
| 343 | the country code is not recognised or not supported. | ||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||
| 345 | As an example, the regular expression for Australia (AU) will match a number of | ||||||||||||||||||||||||||||
| 346 | IDD prefixes, including 0011, 0014, and 0015, making it possible to determine | ||||||||||||||||||||||||||||
| 347 | that 001516502530000 could be a US number being dialled from Australia. | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | Be aware that some numbers will match against IDD prefixes from multiple | ||||||||||||||||||||||||||||
| 350 | countries. Other numbers will be valid national numbers for one country, and | ||||||||||||||||||||||||||||
| 351 | valid international numbers when called for other countries. For example, | ||||||||||||||||||||||||||||
| 352 | C<01143662111> is a valid national number for Sheffield, England. It could | ||||||||||||||||||||||||||||
| 353 | also be a valid number in Austria: | ||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||
| 355 | Calling from within the UK: | ||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | Area code: 0114 | ||||||||||||||||||||||||||||
| 358 | Subscriber: 366 2111 | ||||||||||||||||||||||||||||
| 359 | National: 0114 366 2111 | ||||||||||||||||||||||||||||
| 360 | International: +441143662111 | ||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | Calling from a country that uses the NANP (North American Numbering Plan): | ||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||
| 364 | IDD: 011 | ||||||||||||||||||||||||||||
| 365 | Country code: 43 (Austria) | ||||||||||||||||||||||||||||
| 366 | Area code: 0662 (Salzburg) | ||||||||||||||||||||||||||||
| 367 | Subscriber: 111 | ||||||||||||||||||||||||||||
| 368 | National: 0662 111 | ||||||||||||||||||||||||||||
| 369 | International: +43662111 | ||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | =item ndd_code($country) | ||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||
| 373 | Returns the National Direct Dialing prefix for the given country. This is | ||||||||||||||||||||||||||||
| 374 | the prefix used to make a call B<within a country> from one city to | ||||||||||||||||||||||||||||
| 375 | another. This prefix may not be necessary when calling another city in the | ||||||||||||||||||||||||||||
| 376 | same vicinity. This is followed by the city or area code for the place you | ||||||||||||||||||||||||||||
| 377 | are calling. For example, in the US, the NDD prefix is "1", so you must | ||||||||||||||||||||||||||||
| 378 | dial 1 before the area code to place a long distance call within the | ||||||||||||||||||||||||||||
| 379 | country. | ||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | =item phone2country | ||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | Wrapper around phone2country_and_idd, that takes the same | ||||||||||||||||||||||||||||
| 384 | arguments. | ||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||
| 386 | B<DEPRECATED> Returns the ISO country code (or XK for Kosovo) for a phone number. | ||||||||||||||||||||||||||||
| 387 | eg, for +441234567890 it returns 'GB' (or 'UK' if you've told it to). | ||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | =item phone2country_and_idd($phone, $ignore_translations) | ||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||
| 391 | Returns a list containing the ISO country code and IDD prefix for the given | ||||||||||||||||||||||||||||
| 392 | phone number. eg for +441234567890 it returns ('GB', 44). | ||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||
| 394 | By default, if a number is translated from one country to another - | ||||||||||||||||||||||||||||
| 395 | for example +353 48 xxx numbers in the Irish numbering plan are | ||||||||||||||||||||||||||||
| 396 | really just a mapping to +44 28 xxx numbers in the UK plan - then the | ||||||||||||||||||||||||||||
| 397 | data for the translated number is returned. If you want the country | ||||||||||||||||||||||||||||
| 398 | according to the ITU country code then set C<$ignore_translations> to | ||||||||||||||||||||||||||||
| 399 | a true value. | ||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | =item canonicalize_number($phone) | ||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||
| 403 | Translates a phone number that exists in one country's number plan but ends up | ||||||||||||||||||||||||||||
| 404 | in another country to its canonical E.164 form. Some numbers are reachable via | ||||||||||||||||||||||||||||
| 405 | another country's dial plan, for example Northern Ireland (+44 28 ...) can be | ||||||||||||||||||||||||||||
| 406 | dialled as +353 48 ... in the republic of Ireland's number plan, and the entire | ||||||||||||||||||||||||||||
| 407 | country of San Marino (+378) is also the Italian area code +39 0549. | ||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||
| 409 | This function translates such numbers to their canonical form, ie +353 48 ... | ||||||||||||||||||||||||||||
| 410 | is translated to +44 28 ... | ||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||
| 412 | Numbers that have no such translation are returned unchanged. | ||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | =back | ||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | L<Parse::PhoneNumber> | ||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||
| 420 | =head1 BUGS | ||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||
| 422 | It has not been possible to maintain complete backwards compatibility with | ||||||||||||||||||||||||||||
| 423 | the original 0.01 release. To fix a | ||||||||||||||||||||||||||||
| 424 | bug, while still retaining the ability to look up plain un-adorned NANP | ||||||||||||||||||||||||||||
| 425 | numbers without the +1 prefix, all non-NANP numbers *must* have their | ||||||||||||||||||||||||||||
| 426 | leading + sign. | ||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||
| 428 | Another incompatibility - it was previously assumed that any number not | ||||||||||||||||||||||||||||
| 429 | assigned to some other country was in the US. This was incorrect for (eg) | ||||||||||||||||||||||||||||
| 430 | 800 numbers. These are now identified as being generic NANP numbers. | ||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||
| 432 | Will go out of date every time the NANP has one of its code splits/overlays. | ||||||||||||||||||||||||||||
| 433 | So that's about once a month then. I'll do my best to keep it up to date. | ||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||
| 435 | =head1 WARNING | ||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||
| 437 | The Yugoslavs keep changing their minds about what country they want to be | ||||||||||||||||||||||||||||
| 438 | and what their ISO 3166 code and IDD prefix should be. YU? CS? RS? ME? | ||||||||||||||||||||||||||||
| 439 | God knows. And then there's Kosovo ... | ||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||
| 441 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||
| 443 | now maintained by David Cantrell E<lt>david@cantrell.org.ukE<gt> | ||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||
| 445 | originally by TJ Mather, E<lt>tjmather@maxmind.comE<gt> | ||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||
| 447 | country/IDD/NDD contributions by Michael Schout, E<lt>mschout@gkg.netE<gt> | ||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||
| 449 | Thanks to Shraga Bor-Sood for the updates in version 1.4. | ||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||
| 451 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||
| 453 | Copyright 2003 by MaxMind LLC | ||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||
| 455 | Copyright 2004 - 2026 David Cantrell | ||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||
| 457 | This library is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 458 | it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||
| 460 | =cut | ||||||||||||||||||||||||||||