2

我正在尝试修改现有的 perl 脚本以支持地理编码。为它找到了这个模块:http ://metacpan.org/pod/Geo::Coder::Google

我只是不知道如何从它返回的哈希结构中提取数据(我不是 perl 编码器,这只是我必须修复的一些遗留脚本)。

       {
        '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
          ]
        }
      }

尝试了我在谷歌上找到的所有哈希教程,我能打印的最多的是像 HASH(0x91e5558) 这样的东西。到目前为止,我的代码是以模块显示的为例:

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'); 

我只想要 Point -> 将数据坐标到它自己的变量中,然后我可以将其写入数据库。

4

3 回答 3

5

这是你想要的吗?

$lon = $location->{Point}{coordinates}[0];
$lat = $location->{Point}{coordinates}[1];
于 2011-09-16T09:34:15.523 回答
2

我只是想展示一个更容易编码的 OO 版本。由于 Tatsuhiko 没有提供它,我想证明它是可能的

  • bless 在别处创建的数据结构,并赋予数据结构行为。
  • 将方法推入类(包)

所以这里是包定义。

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 );
    }
}

然后,您可以像这样使用它:

my ( $lat, $long )
    = $geocoder->get_geocode( 
       location => 'Hollywood and Highland, Los Angeles, CA'
     )->coordinates
     ;

这创建了一个快速封装,使将来的代码访问更容易,并为使用代码提供简单的更改。

您还可以添加此功能:

{ 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;
    }
}

然后调用:

my ( $lat, $long ) 
    = $geocoder->get_coordinates( location => 'Hollywood and Highland, Los Angeles, CA' )
    ;
于 2011-09-16T15:04:26.187 回答
0

您正在寻找这样的东西:

$hashref = {
    'a' => 'A',
    'b' => {
        'c' => 'C',
        'some' => 'value',
    },
};

print "$hashref->{b}{some}\n"; # output: value
于 2011-09-16T09:43:22.353 回答