12 August 2006
This post may be outdated due to it was written on 2006. The links may be broken. The code may be not working anymore. Leave comments if needed.
Template-Toolkit 里是没有 gt 这样的字符串比较操作符的。

像这样的东西,在 Perl 里是正确的。
if ('2006-08-12 06:32:38' gt '2006-08-11 16:43:41') {
print 'ok';
} else {
print 'no';
}
会输出 ok

但在 TT 里用 > 比较是错误的:
[% IF '2006-08-12 06:32:38' > '2006-08-11 16:51:08' %]
ok
[% ELSE %]
no
[% END %]
输出的是 no

正确的用法应当是用 Template::Plugin::String::Compare
[% USE String.Compare %]
[% IF String.Compare.new('2006-08-12 06:32:38') > '2006-08-11 16:51:08' %]

String.Compare 用 overload 重载了比较操作符:
use overload (
'<' => \&_lt,
'>' => \&_gt,
'<=' => \&_le,
'>=' => \&_ge,
fallback => 1,
);

Have fun!


blog comments powered by Disqus