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)
|