Text::Template is a module for filling in templates. A template is a file or a string that has bits of Perl code, called program fragments, embedded in it. Program fragments are delimited by curly braces. They can be simple variable references like {$var} or complicated programs that define and call functions and assemble big chunks of HTML text. When a template is filled in, the program fragments are executed, and each is replaced with the values they compute, so for example {$var} is replaced with the value of $var. Parts of the template that are not program fragments are returned verbatim.
This is useful for generating form letters, CGI program output, and many other kinds of text. For example, you might have a form letter like this:
Dear {$title} {$lastname}, It has come to our attention that you are delinquent in your {$monthname[$last_paid_month]} payment. Please remit ${sprintf("%.2f", $amount)} immediately, or your patellae may be needlessly endangered. Love, Mark "{nickname(rand(100))}" Dominus
The result of filling in this template is a string, which might look something like this:
Dear Mr. Gates, It has come to our attention that you are delinquent in your February payment. Please remit $392.12 immediately, or your patellae may be needlessly endangered. Love, Mark "Vizopteryx" Dominus
To join a low-volume mailing list for Text::Template announcements, send a blank mail message to [email protected].
Illegal division by zero at template line 37.If the template comes from a file, the filename will be reported as well:
Illegal division by zero at catalog.tmpl line 37.
The format of the default error message has changed. It used to look like:
Program fragment at line 30 delivered error ``Illegal division by zero''It now looks like:
Program fragment delivered error ``Illegal division by zero at catalog.tmpl line 37''
Note that the default message used to report the line number at which the program fragment began; it now reports the line number at which the error actually occurred.
Maintenance and bug fix release
You can now specify that certain Perl statements be prepended to the beginning of every program fragment in a template, either per template, or for all templates, or for the duration of only one call to fill_in. This is useful, for example, if you want to enable `strict' checks in your templates but you don't want to manually add use strict to the front of every program fragment everywhere.
Small bug fix: DELIMITER and other arguments were being ignored in calls to fill_in_file and fill_this_in. (Thanks to Jonathan Roy for reporting this.)
See the manual section on `Alternative Delimiters'.
Fixed a bug in the way backslashes were processed. The 1.10 behavior was incompatible with the beta versions and was also inconvenient. (\n in templates was replaced with n before it was given to Perl for evaluation.) The new behavior is also incompatible with the beta versions, but it is only a little bit incompatible, and it is probably better.
Documentation for the new behavior, and tests for the bug.
New OUTPUT option delivers template results directly to a filehandle instead of making them into a string. Saves space and time.
PACKAGE and HASH now work intelligently with SAFE. This combination was broken before.
Fragments may now output data directly to the template, rather than having to arrange to return it as a return value at the end. This means that where you used to have to write this:
{ my $blist = ''; foreach $i (@items) { $blist .= qq{ * $i\n}; } $blist; }
You can now write this instead, because $OUT is special:
{ foreach $i (@items) { $OUT.= " * $i\n"; } }
(`A spoonful of sugar makes the medicine go down.')
Fixed some small bugs. Worked around a bug in Perl that does the wrong thing with $x = <Y> when $x contains a glob.
More documentation. Errors fixed.
Lots more tests.
Return to: Universe of Discourse main page | What's new page | Perl Paraphernalia