Getting data out of perl hash structure?
I'm trying to modify existing perl script to support geocoding. Found this module for it: http://metacpan.org/pod/Geo::Coder::Google
I just can't figure out how to extract data from the hash structure it returns (I'm not a perl coder, this is just some legacy script I have to fix).
{
'AddressDetails' => {
'Country' => {
'AdministrativeArea' => {
'SubAdministrativeArea' => {
'SubAdministrativeAreaName' => 'San Francisco',
'Locality' => {
'PostalCode' => {
'PostalCodeNumber' => '94107'
},
'LocalityName' => 'San Francisco',
'Thoroughfare' => {
'ThoroughfareName' => '548 4th St'
}
}
},
'AdministrativeAreaName' => 'CA'
},
'CountryNameCode' => 'US'
}
},
'address' => '548 4th St, San Francisco, CA 94107, USA',
'Point' => {
'开发者_开发百科coordinates' => [
'-122.397323',
'37.778993',
0
]
}
}
Tried all the hash tutorials I found on google already, the most I can get it to print is something like HASH(0x91e5558). My code thus far is what the module show's as an example:
use Geo::Coder::Google;
my $geocoder = Geo::Coder::Google->new(apikey => 'Your API Key');
my $location = $geocoder->geocode( location => 'Hollywood and Highland, Los Angeles, CA');
I'd just want the Point -> coordinates data to it's own variables which I can then write to database.
Is this what you want?
$lon = $location->{Point}{coordinates}[0];
$lat = $location->{Point}{coordinates}[1];
I just wanted to present an easier to code OO version of this. Since Tatsuhiko did not provide it, I wanted to show it that it's possible to
- bless data structures created elsewhere and give behavior to data structures.
- push methods into a class (package)
So here's the package definition.
package Geo::Coder::Google::Geocode;
use strict;
use warnings;
use Carp qw<croak>;
use Params::Util qw<_ARRAY _CLASS _CLASSISA _HASH _INSTANCE>;
sub new {
croak( 'Not a valid subclass' )
unless my $class = _SUBCLASS( _CLASS( $_[0] ), __PACKAGE__ )
;
croak( 'Not a valid structure!' )
unless my $struct = _HASH( $_[1] )
and _HASH( $_[0]->{Point} )
;
# Done with checks, just bless it
return bless( $struct, $class );
}
sub coordinates {
my ( $self, $point, $coords ) = shift;
# Make sure each link in the chain exists ( and is populated ).
return unless _INSTANCE( $self, __PACKAGE__ )
and $point = _HASH( $self->{Point} )
and $coords = _ARRAY( $point->{coordinates} )
;
We have an array ref here, return it.
return wantarray ? @$coords : $coords;
}
{ package Geo::Coder::Google;
use Carp qw<croak>;
use Params::Util qw<_HASH>;
sub get_geocode {
croak( 'Geocode not created!' ) unless my $gcode = _HASH( &geocode );
return Geo::Coder::Google::Geocode->new( $gcode );
}
}
Then, you can use it like so:
my ( $lat, $long )
= $geocoder->get_geocode(
location => 'Hollywood and Highland, Los Angeles, CA'
)->coordinates
;
This creates a quick encapsulation to make it easier to code accesses in the future as well as provide simple changes to the using code.
You could also add this function:
{ package Geo::Coder::Google;
use Carp qw<croak>;
use Params::Util qw<_HASH>;
sub get_coordinates {
croak( 'Geocode not created!' ) unless my $gcode = _HASH( &geocode );
return Geo::Coder::Google::Geocode->new( $gcode )->coordinates;
}
}
And then call:
my ( $lat, $long )
= $geocoder->get_coordinates( location => 'Hollywood and Highland, Los Angeles, CA' )
;
You looking for something like this:
$hashref = {
'a' => 'A',
'b' => {
'c' => 'C',
'some' => 'value',
},
};
print "$hashref->{b}{some}\n"; # output: value
精彩评论