dkhosla1 has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: try catch getting ignored by SIG DIE subroutine (updated!)
by haukex (Archbishop) on Feb 25, 2018 at 08:27 UTC | |
by ikegami (Patriarch) on Feb 26, 2018 at 05:18 UTC | |
by dkhosla1 (Sexton) on Feb 25, 2018 at 20:16 UTC | |
|
Re: try catch getting ignored by SIG DIE subroutine
by Athanasius (Archbishop) on Feb 25, 2018 at 07:44 UTC | |
by dkhosla1 (Sexton) on Feb 25, 2018 at 20:11 UTC | |
|
Re: try catch getting ignored by SIG DIE subroutine
by Corion (Patriarch) on Feb 25, 2018 at 07:21 UTC |