I have been playing with a few ideas since we spoke on Friday, and so far this is the closest thing to a solution:
use strict; use warnings; use base qw( Exporter ); use B::Deparse; use Data::Dumper; our @EXPORT = qw( local_eval ); # # Use the & prototype to convert the {} block into a coderef # sub local_eval(&) { my $code = shift; my ($pkg,$file,$line) = caller(); my $context = find_context($code); print Dumper($context); eval { $code->(@_); }; if ($@) { my $exception = $@; # This will only work if the error contains something like thi +s: # "at local_err.pl line 36." my ($err_file,$err_line) = $exception =~ /at (.*?) line (\d+)/ +; if ($context->{$err_file}{$err_line}) { # Make sure $@ was not changed locally. $@ = $exception; return; } else { # Rethrow the exception die $exception; }; } } # # Deparse the passed codeblock, stripping out the file names and # line numbers provided by the '-l' option. sub find_context { my $code = shift; my $deparse = B::Deparse->new('-p','-l','-sC'); my $src = $deparse->coderef2text($code); #Expects things like: line 31 "local_err.pl" my @lines = $src =~ /^#line (\d+ ".*")$/mg; my %context = (); for my $entry (@lines) { my ($line,$file) = split / /,$entry,2; $file =~ s/^"|"$//g; $context{$file}{$line}++; } return \%context; } 1;
And a test script:
use strict; use warnings; use LocalEval; local_eval { print "hello\n"; splode(); }; print "Should still run.\n";
It's not 100% reliable, but it might be an avenue to explore. If I have time later I'll play with it some more. Update - I haven't played with B::Deparse enough to know what the pitfalls are. Will write a test suite this evening or tomorrow to try and identify shortcomings.

In reply to Re: Trapping errors with specificity - LocalEval by imp
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.