Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Validate Country by ISO 3166 data

by rob_au (Abbot)
on Mar 07, 2002 at 01:35 UTC ( #149904=note: print w/replies, xml ) Need Help??


in reply to Validate Country by ISO 3166 data

Neat package, but unfortunately this has already been done and found its was into 5.7.3 as Locale::Country - This module provides methods for conversion between country name, two and three letter and numeric ISO-3166 country codes.

In addition to this, there is Locale::Language (ISO-639 two letter codes for identification of language), Locale::Script (ISO-15924 codes for identification of scripts) and Locale::Currency (ISO-4217 three letter codes for identification of currency and funds).

 

perl -e 's&&rob@cowsnet.com.au&&&split/[@.]/&&s&.com.&_&&&print'

Replies are listed 'Best First'.
Re: (2): New Validate ISO 3166
by shotgunefx (Parson) on Mar 07, 2002 at 21:13 UTC
    Thanks for pointing out the now standard Locale::Country. I've modified it to get the data from it.
    package Locale::Country::TieHash; use 5.002; use strict; use warnings; use Tie::Hash; use Carp; use Locale::Country; our $VERSION = '0.01'; our @ISA = qw(Tie::StdHash); my %CountryHash = (); sub TIEHASH { my $class = shift; my $self = {}; my @countries = all_country_codes(); foreach my $country (@countries){ $CountryHash{$country} = "\U$country ".code2country($count +ry); $CountryHash{lc(code2country($country))} = "\U$country ".c +ode2country($country); } $self->{DAT} = \%CountryHash; $self->{keys} = \@countries; $self->{key_index} = 0; return bless $self, $class; } sub STORE { my ($self, $key, $value) = @_; croak __PACKAGE__,":Cannot set value for $key! READ-ONLY"; } sub DELETE { my ($self, $key, $value) = @_; croak __PACKAGE__,":Cannot delete value for $key! READ-ONLY"; } sub FETCH { my ($self, $key) = @_; $key=~/^(\w\w)\s/; my $ikey = lc($1 || $key); return $self->{DAT}{lc $ikey}; } sub EXISTS { my ($self, $key) = @_; $key=~/^(\w\w)\s/; my $ikey = lc($1 || $key); return exists $self->{DAT}{lc $ikey}; } sub DEFINED { my ($self, $key) = @_; $key=~/^(\w\w)\s/; my $ikey = lc($1 || $key); return defined $self->{DAT}{lc $ikey}; } sub FIRSTKEY { my $self = shift; $self->{key_index} = 0; return $self->{DAT}{ $self->{keys}->[$self->{key_index}++] }; } sub NEXTKEY { my $self = shift; return undef unless defined($self->{keys}[ $self->{key_index}]); return $self->{DAT}{$self->{keys}[ $self->{key_index}++] }; } sub CLEAR { croak __PACKAGE__,":Cannot clear Country Hash! READ-ONLY"; } 1; __END__ # Below is stub documentation for your module. You better edit it! =head1 NAME WWE::Country::ISO - Perl extension for Validating Country codes via ti +ed-hash =head1 SYNOPSIS use Locale::Country::TieHash; tie %Countries, 'Locale::Country::TieHash'; =head1 DESCRIPTION use Locale::Country::TieHash; tie %Countries, 'Locale::Country::TieHash' or die "Couldn't tie"; my $country = "United StAtes"; die "Invalid $country" unless exists($Countries{$country}); print $Countries{'Us'},"\n"; # Prints US United + States print $Countries{'Us UnitEd StateS'},"\n"; # Prints US United S +tates print $Countries{'United States'},"\n"; # Prints US United +States my @states = keys %Countries; # Returns ISO 3166 + names sorted. Let's you look up countries by ISO 3166 Alpha-2abbreviations, Country +name and ISO 3166 country name. (Case-insensitive) Exists and defined work as you would expect. Use at your own risk :) =head2 EXPORT None by default. =head1 AUTHOR Pumphret, Lee, E<lt>perl@leeland.net<gt> =head1 COPYRIGHT Copyright (C) 2002 Lee Pumphret All Rights Reserved. This module is free software; you can redistribute it and/or modify it + under the same terms as Perl itself. =head1 SEE ALSO Locale::Country L<perl>. =cut


    -Lee

    "To be civilized is to deny one's nature."
Re: Re: Validate Country by ISO 3166 data
by shotgunefx (Parson) on Mar 07, 2002 at 01:49 UTC
    Cool. I didn't see that. I mainly used this for some US CGI stuff. I still like how this does the lookups without any explicit conversions though. Maybe I'll just write a wrapper around those to provide this.

    -Lee

    "To be civilized is to deny one's nature."

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://149904]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2020-04-05 12:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The most amusing oxymoron is:
















    Results (34 votes). Check out past polls.

    Notices?