FFI-Platypus-Lang-Fortran
view release on metacpan or search on metacpan
lib/FFI/Platypus/Lang/Fortran.pm view on Meta::CPAN
$ gfortran -shared fib.f90 -o fib.so
$ perl fib.pl
1
1
2
3
5
8
13
21
34
55
=head3 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.
=head2 Complex numbers
=head3 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
=head3 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";
=head3 Execute
$gfortran -shared complex.f90 -o complex.so
$ perl complex.pl
1.5 + 2.5i
=head3 Discussion
Platypus now supports complex types of various sizes. This means that
you can transparently use complex arguments and arrays of complex types.
=head2 Arrays
=head3 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
=head3 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);
=head3 Execute
$ gfortran -shared array.f90 -o array.so
$ perl array.pl
5
10
15
20
25
30
35
40
45
50
=head3 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.
=head2 Multidimensional Arrays
=head3 Fortran
subroutine print_array2x5(a)
implicit none
integer, dimension(2,5) :: a
integer :: i,n
( run in 1.777 second using v1.01-cache-2.11-cpan-54e63673c56 )