Re: [PATCH] for broken /proc/curproc/file on FreeBSD
[p5sagit/p5-mst-13.2.git] / lib / Locale / Country.pm
CommitLineData
6b14ceb7 1#
2# Locale::Country - ISO codes for country identification (ISO 3166)
3#
4# $Id: Country.pm,v 2.1 2002/02/06 04:07:09 neilb Exp $
5#
47a334e9 6
7package Locale::Country;
8use strict;
9require 5.002;
10
47a334e9 11require Exporter;
12use Carp;
13use Locale::Constants;
14
15
16#-----------------------------------------------------------------------
17# Public Global Variables
18#-----------------------------------------------------------------------
19use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
6b14ceb7 20$VERSION = sprintf("%d.%02d", q$Revision: 2.1 $ =~ /(\d+)\.(\d+)/);
47a334e9 21@ISA = qw(Exporter);
22@EXPORT = qw(code2country country2code
23 all_country_codes all_country_names
24 country_code2code
25 LOCALE_CODE_ALPHA_2 LOCALE_CODE_ALPHA_3 LOCALE_CODE_NUMERIC);
26
27#-----------------------------------------------------------------------
28# Private Global Variables
29#-----------------------------------------------------------------------
30my $CODES = [];
31my $COUNTRIES = [];
32
33
34#=======================================================================
6b14ceb7 35#
36# code2country ( CODE [, CODESET ] )
37#
47a334e9 38#=======================================================================
39sub code2country
40{
41 my $code = shift;
42 my $codeset = @_ > 0 ? shift : LOCALE_CODE_DEFAULT;
43
44
45 return undef unless defined $code;
46
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)
54 {
55 return undef if ($code =~ /\D/);
56 $code = sprintf("%.3d", $code);
57 }
58 else
59 {
60 $code = lc($code);
61 }
62
63 if (exists $CODES->[$codeset]->{$code})
64 {
65 return $CODES->[$codeset]->{$code};
66 }
67 else
68 {
69 #---------------------------------------------------------------
70 # no such country code!
71 #---------------------------------------------------------------
72 return undef;
73 }
74}
75
6b14ceb7 76
77#=======================================================================
78#
79# country2code ( NAME [, CODESET ] )
80#
81#=======================================================================
47a334e9 82sub country2code
83{
84 my $country = shift;
85 my $codeset = @_ > 0 ? shift : LOCALE_CODE_DEFAULT;
86
87
88 return undef unless defined $country;
89 $country = lc($country);
90 if (exists $COUNTRIES->[$codeset]->{$country})
91 {
92 return $COUNTRIES->[$codeset]->{$country};
93 }
94 else
95 {
96 #---------------------------------------------------------------
97 # no such country!
98 #---------------------------------------------------------------
99 return undef;
100 }
101}
102
6b14ceb7 103
104#=======================================================================
105#
106# country_code2code ( NAME [, CODESET ] )
107#
108#=======================================================================
47a334e9 109sub country_code2code
110{
111 (@_ == 3) or croak "country_code2code() takes 3 arguments!";
112
113 my $code = shift;
114 my $inset = shift;
115 my $outset = shift;
6b14ceb7 116 my $outcode;
47a334e9 117 my $country;
118
119
120 return undef if $inset == $outset;
121 $country = code2country($code, $inset);
122 return undef if not defined $country;
123 $outcode = country2code($country, $outset);
124 return $outcode;
125}
126
47a334e9 127
128#=======================================================================
6b14ceb7 129#
130# all_country_codes ( [ CODESET ] )
131#
132#=======================================================================
47a334e9 133sub all_country_codes
134{
135 my $codeset = @_ > 0 ? shift : LOCALE_CODE_DEFAULT;
136
137 return keys %{ $CODES->[$codeset] };
138}
139
6b14ceb7 140
141#=======================================================================
142#
143# all_country_names ( [ CODESET ] )
144#
145#=======================================================================
47a334e9 146sub all_country_names
147{
148 my $codeset = @_ > 0 ? shift : LOCALE_CODE_DEFAULT;
149
150 return values %{ $CODES->[$codeset] };
151}
152
47a334e9 153
6b14ceb7 154#=======================================================================
155#
156# _alias_code ( ALIAS => CODE [ , CODESET ] )
157#
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).
160#
161# Locale::Country::_alias_code('uk' => 'gb');
162#
163#=======================================================================
47a334e9 164sub _alias_code
165{
166 my $alias = shift;
167 my $real = shift;
168 my $codeset = @_ > 0 ? shift : LOCALE_CODE_DEFAULT;
169
170 my $country;
171
172
173 if (not exists $CODES->[$codeset]->{$real})
174 {
175 carp "attempt to alias \"$alias\" to unknown country code \"$real\"\n";
176 return undef;
177 }
178 $country = $CODES->[$codeset]->{$real};
179 $CODES->[$codeset]->{$alias} = $country;
180 $COUNTRIES->[$codeset]->{"\L$country"} = $alias;
181
182 return $alias;
183}
184
47a334e9 185
186#=======================================================================
6b14ceb7 187#
47a334e9 188# initialisation code - stuff the DATA into the ALPHA2 hash
6b14ceb7 189#
47a334e9 190#=======================================================================
191{
192 my ($alpha2, $alpha3, $numeric);
6b6e008c 193 my ($country, @countries);
47a334e9 194
195
196 while (<DATA>)
197 {
198 next unless /\S/;
199 chop;
6b6e008c 200 ($alpha2, $alpha3, $numeric, @countries) = split(/:/, $_);
47a334e9 201
6b6e008c 202 $CODES->[LOCALE_CODE_ALPHA_2]->{$alpha2} = $countries[0];
203 foreach $country (@countries)
204 {
205 $COUNTRIES->[LOCALE_CODE_ALPHA_2]->{"\L$country"} = $alpha2;
206 }
47a334e9 207
208 if ($alpha3)
209 {
6b6e008c 210 $CODES->[LOCALE_CODE_ALPHA_3]->{$alpha3} = $countries[0];
211 foreach $country (@countries)
212 {
213 $COUNTRIES->[LOCALE_CODE_ALPHA_3]->{"\L$country"} = $alpha3;
214 }
47a334e9 215 }
216
217 if ($numeric)
218 {
6b6e008c 219 $CODES->[LOCALE_CODE_NUMERIC]->{$numeric} = $countries[0];
220 foreach $country (@countries)
221 {
222 $COUNTRIES->[LOCALE_CODE_NUMERIC]->{"\L$country"} = $numeric;
223 }
47a334e9 224 }
225
226 }
227}
228
2291;
230
231__DATA__
232ad:and:020:Andorra
233ae:are:784:United Arab Emirates
234af:afg:004:Afghanistan
235ag:atg:028:Antigua and Barbuda
236ai:aia:660:Anguilla
237al:alb:008:Albania
238am:arm:051:Armenia
239an:ant:530:Netherlands Antilles
240ao:ago:024:Angola
241aq:::Antarctica
242ar:arg:032:Argentina
243as:asm:016:American Samoa
244at:aut:040:Austria
245au:aus:036:Australia
246aw:abw:533:Aruba
247az:aze:031:Azerbaijan
248ba:bih:070:Bosnia and Herzegovina
249bb:brb:052:Barbados
250bd:bgd:050:Bangladesh
251be:bel:056:Belgium
252bf:bfa:854:Burkina Faso
253bg:bgr:100:Bulgaria
254bh:bhr:048:Bahrain
255bi:bdi:108:Burundi
256bj:ben:204:Benin
257bm:bmu:060:Bermuda
258bn:brn:096:Brunei Darussalam
259bo:bol:068:Bolivia
260br:bra:076:Brazil
261bs:bhs:044:Bahamas
262bt:btn:064:Bhutan
263bv:::Bouvet Island
264bw:bwa:072:Botswana
265by:blr:112:Belarus
266bz:blz:084:Belize
267ca:can:124:Canada
268cc:::Cocos (Keeling) Islands
6b6e008c 269cd:cod:180:Congo, The Democratic Republic of the:Congo, Democratic Republic of the
47a334e9 270cf:caf:140:Central African Republic
271cg:cog:178:Congo
272ch:che:756:Switzerland
273ci:civ:384:Cote D'Ivoire
274ck:cok:184:Cook Islands
275cl:chl:152:Chile
276cm:cmr:120:Cameroon
277cn:chn:156:China
278co:col:170:Colombia
279cr:cri:188:Costa Rica
280cu:cub:192:Cuba
281cv:cpv:132:Cape Verde
282cx:::Christmas Island
283cy:cyp:196:Cyprus
284cz:cze:203:Czech Republic
285de:deu:276:Germany
286dj:dji:262:Djibouti
287dk:dnk:208:Denmark
288dm:dma:212:Dominica
289do:dom:214:Dominican Republic
290dz:dza:012:Algeria
291ec:ecu:218:Ecuador
292ee:est:233:Estonia
293eg:egy:818:Egypt
294eh:esh:732:Western Sahara
295er:eri:232:Eritrea
296es:esp:724:Spain
297et:eth:231:Ethiopia
298fi:fin:246:Finland
299fj:fji:242:Fiji
6b6e008c 300fk:flk:238:Falkland Islands (Malvinas):Falkland Islands (Islas Malvinas)
47a334e9 301fm:fsm:583:Micronesia, Federated States of
302fo:fro:234:Faroe Islands
303fr:fra:250:France
304fx:::France, Metropolitan
305ga:gab:266:Gabon
6b6e008c 306gb:gbr:826:United Kingdom:Great Britain
47a334e9 307gd:grd:308:Grenada
308ge:geo:268:Georgia
309gf:guf:254:French Guiana
310gh:gha:288:Ghana
311gi:gib:292:Gibraltar
312gl:grl:304:Greenland
313gm:gmb:270:Gambia
314gn:gin:324:Guinea
315gp:glp:312:Guadeloupe
316gq:gnq:226:Equatorial Guinea
317gr:grc:300:Greece
318gs:::South Georgia and the South Sandwich Islands
319gt:gtm:320:Guatemala
320gu:gum:316:Guam
321gw:gnb:624:Guinea-Bissau
322gy:guy:328:Guyana
323hk:hkg:344:Hong Kong
324hm:::Heard Island and McDonald Islands
325hn:hnd:340:Honduras
326hr:hrv:191:Croatia
327ht:hti:332:Haiti
328hu:hun:348:Hungary
329id:idn:360:Indonesia
330ie:irl:372:Ireland
331il:isr:376:Israel
332in:ind:356:India
333io:::British Indian Ocean Territory
334iq:irq:368:Iraq
6b6e008c 335ir:irn:364:Iran, Islamic Republic of:Iran
47a334e9 336is:isl:352:Iceland
337it:ita:380:Italy
338jm:jam:388:Jamaica
339jo:jor:400:Jordan
340jp:jpn:392:Japan
341ke:ken:404:Kenya
342kg:kgz:417:Kyrgyzstan
343kh:khm:116:Cambodia
344ki:kir:296:Kiribati
345km:com:174:Comoros
346kn:kna:659:Saint Kitts and Nevis
6b6e008c 347kp:prk:408:Korea, Democratic People's Republic of:Korea, North:North Korea
348kr:kor:410:Korea, Republic of:Korea, South:South Korea
47a334e9 349kw:kwt:414:Kuwait
350ky:cym:136:Cayman Islands
351kz:kaz:398:Kazakstan
352la:lao:418:Lao People's Democratic Republic
353lb:lbn:422:Lebanon
354lc:lca:662:Saint Lucia
355li:lie:438:Liechtenstein
356lk:lka:144:Sri Lanka
357lr:lbr:430:Liberia
358ls:lso:426:Lesotho
359lt:ltu:440:Lithuania
360lu:lux:442:Luxembourg
361lv:lva:428:Latvia
6b6e008c 362ly:lby:434:Libyan Arab Jamahiriya:Libya
47a334e9 363ma:mar:504:Morocco
364mc:mco:492:Monaco
365md:mda:498:Moldova, Republic of
366mg:mdg:450:Madagascar
367mh:mhl:584:Marshall Islands
6b6e008c 368mk:mkd:807:Macedonia, the Former Yugoslav Republic of:Macedonia, Former Yugoslav Republic of:Macedonia
47a334e9 369ml:mli:466:Mali
370mm:mmr:104:Myanmar
371mn:mng:496:Mongolia
372mo:mac:446:Macau
373mp:mnp:580:Northern Mariana Islands
374mq:mtq:474:Martinique
375mr:mrt:478:Mauritania
376ms:msr:500:Montserrat
377mt:mlt:470:Malta
378mu:mus:480:Mauritius
379mv:mdv:462:Maldives
380mw:mwi:454:Malawi
381mx:mex:484:Mexico
382my:mys:458:Malaysia
383mz:moz:508:Mozambique
384na:nam:516:Namibia
385nc:ncl:540:New Caledonia
386ne:ner:562:Niger
387nf:nfk:574:Norfolk Island
388ng:nga:566:Nigeria
389ni:nic:558:Nicaragua
390nl:nld:528:Netherlands
391no:nor:578:Norway
392np:npl:524:Nepal
393nr:nru:520:Nauru
394nu:niu:570:Niue
395nz:nzl:554:New Zealand
396om:omn:512:Oman
397pa:pan:591:Panama
398pe:per:604:Peru
399pf:pyf:258:French Polynesia
400pg:png:598:Papua New Guinea
401ph:phl:608:Philippines
402pk:pak:586:Pakistan
403pl:pol:616:Poland
404pm:spm:666:Saint Pierre and Miquelon
6b6e008c 405pn:pcn:612:Pitcairn:Pitcairn Island
47a334e9 406pr:pri:630:Puerto Rico
407ps:pse:275:Palestinian Territory, Occupied
408pt:prt:620:Portugal
409pw:plw:585:Palau
410py:pry:600:Paraguay
411qa:qat:634:Qatar
412re:reu:638:Reunion
413ro:rom:642:Romania
6b6e008c 414ru:rus:643:Russian Federation:Russia
47a334e9 415rw:rwa:646:Rwanda
416sa:sau:682:Saudi Arabia
417sb:slb:090:Solomon Islands
418sc:syc:690:Seychelles
419sd:sdn:736:Sudan
420se:swe:752:Sweden
421sg:sgp:702:Singapore
422sh:shn:654:Saint Helena
423si:svn:705:Slovenia
6b6e008c 424sj:sjm:744:Svalbard and Jan Mayen:Jan Mayen:Svalbard
47a334e9 425sk:svk:703:Slovakia
426sl:sle:694:Sierra Leone
427sm:smr:674:San Marino
428sn:sen:686:Senegal
429so:som:706:Somalia
430sr:sur:740:Suriname
431st:stp:678:Sao Tome and Principe
432sv:slv:222:El Salvador
6b6e008c 433sy:syr:760:Syrian Arab Republic:Syria
47a334e9 434sz:swz:748:Swaziland
435tc:tca:796:Turks and Caicos Islands
436td:tcd:148:Chad
437tf:::French Southern Territories
438tg:tgo:768:Togo
439th:tha:764:Thailand
440tj:tjk:762:Tajikistan
441tk:tkl:772:Tokelau
442tm:tkm:795:Turkmenistan
443tn:tun:788:Tunisia
444to:ton:776:Tonga
445tp:tmp:626:East Timor
446tr:tur:792:Turkey
447tt:tto:780:Trinidad and Tobago
448tv:tuv:798:Tuvalu
6b6e008c 449tw:twn:158:Taiwan, Province of China:Taiwan
450tz:tza:834:Tanzania, United Republic of:Tanzania
47a334e9 451ua:ukr:804:Ukraine
452ug:uga:800:Uganda
453um:::United States Minor Outlying Islands
6b6e008c 454us:usa:840:United States:USA:United States of America
47a334e9 455uy:ury:858:Uruguay
456uz:uzb:860:Uzbekistan
6b6e008c 457va:vat:336:Holy See (Vatican City State):Hole See (Vatican City)
47a334e9 458vc:vct:670:Saint Vincent and the Grenadines
459ve:ven:862:Venezuela
6b6e008c 460vg:vgb:092:Virgin Islands, British:British Virgin Islands
47a334e9 461vi:vir:850:Virgin Islands, U.S.
462vn:vnm:704:Vietnam
463vu:vut:548:Vanuatu
464wf:wlf:876:Wallis and Futuna
465ws:wsm:882:Samoa
466ye:yem:887:Yemen
467yt:::Mayotte
468yu:yug:891:Yugoslavia
469za:zaf:710:South Africa
470zm:zmb:894:Zambia
471zr:::Zaire
472zw:zwe:716:Zimbabwe