Say I've got some bit of library code, not entirely unlike this:
sub try_hard {
my $sub = shift;
my $tries = 3;
my @failures;
while($tries--) {
eval {
my $success = $sub->(@_) or die "sub returned false value";
1;
} or do {
push @failures, $@;
}
}
die "try_hard: failed 3 times: $failures[-1]"
}
This might fail like this:
try_hard: failed 3 times: sub returned false value at BadUtils.pm line 7
...which is not useful if you're calling try_hard
in a number of places and you don't know which call was the one that soft-failed.
If I could get the source of $sub
, I could change that die to:
my $success = $sub->(@_) or die "sub returned false value at $file line $lineno
";
and the library would be slightly DWIMmer. Is it possible to achieve this?