On Mon, 31 Mar 2003, Rob Dixon wrote:

> Janek Schleicher wrote:
> > Rob Dixon wrote at Sat, 29 Mar 2003 12:59:22 +0000:
> >
> > >     use IO::Handle;
> > >     autoflush STDOUT;
> > >     autoflush STDERR;
> > >
> > >     print STDERR "STDERR\n";
> > >     print STDOUT "STDOUT\n";
> > >
> > > so that the output you see is in the order it happens. Without the
> > > autoflush calls ( or even with just $| = 1 ) the above program will
> > > output
> > >
> > >     STDOUT
> > >     STDERR
> >
> > The following snippet:
> > #!/usr/bin/perl
> >
> > $| = 1;
> > print STDERR "STDERR\n";
> > print STDOUT "STDOUT\n";
> >
> > $| = 0;
> > print STDERR "STDERR\n";
> > print STDOUT "STDOUT\n";
> >
> > has the output
> >
> > STDERR
> > STDOUT
> > STDERR
> > STDOUT
> >
> > and not
> > STDOUT
> > STDERR
> > ...
> 
> Hi Janek.
> 
> perldoc perlvar:
> 
>     [$|'s] default is 0 (regardless of whether the channel is really
>     buffered by the system or not; "$|" tells you only whether you've
>     asked Perl explicitly to flush after each write).
> 
> In this case it looks like your system may flush the buffers anyway,
> this code:
> 
>     $| = 1;
>     print STDERR "STDERR\n";
>     print STDOUT "STDOUT\n";
> 
> sets autoflush only on STDOUT and then prints to both filehandles.
> If STDERR were buffered then 'STDOUT' would be flushed first
> and appear as the first line. If you disable autoflush altogether
> then the result depends on which stream the system flushes for
> you first. Try reversing the 'print' statements in your program
> to see if it still prints STDERR first without any autoflush setting.

The above sequence of print statements will print STDERR and STDOUT in 
that order. Both STDERR and STDOUT are line buffered, when a "\n" is seen
the contents of the buffer is flushed. Turning on autoflush is superfluous
in this case. Try this instead :-)

#!/usr/local/bin/perl
use strict;
use warnings;

select (STDERR);
$| = 1;
print STDOUT "STDOUT";
print STDERR "STDERR";


> 
> Cheers,
> 
> Rob
> 
> 
> 
> 
> 


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to