nimdokk has asked for the wisdom of the Perl Monks concerning the following question:

I'm creating a series of index files for a set of directories. I've created a format which displays the information correctly in a file, I need to include some header information on each file. When I use the _TOP designation with format - only the first index file created has the header information - none of the subsequent files contain the header information (although they display the rest of the information correctly).
Here is a snippet of my code:

format INDEXFILE_TOP =
Program Description       Pages Date Filename
------------------------------------------------
.
 
format INDEXFILE =
@<<<<< @<<<<<<<<<<<<<<<<<< @<<<<< @<<<<< @<<<<<<<<<<<
$prog, $desc, $pages, $date, $file
.
( then the rest of the code to get all the
 information for the variables and so forth )
the variables are filled in during a series of foreach loops (probably pretty ugly but I'm just starting out) culminating with write (INDEXFILE);

Anyone one have any ideas as to why the header information is only included on the first instance of the created file and none of the following ones?

Replies are listed 'Best First'.
(jeffa) Re: Format Question
by jeffa (Bishop) on Mar 23, 2002 at 18:43 UTC
    The reason that the header info is only included in the first file is most likely a consequence that your total lines of all index files are less than the value of $= (current page length - see perlvar), which defaults to 60. If not, then find the file with the 58th line (59th, something like that ;)) and you should find a header.

    Top-of-Page Formats are meant for hardcopy devices, but you can achieve the results you want with a simple hack: make $= equal to the number of lines you are about to print plus 2 for the numbers of lines in INDEXFILE_TOP. Here is an example:

    use strict; my ($prog,$desc,$pages,$date,$file); my @line1 = ( qq|prog desc pages data file\n|, qq|foo blah 5 bar foo.txt\n|, qq|bar blah 5 baz bar.txt\n|, qq|baz blah 5 qux baz.txt\n|, ); my @line2 = ( qq|prog desc pages data file\n|, qq|foo blah 5 bar foo.txt\n|, ); $~ = 'INDEXFILE'; write_2_file(@line1); write_2_file(@line2); sub write_2_file { $= = 2 + @_; for (@_) { chomp; ($prog,$desc,$pages,$date,$file) = split; write; # to STDOUT for this example } } format INDEXFILE_TOP = Program Description Pages Date Filename ------------------------------------------------ . format INDEXFILE = @<<<<< @<<<<<<<<<<<<<<<<<< @<<<<< @<<<<< @<<<<<<<<<<< $prog, $desc, $pages, $date, $file .
    The only things that really bugs me about this code is having to hard code the number of lines in INDEXFILE_TOP. There might be a way to derive this number, i'll be on the lookout for it (anybody knows...feel free to share ;))

    UPDATE: Doh! Looks like rinceWind has a much cleaner answer.

    Change '$= = 2 + @_' to '$- = 0' in the above example for a more robust subroutine. rinceWind++

    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)
    
Re: Format Question
by rinceWind (Monsignor) on Mar 23, 2002 at 18:46 UTC
    In order to give you a definite answer, I would need to see more of the code.

    I suspect that you need to clear $- ($FORMAT_LINES_LEFT if in English mode) when moving on to a new file. see perlvar and perlform in the core documentation for more about this.