NAME
Locale::Country - standard codes for country identification
SYNOPSIS
use Locale::Country;
$country = code2country('jp' [,CODESET]); # $country gets 'Japan'
$code = country2code('Norway' [,CODESET]); # $code gets 'no'
@codes = all_country_codes( [CODESET]);
@names = all_country_names();
# semi-private routines
Locale::Country::alias_code('uk' => 'gb');
Locale::Country::rename_country('gb' => 'Great Britain');
DESCRIPTION
The Locale::Country
module provides access to several codes that can be used for identifying countries, such as those defined in ISO 3166-1.
You can either access the codes via the "conversion routines" (described below), or with the two functions which return lists of all country codes or all country names.
All of the routines take an optional additional argument which specifies the code set to use. If not specified, the default ISO 3166-1 two-letter codes will be used.
Unless otherwise specified, codes are not case sensitive. For example, when using the ISO 3166-1 two-character codes, you can use the codes 'br', 'BR', 'Br', or 'bR' to specify 'Brazil'. When a code is returned, the most common form (or the form specified by the standard) is returned. In this example, 'br' will be returned since all lowercase is the standard form of the ISO 3166-1 two-character codes.
COUNTRY NAMES
As of version 2.00, Locale::Country supports variant names for countries. So, for example, the country code for "United States" is "us", so country2code('United States') returns 'us'. Now the following will also return 'us':
country2code('United States of America')
country2code('USA')
Country names come from the standards, but common aliases are included (and others may be added). If you have a common alias for a country, let me know and I'll add it, with some restrictions.
For example, the country name "North Korea" never appeared in any of the official sources (instead, it was "Korea, North" or "Korea, Democratic People's Republic of". I would honor a request to add an alias "North Korea" since that's a very common way to specify the country (please don't request this... I've already added it).
On the other hand, a request to add Zaire as an alias for "Congo, The Democratic Republic of" may not be honored. The country's official name is not Zaire, so adding it as an alias violates the standard. Zaire was kept as an alias in versions prior to 3.00, but it has been removed. Other aliases (if any) which no longer appear in any standard have also been removed.
I may add support for changed country names in the future, but for now, it is not there.
SUPPORTED CODE SETS
There are many different code sets you can use for identifying countries. The ones currently supported are:
- alpha-2
-
This is the set of two-letter (lowercase) codes from ISO 3166-1, such as 'tv' for Tuvalu.
This code set is identified with the symbol
LOCALE_CODE_ALPHA_2
.If no code set is specified in a call to any of the routines described below, it will default to this code set.
- alpha-3
-
This is the set of three-letter (lowercase) codes from ISO 3166-1, such as 'brb' for Barbados. These codes are actually defined and maintained by the U.N. Statistics division.
This code set is identified with the symbol
LOCALE_CODE_ALPHA_3
. - numeric
-
This is the set of three-digit numeric codes from ISO 3166-1, such as 064 for Bhutan. These codes are actually defined and maintained by the U.N. Statistics division.
If a 2-digit code is entered, it is converted to 3 digits by prepending a 0.
This code set is identified with the symbol
LOCALE_CODE_NUMERIC
. - fips
-
The FIPS 10 data are two-letter (uppercase) codes assigned by the National Geospatial-Intelligence Agency.
This code set is identified with the symbol
LOCALE_CODE_FIPS
.
CONVERSION ROUTINES
There are three conversion routines: code2country()
, country2code()
, and country_code2code()
.
- code2country( CODE, [ CODESET ] )
-
This function takes a country code and returns a string which contains the name of the country identified. If the code is not a valid country code in the CODESET specified then
undef
will be returned:$country = code2country('fi');
The country returned is the name of the country as specified in the standard, and as a result, different variations of a country name may be returned for different values of CODESET. For example, the alpha-2 code set defines the two-letter code "bo" to be "Bolivia, Plurinational State of", whereas the alpha-3 code set defines the code 'bol' to be the country "Bolivia (Plurinational State of)". So:
code2country('bo',LOCALE_CODE_ALPHA_2) => 'Bolivia, Plurinational State of' code2country('bol',LOCALE_CODE_ALPHA_3) => 'Bolivia (Plurinational State of)'
- country2code( STRING, [ CODESET ] )
-
This function takes a country name and returns the corresponding country code, if such exists. If the argument could not be identified as a country name, then
undef
will be returned:country2code('Norway', LOCALE_CODE_ALPHA_3); => 'nor'
The country name is not case sensitive. Also, any known variation of a country name may be passed in. For example, even though the country name returned using LOCALE_CODE_ALPHA_2 and LOCALE_CODE_ALPHA_3 for Bolivia is different, either country name may be passed in since for each code set. So:
- country_code2code( CODE, CODESET, CODESET )
-
This function takes a country code from one code set, and returns the corresponding code from another code set.
Both CODESETs must be explicitly entered.
country_code2code('fin', LOCALE_CODE_ALPHA_3, LOCALE_CODE_ALPHA_2); => 'fi'
If the code passed is not a valid country code in the first code set, or if there isn't a code for the corresponding country in the second code set, then
undef
will be returned.
QUERY ROUTINES
There are two function which can be used to obtain a list of all codes, or all country names:
- all_country_codes( [ CODESET ] )
-
Returns a list of all code in the code set. The codes will be sorted.
- all_country_names( [ CODESET ] )
-
Returns a list of all country names for which there is a corresponding country code in the specified code set. The names are capitalized, and not returned in any particular order.
Not all countries have alpha-3 and numeric codes - some just have an alpha-2 code, so you'll get a different number of countries depending on which code set you specify.
SEMI-PRIVATE ROUTINES
Locale::Country provides two semi-private routines for modifying the internal data. Given their status, they aren't exported by default, and so need to be called by prefixing the function name with the package name.
- alias_code( ALIAS, CODE, [CODESET] )
-
Define a new code as an alias for an existing code:
Locale::Country::alias_code( ALIAS, CODE [, CODESET ] )
This feature was added as a mechanism for handling a "uk" code. The ISO standard says that the two-letter code for "United Kingdom" is "gb", whereas domain names are all .uk.
By default the module does not understand "uk", since it is implementing an ISO standard. If you would like 'uk' to work as the two-letter code for United Kingdom, use the following:
Locale::Country::alias_code('uk','gb');
With this code, both "uk" and "gb" are valid codes for United Kingdom, with the reverse lookup returning "uk" rather than the usual "gb".
Note: this function was previously called _alias_code, but the leading underscore has been dropped. The old name was supported for all 2.X releases, but has been dropped as of 3.00.
- rename_country( CODE, NEW_NAME, [CODESET] )
-
If the official country name just isn't good enough for you, you can rename a country. For example, the official country name for code 'gb' is 'United Kingdom'. If you want to change that, you might call:
Locale::Country::rename_country('gb', 'Great Britain');
This means that calling code2country('gb') will now return 'Great Britain' instead of 'United Kingdom'. The original country name is retained as an alias, so for the above example, country2code('United Kingdom') will still return 'gb'.
EXAMPLES
The following example illustrates use of the code2country()
function. The user is prompted for a country code, and then told the corresponding country name:
$| = 1; # turn off buffering
print "Enter country code: ";
chop($code = <STDIN>);
$country = code2country($code, LOCALE_CODE_ALPHA_2);
if (defined $country) {
print "$code = $country\n";
} else {
print "'$code' is not a valid country code!\n";
}
KNOWN BUGS AND LIMITATIONS
Because each code set uses a slightly different list of countries, and in some cases, the countries may be broken up and assigned multiple codes, there is not always a one-to-one correspondence between code sets.
For example, ISO 3166 assigns one code to the country "United States Minor Outlying Islands", but the FIPS 10 codes give different codes to different islands (Baker Island, Howland Island, etc.).
This may cause some confusion... I've done the best that I could do to minimize it.
Country names currently must be all ASCII. I plan on relaxing that limitation in the future.
SEE ALSO
- Locale::Codes
- Locale::Constants
- Locale::SubCountry
-
ISO codes for country sub-divisions (states, counties, provinces, etc), as defined in ISO 3166-2. This module is not part of the Locale-Codes distribution, but is available from CPAN in CPAN/modules/by-module/Locale/
- http://www.iso.org/iso/country_codes
-
Official home page for the ISO 3166 maintenance agency.
Unfortunately, they do not make the actual ISO available for free, so I cannot check the alpha-3 and numerical codes here.
- http://www.iso.org/iso/list-en1-semic-3.txt
-
The source of ISO 3166-1 two-letter codes used by this module.
- http://unstats.un.org/unsd/methods/m49/m49alpha.htm
-
The source of the official ISO 3166-1 three-letter codes and three-digit codes.
For some reason, this table is incomplete! Several countries are missing from it, and I cannot find them anywhere on the UN site. I get as much of the data from here as I can.
- http://earth-info.nga.mil/gns/html/digraphs.htm
-
The official list of the FIPS 10 codes.
- https://www.cia.gov/library/publications/the-world-factbook/appendix/print_appendix-d.html
-
Although not the official source of any of the data, the World Factbook maintained by the CIA is a great source of the data, especially since I can't get the official data from the ISO. Since it's maintained by the CIA, and since it's updated every two weeks, I use this as the source for some missing data.
- http://www.statoids.com/wab.html
-
Another unofficial source of data. Currently, it is not used to get data, but the notes and explanatory material were very useful for understanding discrepancies between the sources.
AUTHOR
See Locale::Codes for full author history.
Currently maintained by Sullivan Beck (sbeck@cpan.org).
COPYRIGHT
Copyright (c) 1997-2001 Canon Research Centre Europe (CRE).
Copyright (c) 2001-2010 Neil Bowers
Copyright (c) 2010-2010 Sullivan Beck
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.