in reply to Re: How to get code of the calling function or object
in thread How to get code of the calling function or object

Just read that Data::Dumper dumps Perl data structures does this mean that code itself is a data structure. If yes can we modify this code properly and feed back to the Interpreter?
  • Comment on Re^2: How to get code of the calling function or object

Replies are listed 'Best First'.
Re^3: How to get code of the calling function or object
by BrowserUk (Patriarch) on Mar 25, 2011 at 07:36 UTC
    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.

      I started doing something like this after I came across a node a while back. I cannot find the link to the node now.

      My test code was

      use B::Deparse; my $sr = sub { print "Hello\n" }; $sr->(); my $code = B::Deparse->new->coderef2text($sr); $code =~ s/print \s\S+/die "Bye";/; $sr = eval "sub $code"; $sr->();

      Thanks for the wonderfull explanation.

      But lets say for the time being I need to do some experiments on that Idea, In that case I will have read up upon the Perl API to work with that right? Or are there way to that through pure Perl code?

      directly to modify the AST of a subroutine on the fly

      I think the issue is that Perl produces an optree rather than an abstract syntax tree;the optree directly represents Perl's concrete syntax while with an AST you have a more abstract view

      I am under the impression that Perl 6 as well as the other languages for Parrot will output ASTs which will map onto a Parrot Abstract Syntax Tree.

      One main reason for that is tree transformation/manipulation which allow things that the OP asked for

      The .NET DLR is a an example of that.It calls the DLR AST as Expressions, which is represented by an intermediate Expressions language;this allows for a myriad of neat tricks.You might want to check Expression Tree Basics

Re^3: How to get code of the calling function or object
by Anonymous Monk on Mar 25, 2011 at 07:20 UTC
    ...does this mean that code itself is a data...

    All code is data, both perl and otherwise, but its not exactly easy to modify