> The documentation says that the block will be evaluated each time.
> But it doesn't say if the block is INCLUDEd or PROCESSed.

Macros localize variables before executing the block, so they
are like INCLUDE (see Template::Directive::macro).  I guess
this should be added to the docs.

> But isn't C a bug?  [% c %] should be equivalent to [% PROCESS c %]
> and should not localize the variables.

The localize happens as the macro is called, so by the time
the PROCESS is executed the stash is already localized.

Here is a new version that takes advantage of the fact that
localization is only at the top-level.  We preset g to an
empty hash, then assign to it inside each block.

    [% BLOCK a %]
       [% g.global1 = "global1" %]
    [% END %]

    [% MACRO b BLOCK %]
      [% g.global2 = "global2" %]
    [% END %]

    [% MACRO c PROCESS c %]
    [% BLOCK c %]
       [% g.global3 = "global3" %]
    [% END %]

    [% g = {}; PROCESS a; b; c; %]
    <br>A = [% g.global1 %]

    <br>B = [% g.global2 %]

    <br>C = [% g.global3 %]

This produces (ignoring whitespace):

    <br>A = global1
    <br>B = global2
    <br>C = global3

Of course, your "global" variables are now all inside the g hash,
which might not suit your aesthetic needs :).

Craig


Reply via email to