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

I am extracting several tests worth of data from 1 file to separate formatted files. The first file is formatted correctly with the Header on the top on the 1st page and the form feed after $= lines. The following output files do not print the header on the first page or insert a form feed after my $= lines (it starts to work from page 2 on).
open (BZF, "< $bzWdF") or die "$!\n"; binmode(BZF); # Read file in binary mode read(BZF, $rcrd1, $BytPrRcrdBZF) == $BytPrRcrdBZF; @tstsLocat = unpack(" n32 ", $rcrd1); for (my $tstcnt=1;$tstcnt<=$tstsLocat[0];$tstcnt++){ seek(BZF, $tstS, 0) or die "seek:$!"; ## Build Performance Data HOH. $validTst= buildTblEntry($tblEnt,$tstcnt); if ( $validTst ==1){ my $outF= $tblEnt.".txt"; open (OUTF, "> $outF") or die "$outF $!\n"; my $ofh = select(OUTF); $% = 0; $= = 59; ## Print report Performance Data Sheet &prntRptPerfmanceSheet($tblEnt); ## Loop thru Equipment List entries for(my $eq1b_a="",my $eq1b_n=0; $doneEq!=1;){ read(BZF, $rcrd1, $BytPrRcrdBZF) == $BytPrRcrdBZF or die "shor +t read EQ\n"; $eq1b_a= unpack(" A2 ", $rcrd1); $eq1b_n= unpack(" n ", $rcrd1); last if(($eq1b_a eq 'XX') || ( $eq1b_n == 0xFFFF)); #XX indica +tes end of entries ## Print report Equipment List $doneEq = prntHdEQ($tblEnt); $~ = "BLANK_LINE";write; } ##Setting up Line Item Table & Message Table files and Info open (LIT, "< $tPI{$tblEnt}{litbl}") or die "Error opening Line Item Table: $!\n"; open (MST, "< $tPI{$tblEnt}{mstbl}") or die "Error opening Message Table: $!\n"; binmode(LIT); # Read Line Item table in binary mode binmode(MST); # Read Message Table in binary mode getLItTblInfo($tblEnt); getMsgTblInfo($tblEnt); ## Loop thru Data entries in BZ file for current test run for(my $doneData=0; (tell BZF)<$tstE; ){ read(BZF, $rcrd1, $BytPrRcrdBZF) == $BytPrRcrdBZF or die "shor +t read Data\n"; last if (unpack(" n ", $rcrd1) == 0xFFFF); #FF indicates end o +f data segment ## Print report Data $doneData= bZData($tblEnt); last if $doneData; } &page; #print blank lines till $- =0; $%=0; #TRying to clean up for next test $-=0; #TRying to clean up for next test select($ofh); close OUTF; } } close BZF or die "Could close file $!\n"; }

Replies are listed 'Best First'.
Re: Format _top
by apl (Monsignor) on Apr 30, 2008 at 14:43 UTC
    I don't see $^ ($FORMAT_TOP_NAME) defined anywhere, so the header doesn't seem to have been defined before you started printing.

    For that matter, I don't see a number of subroutines you call, so for all I know you're resetting formatting variables to 0 or null.

    What you should try to do is produce the minimal program that causes your problem. You can write out a constant string rather than producing real data. But a minimal program will help you (and us) identify the problem.

      OUTF_TOP (file name with _TOP) is the default. I tried to declaring it with $^ it didn't help. I put all my file control in this loop to minimize the confusion with multiple files. All my subroutines use write and I only change $~ for the format. I had some "print" statements and changed $- but I removed all the print statements to insure they were not creating the problem. I will try and strip down the program and reproduce the problem...
        The point I'm trying to make is that I didn't know that was the default. Neither did I know it existed or was set or was defined. (I assume it's a valid format?)
        I tried to declaring it with $^ it didn't help.

        I don't understand. If you didn't populate the parameter, of course you won't see a header. Was a compilation error produced? If so, what was it?

        The minimum program just involving the format definitions, the setting of parameters, etc. (as well as use strict and use warnings) will give you a proof of concept, an easier situation to debug.

Re: Format _top
by Narveson (Chaplain) on Apr 30, 2008 at 18:47 UTC

    Perl formats are controlled by built-in global variables. But global variables are deprecated. Where does this leave formats?

    Formats should be deprecated as well. Conway for one, after a sympathetic discussion of their advantages and drawbacks in Perl Best Practices, concludes with

    Don't use formats.

    If you are used to formats and you get good results with them, this advice does not apply to you. But the current thread suggests otherwise.

    So what are the alternatives?

    Maybe something from the CPAN. Consider Perl6::Form.

    If your needs are as simple as they sound, maybe you'll enjoy just doing it yourself:

    my $page_heading; my @details; my $DETAILS_PER_PAGE; my $FORM_FEED_CHARACTER; PAGE: while (@details) { print $page_heading; DETAIL: for (1..$DETAILS_PER_PAGE) { print shift @details; } print $FORM_FEED_CHARACTER; }