5

I'm using a Perl program to properly format user input into an input file for a Fortran program. The Fortran program creates an output file and error file. The Fortran program is called from Perl like:

system "/mydirectories/fortranexecutable $inputfile $outputfile $errorfile";

I am wondering if there is a way to call the Fortran executable without actually creating the input/output/error files and saving them to the disk before/after the Fortran program is called? I hope my question is clear and not something too obvious. I'm new to Perl and I've tried searching everywhere for this. Thanks for your help in advance.

4
  • That completely depends on the Fortran code, Perl does not enter into it. Edit your question to show the Fortran code instead. Commented Jun 13, 2012 at 18:02
  • I don't want to alter the fortran executable. I am wondering if there is a way to call the fortran program without saving files to the disk. Commented Jun 13, 2012 at 18:10
  • Are you asking if you can pass a Perl array or string as the $inputfile data and then save the $outputfile and $errorfile output to a Perl variable so that it can be post-processed? Commented Jun 13, 2012 at 18:20
  • Yes, I think that would be a good way to avoid writing files. How would I go about doing this? Commented Jun 13, 2012 at 18:34

2 Answers 2

5

If the Fortran code reads sequentially from and writes sequentially to already existing files but you would like to communicate with it in "real time" from the Perl code, then you can kind of get around using named pipes. They still exist as entries in the filesystem and can be opened as usual files by the Fortran code given their name but reading/writing from/to them works like piping.

In Perl you would do something like this (blatantly copied from this answer):

use File::Temp qw(tempdir);
use File::Spec::Functions qw(catfile);
use POSIX qw(mkfifo);

my $dir = tempdir(CLEANUP=>1);
my $inputfifo = catfile($dir, "input");
mkfifo($inputfifo, 0700) or die "mkfifo($inputfifo) failed: $!";
my $outputfifo = catfile($dir, "output");
mkfifo($outputfifo, 0700) or die "mkfifo(output$fifo) failed: $!";
my $errorfifo = catfile($dir, "error");
mkfifo($errorfifo, 0700) or die "mkfifo($errorfifo) failed: $!";

... open the FIFOs ...

system "/mydirectories/fortranexecutable $inputfifo $outputfifo $errorfifo";

... operate with the FIFOs to communicate with the Fortran code ...

... close FIFOs and remove $dir when finished ...
Sign up to request clarification or add additional context in comments.

Comments

3

No. If Fortran program is written in such way that it takes $inputfile as a command line argument, reads data from it, and outputs $outputfile and $errorfile as a result, the only way to do it is through a file.

If you would prefer to pass input data to Fortran executable through standard input, Fortran source code would have to be modified to accomodate this kind of input.

3 Comments

Ok. What type of standard input/output would I need to use for perl and fortran?
@shivsta That depends on how you design your data input in the Fortran program. If you're asking how to use read(,), the question is too broad and you should look into some Fortran language reference. Note that neither your question nor my answer have anything to do with Perl.
The question/answer don't really have much to do with Fortran either ... This is really a question of program design.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.