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

Dear Monks

I have to generate three versions of a directory structure and a number of ASCII files. For each version of the structure the ASCII files are slightly different. I first approached this by reusing some code I wrote in the past. I use heredocs as templates and Perl interpolation to populate them with the correct values. I also use some external ASCII files as templates and process them with some Perl code.

The script works as intended but now it has grown to 2500 lines and I begin to regret the approach. A better solution is obviously to use separate ASCII template files for everything and get rid of the heredocs altogether. I have looked on CPAN but I see about a zillion template modules.

I just need something simple. Replace some values in ASCII files. The template toolkit looks like overkill, maybe Text::Template or Text::Template::Simple? I’m a bit overwhelmed by the number of template modules.

Anybody out there having (good) experience with a simple CPAN template module?

Cheers
dHarry

Replies are listed 'Best First'.
Re: Wanted, template module
by Corion (Patriarch) on Mar 06, 2009 at 09:43 UTC

    The thing about overkill is, you can never have too much of it. Personally, I have had good success using Template Toolkit. The ttree utility sounds like a fit for your problem to me - it basically takes a directory tree of template files, fills in the values and creates a filled-in directory tree of files.

    Of course, there remains the problem that all template systems suck, but at least Template Toolkit is not specifically geared towards HTML, which is a plus in this situation. Of course, the lure of ttree will be that it is too inflexible to handle all situations and hence you will be tempted to put program logic into your templates. Template Toolkit is a horrible language to program in, if you're trying to create expressions more complex than a simple addition (and even that...).

Re: Wanted, template module
by mirod (Canon) on Mar 06, 2009 at 12:12 UTC

    I like Text::Template myself. It is dead easy to use, and as long as the maintainer of the templates knows Perl (or knows not to touch the code between the delimiters) it is OK.

    If the logic in the template is a bit convoluted I use several templates: if you need to generate complex formatting within a loop, create several templates, one for the entire output and one (or more) for the inside bits.

Re: Wanted, template module
by rovf (Priest) on Mar 06, 2009 at 09:49 UTC

    I'm using Text::ScriptTemplate for my project. It is very simple, the main disadvantage being that if you have (syntax-) errors in the embedded Perl code, they are sometimes hard to find because you don't get any information about the line numbers. So I wouldn't recommend it for templates where you have to embed a lot of code logic, but otherwise it's not bad.

    -- 
    Ronald Fischer <ynnor@mm.st>
Re: Wanted, template module
by Tanktalus (Canon) on Mar 06, 2009 at 18:24 UTC

    FYI, I switched from HTML::Template to Template toolkit. It's overkill, absolutely. And there are things that make it take far too much effort for use at $work. But, when I went to write the CB stats, I already knew how to approach it, and it was really, really easy.

    Mind you, I'm of the opinion that it's better to learn one good multipurpose tool and learn it well, than to learn a bazillion tools, and not really know any of them. I still look at other tools, but I only delve deep into them if they appear to solve a set of problems I don't have a tool for, or if they appear to solve a set of problems better than my current tool. That's why I've delved so deep into perl, and that's why I use XML::Twig for basically anything XML-ish (including manipulation of XHTML).

Re: Wanted, template module
by dsheroh (Monsignor) on Mar 06, 2009 at 14:57 UTC
    Despite their names, you may want to consider HTML::Template or CGI::FastTemplate. Both are good with general text and don't actually have any HTML- or CGI-specific functionality in them.

    As for which of the two to use, I'd go with H::T if you need looping or conditional logic in your templates or C::FT if you don't. (C::FT can do looping to generate tables and such, but you have to use a sub-template for the looped portion, which makes the templates less clear and gets to be a pain to write the code for.)