Perl Programming Page 4 - Templating Tools |
Mark-Jason Dominus' Text::Template has established itself as the de facto standard templating system for plain text. Its templating language is very simple indeed--anything between {and} is evaluated by Perl; everything else is left alone. It is an object-oriented module--you create a template object from a file, filehandle, or string, and then you fill it in: use Text::Template; my $output = $template->fill_in(); So, let's say we've got the following template: Dear {$who}, Love, We set up our template object and our variables, and then we process the template: use Text::Template; $who = "Mark"; And the output would look like: Dear Mark, Love, Notice that the fill-in variables--$who, $modulename, and so on--are not my variables. When you think about it, this ought to be obvious--the my variables are not in Text::Template's scope, and therefore it wouldn't be able to see them. This is a bit unpleasant: Text::Template has access to your package variables, and you have to do a bit more work if you want to avoid giving use strict a fit. Text::Template has two solutions to this. The first is pretty simple--just move the fill-in variables into a completely different package: use Text::Template; $Temp::who = "Mark"; That's slightly better, but it still doesn't please people for whom global variables are pure evil. If that's you, you can get around the problem by passing in a portable symbol table--that is, a hash: use Text::Template; print $template->fill_in(HASH => { Please check back next week for the continuation of this article.
blog comments powered by Disqus |
|
|
|
|
|
|
|