Re: Numeric or character ?
- From: anno4000@xxxxxxxxxxxxxxxxxxxxxxx (Anno Siegel)
- Date: 27 Jul 2005 11:56:27 GMT
Sven-Thorsten Fahrbach <sven-thorsten.fahrbach@xxxxxxx> wrote in comp.lang.perl.misc:
> On 27 Jul 2005 10:00:22 GMT
> anno4000@xxxxxxxxxxxxxxxxxxxxxxx (Anno Siegel) wrote:
>
> > Sven-Thorsten Fahrbach <sven-thorsten.fahrbach@xxxxxxx> wrote in
> comp.lang.perl.misc:
[...]
> > > I'm just being interested: is there any specific reason why you make
> > > output unbuffered here? I think it's not necessary for this script but
> > > maybe something has escaped me...
> >
> > 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.
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.
.
- Follow-Ups:
- Re: Numeric or character ?
- From: Sven-Thorsten Fahrbach
- Re: Numeric or character ?
- References:
- Numeric or character ?
- From: John Cecere
- Re: Numeric or character ?
- From: Sven-Thorsten Fahrbach
- Re: Numeric or character ?
- From: Anno Siegel
- Re: Numeric or character ?
- From: Sven-Thorsten Fahrbach
- Numeric or character ?
- Prev by Date: Re: Wget-like for Perl
- Next by Date: Re: OT: Social responsibility when writing HTML
- Previous by thread: Re: Numeric or character ?
- Next by thread: Re: Numeric or character ?
- Index(es):
Relevant Pages
|