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

Hello!!
I'm trying to feed information to a program written in C (mktrace specifically), using a unix box (solaris 5.8). Rather than input 2000 filenames via the command line one by one, I'm trying to call the program in a perl script and feed it the filenames from an array, one element at a time. The problem is how to get the data to mktrace. If mktrace took information via the command line as such mktrace file.txt etc, then I think I could write !system "/myfile/mktrace file.txt ";. Unfortunately, the program begins and then recieves the input file name via STDIN, but trying to use perl to print a STDOUT which is then used by the program doesn't seem to work. Does anyone have an idea on how this might be done (minus rewriting mktrace;-)? Thanks ahead of time!!!
Bioinformatics

Replies are listed 'Best First'.
Re: feeding info to C programs....
by Roger (Parson) on Nov 05, 2003 at 23:46 UTC
    Abigail-II has provided an execellent answer (node 304670) to a similar problem in the answer to "passing string to stdin on exec-command". I think this is probably what you are looking for.

Re: feeding info to C programs....
by graff (Chancellor) on Nov 06, 2003 at 05:04 UTC
    In my job, I am often in a position where I have to repeat a process on a long list of files, and the process is such that I can only do one file at a time -- e.g. each run produces an output file whose name is derived somehow from the input file name, or whatever.

    So one of the first things I did after getting started with perl several years ago was to create a general purpose script that I called "shloop" (as in "shell loop", or more exactly, "sh loop", because it's really just an easier, more flexible tool to use in place of the "for x in ..." loop in (ba)sh). I posted it here.

    If there's a process called "mktrace" which only accepts one file name, and I have a long list of file names, I could do:

    shloop -e mktrace filename.list
    Or if the list were all files in the current directory whose names end in ".fasta" or somesuch, I could do:
    ls *.fasta | shloop -e mktrace
    On unix, it starts by opening a shell with:
    open( SH, "| /bin/sh" ) or die "Couldn't start sub-shell";
    Then on each iteration of the loop (as it reads lines from STDIN or a list file), it assembles a "$commandline" by putting the list element together with the value of the "-e" option, and then just does:
    print SH $commandline;
    The pod provided with the script is pretty extensive (there are a lot more options, like using regex substitutions to create a proper output file spec from the input, putting input and output strings at various points in the command string, etc). If it doesn't suit your needs, it might at least provide some ideas for things to try.
Re: feeding info to C programs....
by zby (Vicar) on Nov 05, 2003 at 23:28 UTC
    If you do system "/myfile/mktrace file.txt "; than the mktrace program receives the file name as an argument (that is in the ARGV table) not in STDIN. If you want to catch the output of the mktrace program you can use the "backtics" operator: my $output = `/myfile/mktrace file.txt`. This way the output from the mktrace program will not be spat in the STDOUT of your perl program.

    By the way you can do it in the shell using the xargs command. This might be easier.

      Thats my whole point. The example does not use STDIN, but mktrace does require the input via STDIN. That being the case, how can I get mktrace the info via STDIN?
      Bioinformatics
Re: feeding info to C programs....
by injunjoel (Priest) on Nov 06, 2003 at 03:00 UTC
    Greetings all,
    Okay This is only an idea based on some of the things I have seen elsewhere... again this is just an thought.
    Could you in your perl script open a filehandle to a run of your C program?
    Something similar to :
    open (MAIL, "| /usr/sbin/sendmail -t") || die "no mail for you!";
    but instead of sendmail it would be your C program
    open (CPROG, "| /path/to/your/program") || die;
    then pipe all the filenames to the filehandle you created?
    or am I way off here?
    Just a thought...
    -injunjoel