How can I catch a “failed to decode JSON” error message in Perl?

前提是你 提交于 2019-11-30 12:43:18

It looks like your error message is coming from stderr, not stdout. Thus,

perl json_load_test.pl >> logs/output.txt 2>> logs/errors.txt

Or something to that effect. If you want both in one file:

perl json_load_test.pl 2>&1 >> logs/output.txt

EDIT:

If for some reason you wanted to trap the error in the perl and send it to stdout, you could:

eval {
  decode_json($actual_response);
  1;
} or do {
  my $e = $@;
  print "$e\n";
};

EDIT2:

As daxim points out in the edit notes, Try::Tiny may be simpler:

use Try::Tiny;
try {
  decode_json($actual_response);
} catch {
  print "$_\n";
};
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!