[Templates] tt annoyances
Japheth Cleaver
cleaver@ixpres.net
Fri, 10 Oct 2003 11:04:27 -0700
>4. Common string functions are available in counter-intuitive ways.
>The builtin scalar string supports: length, repeat, replace, match, search,
>split, chunk. Now, suppose I want the equivalent of the perl functions substr,
>uc, and index. Where do I find them? Well, substr is available by using the
>String plugin, which has no relation to a builtin scalar string:
One thing I do around any template perl wrapper code is add a few stash
methods for my own sanity. Eg:
$Template::Stash::SCALAR_OPS->{ lc } = sub { lc shift };
$Template::Stash::SCALAR_OPS->{ uc } = sub { uc shift };
$Template::Stash::SCALAR_OPS->{ lcfirst } = sub { lcfirst shift };
$Template::Stash::SCALAR_OPS->{ ucfirst } = sub { ucfirst shift };
This allows me to use [% str.uc %] rather than
[% str FILTER upper %]
I also used this as a replacement for substr. It looks odd, but I seemed to
have problems when writing it any other way...
$Template::Stash::SCALAR_OPS->{ substr } = sub {
if (exists $_[3]) {
return substr($_[0], $_[1], $_[2], $_[3]);
} elsif (exists $_[2]) {
return substr($_[0], $_[1], $_[2]);
} elsif (exists $_[1]) {
return substr($_[0], $_[1]);
} else {
return $_[0];
};
};
-Japheth "J.C." Cleaver