Hi, I am trying to write out multiple report files using perl. Each file has the same structure, but with different data. So, my basic code looks something like
#begin code
our $log_fh;
open %log_fh, ">" . $logfile
our $rep;
if (multipleReports)
{
while (@reports) {
printReport($report[0]);
}
}
sub printReports
{
open $rep, ">" . $[0];
printHeaders();
printBody();
close $rep;
}
sub printHeader() {
format HDR =
@>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
$generatedLine
.
format HDR_TOP =
.
$rep->format_name("HDR");
$rep->format_top_name("HDR_TOP");
$generatedLine = "test";
write($rep);
$generatedLine = "next item";
write($rep);
$generatedLine = "last header item";
write($rep);
}
sub printBody #There are multiple such sections in my code. For simplicity, I have just shown 1 here
{
#declare own header and header top. Set report to use these and print items to $rep
}
#end code
The above is just a high level of the code I am using and I hope I have captured all the salient points. However, for some reason, I get the first report file output correctly. The second file instead of having in the first section
test
next item
last item
reads
last item
last item
last item
I have tried a whole lot of options primarily around autoflush, but, for the life of me can't figure out why it is doing this. I am using Perl 5.8.2. Any help/pointers much appreciated.
Thanks George
Edit 1 I tried passing the filehandle as a parameter to the subroutines, but, was still seeing the issue. I then move the format statement outside the subroutine and declared the $generated variable as a global. This seemed to fix it. I think for some reason declaring the format each time, the subroutine was being called, seemed to be messing it up. Not sure exactly why. Wierd thing though is, I moved the format statement back to inside the subroutine (I didn't like the idea of declaring all the variables in all my format statements to be global). But,this time I changed the declaration to be
my $generatedLine = "";
my $format = "format HDR = \n" .
'@>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>' . "\n" .
'$generatedLine' . "\n" .
'.';
my $formatTop = "format HDR_TOP = \n".
'.';
eval $format;
eval $formatTop;
This seems to work as well - I see the correct output in my multiple files (well..I am currently only testing with 2 files..I will be doing some more testing tomorrow).
Any idea why declaring the format this way seems to work? Is there something special going on with eval?
Thanks George