FFI::Platypus is interesting. It seems like real FFI module for Perl 5.

FFI::Platypus is interesting. It seems like real FFI module for Perl 5.

FFI is foreign function interface. This is a little slow than XS, but you can call C/C++ library without C code.

I saw some FFI module for perl 5, FFI, or FFI::Raw, but not enough to create C extension flexibly.

FFI::Platypus seems like excelent module because you can call any C/C++ library using its features.

FFI::Platypus/CPAN

I try some example. All work well.

  use FFI::Platypus;
 
  my $ffi = FFI::Platypus->new;
  $ffi->lib(undef); # search libc

# call dynamically
$ffi->function( puts => ['string'] => 'int' )->call("hello world");

# attach as a xsub and call (much faster)
$ffi->attach( puts => ['string'] => 'int' );
puts("hello world");

You can also create C structure in Perl code by using "record_layout" method.

  package My::UnixTime;

use FFI::Platypus::Record;

record_layout(qw(
int tm_sec
int tm_min
int tm_hour
int tm_mday
int tm_mon
int tm_year
int tm_wday
int tm_yday
int tm_isdst
long tm_gmtoff
string tm_zone
));

my $ffi = FFI::Platypus->new;
$ffi->lib(undef);
# define a record class My::UnixTime and alias it to "tm"
$ffi->type("record(My::UnixTime)" => 'tm');

# attach the C localtime function as a constructor
$ffi->attach( localtime => ['time_t*'] => 'tm', sub {
my($inner, $class, $time) = @_;
$time = time unless defined $time;
$inner->(\$time);
});

package main;

# now we can actually use our My::UnixTime class
my $time = My::UnixTime->localtime;
printf "time is %d:%d:%d %s\n",
$time->tm_hour,
$time->tm_min,
$time->tm_sec,
$time->tm_zone;

If you use FFI::TinyCC module, you can write C code and execute it by jit.

  use FFI::TinyCC;
  use FFI::Platypus::Declare qw( int );

my $tcc = FFI::TinyCC->new;

$tcc->compile_string(q{
int
find_square(int value)
{
return value*value;
}
});

my $address = $tcc->get_symbol('find_square');
attach [$address => 'find_square'] => [int] => int;

print find_square(4), "\n"; # prints 16

If you don't know FFI module for perl5, it is worth using FFI::Platypus
.

FFI::Platypus is new project. Its development start from 2015-01-07.
I didn't know until a few weeks ago.

If you are interesting FFI::Platypus, let's share it on GitHub.

FFI::Platypus/Github

I think good project should get a lot of evaluation.

For example, ruby ffi is shared over than 1200.

ruby people share their projects positively.
The ruby people personality that "Let's share more projects! Open source programmer need more evaluation" is good.

FFI::Platypus is yet new, but good project.
If you plan to use FFI in the feature, let's try to use and share it!

FFI::Platypus/Github

2 Comments

Hi Yuki,

Yes, one thing the Perl community needs to do better is to use social sharing tools. We talk a lot with each other on IRC but that doesn't get the same amount of notice as github stars for example, or mentions on twitter with an official hashtag. This is something that I thing the Mojolicious community has done better than average but most other communities don't do so well. I'm not sure why Perl programmers are so unwilling to give each other useful feedback, this is something that is part of the psychology of our community for some reason.

John (jnap)

Leave a comment

About Yuki Kimoto

user-pic I'm Perl Programmer. I LOVE Perl. I want to contribute Perl community and Perl users.