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

If I want to execute a program and read the output which is stored in $output, I run it like this:
my $output = `command <<EOF; yes EOF`; # print output from `command` after giving it 'yes' as input print $output . "\n";
If I want to write to a command using a filehandle and not see the resulting output I do this:
open(COMMAND, '| command') or die("Can't run command: $!\n"); # give program 'yes' as input print COMMAND 'yes';
But what do I do if I want to write to the command as easily as using a file handle and read whatever comes back as easily as a scalar? Essentially I want to do both of the examples above at the same time.

So I'd like to know how to:
1. Run the command
2. See what comes back and read it from $output
3. Make some decisions and print back to filehandle COMMAND
4. Make some more decisoins
5. Print back to COMMAND
6. etc...

Replies are listed 'Best First'.
Re: Interactively Read / Write to Program
by ikegami (Patriarch) on Jun 29, 2009 at 23:34 UTC
Re: Interactively Read / Write to Program
by Fletch (Bishop) on Jun 30, 2009 at 00:31 UTC

    If you're on an OS which has ptys see also Expect.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: Interactively Read / Write to Program
by rovf (Priest) on Jun 30, 2009 at 10:32 UTC

    In addition what has been said: In environments where for some reason neither the IPC packages nor Expect is feasible, you might want to have a look at Net::Telnet.

    -- 
    Ronald Fischer <ynnor@mm.st>

      Don't use telnet when you can have ssh.

      Telnet offers NO encryption, and all input and output, including the login process itself, depends completely on the implementation (i.e. OS and its configuration). Typical causes for errors are user-defined prompts, fortune cookies, and non-default shells. While it is possible to setup telnetd in a way that only access via the loopback adapter is allowed, it is hard to do so and it is easy to remove the restriction by accident.

      Ssh, on the other hand, is encrypted by default, the login process is independant from the I/O from/to the virtual terminal, and you can invoke a remote process without fiddling with the shell. It is possible (and recommended practice) to use certificates instead of passwords, so there is also no need to store a password in a script or configuration file.

      Of course, CPAN has SSH modules. At least one invokes the installed ssh binary, at least one implements the SSH protocol in Perl (and/or XS).

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
        Don't use telnet when you can have ssh.

        Correct - if I have encryption, and if encryption is important, ssh is the better way to go. There are many approaches, and it really depends on the concrete environment which one is best to use.

        -- 
        Ronald Fischer <ynnor@mm.st>