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

I'm sure there is a simple solution to this, I just can't figure out what it is.

I have downloaded a third party linux program that will not take any arguements on the command line. One has to state the file to be processed once the program has started, press enter, and then the program will do its stuff and exit. I have a relatively large number of files that I want to process using this program but I don't want to have to run the program for each of these files individually.

The obvious solution is a perl program but I'm not sure as to how to go about doing it. Right now, I'm just trying to write some perl code that will execute the third party program and process a single file

I havent gotten very far but here is the code anyway

#!/usr/bin/perl $xtlsstr = "xtlsstr"; open(XTL, "$xtlsstr"); print XTL "1ax8.pdb";
Needless to say this doesnt work. Any suggestions/comments much appreciated.

Replies are listed 'Best First'.
Re: running an interactive only program using perl
by ikegami (Patriarch) on Jul 12, 2005 at 18:09 UTC
    #!/usr/bin/perl $xtlsstr = "xtlsstr"; open(XTL, "| $xtlsstr") # <-- Pipe to the program. or die("Can't run xtls: $!\n"); # <-- Check for errors. print XTL "1ax8.pdb\n"; # <-- Don't forget the \n.
Re: running an interactive only program using perl
by Joost (Canon) on Jul 12, 2005 at 18:12 UTC
Re: running an interactive only program using perl
by merlyn (Sage) on Jul 12, 2005 at 18:53 UTC
    If the program needs /dev/tty (if the piped-in examples don't work) or interaction, look at Expect, which can interact with a pseudo-tty running your application. I'm writing up a magazine article on that right now, in fact.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

Re: running an interactive only program using perl
by zentara (Cardinal) on Jul 12, 2005 at 19:54 UTC
    And I have to mention my favorite IPC::Open3. You can go to http://groups.google.com, or search here for numerous examples. Of course, there are complications sometimes, where the spawned program likes to play games with STDIN and STDOUT. You can also use IPC::Open3 to open "/bin/bash", then print IN "$yourprogram\n" to start your program, then print commands to it.

    Oh yeah, IPC::Open3 is not reliable on windows( just in case you want to run it on windows).


    I'm not really a human, but I play one on earth. flash japh
Re: running an interactive only program using perl
by davidrw (Prior) on Jul 12, 2005 at 19:14 UTC
    dpending on the program behavior, you might just be able to do this in the shell, too by either piping the content (stuff to type) or redirects.
    cat /tmp/stuff.txt | yourprog yourprog < /tmp/stuff.txt # if it's just the filename you need (if not, can always use <c>echo - +e</c> to print \n newlines): echo filename.ext | yourprog # loop (bash syntax) over all your files and process them: for f in *.dat ; do echo $f | yourprog ; done