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.

In reply to Re: Trapping errors with specificity by sgifford
in thread Trapping errors with specificity by diotalevi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.