#!/usr/bin/perl -wT
use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use strict;
use Socket;

# define the list of countries
my %redirects = (fr => "http://www.cgi101.com/example/fr/",
                 es => "http://www.cgi101.com/example/es/");

# lookup hostname
my $hostname = gethostbyaddr(inet_aton($ENV{REMOTE_ADDR}), AF_INET);

# split into a list of strings, ala: ("dialup", "cgi101", "com")
my @hostarray = split(/\./, $hostname);

# country is the LAST item of the list
my $country = $hostarray[$#hostarray];

if (exists $redirects{$country}) {
   print redirect($redirects{$country});
} else {
   print redirect("http://www.cgi101.com/example/en/");
}