dagolden / Capture-Tiny

(Perl) Capture STDOUT and STDERR from Perl, XS or external programs
http://search.cpan.org/dist/Capture-Tiny/
39 stars 19 forks source link

Leaves STDERR in line buffered mode #63

Open grr opened 9 months ago

grr commented 9 months ago

After using capture(), STDERR is left in line buffered mode, i.e. it is no longer "hot". You have to manually set $| to restore unbuffered mode.

Example program demonstrating the issue:

#!/usr/bin/env perl
use v5.10;
use strict;
use warnings;
use Capture::Tiny qw(capture);

print STDERR __LINE__;  # prints immediately as expected

# my $out = `date`;  # stderr is fine if using this line instead of the next
my ($out, $err) = capture { system 'date' };  # leaves stderr in buffered mode
say $out;

# $| = 1, select $_ for select STDERR;
# won't print unless prev line is uncommented or script exits normally.
print STDERR __LINE__;
sleep 10;
grr commented 9 months ago

using binmode(STDERR, ':unix'); is probably better here than $|=1, because although STDERR is initially unbuffered, $| is unset.