in reply to Trapping errors with specificity

Here's a handy trick: caller inside a $SIG{__DIE__} handler will give you more useful information about where you died. Here's an example:
#!/usr/bin/perl use warnings; use strict; sub call_depth { my $i = 1; $i++ while(caller($i)); return $i - 1; } sub test_sub { die "Internal die"; } sub call_sub { # Override $SIG{__DIE__} to get the callstack depth where we died. # If another sub overrides $SIG{__DIE__}, that's OK, because # it means we must have gotten to an internal sub. # $die_call_depth is a lexical that the $SIG{__DIE__} handler will # close around, so this can be called recursively. my $die_call_depth = -1; $SIG{__DIE__} = sub { $die_call_depth = call_depth(); die @_; }; eval { no strict 'refs'; $_[0]->(); }; if ($@) { # die_call_depth will be our depth + 2: one for the eval, and one # for the $SIG{__DIE__} sub. if (($die_call_depth-2) == call_depth()) { warn "No such sub $_[0]: $@"; } else { warn "Failure inside sub $_[0]: $@"; } } } call_sub('test_sub'); call_sub('flerp');
Output:
Failure inside sub test_sub: Internal die at t65 line 15.
No such sub flerp: Undefined subroutine &main::flerp called at t65 line 30.