in reply to Re^3: HTML::Template output to files.
in thread HTML::Template output to files.
That helps, thank you. Although it's possible to avoid the TMPL_LOOP tag by generating your output manually, that doesn't make sense because you're already using HTML::Template the TMPL_LOOP tag seems to be exactly what you need to get the output you want. Here's a an approach that continues to use the module.
Take a look at the TMPL_LOOP documentation, it includes examples. Inside the while loop you'll need to build a data structure, an "array of hashes", see for example here: Generation of an ARRAY OF HASHES. Here's one way you might do it, assuming the NAME stays the same:
$data_tmpl->param(NAME => $name); my @data; while ( my ( $key, $entry ) = each %{ $more_data } ) { push @data, { NUMBER => $entry->{NUMBER}, DATE => $entry->{DATE}, COL => $entry->{COL}, FIRST => $entry->{FIRST}, LAST => $entry->{NAME}, }; } # the following line is JUST FOR DEBUGGING, remove for production code use Data::Dumper; print STDERR Dumper(\@data); # show @data on STDERR $template->param(DATA => \@data); # now you can write your file
(note: untested except for syntax check)
A few things still aren't quite clear: Is the "NAME" the same for every entry? If not, you may need to wrap your TMPL_LOOP inside another TMPL_LOOP, the outer one looping over names with the inner one looping over the data for that name (i.e. nested loops). In the Perl code you would also need to put your while loop inside another while loop. From your sample code it's not quite clear if there are multiple names per file and how the data for different names would be fetched, it would also help to see some of your input data. You can pretty-print it like this: use Data::Dumper; print Dumper($more_data); Also, what do you want the filename(s) to be? In your sample code you're basing them on $key, implying that you want one file per data record?
|
|---|