[Templates] Referring to variables defined outside BLOCKs

Paul Makepeace tt2.org@paulm.com
Tue, 19 Nov 2002 15:23:40 +0000


On Tue, Nov 19, 2002 at 10:16:29AM +0000, Andy Wardley wrote:
> On Tue, Nov 19, 2002 at 01:22:33AM +0000, Paul Makepeace wrote:
> > The subject sounds complicated but really I just want a variable
> > referred to in a block that wasn't defined there to be long lived. For
> > example,
> 
> [...]
> 
> > As ever, pointers to where I missed this in the ::Manual welcome :-)
> 
> It's hidden away in the deep, dark recesses of Template::Manual::Variables 
> 
>        If you want to create or update truly global variables
>        then you can use the 'global' namespace.  This is a hash
>        array automatically created in the top-level namespace
>        which all templates, localised or otherwise see the same
>        reference to.  Changes made to variables within this hash
>        are visible across all templates.
> 
>            [% global.version = 123 %]

Yep I saw that which I think is partly how I got the idea of tying it
with a subroutine name abbreviation.

Suppose I have this construct, (which I actually do, indeed prompting
all this :-)

  [% MACRO do_some_task BLOCK %]
    [% total = 0 %]

    [%# add() in my reality is building up an array of messages to display %]
    [% MACRO add(c) BLOCK %]
      [%# stuff with total %]
    [% END %]

    [%# calls to add() %]

  [% END %]

'total' is (in my intention, and common programmer idiom) a local
variable to do_some_task() but by making it global.* it in effect
goes the *opposite* way and then there's all sorts of issues with
namespace clashing.

Right now I'm prefixing variables like total with an abbreviation like
dsm.total (do_some_task) and hoping I don't get a clash now or later.
This at least is a higher resolution than global.*

Maybe I'm being too ambitious or coming at this from the wrong mindset
or something? I must admit I don't see an obvious way to make things
properly local without a separate keyword.

Thanks for the reassurance I'm not completely addled anyway :)
Paul

-- 
Paul Makepeace ....................................... http://paulm.com/

"If the cow asks a question, then hold on tightly!"
   -- http://paulm.com/toys/surrealism/