Ovid has asked for the wisdom of the Perl Monks concerning the following question:

(This has also been posted to the Template mailing list)

I recently had a bug in a Web page where if we had a single digit day, Template Toolkit v2.08 was failing a conditional check to see if it matched its numeric equivalent. I suspect that this is caused by Template Toolkit using eq when an == is found in the document. This causes 3 == '03' to evaluate as false and violates the DWIM principle.

Is this a known issue, mentioned somewhere in the docs, or just a bug? Below is a short test script that demonstrates the problem.

#!/usr/bin/perl -w use strict; use Template; my $template = Template->new; my %data = ( foo => 3, bar => '03' ); print "Perl results: ", $data{ foo } == $data{ bar } ? "match\n" : "no match\n"; $template->process( \*DATA, \%data ); __DATA__ Template results: [% foo == bar ? 'match' : 'no match' %]

I fixed the problem in my code by forcing 'bar' to be numeric.

Template results: [% foo == bar+0 ? 'match' : 'no match' %]

Cheers,
Ovid

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Replies are listed 'Best First'.
Re: 'eq' vs '==' inTemplate Toolkit
by belkajm (Beadle) on Oct 12, 2002 at 19:48 UTC
    Hi Ovid,

    I had a quick look through the source, and discovered this comment in the Grammer.pm.skel file:
    # for historical reasons, != and == are converted to ne and eq to perf +orm # stringwise comparison (mainly because it doesn't generate "non-numer +ical # comparison" warnings which != and == can) but the others (e.g. < > < += >=) # are not converted to their stringwise equivalents. I added 'gt' et +al, # briefly for v2.04d and then took them out again in 2.04e.
    Pity it wasn't a bug really....who knows how soon we'll get both types of comparison.

    Cheers,
    Yoda
Re: 'eq' vs '==' inTemplate Toolkit
by princepawn (Parson) on Oct 15, 2002 at 12:50 UTC
    From: Andy Wardley <abw@andywardley.com> Date: Tue Oct 15, 2002 07:54:18 AM US/Eastern To: Terrence Brannon <metaperl@mac.com> Cc: "Randal L. Schwartz" <merlyn@stonehenge.com>, abw@kfs.org, Module- +Authors <module-authors@perl.org> Subject: Re: RFC Text::UberText Terrence Brannon wrote: TT2 still has issues with numerical and string equivalence checks: True. It was a difficult design decision - to keep the language simpl +e, but at the risk of making it too simple to be useful. In version 3, the parser will be much more flexible. You'll be able to configure it to identify different operators, like '==' vs 'eq' and have it generate the appropriate Perl code for the compiled template. There are many times when I like having the simplicity of just one comparison operator. I've had feedback from others, particularly those more accustomed to page design and HTML markup than programming, that agree. But also, there are times when these things matter, and you really need the ability to differentiate. So TT3 will allow you to use different grammars, even custom ones, for these different occasions. Of course, there are work-arounds in TT2. You can always define your own Perl subs to perform a particular comparison and bind them in. Sure, it's not very efficient, but it gets the job done. my $vars = { seq => sub { $_[0] eq $_[1] }, neq => sub { $_[0] == $_[1] }, }; [% IF seq(var1, var2) %] ... [% END %] [% IF neq(var1, var2) %] ... [% END %] As I stated in this thread, and as MJD stated in his docs to Text::Template, people start out trying to dumb down a power tool like + Perl and end up creating more issues than they resolve. True. But in the case of TT, it was never meant to be a dumbed down version of Perl. It's something different to Perl, for a different purpose. Leave the programming to programmers. And leave the programming to a real programming language. Absolutely. And that is why my philosophy for TT has been exactly that - leave Perl to handle the programming. TT implements a presentation language, not a programming language. It is a Domain Specific Language designed for doing presentation specific tasks. Sure, it looks a lot like a programming language and you can even use it like a programming language if you really want to, but that's not really what it's designed for. Just like you can do programming in PostScript (if you're really twisted :-) but it's not a general purpose programming language. The main argument against a DSL is that it's Yet Another Language to learn. Well yes, it is, but I don't think that's a big deal. I personally don't have any trouble switching between Perl for programmi +ng, regexes for pattern matching, SQL for database retrieval and TT for presentation. I'm of the opinion that it's better to have the right tool for the right job, even if it takes a little time to learn how to use it. Another consideration is that there are plenty of people who use simplified languages like TT, etc., who are HTML page designers who don't already know programming languages like Perl. They can pick up +a "dumbed-down" presentation language in minutes and don't have to worry + about all the nitty-gritty detail that goes with real grown-up programming languages. TT works particularly well in this respect, allowing the back-end Perl + people to build the "clever" stuff, while the front-end designers make + it all look pretty. Rather than replace Perl, TT puts emphasis on wor +king well with Perl, so that you can have the slightly better of both world +s. 1. developing a good language is very difficult. You said it! And of course, there is no one language that suits all purposes. A
Re: 'eq' vs '==' inTemplate Toolkit
by princepawn (Parson) on Oct 14, 2002 at 19:22 UTC
    I suspect that this is caused by Template Toolkit using eq when an == is found in the document. This causes 3 == '03' to evaluate as false and violates the DWIM principle.
    Well, this is an example of what Dominus talks about in the docs to his module Text::Template. Basically, why use a limited, somewhat different mini-language of Perl when you already know Perl? That is, unless you enjoy remembering a new set of quirks, exceptions and limitations.