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

should be able to use scalarref filehandles? #56

Open karenetheridge opened 5 years ago

karenetheridge commented 5 years ago

I'm not sure if I've run into a limitation of perl or if I'm just doing something boneheaded, but I expected this to work (this is an effort to avoid the use of a tempdir on a read-only filesystem):

use strict;
use warnings;
use Capture::Tiny 'capture';

my $out = '';
my $err = '';
open my $out_fh, '+>', \$out or die "cannot open to scalarref: $!";
open my $err_fh, '+>', \$err or die "cannot open to scalarref: $!";
capture {
    print STDOUT "foo\n";
    print STDERR "bar\n";
} stdout => $out_fh, stderr => $err_fh;

However, I get:

Error from open(GLOB(0x7fca5e810ed0), >&-1): Invalid argument at /Users/ether/.perlbrew/libs/26.3@std/lib/perl5/Capture/Tiny.pm line 107.
Capture::Tiny::_open(GLOB(0x7fca5e810ed0), ">\x{26}-1") called at /Users/ether/.perlbrew/libs/26.3@std/lib/perl5/Capture/Tiny.pm line 194
Capture::Tiny::_open_std(HASH(0x7fca5e810cc0)) called at /Users/ether/.perlbrew/libs/26.3@std/lib/perl5/Capture/Tiny.pm line 370
Capture::Tiny::_capture_tee(undef, undef, undef, undef, 1, 1, 0, 0, ...) called at stderr.pl line 12
haukex commented 5 years ago

Sounds to me like the -1 might be coming from fileno: Returns the file descriptor for a filehandle, or undefined if the filehandle is not open. If there is no real file descriptor at the OS level, as can happen with filehandles connected to memory objects via open with a reference for the third argument, -1 is returned. I don't have any time right now to look into it in more detail, but maybe this is a start...