If you've got one JSON entry per line and are using the JSON package from cpan.org, then you'd handle the exception something like this:
use warnings; use strict; use JSON; . . . # Build a JSON parser my $json = JSON->new->allow_nonref; # Process the lines in the file, we're labelling the WHILE loop to mak +e it obvious # where we skip to after handling the exception JSON_RECORD: while (my $line = <INFILE>) { # We'll declare the variable so we can use it outside of the eval my $data; # Parse the data inside the eval so we can catch the error eval { $data = $json->decode($line); }; # Now we can check $@ to see if we had an exception if ($@) { # parse failed, so log it and let the user know print "JSON parse failed on line $.\n"; print "error was: $@\n"; print "JSON we tried to parse <<$line>>\n\n"; # Now that we've handled the exception, skip to the next recor next JSON_RECORD; } # OK if we get here, we've successfully parse the JSON data, so we + can do what # we normally do now. . . . }
...roboticus
When your only tool is a hammer, all problems look like your thumb.
In reply to Re^5: Not an ARRAY reference
by roboticus
in thread Not an ARRAY reference
by chandantul
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |