Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re: How to execute external programs from perl script

by Anonyrnous Monk (Hermit)
on Feb 03, 2011 at 21:52 UTC ( #886082=note: print w/replies, xml ) Need Help??

in reply to How to execute external programs from perl script

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.

Replies are listed 'Best First'.
Re^2: How to execute external programs from perl script
by chak9988 (Initiate) on Feb 04, 2011 at 14:08 UTC

    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 :-)



      system 'echo "rYr" | phylip sub_program_1';


      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.

        Thanks for explaining

        One more addition if you permit me - How to add delay after each step ? I am thinking something like this-

         system 'echo -e "r"; sleep 2; echo "Y"; sleep 2; echo "r" | phylip sub_program_1';

        I am sure, above code is wrong :( bcoz it is not working !

        I suspect, lack of delay is causing "phylip" to abort after first command.

        Out of context remark - This conversation appears to be like happening face to face... and I am liking it :-)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://886082]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2018-06-17 22:37 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (107 votes). Check out past polls.