cpan-authors / IPC-Run

https://metacpan.org/pod/IPC::Run
Other
21 stars 38 forks source link

start \@cat, '<pipe', \*IN; doesn't work as advertised (doesn't work at all?) [rt.cpan.org #121383] #66

Open toddr opened 7 years ago

toddr commented 7 years ago

Migrated from rt.cpan.org#121383 (status was 'new')

Requestors:

From peter@morch.com on 2017-04-26 05:11:35:

The perldoc has this example :

    @cat = ( 'cat' );
    ....
    $h = start \@cat, '<pipe', \*IN;
    print IN "hello world\n";
    pump $h;
    close IN;
    finish $h;

But it doesn't work... It hangs and never finishes. Swapping the pump and close lines, works, though. Like this:

    $h = start \@cat, '<pipe', \*IN;
    print IN "hello world\n";
    close IN;
    pump $h;
    finish $h;

I think I understand that - cat doesn't print until STDIN has been closed (although that doesn't jive with experiments in a normal terminal)

So then I tried this: Here I just need a single line of input. It also doesn't work:

    my $bashScript = <<'END';
    echo starting - Enter a line and press enter:
    read LINE
    echo Got line: $LINE
    echo ending
    END

    my $h = start ['bash', '-c', $bashScript], '<pipe', \*IN;
    print IN "hello world\n";
    pump $h;
    close IN;
    finish $h;

I don't actually understand why that doesn't work, when this does (using a scalar and '<' instead of a glob and '<pipe'):

    my $bashScript = <<'END';
    echo starting - Enter a line and press enter:
    read LINE
    echo Got line: $LINE
    echo ending
    END

    my $in = '';
    my $h = start ['bash', '-c', $bashScript], '<', \$in;
    $in .= "hello world\n";
    pump $h;
    finish $h;

However this does work. I can't explain that from reading the perldoc...

    my $bashScript = <<'END';
    echo starting - Enter a line and press enter:
    read LINE
    echo Got line: $LINE
    echo ending
    END

    my $h = start ['bash', '-c', $bashScript], '<pipe', \*IN, '>', sub {
print @_ };
    print IN "hello world\n";
    pump $h;
    close IN;
    finish $h;

Using IPC::Run version: 0.95, perl v5.20.2, debian jessie (stable).

-- Peter Valdemar Mørch http://www.morch.com

toddr commented 6 years ago

@pmorch Are you wanting a docs patch here?

pmorch commented 6 years ago

Yes. Most likely.

At least the documented examples should work. So I'm asking for at least a documentation patch to fix that.

As I write, I don't understand the differences between the various scenarios above. Educating me on the details of terminal caching (e.g. why the order of pump and close matters) is perhaps not the purpose of the IPC::Run perldoc or the bug tracker, but if there is a simple explanation or, perhaps less likely, a bug somewhere I (and future perldoc readers?) wouldn't mind to understand where I went wrong.