Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Calling and interacting with external executable

by verity (Initiate)
on Apr 25, 2012 at 18:33 UTC ( #967141=perlquestion: print w/replies, xml ) Need Help??
verity has asked for the wisdom of the Perl Monks concerning the following question:

I have an executable that I need to call from my Perl script. It is pretty simple when run from the command line:

~$:/path/executable "Here is the script. Please choose option 1 or 2" 1 "Thank you. Please provide the input file name:" myInputFile.txt (script runs and output file appears in the directory) ~$:

I can't figure out how to supply the requested information from within the Perl script.

#I have tried my $program = "path-to-executable"; system($program); #The script hangs here until I manually enter the re +quested info at the command line system("1"); system("myInputFile.txt"); #Have also tried my $program = "path-to-executable"; my $i = `$program`; #Script hangs here and nothing appears on the comm +and line at all $i = `1`; $i = `myInputFile.txt`;

I have even tried adding sleep() commands but that does not fix the problem. Is there a way to let my script know it needs to keep talking to the executable?

Replies are listed 'Best First'.
Re: Calling and interacting with external executable
by toolic (Bishop) on Apr 25, 2012 at 19:04 UTC
    Maybe you could use Expect (untested):
    use warnings; use strict; use Expect; my $command = 'path-to-executable'; my $exp = Expect->new(); $exp->raw_pty(1); $exp->spawn($command) or die "Cannot spawn $command: $!\n"; $exp->send("1\n"); $exp->send("myInputFile.txt\n"); $exp->soft_close();
Re: Calling and interacting with external executable
by adler187 (Initiate) on Apr 25, 2012 at 20:55 UTC

    If you are calling an interactive program, that would be expected behavior using system or backticks. There is no way to interact with a program through those APIs, so you're program just sits there waiting for input.

    You can do a few things. You can pipe your answer to the program (if the program only expects 1 input) like so:

    system("echo $input_file_name | $program");

    If you're program asks multiple questions, then you will have to be more manual about it. You need to open a pipe to the program and print data to it, like you would a file. See open and the open tutorial

      Yes, open did it, thank you for the suggestion! Here is the code that worked:
      open(GERMS, "| $germline") || die "Could not call germline!\n"; print GERMS "1\n"; print GERMS "$map\n"; print GERMS "$ped\n"; print GERMS "out"; close GERMS;
Re: Calling and interacting with external executable
by thundergnat (Deacon) on Apr 25, 2012 at 19:20 UTC

    Can you pipe the selection to the executable?

    system( qq{ echo 1\n | $program });
Re: Calling and interacting with external executable
by 2teez (Priest) on Apr 25, 2012 at 20:34 UTC

    You can simply do it like this using system(...):

    use warnings; use strict; die "Usage: path_to_exe_file.exe" unless defined $ARGV +[0]; my $file=$ARGV[0]; ## get the path to your executables from the CLI system($file); # interact with your exe #Or print "Enter your excutable path:"; chomp(my $path=<STDIN>); ## get path from your script system($path); # interact with your exe

    Try it, it works. Hope this helps

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://967141]
Approved by toolic
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2018-01-22 05:12 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (231 votes). Check out past polls.