Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling Fortran program from Perl without saving input/output files

Tags:

perl

fortran

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.

like image 571
shivsta Avatar asked Jun 13 '12 17:06

shivsta


1 Answers

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 ...
like image 155
Hristo Iliev Avatar answered Sep 22 '22 01:09

Hristo Iliev