in reply to Re: Newline vs Return
in thread Newline vs Return

Thanks, BrowserUK. That works! Duane

Replies are listed 'Best First'.
Re^3: Newline vs Return
by revdiablo (Prior) on Oct 07, 2005 at 21:07 UTC
    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

      That's not quite true. Only STDOUT is line-buffered by default. In fact, only STDOUT is ever line-buffered(*). I verified this with the following snippet:

      perl -e "open(FH, q{>test}); print FH (qq{a\n}); sleep(5); print FH (q +q{a\n});"

      The lines only appeared after the 5 seconds were over.

      (*) Maybe XS or PerlIO can change this? Of course, tie could.

        Actually, any filehandle connected to a TTY is line buffered; this is stdio's default. If you connect STDOUT to something other than a TTY, it will be block buffered like any other filehandle. For example (on Unix, not sure what a TTY on Windows is):
        perl -e "open(FH, q{>/dev/tty}); print FH (qq{a\n}); sleep(5); print F +H (qq{a\n});"

        In older versions of Perl you could get line buffering with IO::Handle's setvbuf method, but it looks like that's no longer supported.

        See the stdio function isatty and perl's -t test for more information about how a TTY is detected.