Ubuntu升级后,Perl JSON to_json无法正常工作(14.04 - > 16.04)

时间:2016-09-06 07:32:34

标签: json perl ubuntu hashref

更新:全新安装没有问题。我放弃了尝试修复升级后的Ubuntu。

原始问题:

我使用perl CPAN包JSON使用to_json函数将hasref转换为json。

这在Ubuntu 14.04上使用perl版本5.18.2正常工作,但在使用perl版本5.22.1升级到Ubuntu 16.04后,我收到错误消息:

hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)

原始代码是:

my $lang = {
        'connection_lost' => 'Network connection was lost',
        'connection_lost_more' => 'Please refresh this page to fix this problem'
};
my $json_lang = to_json($lang);

我使用warn ref($lang)检查了返回' HASH'的$lang类型,因此它应该是hashref?

我试图将其更改为:

my %lang;
$lang{'connection_lost'} = 'Network connection was lost';
$lang{'connection_lost_more'} = 'Please refresh this page to fix this problem';
my $json_lang = to_json(%lang);

和此:

my %lang;
$lang{'connection_lost'} = 'Network connection was lost';
$lang{'connection_lost_more'} = 'Please refresh this page to fix this problem';
my $json_lang = to_json(\%lang);

都失败了。 然后我尝试了allow_nonref开关:

my $lang = {
        'connection_lost' => 'Network connection was lost',
        'connection_lost_more' => 'Please refresh this page to fix this problem'
};
my $jsonnonref = JSON->new->allow_nonref; 
my $json_lang = $jsonnonref->to_json($lang);

导致错误消息to_json should not be called as a method

如何让它发挥作用?

对我不起作用的绝对最小代码:

package Handlers::test_handlers;

use strict;
use warnings;
use Apache2::Const -compile => qw(OK);
use Apache2::Request;

use JSON;

sub handler {
        my $lang = {
                'connection_lost' => 'connection_lost',
                'connection_lost_more' => 'connection_lost_more'
        };
        #my $json_lang = 'Hello world';
        my $json_lang = to_json($lang);        
        print $json_lang;
        return Apache2::Const::OK;
}

1;

使用' Hello world' -line有效,而to_json-line则无效。

1 个答案:

答案 0 :(得分:0)

我现在在全新安装时遇到了同样的问题,并决定从项目中完全删除 JSON::XS / libjson-xs-perl,这立即解决了这个问题