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

Hi Monks, again, seems like simple problem but I can't solve this. I would like to control one program (that is normally controlled from terminal) with perl program. So , I understand there are few ways of managing it. I started with reading output of the program using pipes and I can not do it. Here is an example of what I am talking about:
# open my $READ, "-|", "./dialog.pl"; while (<$READ>) { print $_; } close $READ;
Here is the dialog.pl program that I am trying to interact with:
print "How old are you? "; my $age = <>; print "You are $age.";
The way I understand first program will open dialog.pl program and display "How old are you?" in the terminal,but it is not the case. What is the issue here? I would like to catch output from dialog.pl , and respond to it. I also understand that I will have to fork the program to be able to do writing and reading from dialog.pl at the same time. Thanks like always. Robert

Replies are listed 'Best First'.
Re: Controlling input and output of different program
by jeffa (Bishop) on Mar 30, 2015 at 22:44 UTC

    I was able to throw this together after taking a gander at the link provided by Anonymous Monk in this thread:

    use strict; use warnings; use FileHandle; use IPC::Open2; my $pid = open2(*Reader, *Writer, "perl dialog.pl"); print Writer "42\n"; chomp( my $got = <Reader> ); print $got, $/;

    Notice the explicit use of chomp() as well as terminating the sent string with a newline. I recommend you alter your dialog.pl script to chomp() user input:

    chomp( my $age = <> );

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      Great, thanks a lot, I implemented your advice and it is working, link that you provided is fantastic. Thanks again Robert
Re: Controlling input and output of different program
by Anonymous Monk on Mar 30, 2015 at 22:33 UTC

    Why?

    Error checking?

    perlipc has examples , although I'd avoid interactive CLI programs like this ... eew ;)