maxmind / GeoIP2-perl

Perl API for MaxMind's GeoIP2 web services and databases
https://metacpan.org/release/GeoIP2/
Other
18 stars 11 forks source link

Can't load subdivisions #44

Closed ajmcello closed 7 years ago

ajmcello commented 7 years ago

I'm trying to look up the abbreviated state code. What am I missing? I got the state to display as a full, but it says it will also list the abbreviated using subdivisions, but I get this:

Can't locate object method "name" via package "1" (perhaps you forgot to load "1"?) at ./geoip.pl line 112.

I couldn't get the abbreviated country code either. I get United States, but can't find where it is abbreviated US. I couldn't find area code, or zip code, or metro. Are they there in the Lite version of GeoIP?

I was able to get it using the examples with the old DAT file, but I'm not following the newer mmdb format. Almost there, I think.

Code:

my $MMDB_DATABASE_LOCATION = "/usr/share/GeoIP/GeoLite2-City.mmdb";

my $reader = GeoIP2::Database::Reader->new(
    file    => $MMDB_DATABASE_LOCATION,
    locales => [ 'en' ],
);
        my $gi = $reader->city( ip => $ip );
        say $gi->city->name;
        say $gi->location->longitude;
        say $gi->location->latitude;
        say $gi->continent->code;
        say $gi->continent->name;
        say $gi->country->name;
        say $gi->city->name;
        say $gi->most_specific_subdivision->name;
        say $gi->subdivisions->name; # Problem here, I can't figure it out..Tried every combination I could think of
autarch commented 7 years ago

The subdivisions method returns a list. You want something like:

my @subs = $gi->subdivisions;
say $_->name for @subs;

Cheers,

-dave