tags:

views:

284

answers:

2

I want to run and executable ./runnable on argument input.afa. The standard input to this executable is through a file finalfile. I was earlier trying to do the same using a bash script, but that does not seem to work out. So I was wondering whether Perl provides such functionality. I know I can run the executable with its argument using backticks or system() call. Any suggestions on how to give standard input through file.

_ UPDATE _

As I said I had written a bash script for the same. I not sure how to go about doing it in Perl. The bash script I wrote was:

#!/bin/bash

OUTFILE=outfile
(

while read line
do 

./runnable input.afa
echo $line


done<finalfile

) >$OUTFILE

The data in standard input file is as follows, where each line correspond to one time input. So if there are 10 lines then the executable should run 10 times.

__DATA__

2,9,2,9,10,0,38

2,9,2,10,11,0,0

2,9,2,11,12,0,0

2,9,2,12,13,0,0

2,9,2,13,0,1,4

2,9,2,13,3,2,2

2,9,2,12,14,1,2
+1  A: 

If I understood your question correctly, then you are perhaps looking for something like this:

# The command to run.
my $command = "./runnable input.afa";

# $command will be run for each line in $command_stdin
my $command_stdin = "finalfile";

# Open the file pointed to by $command_stdin
open my $inputfh, '<', $command_stdin or die "$command_input: $!";

# For each line
while (my $input = <$inputfh>) {
    chomp($input); # optional, removes line separator

    # Run the command that is pointed to by $command,
    # and open $write_stdin as the write end of the command's
    # stdin.
    open my $write_stdin, '|-', $command or die "$command: $!";

    # Write the arguments to the command's stdin.
    print $write_stdin $input;
}

More info about opening commands in the documentation.

Inshallah
@Inshalla: can you please add comments for me to understand it better.
shubster
A: 

Perl code:

$stdout_result = `exescript argument1 argument2 < stdinfile`;

Where stdinfile holds the data you want to be passed through stdin.


edit

The clever method would be to open stdinfile, tie it via select to stdin, and then execute repeatedly. The easy method would be to put the data you want to pass through in a temp file.

Example:

open $fh, "<", "datafile" or die($!);
@data = <$fh>; #sucks all the lines in datafile into the array @data
close $fh;

foreach $datum (@data) #foreach singluar datum in the array
{
    #create a temp file
    open $fh, ">", "tempfile" or die($!);
    print $fh $datum;
    close $fh;

    $result = `exe arg1 arg2 arg3 < tempfile`; #run the command. Presumably you'd want to store it somewhere as well...

    #store $result
}

unlink("tempfile"); #remove the tempfile
Paul Nathan
In this example the backticks `` execute the command through the shell, which handles the input redirection.
Justin
@Paul: RC also suggested the same. But, on running it I get a segmentation error.
shubster
That should not be perl's fault. Is your C executable handling the arguments correctly.
Paul Nathan
when I run it on the command line, it works.
shubster
Can you paste the perl you've tried?
Paul Nathan
@Paul: So what you suggested works, but again the problem is similar to the one I was facing with bash script. It runs only once i.e. one iteration. The finalfile contains 10 lines so the number of iterations should be 10 and not one. Any suggestions how to resolve the same.
shubster
@shubster: Thanks for the accept. Inshalla did the clever method, fyi. :-)
Paul Nathan
@Paul Nathan: Perhaps my comments were too unintelligible :-).
Inshallah