【问题标题】:I've got HTTP::Response fail when connect plotly site with Perl API使用 Perl API 连接 plotly 站点时出现 HTTP::Response 失败
【发布时间】:2014-01-11 10:11:48
【问题描述】:

最近,我找到了 plot.ly 网站并正在尝试使用它。 但是,当我使用 Perl API 时,我无法成功。 我的步骤如下。

  1. 我用谷歌账号注册了 plot.ly
  2. 已安装 Perl 模块(WebService::Plotly)
  3. 键入基本示例(“https://plot.ly/api/perl/docs/line-scatter”)

..跳过..

use WebService::Plotly;
use v5.10;
use utf8;

my $user = "MYID";
my $key = "MYKEY";

my $py= WebService::Plotly->new( un => $user, key => $key );

say __LINE__; # first say

my $x0 = [1,2,3,4]; 
my $y0 = [10,15,13,17];
my $x1 = [2,3,4,5]; 
my $y1 = [16,5,11,9];

my $response = $py->plot($x0, $y0, $x1, $y1);
say __LINE__ ; # second say

..跳过...

然后,执行示例 perl 代码 =>> 但是,在这一步中,$py->plot 总是返回“HTTP::Response=HASH(0x7fd1a4236918)” 第二个说没有执行 (我使用的 Perl 版本是 5.16.2 和 5.19.1,操作系统是 MacOS X)

在手上,python example("https://plot.ly/api/python/docs/line-scatter") 总是成功的。

请告诉我这个问题。 非常感谢!

【问题讨论】:

  • 在脚本开头添加这一行并显示输出use Carp; $SIG{__DIE__} = sub { confess @_; };
  • 我已将您的代码添加到脚本的开头行。我得到的结果是 12 和 HTTP::Response=HASH(0x100e5e568) 我应该怎么做?谢谢!

标签: perl api plotly


【解决方案1】:

您是否查看过 $py->content 或 HTTP::Response 对象的任何其他属性?

除了您尝试打印对象引用的值之外,您没有告诉我们任何其他信息,您从中提供的输出是人们期望的输出。

【讨论】:

  • 很高兴我能帮上忙 :-)
  • 您应该选择正确的响应并将其标记为这样。您的“未回答”问题越多,有人回答下一个问题的可能性就越小。
【解决方案2】:

快速查看该模块的源代码后,我可以建议像下面的示例一样使用它。因为任何方法都可能引发异常。在 http 错误时,这将是 HTTP::Response 对象

eval {
    my $response = $py->plot($x0, $y0, $x1, $y1);
};
if (my $err = $@) {
    if (!ref $err) {
        die "Plotly error: ", $err;
    }
    elsif ($err->isa('HTTP::Response')) {
        die "HTTP error: ", $err->status_line;
    }
    else {
        die "Unknown error: ", ref($err), " ($err)"
    }
}

【讨论】:

  • 感谢您的友好建议,我可以解决我的问题。原因是没有安装 LWP::Protocol::https。我从来没有使用过 eval 函数。我从你那里学到了很好的技巧。谢谢!
猜你喜欢
  • 2013-08-13
  • 2016-01-20
  • 1970-01-01
  • 2023-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多