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

I have a driver written in perl that is used to call up an executable. My perl script asks two questions and reads from STDIN. The fortran executable posses several questions during run-time and reads from STDIN. The questions have to be where they are - they can't be moved, since they depend on what is going on in the code..

Everything works fine when I answers the questions manually. However, I would like to be able to pipe in an a text file containing the answers, to automate the process (Eg program.x < answers). This works well with a csh script, but not with the Perl one. The fortran program prints an end of file error, so it knows to read from STDIN, but there is nothing being passed. I think that the problem comes from the split read of STDIN, and all I need to do is pump the rest of the data in STDIN down to the executable.

Currently I am opening a temp file, reading the STDIN stream to it and then piping it into the executable in the system command. I don't really like this.

open(IN,">tmp.input"); print IN <STDIN>; close(IN); $error = system("$runbin/$program.x < tmp.input");

Can anyone think of a cleverer way to use a pipe (or anything else) to redirect the rest of STDIN into the executable?

Stupid.

Replies are listed 'Best First'.
Re: Split STDIN Pipe
by stephen (Priest) on Apr 13, 2001 at 01:35 UTC
    Yep-- use open(). Like so:
    use File::Copy; open(PROGRAM_OUT, "| $runbin/$program.x") or die "Couldn't open '$runbin/$program.x': $!; stopped"; copy(\*STDIN, PROGRAM_OUT) or die "Couldn't copy: $!; stopped"; close(PROGRAM_OUT) or die "Error while closing: $!/$?; stopped";

    Untested.

    That runs your FORTRAN process, copying STDIN to it, then checks the error code on file close.

    Update: Actually, system() by itself should work fine... it inherits your STDIN from the main program. So if you get rid of the redirection and temporary file, it should work fine. So just

    system("$runbin/$program") == 0 or die("'$runbin/$program' failed: returned signal @{[ $? >> 8 ]} +stopped";

    stephen

      I tried the simple system call as you suggested, and although the Fortran program tries to read from STDIN rather than wait for answers it returns an end of file error. Thats why I thought that the fortran knew to read from STDIN, but found nothing in the stream. I think that I will try the pipe method for now.

      Thanks for your help and if anyone has any ideas as to what might be going on, let me know.