The Perl Advent Calendar needs more articles for 2022. Submit your idea today!


Locale::Object::Country - country information objects


Locale::Object::Country allows you to create objects containing information about countries such as their ISO codes, currencies and so on.


    use Locale::Object::Country;
    my $country = Locale::Object::Country->new( code_alpha2 => 'af' );
    my $name         = $country->name;         # 'Afghanistan'
    my $code_alpha3  = $country->code_alpha3;  # 'afg'
    my $dialing_code = $country->dialing_code; # '93'
    my $currency     = $country->currency;
    my $continent    = $country->continent;

    my @languages    = $country->languages;
    my @official     = $country->languages_official;
    my $timezone     = $country->timezone;
    my @allzones     = @{$country->all_timezones};



    my $country = Locale::Object::Country->new( code => 'af' );

The new method creates an object. It takes a single-item hash as an argument - valid options to pass are ISO 3166 values - 'code_alpha2', 'code_alpha3', 'code_numeric' and 'name'.

The objects created are singletons; if you try and create a country object when one matching your specification already exists, new() will return the original one.

code_alpha2(), code_alpha3(), code_numeric(), name(), dialing_code()

    my $name = $country->name;

These methods retrieve the values of the attributes whose name they share in the object.

currency(), continent()

These methods return Locale::Object::Currency and Locale::Object::Continent objects respectively. Both of those have their own attribute methods, so you can do things like this:

    my $currency      = $country->currency;
    my $currency_name = $currency->name;

See the documentation for those two modules for a listing of currency and continent attributes.

languages(), languages_official()

    my @languages = $country->languages;

languages() returns an array of Locale::Object::Language objects in array context, or a reference in scalar context. The objects have their own attribute methods, so you can do things like this:

    foreach my $lang (@languages)
      print $lang->name, "\n";

languages_official() does much the same thing, but only gives languages that are official in that country. Note: you can also use the official() method of a Locale::Object::Language object on a country object; this will return a boolean value describing whether the language is official in that country.


    my $timezone = $country->timezone;

This method will return you a DateTime::TimeZone object corresponding with the time zone in the capital of the country your object represents. See the documentation for that module to see what methods it provides; as a simple example:

    my $timezone_name = $timezone->name;


    my @allzones     = @{$country->all_timezones};

This method will return an array or array reference, depending on context, of DateTime::TimeZone objects for all time zones that occur in the country your object represents. In most cases this will be only one, and in some cases it will be quite a few (for example, the US, Canada, and Russian Federation).


Originally by Earle Martin


Originally by Earle Martin. To the extent possible under law, the author has dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty. You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see <>.