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

Is there an alternative to perl formats? I basically want a module that I can use to specify text templates in one file, and then have my script populate the templates and return the output. I'm running into problems with formats under mod_perl and I'd also like to be able to define the formats in a separate file (like HTML::Template) that someone else can modify.

The various template engines I have found will not work because there is no easy way to produce tabular output (like a table but built in text - basically padded to the specified length with spaces).

I have found Text::Reform but it's a little confusing and the formats are again kept with the code instead of in an external file.

Replies are listed 'Best First'.
Re: CPAN Alternative to perl formats?
by perrin (Chancellor) on Dec 04, 2004 at 01:56 UTC
Re: CPAN Alternative to perl formats?
by Zed_Lopez (Chaplain) on Dec 04, 2004 at 00:56 UTC

    There's Text::Template.

    And it'd be easy to write a wrapper around Perl6::Form or the other possibilities to automate getting the template from a file.

      I think Text::Template is closer to HTML::Template where you have to explicitly say the formatting - so I can't have like a placeholder @>>>>>>>>>>>> and put in the string "test" and have it automatically padded with the correct number of spaces.
Re: CPAN Alternative to perl formats?
by TedYoung (Deacon) on Dec 04, 2004 at 00:55 UTC

    Well, I don't know a much about Text::Reform, but, it seems like you could store the format in a file, and when you want to use it, just open the file and read the format.

    You may also want to look at Perl6::Form. It impliments Perl6's formats which are very powerful. Unlike most Perl6 packages, it doesn't use source filters and is likely very suitable for production use. You will probably still have to read the format from the file.

    Update

    I wasn't thinking too well... Perl6::Form still requires you to intermix the format with the variables. I'll keep thinking!

    Ted Young

    ($$<<$$=>$$<=>$$<=$$>>$$) always returns 1. :-)
      I took a look at Perl6::Form and it looked surprisingly similar to Text::Reform. And guess what? It's made by the same author ;) I bet they just used Text::Reform as the basis for Perl6::Form. Thanks for the suggestion, I think I'll keep looking but I'll keep this one in mind.
Re: CPAN Alternative to perl formats?
by jZed (Prior) on Dec 04, 2004 at 00:37 UTC
      It doesn't appear to support the creation of external files which are used to define what the resulting text looks like after variables are interpolated. Thanks, though.

      It is interesting and I will be able to hack something together using a combination of this and HTML::Template (using a regular .tpl file for the header and footer and just have a "table" variable in the middle), but if anyone else has other suggestions I'm going to hold off for a while just in case.