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

I am using the following script to run an external program:
use IPC::Open2; my $infile = shift; open (INFILE, "<$infile"); my $output; my $input; { local $/ = undef; $input = <INFILE>; } my $pid = open2(*README, *WRITEME, 'progam.exe'); print WRITEME "$input"; { local $/ = undef; $output = <README>; } close(README); close(WRITEME); waitpid($pid, 0); print "$output";
When I start the script it starts the program and, but does not output anything. Only when I kill the process of the external program through the windows task manager, the (correct) output is printed to the screen. Do I somehow have to end the process in the script? When I run the program just from the command line with input on standard input, it prints it's results and exits.

Replies are listed 'Best First'.
Re: IPC:Open2 question
by pbeckingham (Parson) on Jul 13, 2004 at 16:45 UTC

    Consider using the Expect module to do this. It eliminates the problems you are experiencing right now, and allows you to handle many cases that your code is not.

Re: IPC:Open2 question
by holo (Monk) on Jul 13, 2004 at 17:25 UTC

    You need to close WRITME; immediately after print WRITEME "$input";. If you do not close the input pipe, the program will wait for more input for ever.

      No, that doesn't change anything. Thanks anyway!
Re: IPC:Open2 question
by keszler (Priest) on Jul 13, 2004 at 17:20 UTC
    waitpid PID,FLAGS
    Waits for a particular child process to terminate and returns the pid of the deceased process, or -1 if there is no such child process.

    (Also, note that the flag 0 indicates a blocking wait. On systems that support it the WNOHANG flag causes an immediate return if the child pid is not dead.)

      Yes, I understod what waitpid does (I think), but why does the process not terminate? That's why I said it terminates ok when just run from the shell... The WNOHANG flag does not change anything.
        I did some testing using an ancient DOS port of "od" as my program.exe. On a win98 box running perl 5.8.3, closing WRITEME immediately after printing to WRITEME fixes the problem.

        On a win2k box with perl 5.8.4, running the "fixed" program locks up until "od" is manually killed. However, the problem occurs not at the waitpid command, but at the $output = <README>; command.

        Try using "perl -d:ptkdb perlprog inputfile" (assumes you have Devel::Ptkdb installed) and step through the program.

Re: IPC:Open2 question
by zentara (Cardinal) on Jul 14, 2004 at 15:47 UTC
    When I start the script it starts the program and, but does not output anything. Only when I kill the process of the external program through the windows task manager, the (correct) output is printed to the screen. Do I somehow have to end the process in the script? When I run the program just from the command line with input on standard input, it prints it's results and exits.

    I've encountered similar situations when playing around with IPC. It sounds to me like you need to somehow "flush" the pipe which IPC2 sets up for <README>. I'm no expert, I usually just "hack at it" until I get results. As a guess, I would say the local $/ = undef; statement may be interfering with IPC2 's ability to detect a newline? (which signals a pipe flush?)

    I know with sockets, earlier version of IO::Socket did not have autoflushing turned on, and you needed to manually issue a $sock->flush to avoid the problem you describe.

    If the program you are running is a perl script, you should be able to make it flush, but if it's not a script...then you may have to get "low-level" with it.

    You can "suck data" out of the pipe, if you know how much data is waiting in the pipe Read "perldoc -q filehandle" there is a section on it:

    #read man h2ph if you don't have sys/ioctl.ph require 'sys/ioctl.ph'; $size = pack("L", 0); ioctl(FH, FIONREAD(), $size) or die "Couldn't call ioctl +: $!\n"; $size = unpack("L", $size);
    So you can keep running this test, and when $size > 0, you can use sysread to grab a $size chunk out of the filehandle.

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