Just read that Data::Dumper dumps Perl data structures does this mean that code itself is a data structure.

Internally, code is held in data structures, but these are not standard Perl visible data structures like hashes or arrays, so you cannot easily manipulate them from Perl.

DDS just encapsulates B:;:Deparse to retrieve the source code.

can we modify this code properly and feed back to the Interpreter?

The return from DDS is a string. Perl can edit strings. Perl can also eval strings. So, you certainly could, edit the subroutine code and then eval (ie. reparse) the edited string to dynamically overlay the original subroutine.

Also, I believe that it is possible to use B::Deparse directly to modify the AST of a subroutine on the fly. I did look at this once (briefly) with the idea that it might be possible to extract the body of a subroutine and 'inline it', at the call sites to provide a macro facility. A nice idea, but fraught with complexities. It took very little reading before I gave up on the idea.

And remember, just because you can doesn't mean you should!.

I'm not shy of pushing at the boundaries, but my experience of using self-modifying code from way back when, is that it is a very bad idea. Imagine what happens when things go awry. Something failed, but you don't know what source code was being run when it went wrong. Was it that the editing process screwed up and produced valid, but unintended wrong code? Or Did it produce code as designed, but the design is wrong?

What happens if you have 2, 3 or half a dozen routines that optionally, dynamically self-modify, and interact to cause failures. But which combination of original and modified routines interact to cause that failure? It's a combinatorial nightmare.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re^3: How to get code of the calling function or object by BrowserUk
in thread How to get code of the calling function or object by Anonymous Monk

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.