Note that there are some explanatory texts on larger screens.

plurals
  1. POWriting reports with Perl
    primarykey
    data
    text
    <p>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</p> <pre><code>#begin code our $log_fh; open %log_fh, "&gt;" . $logfile our $rep; if (multipleReports) { while (@reports) { printReport($report[0]); } } sub printReports { open $rep, "&gt;" . $[0]; printHeaders(); printBody(); close $rep; } sub printHeader() { format HDR = @&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; $generatedLine . format HDR_TOP = . $rep-&gt;format_name("HDR"); $rep-&gt;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 </code></pre> <p>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</p> <p>test<br> next item<br> last item </p> <p>reads </p> <p>last item<br> last item<br> last item </p> <p>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.</p> <p>Thanks George</p> <p>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</p> <pre><code>my $generatedLine = ""; my $format = "format HDR = \n" . '@&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;' . "\n" . '$generatedLine' . "\n" . '.'; my $formatTop = "format HDR_TOP = \n". '.'; eval $format; eval $formatTop; </code></pre> <p>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).</p> <p>Any idea why declaring the format this way seems to work? Is there something special going on with eval? </p> <p>Thanks George</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload