On Thu, 2002-03-28 at 10:19, Aaron Sherman wrote:

> Here's what I suggest as a compromise:
> 
>     macro forall ($iterator, $list, $block) {
>       my @ltmp = ($list);
>       foreach $iterator -> @ltmp $block
>     }
>     forall{$var}{@list}{{print;}};
> 
> Where the parser sees "macro NAME PARAMS BLOCK" it interpolates ONLY the
> variables in PARAMS into BLOCK for every occurance of the macro. The
> macro itself is then treated as a quoting operator (I'm uncomfortable
> with "forall%x%%y%%z%;", but it seems to be an expected consequence of
> this way of defining macros). So, the above would become:

To follow up, and further compromise, you might have a second form of
macro definition:

    macro idiv($num,$dev) = {
        if ($dev =~ /^(0x)?[0_]+(\.[0_]*)?$/) {
                die "divide by zero"; # Compile-time error
        } else {
                return "int($num)/int($dev)";
        }
    };

I think it will lead to some ugly code that's hard to maintain, but if
you truely want to be able to construct such painful beasts as "if",
you're going to need to do this. The win is that without the =, you fall
back on what I suggested previously, and then it looks and behaves the
way I think most people think of macros.

I thought of using the BEGIN keyword after the =, but that would be
confusing (as it would be evaluated every time the macro is seen, not
once as Larry suggests in Apoc4).

You would still treat idiv as a quoting operator after seeing the macro:

    idiv{1}{0};

You may also desire to have the normal function-call form of a macro
invocation in order to allow macros to replace functions in libraries.
So, for example:

    idiv(1,0);

Which would have to work very hard to defer evaluation (easy here, but
theoretically very hard), but would nominally work the same as the
quoting form.


Reply via email to