Hello,
I'm working on a small API for a project that I'm
involved in at the moment. One of the things I want to
be able to do is allow somebody to send me some template
text along with placeholders for special information that
my object knows about, and then return the text with
the required information substituted in. Very similar to
the way apache allows you to specify custom log formats.
It seems to me that using placeholders the way that
printf does ( % then some identifier, perhaps with some
formatting options) seems to be a standard way of doing
this.
My main question: is there any module out there that
specializes in parsing these kinds of template strings, or
do I pretty much have to start from the ground up?
If I do have to start from the ground up, then I'm
thinking that initially, I'm going to try something
simple like this:
my $template_text =~ s/\%g/$g_var/g;
There are a ton of problems with this:
- It really isn't scalable, if I end up with 50 possible
things to substitute, I have to have 50 reg exp's.
- It doesn't deal with escaping characters. My regular
expression experience is really limited in this area.
How can I make '%g' substitute to $g_var
and '\\%g' substitute to '\' . $g_var and
make '\%g' substitute to '%g' with a replacement expression?
- My gut instinct just tells me this is wrong.
I really appreciate any assistance with this, especially
if somebody can tell me that this has already been written!
:)
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.