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

Use another hash to hold the variables and just keep adding your new hash to it. Anything you change will override the previous values, anything you don't will just stay there.
#!/usr/bin/perl5.6 use Text::Template; my $template = Text::Template->new(SOURCE => 'formletter.tmpl') or die "Couldn't construct template: $Text::Template::ERROR" +; my @monthname = qw(January February March April May June July August September October November Dece +mber); my %vars1 = (title => 'Mr.', last_paid_month => 1, # February amount => 392.12, monthname => \@monthname, ); my %keepvars = %vars1; my $result = $template->fill_in(HASH => \%keepvars); if (defined $result) { print $result } else { die "Couldn't fill in template: $Text::Template::ERROR" } my %vars2 = (title => 'Mr.', firstname => 'Bill', lastname => 'Gates', ); %keepvars = (%keepvars, %vars2); $result = $template->fill_in(HASH => \%keepvars); if (defined $result) { print $result } else { die "Couldn't fill in template: $Text::Template::ERROR" }
  • Comment on Re: Re: Re: Text::Template=>How to fill in with more than one time?
  • Download Code

Replies are listed 'Best First'.
Re: Re: Re: Re: Text::Template=>How to fill in with more than one time?
by taizica (Novice) on Sep 04, 2003 at 13:36 UTC
    Thanks to all for your very helpful responses.

    My intention was to fill in a template by diiferent hash tables since my template will be big with a lot of variables to be defined. It looks like that I have no choice but just fill-in the template after collected all the varaibles in one shoot.

    I really hope that the Text::Template could allow people to save previous filled-in data, i.e. do accumulation and also allow people to "refresh" if needed. This may not make sense?

    Regards
    Taizi
Re: Re: Re: Re: Text::Template=>How to fill in with more than one time?
by ctilmes (Vicar) on Sep 04, 2003 at 12:57 UTC
    Instead of:
    %keepvars = (%keepvars, %vars2);
    You could also do this:
    @keepvars{keys %vars2} = values %vars2;