4 Locale::Country - ISO codes for country identification (ISO 3166)
10 $country = code2country('jp'); # $country gets 'Japan'
11 $code = country2code('Norway'); # $code gets 'no'
13 @codes = all_country_codes();
14 @names = all_country_names();
16 # semi-private routines
17 Locale::Country::alias_code('uk' => 'gb');
18 Locale::Country::rename_country('gb' => 'Great Britain');
23 The C<Locale::Country> module provides access to the ISO
24 codes for identifying countries, as defined in ISO 3166-1.
25 You can either access the codes via the L<conversion routines>
26 (described below), or with the two functions which return lists
27 of all country codes or all country names.
29 There are three different code sets you can use for identifying
36 Two letter codes, such as 'tv' for Tuvalu.
37 This code set is identified with the symbol C<LOCALE_CODE_ALPHA_2>.
41 Three letter codes, such as 'brb' for Barbados.
42 This code set is identified with the symbol C<LOCALE_CODE_ALPHA_3>.
46 Numeric codes, such as 064 for Bhutan.
47 This code set is identified with the symbol C<LOCALE_CODE_NUMERIC>.
51 All of the routines take an optional additional argument
52 which specifies the code set to use.
53 If not specified, it defaults to the two-letter codes.
54 This is partly for backwards compatibility (previous versions
55 of this module only supported the alpha-2 codes), and
56 partly because they are the most widely used codes.
58 The alpha-2 and alpha-3 codes are not case-dependent,
59 so you can use 'BO', 'Bo', 'bO' or 'bo' for Bolivia.
60 When a code is returned by one of the functions in
61 this module, it will always be lower-case.
63 As of version 2.00, Locale::Country supports variant
64 names for countries. So, for example, the country code for "United States"
65 is "us", so country2code('United States') returns 'us'.
66 Now the following will also return 'us':
68 country2code('United States of America')
72 =head1 CONVERSION ROUTINES
74 There are three conversion routines: C<code2country()>, C<country2code()>,
75 and C<country_code2code()>.
79 =item code2country( CODE, [ CODESET ] )
81 This function takes a country code and returns a string
82 which contains the name of the country identified.
83 If the code is not a valid country code, as defined by ISO 3166,
84 then C<undef> will be returned:
86 $country = code2country('fi');
88 =item country2code( STRING, [ CODESET ] )
90 This function takes a country name and returns the corresponding
91 country code, if such exists.
92 If the argument could not be identified as a country name,
93 then C<undef> will be returned:
95 $code = country2code('Norway', LOCALE_CODE_ALPHA_3);
96 # $code will now be 'nor'
98 The case of the country name is not important.
99 See the section L<KNOWN BUGS AND LIMITATIONS> below.
101 =item country_code2code( CODE, CODESET, CODESET )
103 This function takes a country code from one code set,
104 and returns the corresponding code from another code set.
106 $alpha2 = country_code2code('fin',
107 LOCALE_CODE_ALPHA_3, LOCALE_CODE_ALPHA_2);
108 # $alpha2 will now be 'fi'
110 If the code passed is not a valid country code in
111 the first code set, or if there isn't a code for the
112 corresponding country in the second code set,
113 then C<undef> will be returned.
118 =head1 QUERY ROUTINES
120 There are two function which can be used to obtain a list of all codes,
121 or all country names:
125 =item C<all_country_codes( [ CODESET ] )>
127 Returns a list of all two-letter country codes.
128 The codes are guaranteed to be all lower-case,
129 and not in any particular order.
131 =item C<all_country_names( [ CODESET ] )>
133 Returns a list of all country names for which there is a corresponding
134 country code in the specified code set.
135 The names are capitalised, and not returned in any particular order.
137 Not all countries have alpha-3 and numeric codes -
138 some just have an alpha-2 code,
139 so you'll get a different number of countries
140 depending on which code set you specify.
145 =head1 SEMI-PRIVATE ROUTINES
147 Locale::Country provides two semi-private routines for modifying
149 Given their status, they aren't exported by default,
150 and so need to be called by prefixing the function name with the
155 Define a new code as an alias for an existing code:
157 Locale::Country::alias_code( ALIAS => CODE [, CODESET ] )
159 This feature was added as a mechanism for handling
160 a "uk" code. The ISO standard says that the two-letter code for
161 "United Kingdom" is "gb", whereas domain names are all .uk.
163 By default the module does not understand "uk", since it is implementing
164 an ISO standard. If you would like 'uk' to work as the two-letter
165 code for United Kingdom, use the following:
167 Locale::Country::alias_code('uk' => 'gb');
169 With this code, both "uk" and "gb" are valid codes for United Kingdom,
170 with the reverse lookup returning "uk" rather than the usual "gb".
172 B<Note:> this function was previously called _alias_code,
173 but the leading underscore has been dropped.
174 The old name will be supported for all 2.X releases for
175 backwards compatibility.
177 =head2 rename_country
179 If the official country name just isn't good enough for you,
180 you can rename a country. For example, the official country
181 name for code 'gb' is 'United Kingdom'.
182 If you want to change that, you might call:
184 Locale::Country::rename_country('gb' => 'Great Britain');
186 This means that calling code2country('gb') will now return
187 'Great Britain' instead of 'United Kingdom'.
188 The original country name is retained as an alias,
189 so for the above example, country2code('United Kingdom')
190 will still return 'gb'.
195 The following example illustrates use of the C<code2country()> function.
196 The user is prompted for a country code, and then told the corresponding
199 $| = 1; # turn off buffering
201 print "Enter country code: ";
202 chop($code = <STDIN>);
203 $country = code2country($code, LOCALE_CODE_ALPHA_2);
204 if (defined $country)
206 print "$code = $country\n";
210 print "'$code' is not a valid country code!\n";
215 Most top-level domain names are based on these codes,
216 but there are certain codes which aren't.
217 If you are using this module to identify country from hostname,
218 your best bet is to preprocess the country code.
220 For example, B<edu>, B<com>, B<gov> and friends would map to B<us>;
221 B<uk> would map to B<gb>. Any others?
223 =head1 KNOWN BUGS AND LIMITATIONS
229 When using C<country2code()>, the country name must currently appear
230 exactly as it does in the source of the module. The module now supports
231 a small number of variants.
233 Possible extensions to this are: an interface for getting at the
234 list of variant names, and regular expression matches.
238 In the current implementation, all data is read in when the
239 module is loaded, and then held in memory.
240 A lazy implementation would be more memory friendly.
244 Support for country names in different languages.
252 =item Locale::Language
254 ISO two letter codes for identification of language (ISO 639).
258 ISO codes for identification of scripts (ISO 15924).
260 =item Locale::Currency
262 ISO three letter codes for identification of currencies
263 and funds (ISO 4217).
265 =item Locale::SubCountry
267 ISO codes for country sub-divisions (states, counties, provinces, etc),
268 as defined in ISO 3166-2.
269 This module is not part of the Locale-Codes distribution,
270 but is available from CPAN in CPAN/modules/by-module/Locale/
274 The ISO standard which defines these codes.
276 =item http://www.iso.org/iso/en/prods-services/iso3166ma/index.html
278 Official home page for the ISO 3166 maintenance agency.
280 =item http://www.egt.ie/standards/iso3166/iso3166-1-en.html
282 Another useful, but not official, home page.
284 =item http://www.cia.gov/cia/publications/factbook/docs/app-d-1.html
286 An appendix in the CIA world fact book which lists country codes
287 as defined by ISO 3166, FIPS 10-4, and internet domain names.
294 Neil Bowers E<lt>neil@bowers.comE<gt>
298 Copyright (C) 2002-2004, Neil Bowers.
300 Copyright (c) 1997-2001 Canon Research Centre Europe (CRE).
302 This module is free software; you can redistribute it and/or
303 modify it under the same terms as Perl itself.