On Jul 26, 2007, at 3:31 PM, Bill Ward wrote:

On 7/25/07, Matisse Enzer <[EMAIL PROTECTED]> wrote:
How do get Template Toolkit to throw an exception if it encounters a
call to a non-existent method?

In the following example  zoo   is an object, but there is no
get_animals()  method - so I want TT to throw an exception at that
point.

            [% FOREACH animal IN zoo.get_animals %]

Everyone's suggesting ways to work around your problem... but I think
this is a flaw in Template.  I thought it was the case that if foo is
an object, then foo.bar is the bar method, or if foo is a hash, and
foo.bar is the value corresponding to the key bar.  But apparently, if
foo is an object implemented as a hash that lacks a bar method,
Template is looking up the value corresponding to the key bar.  The
problem with that is you are giving users the chance to peek inside
objects waaaaay too easily.  Objects are supposed to be accessed using
their API.

Am I correct in assuming that in your example, zoo is an object whose
implementation is a hash?  What do you get if you do the same thing
for an object whose implementation is something other than a hash?

For backward compatibility sake, I don't think we can change this
behavior.  But what if an option could be added that would make the
Template engine treat objects more strictly?  Seems analagous to the
'use strict' pragma in Perl to me.

Bill, I think it would be helpful for TT be more strict.

Here's a further problem I ran into while trying DEBUG_UNDEF (which sent a separate email to the list about):

If there IS a get_animals() method, but it happens to return an empty list, then DEBUG_UNDEF will also treat that as an error in that FOREACH loop. That's a problem for me - an empty list is OK, but an undefined value is not. I want a way to generate an exception if something is undefined, but no error if the value is empty.

So in:

    [% FOREACH thing IN object.someMethod %]
        [% thing %]
    [% END %]

if $object->someMethod() returns an empty list, I don't want that to be an error. If $object->someMethod() doesn't exist, or returns undef, then I want an error.


-M

------------------------------------
Matisse Enzer
[EMAIL PROTECTED]


_______________________________________________
templates mailing list
[email protected]
http://lists.template-toolkit.org/mailman/listinfo/templates

Reply via email to