[Templates] undefined warnings

Andy Wardley abw@andywardley.com
Tue, 7 Oct 2003 10:18:48 +0100


Mark Anderson wrote:
> Correct me if I'm wrong, but the situation concerning warnings
> for undefined variables and methods is kind of dismal :(.

Kinda.  Undefined variabled and methods are the one thing that can be
reported reliably.  But I admit that the debugging facilities are 
poor in general.

> First off, the ttree utility has a --dbg utility, but it just
> causes a bit more STDERR output from the program itself.

Yep, the DEBUG support has recently been upgraded in TT but ttree
is lagging behind...

> I fixed that with a small patch:
> 364d363
> <         'template_debug|debug=s',

...until now!  Thanks!  :-)

> So then I specify "debug = undef" in my ttree config, and I now
> get the benefit of a fatal die with a message like this:
>    "  ! undef error - whatever is undefined"

Yep.

> Note that it provides no context information (file and line).

Yep.  Leon asked about this the other day and I've been pondering how
easy it would be to add that.  It annoys the hell out of me too to get
an undefined variable error with no clue as to where it is.  Bad Template
Toolkit!

The problem is that the architecture of the TT2 parser is just not
cut out for the job.  That's why I've been re-writing it from scratch for
TT3 and aim to fix most, if not all of these problems.

If I could have written the TT2 parser any better then I would have done.
But I didn't know back then what I know now... and I wouldn't know what
I now know if it wasn't for doing it "wrong" a few times.

Sometimes you need to put a stick in the ground and wiggle it for a while
before you fully understand what kind of stick you really need.

In the short term, I am going to try and fix some of these issues in
the v2 parser.  But I'm loathed to spend too much time fixing something
that I've already decided to replace, so it all depends on how easy it
is.

> There also appears to be no way to make it just be a warning,
> or to make the warning appear in the generated template (vs. console 
> stderr).

The TRY...CATCH block will catch these errors and you can do what you
like with them.  You can use the PROCESS option to define a global 
catch-all handler template.  Like so:

  ttree --process=process.tt

process.tt:

   [% TRY;
        PROCESS $template;
      CATCH undef;
        "Hey silly, you accessed an undefined var: $error.info";
      CATCH;
        "An error occured; [$error.type] $error.info\n";
      END;
   %]
    
Or you can set the ERROR option to define a handler template for 
catching errors like these.

  ttree --error=error.tt

error.tt:

  [% IF error.type == 'undef';
        "Hey silly, you accessed an undefined var: $error.info";
     ELSE;
        "An error occured; [$error.type] $error.info\n";
     END
  %]

> Is this easy to fix or work around?

Yes, if you re-write the parser from scratch :-)

I'm hoping that I'll be able to dedicate some serious time to working on 
this Real Soon Now.  

A