P
philkime
I seem to have hit a rather tricky issue with STDERR redirection and libraries used via XS and wonder if anybody has any ideas.
I have an XS module which uses an external library. This library uses stdio, not PerlIO and can't be changed. I need to capture STDERR that this library outputs. This can't be done using PerlIO tricks like opening to an in memory scalar I realise. On non-windows, I can redirect STDERR to a file and read it in later when the library calls are finished. This works fine. On windows, it doesn't work because apparently, as soon as you re-open STDERR to a file, it decouples the library STDERR from perl's STDERR and the library continues to write to its own STDERR and it seems impossible to capture it.
Example below. On non-Windows, the STDERR from the library comes between the "|" characters as it should. On Windows, it comes after "||" as it's not captured. Any ideas on how to capture this?
#!/bin/perl
use Text::BibTeX;
use Capture::Tiny qw(capture);
my $stderr;
my $bib_entry = <<'END';
@BOOK{test1,
AUTHOR = {Arthur Author},
TITLE = {Arranging Arguments},,
YEAR = {1935}
}
END
my ($stdout, $stderr) = capture {
my $entry = new Text::BibTeX::Entry $bib_entry;
};
print "|$stderr|\n";
exit 0;
I have an XS module which uses an external library. This library uses stdio, not PerlIO and can't be changed. I need to capture STDERR that this library outputs. This can't be done using PerlIO tricks like opening to an in memory scalar I realise. On non-windows, I can redirect STDERR to a file and read it in later when the library calls are finished. This works fine. On windows, it doesn't work because apparently, as soon as you re-open STDERR to a file, it decouples the library STDERR from perl's STDERR and the library continues to write to its own STDERR and it seems impossible to capture it.
Example below. On non-Windows, the STDERR from the library comes between the "|" characters as it should. On Windows, it comes after "||" as it's not captured. Any ideas on how to capture this?
#!/bin/perl
use Text::BibTeX;
use Capture::Tiny qw(capture);
my $stderr;
my $bib_entry = <<'END';
@BOOK{test1,
AUTHOR = {Arthur Author},
TITLE = {Arranging Arguments},,
YEAR = {1935}
}
END
my ($stdout, $stderr) = capture {
my $entry = new Text::BibTeX::Entry $bib_entry;
};
print "|$stderr|\n";
exit 0;