Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

"Answer" questions in an interactive program?

by Anonymous Monk
on May 11, 2010 at 09:03 UTC ( [id://839401]=perlquestion: print w/replies, xml ) Need Help??

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

Hello fellow monks!
I need to call a rather old program through a perl script that I have written.
The problem is that this program is interactive and "asks" the user some questions.
Is there a way that perl can help me answer this questions by passing the answers as arguments when I call the program in the command line?
The questions are:
1. enter file name (you must specify the file name and press Enter) 2. do you want to specify another file name (Y/N) (you must enter N in + most cases and press Enter) 3.enter name for output file (you must specify a name for the output f +ile, eg ttt and press Enter)
I really don't have a clue as what to do, I tried (unsuccessfully) to write something like:
run_prog (name of the program) myfile.txt N myfile.txt.out

Apparently it can't take these arguments as I tried.
Any thoughts?

Replies are listed 'Best First'.
Re: "Answer" questions in an interactive program?
by BrowserUk (Patriarch) on May 11, 2010 at 09:47 UTC

    Pretend that this is your old program asking the questions. It doesn't do anything except ask them, read the answers and print out the names it read:

    #! perl -slw use strict; print "enter file name (you must specify the file name and press Ente +r)"; chomp( my $infile = <STDIN> ); print "do you want to specify another file name (Y/N) (you must enter +N in most cases and press Enter)"; chomp( my $another = <STDIN> ); print "enter name for output file (you must specify a name for the out +put file, eg ttt and press Enter)"; chomp( my $outfile = <STDIN> ); print "reading $infile; writing $outfile"; exit 99;

    Then you can drive that program from perl using something like this:

    #! perl -slw use strict; my $progname = 'junk33'; my $pid = open CMD, '|-', $progname or die $!; print CMD 'myInfile'; print CMD 'N'; print CMD 'myOutfile'; close CMD; waitpid $pid, 0; print "$progname ended: status: ", $? >>8;

    And when you run that, you'll see:

    c:\test>junk32 enter file name (you must specify the file name and press Enter) do you want to specify another file name (Y/N) (you must enter N in mo +st cases and press Enter) enter name for output file (you must specify a name for the output fil +e, eg ttt and press Enter) reading myInfile; writing myOutfile junk33 ended: status: 99

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      ... close CMD; waitpid $pid, 0; print "$progname ended: status: ", $? >>8;

      I'm a little surprised that you get the correct status value here, because close already does wait for the subprocess at the other end of the pipe (so the waitpid is neither needed nor setting the $? correctly). At least on Unix.  Does this behave differently on Windows?

      #!/usr/bin/perl -l my $cmd = q(perl -e 'chomp(my $ret = <STDIN>); exit $ret+1'); my $pid = open CMD, '|-', $cmd or die $!; print CMD 98; close CMD; print "status from close: ", $? >> 8; waitpid $pid, 0; print "status from waitpid: ", $? >> 8; __END__ $ ./839409.pl status from close: 99 status from waitpid: 72057594037927935
        Does this behave differently on Windows?

        Seems so:

        c:\test>perl #!/usr/bin/perl -l my $cmd = q(perl -e "chomp(my $ret = <STDIN>); exit $ret+1"); my $pid = open CMD, '|-', $cmd or die $!; print CMD 98; close CMD; print "status from close: ", $? >> 8; waitpid $pid, 0; print "status from waitpid: ", $? >> 8; ^Z status from close: 99 status from waitpid: 99

        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
      Thanks a lot for your time!
      I have 2 questions:
      a) The code you kindly provided should be in 2 separate programs? I put it in one perl script, correct?
      b) The old program I need to run has its executable in /usr/local/bin. Will the script need me to specify its full path?
      I tried what you wrote and returned
      enter file name (you must specify the file name and press Enter) Test.file do you want to specify another file name (Y/N) (you must enter N in mo +st cases and press Enter) N enter name for output file (you must specify a name for the output fil +e, eg ttt and press Enter) Result.file reading Test.file; writing Result.file
      I suppose it didn't execute the external program, right? I used your code, like:
      #! perl -slw use strict; print "enter file name (you must specify the file name and press Ente +r)"; chomp( my $infile = <STDIN> ); print "do you want to specify another file name (Y/N) (you must enter +N in most cases and press Enter)"; chomp( my $another = <STDIN> ); print "enter name for output file (you must specify a name for the out +put file, eg ttt and press Enter)"; chomp( my $outfile = <STDIN> ); print "reading $infile; writing $outfile"; exit 99; my $progname = '/usr/local/bin/simpa96'; #--->this is the name of the + external program I need to pass the parameters to my $pid = open CMD, '|-', $progname or die $!; print CMD 'myInfile'; print CMD 'N'; print CMD 'myOutfile'; close CMD; waitpid $pid, 0; print "$progname ended: status: ", $? >>8;

        The first program is just a substitute for your real program (which I obviously do not have), soley for the purposes of demonstration.

        You don't need it. You only need (something like) the second program.

        Does that make it clearer?

        Will the script need me to specify its full path?

        Not if it is in your path. But it will do no harm to specify the full path.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: "Answer" questions in an interactive program?
by marto (Cardinal) on May 11, 2010 at 09:06 UTC

    Use Expect or alter your program to accept inputs as arguments?

    A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://839401]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2025-02-18 16:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found