Try the reverse approach to fully leverage the power of perl

Here's something to consider ... instead of loading a template string into your perl script that has some data, why not load the data into your perl script that has some template string? It's just as quick and dirty, it obviates the need to use a Templating module (if you're opposed to that) and it's still relatively 'readable' for a simple fill-in-the-blank type document. For example:

### INIT pragma and lib use strict; use warnings; use YAML; ### INIT vars my $sRaw = join '', (<DATA>); my $d = YAML::Load($sRaw)->{data}[0]; ### OUTPUT template print q% This is my template. I am %.$d->{fname}.q% %.$d->{lname}.q%, %.$d->{job}.q%. I own %.$d->{assets}.q%. Dont be a *@#!! You can make big $$bucks$$ by writing template code!! %; __DATA__ data: - fname: Elmer J. lname: Fudd job: millionaire assets: a mansion and a yacht

Your interpolated vars look like this:

%.$d->{foo}.q%
It's a bit more typing than just ...
$foo
but less of a security risk, and the 'q' operator will not interpolate *anything* except your interpolated vars. This is nice when you have an output template that includes funky metacharacters or anything else that might trip up your script. Also, you don't have to use the percent symbol, you can use any character that you know will never appear in your output template.

When you think about it, it's rather silly to write "template munging code" ... because perl already does that quite well! Instead, focus on "data munging code" ...
There's a million possible data formats out there.
There's a multitude of ways to fill in a template (otherwise why would you generalize it into a template to begin with?).
There's a million ways to get data into a perl script, and you can make them interchangeable without having to change a single line of your template string.

Just some bits to consider


In reply to Re: Interpolating $var in a $string by dimar
in thread Interpolating $var in a $string by freddo411

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.