An other solution is to use Text::Template:

use Text::Template 'fill_in_file'; my $TEMPLATE= 'my.templ'; # ... my $filled_template= fill_in_file( $TEMPLATE); # I usually use the HASH option to separate variables that # go in the template from variables in the code # my $filled_template= fill_in_file( $TEMPLATE, # HASH => { title => $title, # fields => \%fields # } # ); print $filled_template;

In the template, variables (or any Perl code) are delimited by { ... }, you can use alternate delimiters if you want (it will be a little faster but the delimiter should not appear in the embedded code), use the argument DELIMITERS => [$open, $close]:

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.0//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic10.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en-US"> <head> <title>My Kewl Page</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" +> </head> <body> <h1>{$title}</h1> <p>Field1: {$field{field1}}</p> { "<p>Field2: $field{field2}</p>" if( $field{field2} } </body> </html>

In reply to Re: Template Question by mirod
in thread Template Question by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.