Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code in IPC::Open3 doc doesn't work #22608

Closed
ikegami opened this issue Sep 18, 2024 · 8 comments · Fixed by #22615
Closed

Code in IPC::Open3 doc doesn't work #22608

ikegami opened this issue Sep 18, 2024 · 8 comments · Fixed by #22615

Comments

@ikegami
Copy link
Contributor

ikegami commented Sep 18, 2024

Where

IPC::Open3

Description

The synopsis contains this code. It doesn't work. No output ends up in output.txt.

open my $outfile, '>>', 'output.txt' or die "open failed: $!";
my $pid = open3('<&STDIN', $outfile, undef,
                'some', 'cmd', 'and', 'args');

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.

@ikegami
Copy link
Contributor Author

ikegami commented Sep 18, 2024

Working version:

open local *OUTFILE, '>>', 'output.txt' or die "open failed: $!";
my $pid = open3('<&STDIN', '>&OUTFILE', undef, "printf", "foo\\n");

@ikegami
Copy link
Contributor Author

ikegami commented Sep 18, 2024

Bug first reported on StackOverflow

@guest20
Copy link

guest20 commented Sep 18, 2024

'>&' . fileno $outfile

... should do the job without needing the bareword handle

@haarg
Copy link
Contributor

haarg commented Sep 18, 2024

These docs were changed in ef395fa. The previous version didn't include any examples of doing this.

@guest20
Copy link

guest20 commented Sep 18, 2024

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

@ikegami
Copy link
Contributor Author

ikegami commented Sep 18, 2024

'>&' . 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.

@ikegami
Copy link
Contributor Author

ikegami commented Sep 18, 2024

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).

@guest20
Copy link

guest20 commented Sep 18, 2024

@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)

mauke added a commit to mauke/perl5 that referenced this issue Sep 20, 2024
Unfortunately, this feature is based on parsing out filehandle names
from strings, so it requires bareword filehandles to work.

Try to make this clearer in the documentation  and mention that
arguments starting with `>&` or `<&` are input parameters (i.e. existing
open handles to use) as opposed to regular output parameters (which
open3 connects to newly created pipes).

Fixes Perl#22608.
mauke added a commit that referenced this issue Sep 26, 2024
Unfortunately, this feature is based on parsing out filehandle names
from strings, so it requires bareword filehandles to work.

Try to make this clearer in the documentation  and mention that
arguments starting with `>&` or `<&` are input parameters (i.e. existing
open handles to use) as opposed to regular output parameters (which
open3 connects to newly created pipes).

Fixes #22608.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants