JSON数据perl CGI脚本的错误处理

JSON数据perl CGI脚本的错误处理,json,perl,error-handling,cgi,Json,Perl,Error Handling,Cgi,我是perl新手,我正在用perl read Json数据编写一个简单的cgi脚本。。看起来像这样 use CGI; use JSON; use strict; my $cgi = CGI->new; my $error=0; $cgi->param(); my $data = $cgi->param('POSTDATA') || '{ "field1":"value1", "field2":"value2" }'; # Used a sample JSO

我是perl新手,我正在用perl read Json数据编写一个简单的cgi脚本。。看起来像这样

use CGI;
use JSON;
use strict;

my $cgi = CGI->new;
my $error=0;
$cgi->param();

my $data = $cgi->param('POSTDATA')  || '{
    "field1":"value1",
    "field2":"value2"
}'; # Used a sample JSON with

my $json = JSON->new->utf8;
my $input = $json->decode ($data) || $error++;
my @errors=();

my %slots;
$slots{'coloumn1'} = $input->{'field1'} || $error++;
$slots{'coloumn2'} = $input->{'field2'} || $error++; # if there is no field2 in JSON it will increment value

if ( $error > 0)  {
    print $cgi->header('text/html','400 Bad Data');
    print "error with $data  ";
    exit;
}

如何对JSON进行更复杂的错误处理,以检查其是否为有效的JSON字符串……并在JSON字符串中缺少字段时使用eval或其他方法引发异常???

我很难理解您在这里实际提出的问题。如果您提供的JSON无效,JSON->decode将出错,如果这是您的意思?好的,JSON键中缺少了什么或者它有空值??我如何使用错误处理来检查它???