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

Hi, I'd like to be able to launch a program in Perl (such as R) and then I'd like to be able to send commands to that program, and then obtain the output in Perl (launching the program and obtaining its output aren't the biggest challenge since I have been able to launch programs using Win32::Process::Create and to obtain the output as long as I can write to the program I should be able to have it output to a text file--the biggest challenge has been to write something to the program once it's been launched). I know this is probably possible in unix, but I'd like to be able to do this in windows. Thanks very much. Al
  • Comment on launch a program and then write to the program

Replies are listed 'Best First'.
Re: launch a program and then write to the program
by Corion (Patriarch) on Jul 14, 2008 at 07:08 UTC
Re: launch a program and then write to the program
by moritz (Cardinal) on Jul 14, 2008 at 07:09 UTC
    You can open a pipe to that process:
    open my $x, "|-", "R" or die "Can't launch R: $!"; pritn $x $your_data;

    If you also need to capture STDOUT, IPC::Open2 or IPC::Open3 might what you want.

      hi, Thanks for the code. I tried opening a pipe to the R.exe program, and then attempted to write to it using: my $program = "C:\\Program\ Files\\R\\R-2.7.1\\bin\\R.exe"; open my $x, "|-", "$program" or die "Can't launch R: $!"; $your_data="hello"; print $x $your_data; I obtained an error: Fatal error: you must specify '--save', '--no-save' or '--vanilla' I also tried the above code replacing the R.exe with the Rgui.exe. Although I didn't receive an error, I didn't obtain the output to the Rgui window. Further, using this option, perl doesn't continue past the point where the program is called (this is probably why I receive no error). I would like to be able to call the program, send some commands to it, obtain its output and then terminate the program. Could you please suggest where I can modify my code to make this work? Thanks! Al
        I obtained an error: Fatal error: you must specify '--save', '--no-save' or '--vanilla'

        So did you specify any of these options to R.exe? what was the result?

        First learn how to use your programs from the command line, and when you got that try to automate that with perl.

Re: launch a program and then write to the program
by zentara (Cardinal) on Jul 14, 2008 at 12:59 UTC