Subtle Template Toolkit bug / quiz
What does this produce:
[%-
SET foo = 0;
foo = 1 IF 0;
'--'; foo; '--';
%]
(If you don't see the answer, click through to the page ;)
Answer?
----
Fix?
[% IF 0; foo = 1; END %]
Wanna play?
#!/usr/bin/env perl
use Template;
my $tt = Template->new();
my $tmpl = q{
[%-
SET foo = 0;
foo = 1 IF 0;
'--'; foo; '--';
%]
};
$tt->process(\$tmpl);
I wonder if this is related to http://davesource.com/Bugs/perl.5.html
Tried on TT 2.22 on Perl 5.10.1, Debian Squeeze stock packages, and TT 2.24 on v5.16.2 built via perlbrew.
(Updated: to use q{} rather than q[] to quote the template code)
I'm no TT guy, so I can't help there, but I can suggest you not use square brackets to delimit the q operator if you are going to use them in the string (for the TT tags). Looks like a job for a non-interpolating heredoc (
@joel
fair point, tho it works just fine, and it's not at all related to the actual bug.
but if it helps, you can use:
Should work as expected if you use the SET syntax instead, I think?
SET foo = 1 IF 0;
Without that, a new value will be stashed in foo regardless of the expression - last I checked it'll compile to something like ->set(foo => sub { my $rslt = ''; $rslt = 1 if 0; $rslt });, so you'll end up with the starting value of the temporary (an empty string) rather than the original value. Not sure offhand why this is the case, but I find it's usually safer to use the block syntax for conditional assignment - 'IF something; x = y; END' or to use the explicit SET version.
Details are probably buried deep in Template::Parser or Template::Directive...
Isn't this just a difference in operator precedence.
instead of
Its for this reason that I like Mojo::Template. There is no new language/syntax to learn, its just Perl. Now I know that's not for everyone, but I like it because I don't have to learn something new.
For someone accustomed to Perl, TT is often full of (good or bad) surprises..
Which is quite unfortunate, given the fact that it's mainly used in Perl based shops.