Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.

Re: (2): New Validate ISO 3166

by shotgunefx (Parson)
on Mar 07, 2002 at 21:13 UTC ( [id://150130] : note . print w/replies, xml ) Need Help??

in reply to Re: Validate Country by ISO 3166 data
in thread Validate Country by ISO 3166 data

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><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


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