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

Hi, I start working with <format> to produce some report, all is nice however in final spool records are in other order, completely different then they were done in script. Total confuse , looks like a bug for me. I have series of <format> handls pointing to the same file, I also do <print> to the same file. I figured it out that I can solve this doing OPEN/CLOSE for each <write>, but probably it would be too much. I post my test script, expecting output file records be in order of $cc.
Appreciate you advice, I looked at reporting method too and might use them later, but for now I need to solve this problem to get good sleep. Really can't understand, what is behind this <write>, where data is actually written to >>outfile or some temp storage and then all synced at the end? Or iti could be because I use very same file '>>spool.dat' for all filehandles. Is there any COMMIT in Perl. I'm new to Perl, really blown away how compact and powerful it is, but need more system knowledge. Any info appreciated, here is my code below:
Best to all monks Trent P.S. I'm testing this on WindowsXP with latest ActivePerl.
#!/usr/bin/perl use strict; use warnings; my $cc="1"; unlink ('spool.dat'); open (OUT_LOG, '>>spool.dat'); open (OUT_HEADER, '>>spool.dat'); open (OUT_DETL, '>>spool.dat'); open (OUT_FOOT, '>>spool.dat'); open (OUT_BR, '>>spool.dat'); #========================================================= format OUT_HEADER= @<<<==== f0rmat HEADER HEADER HEADER Page : @<<< $cc, $cc . format OUT_DETL= @<<<======== f0ramt detail line ======================= $cc . format OUT_FOOT= @<<<======== f0rmat ===========FOOTER $cc . format OUT_BR= @<<<========= fOrmat BREAK $cc . #===================================================== write OUT_HEADER; $cc++; write OUT_DETL; $cc++; write OUT_DETL; $cc++; write OUT_DETL; $cc++; write OUT_BR; $cc++; print OUT_LOG "$cc ................print after 1st break \n"; $cc++; write OUT_DETL; $cc++; write OUT_DETL; $cc++; write OUT_DETL; $cc++; write OUT_BR; $cc++; print OUT_LOG "$cc ...............print after 2nd break \n"; $cc++; write OUT_FOOT; $cc++; print "basta \n";

Replies are listed 'Best First'.
Re: <write format> records order error
by JavaFan (Canon) on Jan 25, 2010 at 08:46 UTC
    You have different filehandles, each with a buffer. Either make all the filehandles unbuffered (select them, then set $| = 1;), or just use a single filehandle, and use the $~ variable to switch formats.
      Tx, JavaFan for lead. I tried to use var in filehandle, but alwayse has an error: Can't use string ("OUT_HEADER") as a symbol ref while "strict refs" in use: what I'm missing now?
      my $OUT_WWW="test"; open ('$OUT_WWW', '>>spool.dat'); #======================================================= format OUT_HEADER= @<<<==== f0rmat HEADER HEADER HEADER Page : @<<< $cc, $cc . format OUT_DETL= @<<<======== f0ramt detail line ============= $cc . #======================================================== $OUT_WWW="OUT_HEADER"; write "$OUT_WWW"; $cc++;
        '$OUT_WWW' is a string starting with a dollar sign. There's no interpolation.

        But even if there were, I don't know what you are trying to do here.

      Tx, JavaFan for lead. I tried to use var in filehandle, but alwayse has an error: Can't use string ("OUT_LOG") as a symbol ref while "strict refs" in use: what I'm missing now?
      my $OUT_WWW="test"; open ('$OUT_WWW', '>>spool.dat'); #======================================================= format OUT_HEADER= @<<<==== f0rmat HEADER HEADER HEADER Page : @<<< $cc, $cc . format OUT_DETL= @<<<======== f0ramt detail line ============= $cc . #======================================================== $OUT_WWW="OUT_HEADER"; write "$OUT_WWW"; $cc++;