2 # Locale::Country - ISO codes for country identification (ISO 3166)
4 # $Id: Country.pm,v 2.6 2002/07/10 16:33:27 neilb Exp $
7 package Locale::Country;
13 use Locale::Constants;
16 #-----------------------------------------------------------------------
17 # Public Global Variables
18 #-----------------------------------------------------------------------
19 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
20 $VERSION = sprintf("%d.%02d", q$Revision: 2.61 $ =~ /(\d+)\.(\d+)/);
22 @EXPORT = qw(code2country country2code
23 all_country_codes all_country_names
25 LOCALE_CODE_ALPHA_2 LOCALE_CODE_ALPHA_3 LOCALE_CODE_NUMERIC);
27 #-----------------------------------------------------------------------
28 # Private Global Variables
29 #-----------------------------------------------------------------------
34 #=======================================================================
36 # code2country ( CODE [, CODESET ] )
38 #=======================================================================
42 my $codeset = @_ > 0 ? shift : LOCALE_CODE_DEFAULT;
45 return undef unless defined $code;
47 #-------------------------------------------------------------------
48 # Make sure the code is in the right form before we use it
49 # to look up the corresponding country.
50 # We have to sprintf because the codes are given as 3-digits,
51 # with leading 0's. Eg 052 for Barbados.
52 #-------------------------------------------------------------------
53 if ($codeset == LOCALE_CODE_NUMERIC)
55 return undef if ($code =~ /\D/);
56 $code = sprintf("%.3d", $code);
63 if (exists $CODES->[$codeset]->{$code})
65 return $CODES->[$codeset]->{$code};
69 #---------------------------------------------------------------
70 # no such country code!
71 #---------------------------------------------------------------
77 #=======================================================================
79 # country2code ( NAME [, CODESET ] )
81 #=======================================================================
85 my $codeset = @_ > 0 ? shift : LOCALE_CODE_DEFAULT;
88 return undef unless defined $country;
89 $country = lc($country);
90 if (exists $COUNTRIES->[$codeset]->{$country})
92 return $COUNTRIES->[$codeset]->{$country};
96 #---------------------------------------------------------------
98 #---------------------------------------------------------------
104 #=======================================================================
106 # country_code2code ( NAME [, CODESET ] )
108 #=======================================================================
109 sub country_code2code
111 (@_ == 3) or croak "country_code2code() takes 3 arguments!";
120 return undef if $inset == $outset;
121 $country = code2country($code, $inset);
122 return undef if not defined $country;
123 $outcode = country2code($country, $outset);
128 #=======================================================================
130 # all_country_codes ( [ CODESET ] )
132 #=======================================================================
133 sub all_country_codes
135 my $codeset = @_ > 0 ? shift : LOCALE_CODE_DEFAULT;
137 return keys %{ $CODES->[$codeset] };
141 #=======================================================================
143 # all_country_names ( [ CODESET ] )
145 #=======================================================================
146 sub all_country_names
148 my $codeset = @_ > 0 ? shift : LOCALE_CODE_DEFAULT;
150 return values %{ $CODES->[$codeset] };
154 #=======================================================================
156 # alias_code ( ALIAS => CODE [ , CODESET ] )
158 # Add an alias for an existing code. If the CODESET isn't specified,
159 # then we use the default (currently the alpha-2 codeset).
161 # Locale::Country::alias_code('uk' => 'gb');
163 #=======================================================================
168 my $codeset = @_ > 0 ? shift : LOCALE_CODE_DEFAULT;
173 if (not exists $CODES->[$codeset]->{$real})
175 carp "attempt to alias \"$alias\" to unknown country code \"$real\"\n";
178 $country = $CODES->[$codeset]->{$real};
179 $CODES->[$codeset]->{$alias} = $country;
180 $COUNTRIES->[$codeset]->{"\L$country"} = $alias;
185 # old name of function for backwards compatibility
186 *_alias_code = *alias_code;
189 #=======================================================================
193 # change the official name for a country, eg:
194 # gb => 'Great Britain'
195 # rather than the standard 'United Kingdom'. The original is retained
196 # as an alias, but the new name will be returned if you lookup the
199 #=======================================================================
203 my $new_name = shift;
204 my $codeset = @_ > 0 ? shift : _code2codeset($code);
209 if (not defined $codeset)
211 carp "rename_country(): unknown country code \"$code\"\n";
215 $country = $CODES->[$codeset]->{$code};
217 foreach my $cset (LOCALE_CODE_ALPHA_2,
221 if ($cset == $codeset)
227 $c = country_code2code($code, $codeset, $cset);
230 $CODES->[$cset]->{$c} = $new_name;
231 $COUNTRIES->[$cset]->{"\L$new_name"} = $c;
238 #=======================================================================
242 # given a country code in an unknown codeset, return the codeset
243 # it is from, or undef.
245 #=======================================================================
251 foreach my $codeset (LOCALE_CODE_ALPHA_2, LOCALE_CODE_ALPHA_3,
254 return $codeset if (exists $CODES->[$codeset]->{$code})
261 #=======================================================================
263 # initialisation code - stuff the DATA into the ALPHA2 hash
265 #=======================================================================
267 my ($alpha2, $alpha3, $numeric);
268 my ($country, @countries);
276 ($alpha2, $alpha3, $numeric, @countries) = split(/:/, $_);
278 $CODES->[LOCALE_CODE_ALPHA_2]->{$alpha2} = $countries[0];
279 foreach $country (@countries)
281 $COUNTRIES->[LOCALE_CODE_ALPHA_2]->{"\L$country"} = $alpha2;
286 $CODES->[LOCALE_CODE_ALPHA_3]->{$alpha3} = $countries[0];
287 foreach $country (@countries)
289 $COUNTRIES->[LOCALE_CODE_ALPHA_3]->{"\L$country"} = $alpha3;
295 $CODES->[LOCALE_CODE_NUMERIC]->{$numeric} = $countries[0];
296 foreach $country (@countries)
298 $COUNTRIES->[LOCALE_CODE_NUMERIC]->{"\L$country"} = $numeric;
311 ae:are:784:United Arab Emirates
312 af:afg:004:Afghanistan
313 ag:atg:028:Antigua and Barbuda
317 an:ant:530:Netherlands Antilles
319 aq:ata:010:Antarctica
321 as:asm:016:American Samoa
325 az:aze:031:Azerbaijan
326 ba:bih:070:Bosnia and Herzegovina
328 bd:bgd:050:Bangladesh
330 bf:bfa:854:Burkina Faso
336 bn:brn:096:Brunei Darussalam
341 bv:bvt:074:Bouvet Island
346 cc:cck:166:Cocos (Keeling) Islands
347 cf:caf:140:Central African Republic
348 cg:cog:178:Congo:Congo, Republic of the
349 ch:che:756:Switzerland
350 ci:civ:384:Cote D'Ivoire
351 ck:cok:184:Cook Islands
356 cr:cri:188:Costa Rica
358 cv:cpv:132:Cape Verde
359 cx:cxr:162:Christmas Island
361 cz:cze:203:Czech Republic
366 do:dom:214:Dominican Republic
371 eh:esh:732:Western Sahara
377 fk:flk:238:Falkland Islands (Malvinas):Falkland Islands (Islas Malvinas)
378 fm:fsm:583:Micronesia, Federated States of
379 fo:fro:234:Faroe Islands
381 fx:fxx:249:France, Metropolitan
383 gb:gbr:826:United Kingdom:Great Britain
386 gf:guf:254:French Guiana
392 gp:glp:312:Guadeloupe
393 gq:gnq:226:Equatorial Guinea
395 gs:sgs:239:South Georgia and the South Sandwich Islands
398 gw:gnb:624:Guinea-Bissau
401 hm:hmd:334:Heard Island and McDonald Islands
410 io:iot:086:British Indian Ocean Territory
412 ir:irn:364:Iran, Islamic Republic of:Iran
419 kg:kgz:417:Kyrgyzstan
423 kn:kna:659:Saint Kitts and Nevis
424 kp:prk:408:Korea, Democratic People's Republic of:Korea, North:North Korea
425 kr:kor:410:Korea, Republic of:Korea, South:South Korea
427 ky:cym:136:Cayman Islands
428 kz:kaz:398:Kazakhstan:Kazakstan
429 la:lao:418:Lao People's Democratic Republic
431 lc:lca:662:Saint Lucia
432 li:lie:438:Liechtenstein
437 lu:lux:442:Luxembourg
439 ly:lby:434:Libyan Arab Jamahiriya:Libya
442 md:mda:498:Moldova, Republic of:Moldova
443 mg:mdg:450:Madagascar
444 mh:mhl:584:Marshall Islands
445 mk:mkd:807:Macedonia, the Former Yugoslav Republic of:Macedonia, Former Yugoslav Republic of:Macedonia
449 mo:mac:446:Macao:Macau
450 mp:mnp:580:Northern Mariana Islands
451 mq:mtq:474:Martinique
452 mr:mrt:478:Mauritania
453 ms:msr:500:Montserrat
460 mz:moz:508:Mozambique
462 nc:ncl:540:New Caledonia
464 nf:nfk:574:Norfolk Island
467 nl:nld:528:Netherlands
472 nz:nzl:554:New Zealand
476 pf:pyf:258:French Polynesia
477 pg:png:598:Papua New Guinea
478 ph:phl:608:Philippines
481 pm:spm:666:Saint Pierre and Miquelon
482 pn:pcn:612:Pitcairn:Pitcairn Island
483 pr:pri:630:Puerto Rico
484 ps:pse:275:Palestinian Territory, Occupied
491 ru:rus:643:Russian Federation:Russia
493 sa:sau:682:Saudi Arabia
494 sb:slb:090:Solomon Islands
495 sc:syc:690:Seychelles
499 sh:shn:654:Saint Helena
501 sj:sjm:744:Svalbard and Jan Mayen:Jan Mayen:Svalbard
503 sl:sle:694:Sierra Leone
504 sm:smr:674:San Marino
508 st:stp:678:Sao Tome and Principe
509 sv:slv:222:El Salvador
510 sy:syr:760:Syrian Arab Republic:Syria
512 tc:tca:796:Turks and Caicos Islands
514 tf:atf:260:French Southern Territories
517 tj:tjk:762:Tajikistan
519 tm:tkm:795:Turkmenistan
522 tl:tls:626:East Timor
524 tt:tto:780:Trinidad and Tobago
526 tw:twn:158:Taiwan, Province of China:Taiwan
527 tz:tza:834:Tanzania, United Republic of:Tanzania
530 um:umi:581:United States Minor Outlying Islands
531 us:usa:840:United States:USA:United States of America
533 uz:uzb:860:Uzbekistan
534 va:vat:336:Holy See (Vatican City State):Holy See (Vatican City)
535 vc:vct:670:Saint Vincent and the Grenadines
537 vg:vgb:092:Virgin Islands, British:British Virgin Islands
538 vi:vir:850:Virgin Islands, U.S.
541 wf:wlf:876:Wallis and Futuna
545 yu:yug:891:Yugoslavia
546 za:zaf:710:South Africa
548 zr:zar:180:Zaire:Congo, The Democratic Republic of the:Congo, Democratic Republic of the