Appreciate insight on why if I have a defined SIG{__DIE__}, it is overriding a try/catch block? If I comment out the SIG{__DIE__} code, the 'catch' works; but otherwise it does not.
use strict; #use diagnostics; use JSON qw(decode_json encode_json); use Try::Tiny; use warnings; $SIG{__DIE__} = sub { my $item = shift; my $lasterr = ""; $lasterr = $@ if (defined($@)); chomp ($item); print STDERR "DIE ERROR: $item : $lasterr\n"; local $! = 2; exit $!; }; # end SIG_DIE sub write_mets { my $collref; # Read first line of test file my $file = "data1"; open (my $f, "<", $file) || die "Error opening: $!"; my $json1 = <$f>; close ($f); try { $collref = decode_json ($json1); print STDOUT "Read file $file\n"; } catch { print STDERR "Error decoding data from $file $@ \n"; }; } write_mets(); 1;
As run above, I get the following: (data1 has some bad json)
$ perl S2.pl DIE ERROR: garbage after JSON object, at character offset 7413 (before + "n\n") at S2.pl line 30. :
If I comment out the SIG_DIE code, I get
Noticed also that $@ also does not have a value in this case. The detailed error is missing. I really need the SIG_DIE functionality for the rest of the code, just not have it get int he way in the try/catch section :-). After several hours of googling and trying, have given up. Monks! Help! Thanks!$ perl S2.pl Error decoding data from data1
In reply to try catch getting ignored by SIG DIE subroutine by dkhosla1
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |