The Template::Stash::get method will always call the undefined method when a variable lookup returns undefined. The undefined method returns '' by default. It is therefore not possible to use undef as a useful value in TT2.
I would propose having the get method always call undefined_any instead which would return undef by default. I would then propose that the GET directive explictly call undefined_get if the returned value is not defined. This would allow [% "yes" IF foo %] to not call the undefined method if "foo" was not defined - while [% GET foo %] would call the undefined method. I would propose that the set method allow setting a variable in the stash to undef (if you attempt this now it will always set the value to ''. All of these proposals make it possible to add working Perl 6 // and err operators. These are called the "defined-or" operators. They work like the || operator, except that the left hand side is checked for definedness rather than truth. The Velocity parser of Template::Alloy relies upon the // operator for handling the standard Velocity interpolation behavior. It would be nice to have this capability so that TT2 could fully support the Velocity syntax. Template::Alloy currently supports all of the proposed behavior - but can't extend it to Template::Parser::CET because the playing of the template is handled by TT2 which automatically translates all undef values to ''. The proposed changes should be relatively easy to introduce to TT2 without breaking most installations. The items that would break are deployments that use Template::Stash::undefined to autocreate variables used in non-GET directives. It could even be made configurable behavior. The following works in Template::Alloy [% SET foo %] # explictly sets foo to undef [% foo //= 6 %] [% foo %] # prints 6 [% SET foo %] # explictly sets foo to undef [% foo = '' %] [% foo //= 6 %] [% foo %] # prints '' [% bar // baz // bing %] # shows the first defined value. Paul Seamons _______________________________________________ templates mailing list [email protected] http://lists.template-toolkit.org/mailman/listinfo/templates
