Yes. First, you create the template:
my $template = Text::Template->new( TYPE => 'FILE', SOURCE => 'formletter.tmpl', ) or die $Text::Template::ERROR;
But before we move, here is a modified version of that file that i want you to consider:
Dear {$name}, It has come to our attention that you are delinquent in your {$month} payment. Please remit {$amount} immediately, or your patellae may be needlessly endangered. Love, Mark "Vizopteryx" Dominus
Back the Perl script, now that we have a template ready to go, we just need to fill it in and print. Usually, you are doing this inside of a while or foreach loop, because your data usually is going to either be coming from a (delimited) file or a data base. Let's say, for simplicity's sake that this is a CSV file like so:
Gates,Bill,Mr.,1,42000000000 Wall,Larry,Mr.,5,2
That's last name, first name, salutation, the month index, and the amount owed. So, all we need to do is extract the data. I recommend Text::CSV_XS, but i am going to use split, again for simplicity:
my @monthname = qw(January yadda yadda December); open FH, '<', 'data.csv' or die "can't open data file: $!"; while (<FH>) { chomp; my ($last,$first,$title,$month,$amount) = split ',', $_; my $result = $template->fill_in(HASH => { name => "$title $first $last", month => $monthname[$month], amount => sprintf('%.2f',$amount), }); die $Text::Template::ERROR unless $result; print $result; }
And that's it. :)

One thing you may have noticed is that i calculated the month in the Perl code, not the template. I also formatted the amount in the Perl code, but this particular decoration might be better in the template. Hope this helps.

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

In reply to Re: Text::Template=>How to fill in with more than one time? by jeffa
in thread Text::Template=>How to fill in with more than one time? by taizica

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.