{
my $file = "$dir/GeoLite2-Country-Locations-en.csv";
+ sub id; sub cc; sub long; sub ct; sub cn;
+
%countryId = ();
%countryName = ();
# verify that the columns we need are present
map { die "Table has no $pairs{$_} column\n" unless (exists $header{$_}); } keys %pairs;
- my $id = $header{geoname_id};
- my $cc = $header{country_iso_code};
- my $long = $header{country_name};
- my $ct = $header{continent_code};
- my $cn = $header{continent_name};
+ my %remapping = (
+ id => 'geoname_id',
+ cc => 'country_iso_code',
+ long => 'country_name',
+ ct => 'continent_code',
+ cn => 'continent_name',
+ );
+
+ # now create a function which returns the value of that column #
+ map { eval "sub $_ () { \$header{\$remapping{$_}}; }" ; } keys %remapping;
while (my $row = $csv->getline($fh)) {
- if ($row->[$cc] eq '' && $row->[$long] eq '') {
- $countryId{$row->[$id]} = $row->[$ct];
- $countryName{$row->[$ct]} = $row->[$cn];
+ if ($row->[cc] eq '' && $row->[long] eq '') {
+ $countryId{$row->[id]} = $row->[ct];
+ $countryName{$row->[ct]} = $row->[cn];
} else {
- $countryId{$row->[$id]} = $row->[$cc];
- $countryName{$row->[$cc]} = $row->[$long];
+ $countryId{$row->[id]} = $row->[cc];
+ $countryName{$row->[cc]} = $row->[long];
}
}
$countryName{O1} = 'Other Country';
close($fh);
+
+ # clean up the namespace
+ undef &id; undef &cc; undef &long; undef &ct; undef &cn;
}
sub lookupCountry
sub collect
{
- my ($file, $fh, $net, $id, $rid, $proxy, $sat, $row);
+ my ($file, $fh, $row);
my (%country, %header);
+ sub net; sub id; sub rid; sub proxy; sub sat;
+
my %pairs = (
network => 'Network',
registered_country_geoname_id => 'Registered Country ID',
# verify that the columns we need are present
map { die "Table has no %pairs{$_} column\n" unless (exists $header{$_}); } keys %pairs;
- $net = $header{network};
- $id = $header{geoname_id};
- $rid = $header{registered_country_geoname_id};
- $proxy = $header{is_anonymous_proxy};
- $sat = $header{is_satellite_provider};
+ my %remapping = (
+ net => 'network',
+ id => 'geoname_id',
+ rid => 'registered_country_geoname_id',
+ proxy => 'is_anonymous_proxy',
+ sat => 'is_satellite_provider',
+ );
+
+ # now create a function which returns the value of that column #
+ map { eval "sub $_ () { \$header{\$remapping{$_}}; }" ; } keys %remapping;
while ($row = $csv->getline($fh)) {
my ($cc, $cidr);
- $cc = lookupCountry($row->[$id], $row->[$rid], $row->[$proxy], $row->[$sat]);
- $cidr = $row->[$net];
+ $cc = lookupCountry($row->[id], $row->[rid], $row->[proxy], $row->[sat]);
+ $cidr = $row->[net];
$country{$cc}->{pool_v4}->add($cidr);
if ($. % 4096 == 0) {
close($fh);
+ # clean up the namespace
+ undef &net; undef &id; undef &rid; undef &proxy; undef &sat;
+
$file = "$dir/GeoLite2-Country-Blocks-IPv6.csv";
open($fh, '<', $file) || die "Can't open IPv6 database\n";
# verify that the columns we need are present
map { die "Table has no %pairs{$_} column\n" unless (exists $header{$_}); } keys %pairs;
- $net = $header{network};
- $id = $header{geoname_id};
- $rid = $header{registered_country_geoname_id};
- $proxy = $header{is_anonymous_proxy};
- $sat = $header{is_satellite_provider};
+ # unlikely the IPv6 table has different columns, but just to be sure
+ # create a function which returns the value of that column #
+ map { eval "sub $_ () { \$header{\$remapping{$_}}; }" ; } keys %remapping;
while ($row = $csv->getline($fh)) {
my ($cc, $cidr);
- $cc = lookupCountry($row->[$id], $row->[$rid], $row->[$proxy], $row->[$sat]);
- $cidr = $row->[$net];
+ $cc = lookupCountry($row->[id], $row->[rid], $row->[proxy], $row->[sat]);
+ $cidr = $row->[net];
$country{$cc}->{pool_v6}->add($cidr);
if ($. % 4096 == 0) {
close($fh);
+ # clean up the namespace
+ undef &net; undef &id; undef &rid; undef &proxy; undef &sat;
+
return \%country;
}