in reply to Re^3: Executing command from perl script with input/output
in thread Executing command from perl script with input/output

Zentara,

Thanks four your reply. After adding your changes to my script, I noticed that READ and ERROR do not have any data at all. This is weird because soon after I execute the binary (from a command line) it prompts with a list of choices to choose from. My suspicion is that the binary only sends out data if its STDOUT is connected to a terminal. I am not sure if open3 provides any connection to a terminal..

I tried the write at the beginning of the program (as above) but neither of $answer or $error was populated.

The pipe method you mention is what I've been using until now. I wanted to add more error checking by examining the choices the program gives the user before writing data back to its STDIN.

Thanks for your time!

  • Comment on Re^4: Executing command from perl script with input/output

Replies are listed 'Best First'.
Re^5: Executing command from perl script with input/output
by zentara (Cardinal) on Aug 10, 2005 at 11:30 UTC
    Hi, I have run into that "terminal" problem with a few scripts before. What I end up doing is the following "trick". Instead of opening $cmd with IPC::Open3, open "/bin/sh", then print $cmd to it.
    my $pid=open3(\*WRITE, \*READ, \*ERROR, '/bin/bash'); print WRITE $cmd;
    That usually works, because you are simualting a terminal.

    I'm not really a human, but I play one on earth. flash japh
      Thanks Zentara,

      I tried the method you suggested above, but this still doesn't work for me :-( After writing the $cmd to WRITE, I created two variables $selread and $selerror (like earlier) - one each to read from READ and ERROR. A read on both of these did not yield anything at all.

      I want to use a debugger to see what is happening, but my program is split over several modules, and I am not sure how to use the debugger for something that spans over many modules.

      Regards,
      ravi

        Oops, I meant to change the $cmd to "$cmd\n", but I hoped you would figure that out. Remember, the bash shell needs a newline (or enter) to start executing the command. Maybe even use 2 print statements, one to print $cmd, and one to print the code for enter(a newline). Additionally you may need to set $|=1; and maybe even try using syswrite instead of print. If none of those things work, you can try to setup a pseudo-tty, which I find quite difficult. See IO::Pty.

        If all fails you can always fall back to using the old standby "expect" instead of IPC::Open3.

        Quite often, it is some little tiny error you are making which prevents you from getting output, even if you have it setup right, and it usually involves buffering and flushing, so the $|=1 is important. If buffering is the problem, you might see the output as you quit the program.


        I'm not really a human, but I play one on earth. flash japh