jim_g has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I've run into a situation where I need asynchronous access to the STDIN and STDOUT of a child process. In C, I'd use popen(), but this seems to have been omitted from perl :-)

The problem is that I need to feed a stream of data to the child, but at the same time listen for messages coming back from the child and respond to them by inserting replies into the outgoing data stream.

open2() and open3() won't help, as all input is required before any output is sent.

Any ideas?

Thanks,

Jim

Replies are listed 'Best First'.
Re: Simulating popen()
by merlyn (Sage) on May 24, 2001 at 18:46 UTC
    popen(3) is indeed available in Perl. It's just spelled o-p-e-n. Your standard open with a vertical bar at the beginning or the end maps to popen, effectively. And as I recall, the standard popen(3) was also one way or the other, and not both ways as you wanted.

    So you wanted something beyond popen that does bidirectional opens. And the other messages in this thread are describing them to you, so I won't repeat that.

    -- Randal L. Schwartz, Perl hacker

      Um, yeah, naturally after pressing the <submit> button I realised that what I really wanted to use was socketpair(), fork() and exec(), and now I'm romping away.

      Jim the Embarrassed

Re: Simulating popen()
by clintp (Curate) on May 24, 2001 at 18:38 UTC
    I'd use the Expect module. Only because if you're interacting with this external process, there's lots of nitpicky details that Expect will take care of for you. This frees you to think about the conversation and not worry too much about buffering, deadlock, ptys, etc..