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

Hello everyone, first this is a fanastic website. I really like it. Now to my problem. I have a linux terminal program which waits for user input a.k.a. a user to press the return key to get back to the terminal prompt again. So basically I want to parse the stdout off that program ( i need information about the parameters). So I made the following:
my $parameter_output = `echo "\n" |programname -help`;
and this works perfectally well for me, the program terminates, the complete stdout is stored in that variable and my perl script moves on... But I thought to me..."hey all this commandline statement above is doing is forking a process and putting that newline character from the stdout from process 1 (parent) to stdin from the second process(child)". So I tried to reproduce this with fork but I cannot find the reason why this does not work. small snippet....
pipe (IN,OUT); my $pid = fork (); die("fork failed") if (!defined($pid)); if ($pid == 0 ) { #the child exec ('programname -help'); } else { #parent sleep (2); #printing the newline character in the child process print OUT "\n"; waitpid ($pid,0); }
I also tried this with signals.... snippet:
sub print_it {print "\n"}; my $SIG{INT}=\&print_it; .... else { #parent sleep (2); kill INT=>$pid; waitpid ($pid,0); }
neither this works.... I think somewhere I have a big error in reasoning I think. So it would be very kind if someone could help me. Thanks in advance joshi

Replies are listed 'Best First'.
Re: Trying to pass a "press any key" program in terminal.
by moritz (Cardinal) on Jan 21, 2008 at 21:48 UTC
    Your newline never reaches the external command, because IN isn't connected to anything. (I believe that if you had used use warnings, it would have warned you about the symbol IN only being used once).

    BTW normally you'd use expect for this kind of automation process.

      thanks for the reply. The problem is I cannot use expect because I am working on a server where I cannot get permissions to install additional modules. Do you know any way to connect the IN filehandle to the child process? Thanks in advance Oliver
Re: Trying to pass a "press any key" program in terminal.
by kyle (Abbot) on Jan 21, 2008 at 21:52 UTC

    When you fork your process, the parent and child don't have any better way of talking to each other than any other two programs on the system (i.e., signals, reading/writing the same files). You can fork via open instead, in which case you'll get a (one-way) file handle to talk through, but it sounds as if you want two-way communication (send a newline, get back the output). For that, look at IPC::Open3 or IPC::Run.