I feel your pain. "Died at (eval 11) line 1" is not particularly useful for debugging, and can easily wind up prompting a trip to 'perl -d'. There are two workarounds I've used:
eval q!die!; if ($@) { die $@ }
Or:
use strict;
use warnings;
sub caught () {
# caught ( )
my $cl = 1;
my ($pack, $file, $sub);
# occasionally $sub will end up undef; this is OK, but we don't wa
+nt a
# warning about it. So, we're lexically disabling it.
no warnings 'uninitialized';
do {
($pack,$file, undef, $sub) = caller($cl++);
} until ($sub ne '(eval)');
$cl--;
(my $exc = $@) =~ s/(.*) at .*$/$1/s; #trim of 'at file line ##' m
+sg.
$exc=~ s/[\r\n]/\x1F/gs; #replace line endings.
return 'Caught exception ['.$exc.'] '
.($cl>0 ? "$cl evals deep " : '').'in '
.($sub ? $sub : 'main');
}
eval q!die "This is a test"!; die caught if ($@);
This provides the very helpful "Caught exception [This is a test] 1 evals deep in main at D:\test.pl line 26.". This tells you the *depth* of the eval, rather than which eval, and gives you the line context nicely (and even tells you what subroutine you were ultimately in).
Other than for context, what would you use this for?
|