Re: Numeric or character ?



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.
.



Relevant Pages

  • Re: Can I put a class in eval block
    ... >> Anno Siegel wrote: ... >>> the script can be run standalone. ... > What's in your BEGIN block doesn't look much like a class definition. ... > the broken "Reply" link at the bottom of the article. ...
    (comp.lang.perl.misc)
  • Re: Perl to move files by specific size
    ... I have writed the script but having difficulties in stoping the move if ... Anno Siegel wrote: ... >> the script I am looking for is to stop moving if there is a file found ... > the broken "Reply" link at the bottom of the article. ...
    (comp.lang.perl.misc)
  • Re: Creating script support for console application
    ... After mapping the file into memory, you may want to allocate a new buffer ... Of course, a decent script will keep track of line-numbers, so syntax errors ... > commands that tell the program where to BREAK, LOOP, START. ... > thinking of putting each line in an element of an array. ...
    (microsoft.public.vc.language)
  • pyodbc.Error Crash
    ... I believe this bug is also related to the other problem I just reported. ... DB = Microsoft Access XP ... # or buffer length ') ... The broke.py script has 4 select statements but only one is executed. ...
    (comp.lang.python)
  • Re: .Net Equivilant for sqlcmd or osql
    ... applications like SQL Management Studio work. ... Also, you do not have to strip the comments, it works just fine, ... Read EACH line into a string buffer. ... Read your script file and split it on the GO ...
    (microsoft.public.dotnet.languages.csharp)