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 make it obvious # where we skip to after handling the exception JSON_RECORD: while (my $line = ) { # 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. . . . }