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

Basically I have a server program that I'm trying to admin via perl. When you run the server, it takes over the console and provides it's own input/output, ignoring the shell. If that wasn't clear, perhaps this example might help. I invoke the server, it prints some status information then waits for input. I can type various commands to it, such as status and others, then it returns the information and awaits more input. It's basically an infinite read loop. My question was, how can I best interface with this program via perl?

By interface I mean read the data it prints, make some decisions and then "type in" more commands, read the data, repeat.

I of course tried some simple versions such as just `server` but of course the server program never finishes until you manually kill it, so the `server` statement doesn't return until the servers dead. I messed around a bith with a few other forms of open, but as far as I know I need both reading and writing, which plain old open doesn't do.

Replies are listed 'Best First'.
Re: Best way to communicate with a shell like process?
by Zaxo (Archbishop) on Aug 24, 2003 at 06:50 UTC
Re: Best way to communicate with a shell like process?
by bobn (Chaplain) on Aug 24, 2003 at 06:54 UTC

    IPC::Open2 or IPC::Open3 might work. Expect.pm would be another option, especially good with programs that really, really want to talk to a terminal. And if you want to do it remotely, Net::Telnet would also be an option.

    --Bob Niederman, http://bob-n.com

    All code given here is UNTESTED unless otherwise stated.

Re: Best way to communicate with a shell like process?
by BUU (Prior) on Aug 24, 2003 at 06:43 UTC
    On further thought, I think the key issue is that I wish to run this program and then "take over" it's stdin/stdout, so the perl script can read/write to them instead of the console it provides.
      Yes - that is what the IPC::Open's do.

      Basically, they start the process (server in your case), then redirect the STDIN and STDOUT such that they go through your perl script. You get full control of content in and out.

      It is not clear from your post whether the "server" script is started separately. The IPC::Opens require that the perl script start the "server" program.

      Also, you use the therm "console" loosely. Most likely, you are referring the the STDIN, STDOUT and STDERR of the "server" script. Please use this terminology for future posting. The "Console" referrs to the window (or terminal) in which the script runs. Hence, you can REDIRECT the STDOUT to the console, and receive STDIN from the console, but they are not to be confused with the console itself.

Re: Best way to communicate with a shell like process?
by duelafn (Parson) on Aug 24, 2003 at 22:26 UTC
    I use IPC::Run which has better documentation and other features.