Output:#!/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');
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |