LanX has asked for the wisdom of the Perl Monks concerning the following question:

Hi

I'm trying to realize a Lisp-like macro mechanism in Perl to get a general and reliable extension mechanism.

I successfully hacked B::Deparse such that function calls to macros are replaced with the string returned.

This works fine when translating the body of single functions with B::Deparse::coderef2text the resulting code is evaluated afterwards to replace the body.

Now I'm looking for ideas how to this for complete files.

My current approach is to use a codefilter to wrap the whole file with

macroexpand { ... oldfile ... }

But it gets tricky when handling __DATA__ and __END__ and anyway the idea was to get rid of codefilters

So does anybody know of a way to change the op-tree just after compilation?

I'm pondering to modify B::Deparse::compile for this purpose and to call it within CHECK { ... } or UNITCHECK {...}

An other approach would be to reset the implicit DATA filehandle to reread the code just after compilation phase.

Any brain storming idea is welcome.

Cheers Rolf

Replies are listed 'Best First'.
Re: Implicetely deparse, change and replace perlcode in file
by ikegami (Patriarch) on Jun 20, 2011 at 18:06 UTC

    the idea was to get rid of codefilters

    Zephram has been making Perl's parser callable. Look into his work.

    So does anybody know of a way to change the op-tree just after compilation?

    I don't see how that's useful here, but autovivification does this.

      Do you mean Zefram's B::CallChecker ?

      Thanks I'll have a look into it, but I forgot to mention that I'm looking for pure perl solutions.

      Cheers Rolf

        He's changing Perl itself. I don't know if his modules take advantage of his changes yet, but some of his modules should provide ideas as well.

        I didn't mention anything specific, because I don't know the specifics.

        Devel::Declare, Devel::CallParser and Devel::CallChecker are definitely worth studying. The latter two do appear to take advantage of his change to Perl.

Re: Implicetely deparse, change and replace perlcode in file
by Anonymous Monk on Jan 18, 2012 at 14:31 UTC
    Did this project succeed? I am curious to know.
      I presented talks on block-scoped macros at last YAPC::EU and GPW.

      I had some success experimenting with file-global macros but since last summer I was mainly busy with earning money, preparing talks and helping to organize comming YAPC::EU.

      Cheers Rolf