Geography-States

 view release on metacpan or  search on metacpan

lib/Geography/States.pm  view on Meta::CPAN

        $str =  uc $str;
    }
    else {
        $str =  join " " => map {ucfirst lc} split /\s+/ => $str;
        $str =~ s/\bOf\b/of/         if $country eq lc 'USA';
        $str =~ s/\bD([eo])\b/d$1/   if $country eq lc 'Brazil';
    }

    $str;
}

{
    my $data = init_data;

    while (my ($country, $country_data) = each %$data) {
        while (my ($code, $state_data) = each %$country_data) {
            my ($strict, $name) = @$state_data;
            my $info     = [$code, $name, !$strict];
            my $_code    = _norm ($code, $country);
            my $_name    = _norm ($name, $country);
            my $_country = lc $country;
            $states {$_country} -> {$_code} = $info;
            $states {$_country} -> {$_name} = $info;
        }
    }
}


sub new {
    die "Not enough arguments for Geography::States -> new ()\n" unless @_ > 1;

    my $proto   =  shift;
    my $class   =  ref $proto || $proto;

    my $country =  lc shift;
       $country =~ s/\s+/ /g;

    die "No such country $country\n" unless $states {$country};

    my $strict  =  shift;

    my $self;
    my ($cs, $info);
    while (($cs, $info) = each %{$states {$country}}) {
        next unless $cs eq $info -> [0];
        next if $strict && $info -> [2];
        my $inf = [@$info [0, 1]];
        foreach my $i (0 .. 1) {
            #
            # Hardcoded exception.
            #
            next if $country  eq 'canada' &&
                    $$inf [0] eq 'PQ'     &&
                    $i == 1;
            $self -> {cs} -> {$info -> [$i]} = $inf unless
                       exists $self -> {cs} -> {$info -> [$i]};
        }
    }
    $self -> {country} = $country;

    bless $self => $class;
}


sub state {
    my $self = shift;
    unless (@_) {
        my %h;
        return grep {!$h {$_} ++} values %{$self -> {cs}};
    }
    my $query  =  _norm shift, $self -> {country};
    my $answer =  $self -> {cs} -> {$query} or return;
    return @$answer if wantarray;
    $answer -> [$answer -> [0] eq $query ? 1 : 0];
}


1;
    
=pod

=head1 NAME

Geography::States - Map states and provinces to their codes, and vica versa.

=head1 SYNOPSIS

 use Geography::States;

 my $obj = Geography::States -> new (COUNTRY [, STRICT]);


=head1 EXAMPLES

 my $canada = Geography::States -> new ('Canada');

 my  $name          =  $canada -> state ('NF');      # Newfoundland.
 my  $code          =  $canada -> state ('Ontario'); # ON.
 my ($code, $name)  =  $canada -> state ('BC');      # BC, British Columbia.
 my  @all_states    =  $canada -> state;             # List code/name pairs.


=head1 DESCRIPTION

This module lets you map states and provinces to their codes, and codes 
to names of provinces and states.

The C<< Geography::States -> new () >> call takes 1 or 2 arguments. The
first, required, argument is the country we are interested in. Current
supported countries are I<USA>, I<Brazil>, I<Canada>, I<The Netherlands>,
and I<Australia>. If a second non-false argument is given, we use I<strict
mode>. In non-strict mode, we will map territories and alternative codes
as well, while we do not do that in strict mode. For example, if the
country is B<USA>, in non-strict mode, we will map B<GU> to B<Guam>,
while in strict mode, neither B<GU> and B<Guam> will be found.

=head2 The state() method

All queries are done by calling the C<state> method in the object. This method
takes an optional argument. If an argument is given, then in scalar context,
it will return the name of the state if a code of a state is given, and the

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 1.109 second using v1.00-cache-2.02-grep-82fe00e-cpan-b63e86051f13 )