in reply to Re: {Perl6} Macros and native compilation
in thread {Perl6} Macros and native compilation

It sounds really good, just one thing is unclear...

When the macro body is a string that is "shoved back into the input and reparsed", can I ask which variables to evaluate and which not ? I.e.

my macro this {"self{$foo}"}

Will $foo be interpolated/evaluated at compile-time, when the macro is expanded, or at runtime, in the context of the macro's usage ? In Lisp, using special syntax you can have both, which is very, *very* powerful.

Replies are listed 'Best First'.
Re^3: {Perl6} Macros and native compilation
by rg0now (Chaplain) on Apr 11, 2005 at 12:42 UTC
    In my understanding, you absolutely can do this with Perl 6 macros. If you want $foo to be avaluated at compile time, you just return a string as you did. Contrariwise, if you return a closure (maybe using the simple my macro this { self{$foo} } syntax?), you get "a generic routine that is to be immediately inlined, treating the closure as a template", such that "any variable not referring back to a parameter is left alone, so that your template can declare its own lexical variables, or refer to a package variable". This way, $foo will be evaluated at runtime...

    But, as always with Perl 6, don't take my words for granted...:-)

    rg0now

Re^3: {Perl6} Macros and native compilation
by iblech (Friar) on Apr 11, 2005 at 14:37 UTC

    I'd say you'd have to use single quotes, e.g.:

    my macro this { 'self{$foo}' }

    I don't think rg0now's idea will work, because $foo would refer to some outer $foo. If my understanding is correct, you can do things like the following:

    my $compilation_time = BEGIN { time }; macro uptime { time - $compilation_time } # And then... say "$*PROGRAM_NAME has been running for {uptime} seconds.";

    (Update: Doing s/macro/sub/ here would work just fine, too.)

    But, as rg0now said, that may be wrong, of course.

    --Ingo