I have been experiencing some odd behaviour that I've tracked down to a print statement (I think). Essentially, the print statement (I/O buffer?) doesn't output specific lines. Every line after the first line in fact although it always prints the first line. When run against different data sets, the loop will not drop lines consistently from set to set but will consistently drop data from the same data set on multiple runs.

The data is output to file if a filename is specified on the command line and to STDOUT if no filename is given.

The problem seems to occur only when I specify the file name on the command line. It does not occur if when printing to STDOUT. I can also correctly redirect the output from STDOUT into a file.

I've tried ...
Additional info & suggestions from various monks ...
System specs ...
Relevent code
if($db01->FetchRow()){ open my $fh,">",$cfg->{outfile} if(defined($cfg->{outfile})); unless($fh){ warn "Unable to open output file $cfg->{outfile}\n$!\nUsing ST +DOUT\n" if(defined($cfg->{outfile})); $usestdout = TRUE; $fh = \*STDOUT; } do{ ++$recnum; printf("\b\b\b\b\b\b\b\b\b%-9s",$recnum) if($cfg->{displaycoun +t}); @obrec = $db01->Data(); $obrec[$_] = sprintf("%-*s",$oblen03[$_],($obrec[$_] || '')) f +oreach (0..$#obrec); $obstr = join("\t",@obrec); # some troubleshooting attempts # print STDOUT "$obstr\n" if($obstr =~ /[[:cntrl:]]+/); #checki +ng for control chars in rec # print STDOUT "$obstr\n" if($obstr =~ /[[:^print:]]+/); #check +ing for unprintable chars in rec # print STDOUT "$obstr\n"; print $fh "$obstr\n"; }while($db01->FetchRow()); close($fh) unless($usestdout); }

At this point, I'm stumped. Any suggestions will be most appreciated.

Update01: added some additional info and the results of some monk suggestions.

Update02: I solved it. Read operator error :o( Under certain circumstances, the program makes a a second pass through the loop. The open statement then clobbers the file and only inputs a portion of the remaining records. The open mode should be append ">>" not create ">". Thanks for all the great suggestions though. I learned a few things.

PJ
use strict; use warnings; use diagnostics;

In reply to Odd behaviour from print statement by periapt

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.