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

Odd, unexpected results. With the following code, I get the expected output to my display with every-10,000th record. However, if I replace "\n" (newline) with "\r" (return), I get nothing showing until the last cycle, which in this case is 350,000. Any ideas why "return" won't display, but "newline" does?
while(<IN>) { if($. % 10000 == 0) { print "Now working on record $.\n"; } }
Thanks, Duane

Replies are listed 'Best First'.
Re: Newline vs Return
by BrowserUk (Patriarch) on Oct 07, 2005 at 20:40 UTC

    Disable buffering on STDOUT by adding $| = 1; somewhere close to the top of your program.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.
      Thanks, BrowserUK. That works! Duane
        That works!

        In case you were curious about why it works: filehandles are STDOUT is line-buffered by default. This means that when you print, the output is buffered until (1) the buffer fills up or (2) you print a newline. Disabling buffering makes it output right away.

        Update: correction as per ikegami's reply

Re: Newline vs Return
by ikegami (Patriarch) on Oct 07, 2005 at 21:09 UTC

    By the way, flushing/autoflushing wasn't needed before because there's special code tied to STDOUT which causes it to flush when a "\n" is encountered. It's a special feature which allows it to work as expected most of the time.

    More on flushing and auto-flushing file handles