Open ikegami opened 1 day ago
Working version:
open local *OUTFILE, '>>', 'output.txt' or die "open failed: $!";
my $pid = open3('<&STDIN', '>&OUTFILE', undef, "printf", "foo\\n");
'>&' . fileno $outfile
... should do the job without needing the bareword handle
These docs were changed in ef395fac90bd16d1d2813616a67dec27ea659678. The previous version didn't include any examples of doing this.
I am always suspicious of "there's a bug in perl", "the other way is ugly" and "i'll do it myself" comments
At the first glance this comment is checking all the boxes:
# simulate autovivification of filehandles because
# it's too ugly to use @_ throughout to make perl do it for us
# tchrist 5-Mar-00
'>&' . fileno $outfile
... should do the job without needing the bareword handle
I don't know if the bug has been fixed, but that has not always been the case. It appears to work, but--IIRC--a close fails, which can cause issues--EOF never reached?--in some situations.
I allude to this problem in this post from 2012:
There is a way of passing lexical file handles (or rather its descriptor), but there's a bug concerning them, so I always use package variables with
open3
.
I am always suspicious of "there's a bug in perl", "the other way is ugly" and "i'll do it myself" comments
As you should be. But rather than making such a comment, you should have tested it like I did.
$ perl -e'
use strict;
use warnings;
use IPC::Open3 qw( open3 );
open my $outfile, ">>", "output.txt" or die "open failed: $!";
my $pid = open3( "<&STDIN", $outfile, undef, "printf", "foo\\n" );
waitpid( $pid, 0 );
'
$ ls -l
total 0
-rw-rw---- 1 ikegami ikegami 0 Sep 18 09:23 output.txt
I've answered "hundreds" of questions about open3
on PerlMonks and StackOverflow, and I've even fixed a bug in the module.
The documentation doesn't reflect the behaviour of the module (past or present).
@ikegami Sorry I wasn't clear, I was talking about the comment I quoted (from IPC::Open3), not the reproducibility of your issue.
I did run the example you gave (from the docs) and it did the thing you said it would (leaving the file untouched) so I didn't have much to add on that topic (especially when I only tested it on BSD with 5.20.2 and your issue talked about windows too)
Where
IPC::Open3
Description
The synopsis contains this code. It doesn't work. No output ends up in
output.txt
.Passing a lexical variable containing a file handle to
open3
has never worked. At least not on non-Windows systems. Never tried it on a Windows system which uses a very different implementation.