On Tue May 07 10:06:58 2013, bniessen@3taps.com wrote:
Show quoted text> Hi Ovid.
>
> I am using it in a program to test the validity of the JSON object
> prior to
> using json->decode (which dies).
>
> eg:
>
> if (is_valid_json($data[$mythreadnum]))
> {
> $js=$json->decode($data[$mythreadnum]) or die()."\n\n\n\n";;
> }
> else
> {
> print "\007PUKED ON JSON\n\n".$data[$mythreadnum]."\n\n";
> $hide_record->execute($id);
> exit;
> }
OK, it's a common issue with people trying to push test code into their production code. You generally want to avoid that because test code is generally not designed to function that way.
In this case, if you look at is_valid_json(), you'll see that it merely wraps json->decode in an eval and tests $@. That's the solution you can consider, or perhaps:
use Try::Tiny;
my $js;
try {
$js = $json->decode($data[$mythreadnum]);
}
catch {
my $error = $_;
print "\007PUKED ON JSON with error ($error)\n\n".$data[$mythreadnum]."\n\n";
$hide_record->execute($id);
exit;
};
Cheers,
Ovid