FFI-Platypus-Lang-Fortran
view release on metacpan or search on metacpan
$ gfortran -shared fib.f90 -o fib.so
$ perl fib.pl
1
1
2
3
5
8
13
21
34
55
Discussion
If you have a newer Fortran compiler that understands Fortran 90 or 95,
you can take advantage of its advanced features like recursion and
pointers. In this example we compute 10 Fibonacci numbers.
Complex numbers
Fortran
subroutine complex_decompose(c, r, i)
implicit none
complex*16, intent(in) :: c
real*8, intent(out):: r
real*8, intent(out) :: i
r = real(c)
i = aimag(c)
end subroutine complex_decompose
Perl
use FFI::Platypus 2.00;
use Math::Complex;
my $ffi = FFI::Platypus->new(
api => 2,
lang => 'Fortran',
lib => './complex.so',
);
$ffi->attach( complex_decompose => ['complex_16*','real_8*','real_8*'] );
complex_decompose( \(1.5 + 2.5*i), \my $r, \my $i);
print "${r} + ${i}i\n";
Execute
$gfortran -shared complex.f90 -o complex.so
$ perl complex.pl
1.5 + 2.5i
Discussion
Platypus now supports complex types of various sizes. This means that
you can transparently use complex arguments and arrays of complex
types.
Arrays
Fortran
subroutine print_array10(a)
implicit none
integer, dimension(10) :: a
integer :: i
do i=1,10
print *, a(i)
end do
end subroutine print_array10
Perl
use FFI::Platypus 2.00;
my $ffi = FFI::Platypus->new(
api => 2,
lang => 'Fortran',
lib => './array.so',
);
$ffi->attach( print_array10 => ['integer[10]'] => 'void' );
my $array = [5,10,15,20,25,30,35,40,45,50];
print_array10($array);
Execute
$ gfortran -shared array.f90 -o array.so
$ perl array.pl
5
10
15
20
25
30
35
40
45
50
Discussion
In Fortran arrays are 1 indexed unlike Perl and C where arrays are 0
indexed. Perl arrays are passed in from Perl using Platypus as a array
reference.
Multidimensional Arrays
Fortran
subroutine print_array2x5(a)
implicit none
( run in 1.975 second using v1.01-cache-2.11-cpan-364913b4093 )