> The 64 kilobyte question is "Can we, and should we automatically determine
> the variable usage in a template?".
> 
> I'm not sure about the answer to that.  Yes, we can keep track of all 
> static variables get/set in a template but we can't catch everything
> due to the dynamic evaluation of variables.  e.g.
> 
>     [% foo.$var %]
> 
> In that case, I guess we say "Caveat Hacker" and accept the fact that 
> the variable usage can't be determined accurately and the page can't be 
> cached, etc., without further specific clues, e.g. as above.

As correctly pointed out, the only way to reliably determine the
exact variable usage is to process the template with an accurate
set of initial stash values.

But it would be relatively easy in TT3 for $compiler->variable() to
keep a list of all the variables it has seen, perhaps together with
whether it is an lvalue or not.  This will include unevaluated terms
like "foo.$var", "foo(bar).$var" etc, so it would be up to the user to
ignore or handle these as they wish.  But for simple explicit terms
like "foo.bar", "my_ipaddr" etc the user could readily determine which
variables are (probably) used.  One drawback is that this variable
usage info from the parser would have to be maintained as meta data
somewhere attached to the cached, compiled template.

Also, I really like the idea of supporting lazy variable evaluation. How
about for TT3 a config parameter (eg: STASH_DEFAULT) that could be a
scalar/hash/list or code ref?  In the former case this is the default
value (probably not very useful).  But the CODE case is called whenever
a variable doesn't exist.  This callback would typically fill in the
value (or return the value), and then stash evaluation would continue as
usual.  (If the value foo.bar was used and foo did not exist, the
callback would get "foo" and the remainder of the lookahead ("bar"), so,
if necessary, the function could figure out the full variable name.)

Craig


Reply via email to