| Locale-Geocode documentation | Contained in the Locale-Geocode distribution. |
Locale::Geocode::Territory
Locale::Geocode::Territory represents an individual country or territory as listed in ISO-3166-1. This class provides methods for returning information about the territory and any administrative divisions therein.
To be listed in ISO-3166-1, a country or territory must be listed in the United Nations Terminology Bulletin Country Names or Country and Region Codes for Statistical Use of the UN Statistics Division. In order for a country or territory to be listed in the Country Names bulletin, one of the following must be true of the territory:
- is a United Nations member state a member - is a member of any of the UN specialized agencies - a party to the Statute of the International Court of Justice
my $lct = new Locale::Geocode::Territory 'US';
# lookup a subdivision of US
my $lcd = $lct->lookup('TN');
# retrieve ISO-3166-2 information for US-TN
my $name = $lcd->name; # Tennessee
my $code = $lcd->code; # TN
# returns an array of Locale::Geocode::Division
# objects representing all divisions of US
my @divs = $lct->divisions;
returns an array of Locale::Geocode::Division objects representing all territorial divisions. this method honors the configured extensions.
the same as divisions, only all objects are sorted according to the specified metadata. if metadata is not specified (or is invalid), then all divisions are sorted by name. the supported metadata is any data-oriented method of Locale::Geocode::Division (name, code, fips, region, et alia).
Mike Eldridge <diz@cpan.org>
Kim Ryan
L<Locale::Geocode> L<Locale::Geocode::Division>
| Locale-Geocode documentation | Contained in the Locale-Geocode distribution. |
package Locale::Geocode::Territory; use warnings; use strict;
use overload '""' => sub { return shift->alpha2 }, '==' => sub { return shift->num == shift->num }, '!=' => sub { return shift->num != shift->num }; use Locale::Geocode::Division;
sub new { my $proto = shift; my $key = lc(shift); my $lg = shift || new Locale::Geocode; my $class = ref($proto) || $proto; my $self = {}; $self->{lg} = $lg; $self->{data} = Locale::Geocode::data()->{alpha2}->{$key} || Locale::Geocode::data()->{alpha3}->{$key} || Locale::Geocode::data()->{num}->{$key} || Locale::Geocode::data()->{name}->{$key}; return undef if not defined $self->{data}; return undef if not $lg->chkext($self->{data}); return bless $self, $class; }
sub lg { return shift->{lg} }
sub lookup { my $self = shift; my $key = shift; return new Locale::Geocode::Division $key, $self; }
sub lookup_by_index { my $self = shift; my $idx = shift; return new Locale::Geocode::Division $self->{data}->{division}->[$idx], $self; }
sub name { return shift->{data}->{name} }
sub num { return shift->{data}->{num} }
sub alpha2 { return shift->{data}->{alpha2} }
sub alpha3 { return shift->{data}->{alpha3} }
sub fips { return shift->{data}->{fips} }
sub has_notes { my $self = shift; my $data = $self->{data}; return $data->{note} && scalar @{ $data->{note} } > 0 ? 1 : 0 }
sub num_notes { my $self = shift; my $data = $self->{data}; return $data->{note} ? scalar @{ $data->{note} } : 0; }
sub notes { my $self = shift; my $data = $self->{data}; return $data->{note} ? @{ $data->{note} } : (); }
sub note { my $self = shift; my $idx = shift; my $data = $self->{data}; return $data->{note} ? $data->{note}->[$idx] : undef; }
sub divisions { my $self = shift; return map { $self->lookup($_->{code}) || () } @{ $self->{data}->{division} }; }
sub divisions_sorted { my $self = shift; my $meta = lc shift || 'name'; $meta = 'name' if not grep { $meta eq $_ } @Locale::Geocode::Division::meta; return sort { $a->$meta cmp $b->$meta } $self->divisions; }
sub num_divisions { my $self = shift; return scalar grep { $self->lg->chkext($_) } @{ $self->{data}->{division} }; }
1;