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

Capture dies if one of STD handles has :via layer #17

Closed zdm closed 2 years ago

zdm commented 10 years ago

Hi, following code dies:

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

use Capture::Tiny;
use PerlIO::via::QuotedPrint;

binmode STDOUT, q{:via(QuotedPrint)};

my $res = Capture::Tiny::capture_merged {
    system q{ls};
};
print qq{$res\n};

1;
__END__
No package specified at /usr/local/perl-5.18.2/site/lib/Capture/Tiny.pm line 99.
Error from open(IO::Handle=GLOB(0x13386e8), >&STDOUT): Invalid argument at /usr/local/perl-5.18.2/site/lib/Capture/Tiny.pm line 99.
        Capture::Tiny::_open(IO::Handle=GLOB(0x13386e8), ">&STDOUT") called at /usr/local/perl-5.18.2/site/lib/Capture/Tiny.pm line 176
        Capture::Tiny::_copy_std() called at /usr/local/perl-5.18.2/site/lib/Capture/Tiny.pm line 346
        Capture::Tiny::_capture_tee(1, 1, 1, 0, CODE(0x1105f58)) called at ./1.pl line 22

Tested with perl 5.18.2 x64 on windows and linux.

dagolden commented 10 years ago

Looks like the handle needs to be localized if a :via layer is in place. I'll try to find some later this week to turn this into a test file and try a fix.

dagolden commented 10 years ago

Note to self: this may be a Perl bug but we still need to find a workaround and document the limitation. See https://rt.perl.org/Ticket/Display.html?id=121601

zdm commented 10 years ago

Maybe you need to drop handle to default layers configuration with, for example,

binmode STDOUT, ':raw';

before cloning.

dagolden commented 10 years ago

I'm more inclined to localize the way I do for tied handle, but I might try it both ways.