For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > July 2005 > Re: Numeric or character ?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: Numeric or character ?
Sven-Thorsten Fahrbach

2005-07-27, 9:03 am

On 27 Jul 2005 10:00:22 GMT
anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) wrote:

> Sven-Thorsten Fahrbach <sven-thorsten.fahrbach@gmx.net> wrote in comp.lang.perl.misc:
>
> Autoflushing (which is more to the point than "unbuffered") isn't
> exactly needed here, but it's convenient to have. Its main purpose
> is to match the flushing behavior of STDOUT and STDERR so that messages
> on both appear on the screen when they are printed, not when a buffer
> happens to overflow. Otherwise, error messages and normal output may
> appear out of sequence, which can be confusing.
>
> I put "$| = 1" in all my scripts (not modules), and only take it out
> when the script happens to do mass IO via STDOUT (as in a filter).


Okay, I know what it does, I just wasn't aware that this was kind of a custom among some programmers. When I need unbuffered output I let it go to STDERR usually which is unbuffered (or autoflushed if you prefer) by default.
Anno Siegel

2005-07-27, 9:03 am

Sven-Thorsten Fahrbach <sven-thorsten.fahrbach@gmx.net> wrote in comp.lang.perl.misc:
> On 27 Jul 2005 10:00:22 GMT
> anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) wrote:
>
> comp.lang.perl.misc:


[...]

>
> Okay, I know what it does, I just wasn't aware that this was kind of a
> custom among some programmers. When I need unbuffered output I let it go
> to STDERR usually which is unbuffered (or autoflushed if you prefer) by
> default.


It's not just preference. An autoflushed IO channel is still buffered,
the buffer just doesn't fill up so much. A truly unbuffered channel
would be very hard to use.

Otherwise, the purpose of "$| = 1" isn't that we want STDOUT autoflushed,
it's that there already *is* autoflushed output that merges with it,
and the merging is smoother when both are autoflushed.

The problem is often not visible when printing directly to the terminal,
because terminal output is usually line buffered (another form of automatic
flushing). When the output goes to a file or pipe, to "| more" for
instance, the difference becomes apparent. Watch this:

[anno4000@lublin ~/clpm]$ cat script
#!/usr/bin/perl
use strict; use warnings;

$| = shift || 0;

print "one\n";
warn "two\n";
print "three\n";

[anno4000@lublin ~/clpm]$ ./script |& more
two
one
three

4000@lublin ~/clpm]$ ./script 1 |& more
one
two
three

Only in the second (autoflushed) example does the output appear in
the sequence it is produced. That is the problem "$| = 1" avoids.

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com