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

This question might have been asked in this forum before, but I could not find suitable answer in archives. Kindly direct me to node, if it was answered

Here is my query:

I want to run a program called "phylip" from perl script. "phylip" is a command line program, that has many sub-programs in it. Typically a phylip program appears like this when executed at command prompt

chak:~$ phylip sub_program_1 sub_program_1: the file "out-file" that you wanted to use as output file already exists. Do you want to Replace it, Append to it, write to a new File, or Quit? (please type R, A, F, or Q) ..... (phylip subprogram_2 ...... please type R, A, F, or Q) n times
Every-time I run phylip program, I know exactly when to type what character/string. (In above example I have to type: phylip sub_program_1, R, phylip sub_program_2, R).

My perl script generates files required for phylip program, so it would be cool, if I can automate these steps in perl script itself

Can anyone advice me on how to do this ?

Thanks

Chak

Replies are listed 'Best First'.
Re: How to execute external programs from perl script
by hbm (Hermit) on Feb 03, 2011 at 21:28 UTC
    Expect. It allows you to spawn another program and interact with it.
Re: How to execute external programs from perl script
by Anonyrnous Monk (Hermit) on Feb 03, 2011 at 21:52 UTC

    In case your external program reads from stdin, you could try to simply pipe the respective character(s) into the program

    system 'echo R | phylib sub_program_1';

    If that doesn't work, i.e. if the program reads from the pty, you'd need to use Expect instead, as already suggested — but this simple approach is worth a try first.

      It works !

      adding to my query, I want to type 3 commands, but not just 1.

      Something like this

      system 'echo "r"| echo "Y"|echo "r"| phylip sub_program_1';

      first command echo "r" works, but not second and third. Any idea why it is not working ?

      I would appreciate if you direct me to documentation for this function. I would like to learn basics myself :-)

      thanks

        Try

        system 'echo "rYr" | phylip sub_program_1';

        or

        system 'echo -e "r\nY\nr" | phylip sub_program_1';

        The latter would be appropriate in case you have to press Enter after typing the respective letter when you do it interactively.

        The way this works is essentially as follows (somehwat simplified): each program has three standard handles for I/O: stdin, stdout, stderr (we can forget about stderr for the moment).

        When you run a program from the command line "normally" (i.e. without a pipe or redirection) those standard handles are connected to the terminal where your command shell runs in.  What you type on the keyboard is forwarded to the program as input, and what the program outputs is displayed in the terminal.  When you connect two programs via a pipe

        program_A | program_B

        stdout of A is being sent to stdin of program B. The respective handles are also disconnected from the terminal, which means that B no longer gets its input from the keyboard, but from program A (and the output of A is no longer displayed in the terminal).

        In your particular case, program A is the command "echo", which simply sends to stdout whatever you pass it as arguments, which then ends up as input for program B (phylib) as if you had typed it on the keyboard.  In other words, the idea is simply to make echo send to stdout exactly the same characters that you would have typed interactively otherwise.

        Also, program B only reads as many characters as it wants at a time, even if program A has output everything at once. The associated buffering (and blocking of I/O, if needed) is handled behind the scenes by the OS.

Re: How to execute external programs from perl script
by sierpinski (Chaplain) on Feb 03, 2011 at 20:58 UTC
    Hi Chak,

    Welcome to PerlMonks.

    The Super Search tool is very powerful for searching past articles/comments/responses. I noticed something similar to what you are looking for in 868847. You can easily access the Super Search by looking in the upper-right hand corner (right under the Perl Monks banner) where it says Super Search.

    Hope that helps.