in reply to Text::Template=>How to fill in with more than one time?

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)

Replies are listed 'Best First'.
Re: Re: Text::Template=>How to fill in with more than one time?
by taizica (Novice) on Sep 04, 2003 at 20:29 UTC
    Hi Jeffa:

    Thanks for the information. I learned something new to me from your comments. However, my original intention was not to fill in the template with complete information more than once like what you showed here. I tried to fill in the template part by part. For example, get name from one data file, fill-in the template, and then get amount from another data file, then fill in again. In the following fill-in, I like to keep the previously filled-in information. It seems not possible for the current Text::Template

    Taizi
      Ah, it seems I misunderstood your question as well. In that case, consider not using a HASH, but a PACKAGE instead, and just change the values you want to change in the package vars, keeping the previous ones the same. Text::Template itself has no memory function. Alternatively, you could re-use the same HASH each time, and just change the variables that are different..

      C.