My suggestion would be to avoid rolling your own in this situation. There are lots of great pre-existing modules to do templates (ex:
HTML::Template,
Template::Toolkit). They provide the simple "replace" functionality that you're trying to implement as well as much fancier things (loops, for instance) which you can use to further enhance your documents
See
this article to find out which one is best for you.
My own personal preference is to use
HTML::Template because of its simplicity.
Here's a quick example (given
here) using HTML::Template:
A template file (test.tmpl)
<HTML>
<HEAD>
<TITLE>Test Template</TITLE>
</HEAD>
<BODY>
My Home Directory is <TMPL_VAR NAME="home">.
My Path is set to <TMPL_VAR NAME="path">.
</BODY>
</HTML>
...and some code to use the template:
use HTML::Template;
# open the HTML template
my $template = HTML::Template->new(filename => 'test.tmpl');
# fill in some parameters in the template
$template->param(home => $ENV{HOME});
$template->param(path => $ENV{PATH});
# send the obligatory Content-Type
print "Content-Type: text/html\n\n";
# print the template
print $template->output;
Update: Fixed Template::Toolkit link, thanks
zigdon.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.