无法在其他计算机上运行Perl脚本

时间:2013-05-13 01:33:14

标签: perl

当我尝试在第二台计算机上运行脚本时,我收到此消息:

malformed JSON string, neither array, object, number, string or atom, at character offset 0
(before "LWP will support htt...") at iptest.pl line 21,  line 2.

在我的第一台计算机上,脚本运行正常。

第21行:

my $data = decode_json($resp->content);

有谁知道问题是什么?

提前致谢

5 个答案:

答案 0 :(得分:3)

我有点惊讶JSON错误是你得到的唯一错误。但它确实包含一点点暗示:“LWP将支持htt ...”。我敢打赌,LWP缺少一个能够建立https连接的模块。您现在有两个选择:

  1. 打印$response->content以查看完整的错误消息。
  2. 在命令行中,执行lwp-request https://google.com/之类的操作。您应该看到完整的错误消息。
  3. 然后安装缺少的模块。

    当然:拜托,拜托,请:

    • use strictuse warnings
    • 清理那个脚本并扔掉你不需要的每个use行:IO :: Socket,LWP :: Simple,YAML :: Tiny。
    • 阅读您实际使用的模块的文档。你想用LWP::UserAgent->new(keep_alive)做什么?提示:引用keep_alive无效。

答案 1 :(得分:2)

一些问题:

  1. 始终使用use strict; use warnings;
  2. 永远不要使用$response->content。它的回报是无用的。相反,请使用$response->decoded_content( charset => 'none')
  3. 您需要chomp从STDIN获得的值。
  4. 除非被迫(our = ok),否则不应使用our @ISA。应该使用my
  5. my $format = '$format'; "$format"是一种愚蠢的写作方式"\$format"

答案 2 :(得分:1)

我应用了ikegami建议的大部分变化。然后perl给了我很好的错误消息来解决剩下的问题。看起来它现在有效。不要问为什么它以前不起作用。你的代码很奇怪,很难说究竟出了什么问题。通过严格和警告,您不得不编写更好的代码。也许添加一些名称很好的子程序来增加清晰度。

#!/usr/bin/perl
use strict;
use warnings;
use IO::Socket;
use LWP::UserAgent;
use open qw(:std :utf8);
use LWP::Simple;
use YAML::Tiny;
use JSON;
use URI;
use List::MoreUtils qw(uniq);

print "Enter Qve:";
my ( $qve, $loc, $key, $href );
chomp( $qve = <STDIN> );
print "Enter Location:";
chomp( $loc = <STDIN> );

$key = '';
my $format = '$format';
$href =
"https://api.datamarket.azure.com/Bing/Search/v1/Web?Query='$qve [loc:$loc]'&Latitude=43&Longitude=19&$format=JSON";
my $ua = LWP::UserAgent->new('keep_alive');
$ua->credentials( "api.datamarket.azure.com" . ':443', '', '', $key );
my $resp = $ua->get($href);
my $data = decode_json( $resp->decoded_content( charset => 'none' ) );
my @urls = map { $_->{'Url'} } @{ $data->{d}->{results} };

my @za;
for my $i ( 0 .. $#urls ) {
    my $trz  = "www.";
    my $host = URI->new( $urls[$i] )->host;
    $host =~ s/$trz//g;
    push( @za, $host );
}

答案 3 :(得分:1)

为了记录,这为我解决了问题(CentOS):

#yum install perl-Crypt-SSLeay

答案 4 :(得分:0)

我遇到了同样的情况。使用“ yum”安装perl之后,运行perl脚本时仍然出现错误。

$ sudo yum install perl             
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management
...

最终我做到了,而且有效。

$ lwp-request https://google.com/

It returned an error message .. (LWP::Protocol::https not installed)

$ sudo rpm -ivh ~/perl-LWP-Protocol-https-6.07-4.el8.noarch.rpm

$ lwp-request https://google.com/

它返回了HTML页面。

我的perl脚本可以正常运行。

(我的系统是:

$ cat / etc / os-release PRETTY_NAME = Red Hat Enterprise Linux 8.0)

相关问题