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, 5:05 pm |
| On 27 Jul 2005 11:56:27 GMT
anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) wrote:
> 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.
All right, that was enlightening, though I would not consider it a 'problem' exactly. If I come across a situation where non-autoflushed output might not be desireable, I print to STDERR or set $| = 1. I don't come from a profound C background, though, so
I might be a little ignorant ;-).
Excerpt from the Camel Book: 'Contrary to popular belief, setting this [$|] variable does not turn off buffering.'
Okey, my bad.
SveTho
| |
| Anno Siegel 2005-07-27, 5:05 pm |
| Sven-Thorsten Fahrbach <sven-thorsten.fahrbach@gmx.net> wrote in comp.lang.perl.misc:
> On 27 Jul 2005 11:56:27 GMT
> anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) wrote:
[Why do some set $| = 1 routinely? Demo how STDOUT and STDERR get out
of sequence if you don't]
> All right, that was enlightening, though I would not consider it a
> 'problem' exactly. If I come across a situation where non-autoflushed
> output might not be desireable, I print to STDERR or set $| = 1.
Sure, like everyone. The question was, why do it routinely. When working
on a program you don't want to see, say, the error message it died with
in front of material it has produced before it died.
There are other reasons to use it. If you want to watch a program's
file output through "tail -f ...", it will look jerky unless autoflushed.
Also, if you do an improvised user dialog through STDOUT and STDIN,
users may not see your prompt on some systems unless you autoflush
STDOUT. So it also makes a program more portable. Under normal
circumstances it doesn't hurt much. The exception is mass output
which becomes inefficient. It's the only reason I can think of to
turn it off.
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.
|
|
|
|
|