I've been debugging code which has eval'd subs in it. Normally when inside these subs, the debugger just shows something line "(eval 20)" for the current subroutine with no code from the current line.

But once, mysteriously, the debugger actually showed me the code which had been eval'd as I stepped through it. I figured if it can happen once, it can happen again, so I started investigating how it could happen.

perldebguts documents the @{"_<(eval 20)"} variables where the code for eval'd stuff should be stored. The trouble is, most of the time, there is no code there for my evals. I can look in the symbol table and see lots of other (eval ##) entries which do have code in them. For example, like this:

DB<20> p join "\n", map { "$_ $#{$_}" } grep /_<\(/, keys %::
So my question for the perl debug gurus out there is, Why do some eval's get their code stored and others do not? Is there anything I can do to encourage my eval'd code to get stored?

EUREKA!

It is the presence of a named sub, or even a named sub stub, which convinces perl to keep the eval'd code in memory. So for my anonymous subs, I can do this:

my $coderef = eval <<'EOF'; sub forget_me_not; sub { print "do some stuff"; } EOF
The debugger can now show me the code for the anonymous sub, even if I use the same stub name in all of the evals.

In reply to debugging eval'd code with source (SOLVED) by sharkey

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.