linux
  • perl
  • api
  • perl-module
  • 2015-03-08 90 views 0 likes 
    0

    我試圖通過perl獲取d請求。但即時得到以下錯誤:Perl - 僞哈希已棄用

    #!/usr/bin/perl 
    use lib "/usr/packages/perl/perl-5.16.3/lib/5.16.3"; 
    use strict; 
    use warnings; 
    use LWP::UserAgent; 
    use JSON; 
    use MIME::Base64; 
    
    my $url = 'https://example.com:8443/cli/agentCLI'; 
    my $credentials = encode_base64('username:password'); 
    
    my $ua = LWP::UserAgent->new(ssl_opts =>{ verify_hostname => 0}); 
    my $response = $ua->get($url, 'Authorization' =>" Basic $credentials"); 
    
    die 'http status: ' . $response->code . ' ' . $response->message 
    unless ($response->is_success); 
    
    my $json_obj = JSON->new->utf8->decode($response->content); 
    
    # the number of rows returned will be in the 'rowCount' propery 
    print $json_obj->{rowCount} . " rows:n"; 
    
    # and the rows array will be in the 'rows' property. 
    foreach my $row(@{$json_obj->{rows}}){ 
        #Results from this particular query have a "Key" and a "Value" 
        print $row->{Key} . ":" . $row->{Value} . "n"; 
    } 
    

    輸出(誤差):

    僞散列在agent.pl線棄用21. 沒有這樣的僞散列字段「rowCount時」在agent.pl線21 。

    感謝, Kalaiyarasan

    +0

    很明顯,JSON並不像您期望的那樣解碼。嘗試轉儲結果(使用Data :: Dumper;打印Dumper($ json_obj);'看看你實際得到了什麼 – 2015-03-08 18:30:46

    +0

    謝謝安德魯......它的工作很好.. – Kalaiyarasan 2015-03-08 18:33:45

    +0

    PS,'/ usr/packages/perl/perl-5.16.3/lib/5.16.3'應該是'/ usr/packages/perl/perl-5.16.3/lib'。'use lib $ dir;'會尋找arch subdirs('$ dir/$ archname','$ dir/$ version'和'$ dir/$ version/$ archname')並添加它們。 – ikegami 2015-03-08 23:04:00

    回答

    0

    參見:

    http://perldoc.perl.org/5.8.8/perlref.html#Pseudo-hashes%3A-Using-an-array-as-a-hash

    最近的版本:http://perldoc.perl.org/perlref.html#Pseudo-hashes%3a-Using-an-array-as-a-hash

    這已棄用。我會想象(但不能沒有你的JSON),你的JSON頂層是一個數組。

    Data::Dumper可以幫助告訴你你的實際數據結構是什麼。

    相關問題