【问题标题】:Perl - Pseudo-hashes are deprecatedPerl - 不推荐使用伪散列
【发布时间】:2015-03-08 18:21:05
【问题描述】:

我正在尝试通过 perl 获取 d 请求。但我得到以下错误:

#!/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 行不推荐使用伪散列。 agent.pl 第 21 行没有这样的伪散列字段“rowCount”。

谢谢, 卡莱亚拉桑

【问题讨论】:

  • 显然 JSON 没有像您期望的那样解码。尝试转储结果(use Data::Dumper; print Dumper($json_obj); 以查看您实际得到的结果。
  • 谢谢安德鲁..它工作正常..
  • PS,/usr/packages/perl/perl-5.16.3/lib/5.16.3 应该是/usr/packages/perl/perl-5.16.3/libuse lib $dir; 将查找 arch 子目录($dir/$archname$dir/$version$dir/$version/$archname)并添加它们。

标签: linux perl api perl-module


【解决方案1】:

见:

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 可以帮助您告诉您实际的数据结构是什么。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-07
    • 2015-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-12
    • 1970-01-01
    相关资源
    最近更新 更多