Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??


#!/usr/bin/perl use strict; use warnings; use B::Deparse; sub get_eval_text { my $start = shift; # turn the invoking subroutine into a B::Op-derived object my ($package, $sub, $end) = (caller(1))[0, 3, 2]; my $subref = do { no strict 'refs'; *{ $package . '::' . $sub }{C +ODE} }; my $cv = B::svref_2object( $subref ); # create a B::Deparse object and give it a sub to deparse (in part +) my $deparse = B::Deparse->new(); $deparse->{curcv} = $cv; # search the optree for the first op on the eval {} line my $op = deparse_from($cv->START, $start); # and deparse from that op return $deparse->deparse($op, 0) if $op; } sub deparse_from { my ($start, $line) = @_; for (my $op = $start; $$op; $op = $op->next()) { # look for nextstate ops next unless $op->isa( 'B::COP' ); # ... specifically the one representing the start of the eval +{} next unless $op->line == $line; # then grab the sibling op in the tree: leavetry return $op->sibling; } return; } sub main { my $x = 10; my $y = 20; eval { my $x = 1; my $y = $x; die 'aaaarrrr' }; print( $@, get_eval_text( __LINE__ - 1 ) ) if $@; } main();

I haven't found a way to get rid of __LINE__ - 1 reliably, but if you're willing to add that, here you go.

In reply to Re: Any way to access the contents of a block eval? by chromatic
in thread Any way to access the contents of a block eval? by adrianh

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (3)
    As of 2018-06-18 01:10 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (107 votes). Check out past polls.