Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Validate Country by ISO 3166 data

by shotgunefx (Parson)
on Mar 06, 2002 at 23:41 UTC ( [id://149888]=CUFP: print w/replies, xml ) Need Help??

Tie::Hash package that allows you to look up ISO 3166 Country names by alpha-2 code, "Short Country Name" and "alpha-2 Short Country Name". Returns "alpha 2 Short Name". Exists & defined work as expected.
Keys returns only "alpha-2 Short Country Name" names.

UPDATE
Added updated version Locale::Country::TieHash that grabs the ISO data from the now standard Local::Country here
package WWE::Country::ISO; use 5.002; use strict; use warnings; use Tie::Hash; use Carp; our @ISA = qw(Tie::StdHash); my %CountryHash = ( "af" => "AF Afghanistan", "afghanistan" => "AF Afghanistan", "al" => "AL Albania", "albania" => "AL Albania", "dz" => "DZ Algeria", "algeria" => "DZ Algeria", "as" => "AS American Samoa", "american samoa" => "AS American Samoa", "ad" => "AD Andorra", "andorra" => "AD Andorra", "ao" => "AO Angola", "angola" => "AO Angola", "ai" => "AI Anguilla", "anguilla" => "AI Anguilla", "aq" => "AQ Antarctica", "antarctica" => "AQ Antarctica", "ag" => "AG Antigua and Barbuda", "antigua and barbuda" => "AG Antigua and Barbuda", "ar" => "AR Argentina", "argentina" => "AR Argentina", "am" => "AM Armenia", "armenia" => "AM Armenia", "aw" => "AW Aruba", "aruba" => "AW Aruba", "au" => "AU Australia", "australia" => "AU Australia", "at" => "AT Austria", "austria" => "AT Austria", "az" => "AZ Azerbaijan", "azerbaijan" => "AZ Azerbaijan", "bs" => "BS Bahamas", "bahamas" => "BS Bahamas", "bh" => "BH Bahrain", "bahrain" => "BH Bahrain", "bd" => "BD Bangladesh", "bangladesh" => "BD Bangladesh", "bb" => "BB Barbados", "barbados" => "BB Barbados", "by" => "BY Belarus", "belarus" => "BY Belarus", "be" => "BE Belgium", "belgium" => "BE Belgium", "bz" => "BZ Belize", "belize" => "BZ Belize", "bj" => "BJ Benin", "benin" => "BJ Benin", "bm" => "BM Bermuda", "bermuda" => "BM Bermuda", "bt" => "BT Bhutan", "bhutan" => "BT Bhutan", "bo" => "BO Bolivia", "bolivia" => "BO Bolivia", "ba" => "BA Bosnia and Herzegovina", "bosnia and herzegovina" => "BA Bosnia and Herzegovina", "bw" => "BW Botswana", "botswana" => "BW Botswana", "bv" => "BV Bouvet Island", "bouvet island" => "BV Bouvet Island", "br" => "BR Brazil", "brazil" => "BR Brazil", "io" => "IO British Indian Ocean Territory", "british indian ocean territory" => "IO British Indian Ocean T +erritory", "bn" => "BN Brunei Darussalam", "brunei darussalam" => "BN Brunei Darussalam", "bg" => "BG Bulgaria", "bulgaria" => "BG Bulgaria", "bf" => "BF Burkina Faso", "burkina faso" => "BF Burkina Faso", "bi" => "BI Burundi", "burundi" => "BI Burundi", "kh" => "KH Cambodia", "cambodia" => "KH Cambodia", "cm" => "CM Cameroon", "cameroon" => "CM Cameroon", "ca" => "CA Canada", "canada" => "CA Canada", "cv" => "CV Cape Verde", "cape verde" => "CV Cape Verde", "ky" => "KY Cayman Islands", "cayman islands" => "KY Cayman Islands", "cf" => "CF Central African Republic", "central african republic" => "CF Central African Republic", "td" => "TD Chad", "chad" => "TD Chad", "cl" => "CL Chile", "chile" => "CL Chile", "cn" => "CN China", "china" => "CN China", "cx" => "CX Christmas Island", "christmas island" => "CX Christmas Island", "cc" => "CC Cocos (Keeling) Islands", "cocos (keeling) islands" => "CC Cocos (Keeling) Islands", "co" => "CO Colombia", "colombia" => "CO Colombia", "km" => "KM Comoros", "comoros" => "KM Comoros", "cg" => "CG Congo", "congo" => "CG Congo", "cd" => "CD Congo, the Democratic Republic of the", "congo, the democratic republic of the" => "CD Congo, the Demo +cratic Republic of the", "ck" => "CK Cook Islands", "cook islands" => "CK Cook Islands", "cr" => "CR Costa Rica", "costa rica" => "CR Costa Rica", "ci" => "CI Cote D\'Ivoire", "cote d\'ivoire" => "CI Cote D\'Ivoire", "hr" => "HR Croatia", "croatia" => "HR Croatia", "cu" => "CU Cuba", "cuba" => "CU Cuba", "cy" => "CY Cyprus", "cyprus" => "CY Cyprus", "cz" => "CZ Czech Republic", "czech republic" => "CZ Czech Republic", "dk" => "DK Denmark", "denmark" => "DK Denmark", "dj" => "DJ Djibouti", "djibouti" => "DJ Djibouti", "dm" => "DM Dominica", "dominica" => "DM Dominica", "do" => "DO Dominican Republic", "dominican republic" => "DO Dominican Republic", "tp" => "TP East Timor", "east timor" => "TP East Timor", "ec" => "EC Ecuador", "ecuador" => "EC Ecuador", "eg" => "EG Egypt", "egypt" => "EG Egypt", "sv" => "SV El Salvador", "el salvador" => "SV El Salvador", "gq" => "GQ Equatorial Guinea", "equatorial guinea" => "GQ Equatorial Guinea", "er" => "ER Eritrea", "eritrea" => "ER Eritrea", "ee" => "EE Estonia", "estonia" => "EE Estonia", "et" => "ET Ethiopia", "ethiopia" => "ET Ethiopia", "fk" => "FK Falkland Islands (Malvinas)", "falkland islands (malvinas)" => "FK Falkland Islands (Malvina +s)", "fo" => "FO Faroe Islands", "faroe islands" => "FO Faroe Islands", "fj" => "FJ Fiji", "fiji" => "FJ Fiji", "fi" => "FI Finland", "finland" => "FI Finland", "fr" => "FR France", "france" => "FR France", "gf" => "GF French Guiana", "french guiana" => "GF French Guiana", "pf" => "PF French Polynesia", "french polynesia" => "PF French Polynesia", "tf" => "TF French Southern Territories", "french southern territories" => "TF French Southern Territori +es", "ga" => "GA Gabon", "gabon" => "GA Gabon", "gm" => "GM Gambia", "gambia" => "GM Gambia", "ge" => "GE Georgia", "georgia" => "GE Georgia", "de" => "DE Germany", "germany" => "DE Germany", "gh" => "GH Ghana", "ghana" => "GH Ghana", "gi" => "GI Gibraltar", "gibraltar" => "GI Gibraltar", "gr" => "GR Greece", "greece" => "GR Greece", "gl" => "GL Greenland", "greenland" => "GL Greenland", "gd" => "GD Grenada", "grenada" => "GD Grenada", "gp" => "GP Guadeloupe", "guadeloupe" => "GP Guadeloupe", "gu" => "GU Guam", "guam" => "GU Guam", "gt" => "GT Guatemala", "guatemala" => "GT Guatemala", "gn" => "GN Guinea", "guinea" => "GN Guinea", "gw" => "GW Guinea-Bissau", "guinea-bissau" => "GW Guinea-Bissau", "gy" => "GY Guyana", "guyana" => "GY Guyana", "ht" => "HT Haiti", "haiti" => "HT Haiti", "hm" => "HM Heard Island and Mcdonald Islands", "heard island and mcdonald islands" => "HM Heard Island and Mc +donald Islands", "va" => "VA Holy See (Vatican City State)", "holy see (vatican city state)" => "VA Holy See (Vatican City +State)", "hn" => "HN Honduras", "honduras" => "HN Honduras", "hk" => "HK Hong Kong", "hong kong" => "HK Hong Kong", "hu" => "HU Hungary", "hungary" => "HU Hungary", "is" => "IS Iceland", "iceland" => "IS Iceland", "in" => "IN India", "india" => "IN India", "id" => "ID Indonesia", "indonesia" => "ID Indonesia", "ir" => "IR Iran, Islamic Republic of", "iran, islamic republic of" => "IR Iran, Islamic Republic of", "iq" => "IQ Iraq", "iraq" => "IQ Iraq", "ie" => "IE Ireland", "ireland" => "IE Ireland", "il" => "IL Israel", "israel" => "IL Israel", "it" => "IT Italy", "italy" => "IT Italy", "jm" => "JM Jamaica", "jamaica" => "JM Jamaica", "jp" => "JP Japan", "japan" => "JP Japan", "jo" => "JO Jordan", "jordan" => "JO Jordan", "kz" => "KZ Kazakstan", "kazakstan" => "KZ Kazakstan", "ke" => "KE Kenya", "kenya" => "KE Kenya", "ki" => "KI Kiribati", "kiribati" => "KI Kiribati", "kp" => "KP Korea, Democratic People\'S Republic of", "korea, democratic people\'s republic of" => "KP Korea, Democr +atic People\'S Republic of", "kr" => "KR Korea, Republic of", "korea, republic of" => "KR Korea, Republic of", "kw" => "KW Kuwait", "kuwait" => "KW Kuwait", "kg" => "KG Kyrgyzstan", "kyrgyzstan" => "KG Kyrgyzstan", "la" => "LA Lao People\'S Democratic Republic", "lao people\'s democratic republic" => "LA Lao People\'S Democ +ratic Republic", "lv" => "LV Latvia", "latvia" => "LV Latvia", "lb" => "LB Lebanon", "lebanon" => "LB Lebanon", "ls" => "LS Lesotho", "lesotho" => "LS Lesotho", "lr" => "LR Liberia", "liberia" => "LR Liberia", "ly" => "LY Libyan Arab Jamahiriya", "libyan arab jamahiriya" => "LY Libyan Arab Jamahiriya", "li" => "LI Liechtenstein", "liechtenstein" => "LI Liechtenstein", "lt" => "LT Lithuania", "lithuania" => "LT Lithuania", "lu" => "LU Luxembourg", "luxembourg" => "LU Luxembourg", "mo" => "MO Macau", "macau" => "MO Macau", "mk" => "MK Macedonia, the Former Yugoslav Republic of", "macedonia, the former yugoslav republic of" => "MK Macedonia, + the Former Yugoslav Republic of", "mg" => "MG Madagascar", "madagascar" => "MG Madagascar", "mw" => "MW Malawi", "malawi" => "MW Malawi", "my" => "MY Malaysia", "malaysia" => "MY Malaysia", "mv" => "MV Maldives", "maldives" => "MV Maldives", "ml" => "ML Mali", "mali" => "ML Mali", "mt" => "MT Malta", "malta" => "MT Malta", "mh" => "MH Marshall Islands", "marshall islands" => "MH Marshall Islands", "mq" => "MQ Martinique", "martinique" => "MQ Martinique", "mr" => "MR Mauritania", "mauritania" => "MR Mauritania", "mu" => "MU Mauritius", "mauritius" => "MU Mauritius", "yt" => "YT Mayotte", "mayotte" => "YT Mayotte", "mx" => "MX Mexico", "mexico" => "MX Mexico", "fm" => "FM Micronesia, Federated States of", "micronesia, federated states of" => "FM Micronesia, Federated + States of", "md" => "MD Moldova, Republic of", "moldova, republic of" => "MD Moldova, Republic of", "mc" => "MC Monaco", "monaco" => "MC Monaco", "mn" => "MN Mongolia", "mongolia" => "MN Mongolia", "ms" => "MS Montserrat", "montserrat" => "MS Montserrat", "ma" => "MA Morocco", "morocco" => "MA Morocco", "mz" => "MZ Mozambique", "mozambique" => "MZ Mozambique", "mm" => "MM Myanmar", "myanmar" => "MM Myanmar", "na" => "NA Namibia", "namibia" => "NA Namibia", "nr" => "NR Nauru", "nauru" => "NR Nauru", "np" => "NP Nepal", "nepal" => "NP Nepal", "nl" => "NL Netherlands", "netherlands" => "NL Netherlands", "an" => "AN Netherlands Antilles", "netherlands antilles" => "AN Netherlands Antilles", "nc" => "NC New Caledonia", "new caledonia" => "NC New Caledonia", "nz" => "NZ New Zealand", "new zealand" => "NZ New Zealand", "ni" => "NI Nicaragua", "nicaragua" => "NI Nicaragua", "ne" => "NE Niger", "niger" => "NE Niger", "ng" => "NG Nigeria", "nigeria" => "NG Nigeria", "nu" => "NU Niue", "niue" => "NU Niue", "nf" => "NF Norfolk Island", "norfolk island" => "NF Norfolk Island", "mp" => "MP Northern Mariana Islands", "northern mariana islands" => "MP Northern Mariana Islands", "no" => "NO Norway", "norway" => "NO Norway", "om" => "OM Oman", "oman" => "OM Oman", "pk" => "PK Pakistan", "pakistan" => "PK Pakistan", "pw" => "PW Palau", "palau" => "PW Palau", "ps" => "PS Palestinian Territory, Occupied", "palestinian territory, occupied" => "PS Palestinian Territory +, Occupied", "pa" => "PA Panama", "panama" => "PA Panama", "pg" => "PG Papua New Guinea", "papua new guinea" => "PG Papua New Guinea", "py" => "PY Paraguay", "paraguay" => "PY Paraguay", "pe" => "PE Peru", "peru" => "PE Peru", "ph" => "PH Philippines", "philippines" => "PH Philippines", "pn" => "PN Pitcairn", "pitcairn" => "PN Pitcairn", "pl" => "PL Poland", "poland" => "PL Poland", "pt" => "PT Portugal", "portugal" => "PT Portugal", "pr" => "PR Puerto Rico", "puerto rico" => "PR Puerto Rico", "qa" => "QA Qatar", "qatar" => "QA Qatar", "re" => "RE Reunion", "reunion" => "RE Reunion", "ro" => "RO Romania", "romania" => "RO Romania", "ru" => "RU Russian Federation", "russian federation" => "RU Russian Federation", "rw" => "RW Rwanda", "rwanda" => "RW Rwanda", "sh" => "SH Saint Helena", "saint helena" => "SH Saint Helena", "kn" => "KN Saint Kitts and Nevis", "saint kitts and nevis" => "KN Saint Kitts and Nevis", "lc" => "LC Saint Lucia", "saint lucia" => "LC Saint Lucia", "pm" => "PM Saint Pierre and Miquelon", "saint pierre and miquelon" => "PM Saint Pierre and Miquelon", "vc" => "VC Saint Vincent and the Grenadines", "saint vincent and the grenadines" => "VC Saint Vincent and th +e Grenadines", "ws" => "WS Samoa", "samoa" => "WS Samoa", "sm" => "SM San Marino", "san marino" => "SM San Marino", "st" => "ST Sao Tome and Principe", "sao tome and principe" => "ST Sao Tome and Principe", "sa" => "SA Saudi Arabia", "saudi arabia" => "SA Saudi Arabia", "sn" => "SN Senegal", "senegal" => "SN Senegal", "sc" => "SC Seychelles", "seychelles" => "SC Seychelles", "sl" => "SL Sierra Leone", "sierra leone" => "SL Sierra Leone", "sg" => "SG Singapore", "singapore" => "SG Singapore", "sk" => "SK Slovakia", "slovakia" => "SK Slovakia", "si" => "SI Slovenia", "slovenia" => "SI Slovenia", "sb" => "SB Solomon Islands", "solomon islands" => "SB Solomon Islands", "so" => "SO Somalia", "somalia" => "SO Somalia", "za" => "ZA South Africa", "south africa" => "ZA South Africa", "gs" => "GS South Georgia and the South Sandwich Islands", "south georgia and the south sandwich islands" => "GS South Ge +orgia and the South Sandwich Islands", "es" => "ES Spain", "spain" => "ES Spain", "lk" => "LK Sri Lanka", "sri lanka" => "LK Sri Lanka", "sd" => "SD Sudan", "sudan" => "SD Sudan", "sr" => "SR Suriname", "suriname" => "SR Suriname", "sj" => "SJ Svalbard and Jan Mayen", "svalbard and jan mayen" => "SJ Svalbard and Jan Mayen", "sz" => "SZ Swaziland", "swaziland" => "SZ Swaziland", "se" => "SE Sweden", "sweden" => "SE Sweden", "ch" => "CH Switzerland", "switzerland" => "CH Switzerland", "sy" => "SY Syrian Arab Republic", "syrian arab republic" => "SY Syrian Arab Republic", "tw" => "TW Taiwan, Province of China", "taiwan, province of china" => "TW Taiwan, Province of China", "tj" => "TJ Tajikistan", "tajikistan" => "TJ Tajikistan", "tz" => "TZ Tanzania, United Republic of", "tanzania, united republic of" => "TZ Tanzania, United Republi +c of", "th" => "TH Thailand", "thailand" => "TH Thailand", "tg" => "TG Togo", "togo" => "TG Togo", "tk" => "TK Tokelau", "tokelau" => "TK Tokelau", "to" => "TO Tonga", "tonga" => "TO Tonga", "tt" => "TT Trinidad and Tobago", "trinidad and tobago" => "TT Trinidad and Tobago", "tn" => "TN Tunisia", "tunisia" => "TN Tunisia", "tr" => "TR Turkey", "turkey" => "TR Turkey", "tm" => "TM Turkmenistan", "turkmenistan" => "TM Turkmenistan", "tc" => "TC Turks and Caicos Islands", "turks and caicos islands" => "TC Turks and Caicos Islands", "tv" => "TV Tuvalu", "tuvalu" => "TV Tuvalu", "ug" => "UG Uganda", "uganda" => "UG Uganda", "ua" => "UA Ukraine", "ukraine" => "UA Ukraine", "ae" => "AE United Arab Emirates", "united arab emirates" => "AE United Arab Emirates", "gb" => "GB United Kingdom", "united kingdom" => "GB United Kingdom", "us" => "US United States", "united states" => "US United States", "um" => "UM United States Minor Outlying Islands", "united states minor outlying islands" => "UM United States Mi +nor Outlying Islands", "uy" => "UY Uruguay", "uruguay" => "UY Uruguay", "uz" => "UZ Uzbekistan", "uzbekistan" => "UZ Uzbekistan", "vu" => "VU Vanuatu", "vanuatu" => "VU Vanuatu", "ve" => "VE Venezuela", "venezuela" => "VE Venezuela", "vn" => "VN Viet Nam", "viet nam" => "VN Viet Nam", "vg" => "VG Virgin Islands, British", "virgin islands, british" => "VG Virgin Islands, British", "vi" => "VI Virgin Islands, U.S.", "virgin islands, u.s." => "VI Virgin Islands, U.S.", "wf" => "WF Wallis and Futuna", "wallis and futuna" => "WF Wallis and Futuna", "eh" => "EH Western Sahara", "western sahara" => "EH Western Sahara", "ye" => "YE Yemen", "yemen" => "YE Yemen", "yu" => "YU Yugoslavia", "yugoslavia" => "YU Yugoslavia", "zm" => "ZM Zambia", "zambia" => "ZM Zambia", "zw" => "ZW Zimbabwe", "zimbabwe" => "ZW Zimbabwe", ); our $VERSION = '0.01'; sub TIEHASH { my $class = shift; my $self = {}; $self->{DAT} = \%CountryHash; my @k = grep { /^\w\w$/ } sort { $self->{DAT}{$a} cmp $self-> +{DAT}{$b} } keys %{$self->{DAT}}; $self->{keys} = \@k; $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 WWE::Country::ISO; tie %Countries, 'WWE::Country::ISO'; =head1 DESCRIPTION tie %Countries, 'WWE::Country::ISO'; 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 None L<perl>. =cut


-Lee

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

Replies are listed 'Best First'.
Re: Validate Country by ISO 3166 data
by rob_au (Abbot) on Mar 07, 2002 at 01:35 UTC
    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'

      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."
      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."
Re: Validate Country by ISO 3166 data
by theguvnor (Chaplain) on Mar 07, 2002 at 01:46 UTC
    Hi shotgunefx. This looks like it would be useful... but I notice that you appear to have most of the world covered plus all the states in the US, but not the provinces and territories of Canada. I've taken the liberty of copying their English names here for use in your module:
    "ab" => "AB Alberta", "bc" => "BC British Colombia", "mb" => "MB Manitoba", "nb" => "NB New Brunswick", "nf" => "NF Newfoundland", "nt" => "NT Northwest Territories", "ns" => "NS Nova Scotia", "nu" => "NU Nunavut", "on" => "ON Ontario", "pe" => "PE Prince Edward Island", "qc" => "QC Québec", "sk" => "SK Saskatchewan", "yt" => "YT Yukon",
    Thanks for posting your module!

    ..Guv

    Update: upon further inspection, the US states aren't in the list as I had mistakenly thought (mea culpa!) so never mind me.. on the other hand, as shotgunefx mentions the state info is in another module, there's no reason it couldn't be extended with the above... just my 2 cents.

      Thanks for the input. I have the State stuff in another module. Apparently as rob_au pointed out there are (now standard) modules that already deal with this so I'm going to take a look at them before I make any more wheels. I do like the easy lookup functionality of this though.

      -Lee

      "To be civilized is to deny one's nature."
      Nothing is ever so simple, neh?
      "qc" => "PQ Quebec", "pq" => "PQ Quebec",
Re: Validate Country by ISO 3166 data
by seattlejohn (Deacon) on Mar 07, 2002 at 04:21 UTC
    One thought on initialization: To improve readability and perhaps simplify maintenance, I would be inclined to store the list of information in a __DATA__ section and create the hash dynamically, something like this:
    my %country_hash; chomp (my @data_rows = <DATA>); foreach my $this_country (@data_rows) { my $country_code = substr($this_country,0,2); my $country_name = substr($this_country,3); $country_hash{$country_code} = $country_hash{$country_name} = $this_ +country; } # ... your tie code ... __DATA__ AF Afghanistan AL Albania DZ Algeria .....

    In my eyes this would be easier to read and maintain. Also, special chars like the apostrophe Cote d'Ivoire no longer require escaping.

      Actually that's how I generated the hash. Pasted it in a temp script. I was wondering myself is it quicker for perl to parse it or to read it from data. Anyone have any thoughts?

      -Lee

      "To be civilized is to deny one's nature."
      A little cleaner looking w/o any extra vars:
      while (<DATA>) { chomp; $country_hash{substr($_,0,2)} = substr($_,3); }
      I totally agree. Making it more control file type driven makes it much easier to maintain and keeps you out of code that you know already works. Plus you can add more to the list and not have to worry about changing any code.
Re: Validate Country by ISO 3166 data
by krazken (Scribe) on Mar 08, 2002 at 15:55 UTC
    Nice mod. One problem I see is that some of the countries have diacritics in the name, for example, you have
    "reunion" => "RE Reunion",
    when it should be
    "réunion" => "RE Réunion",
    But there is another problem and that is to change the case of the country name to match against the hash involves a change of case. This change of case like using the uc() function or lc() will not handle the diacritics correctly. You can subtract an ascii 32, and catch most of these, but not all. Another option is to go with the
    use utf8;
    But this has some weirdnesses as well. Very useful module, I think it may need to be tweaked a little to handle the weird cases where diacritics appear in the country name.

    cheers!
    krazken
      Thanks for the feedback. I've changed the module to get the values from Locale::Country now. I'll admit that in practice, I haven't had to worry to much about internationalization yet. Now that I'm using local, and the lc is only on the hash key and alpha-2, wouldn't it be ok because the alpha-2 is always ascii and the hash key is never really used for display?

      -Lee

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://149888]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-19 23:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found