The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

NativeCall - Perl 5 interface to foreign functions in Perl code without XS

SYNOPSIS

  use parent qw(NativeCall);
  use feature 'say';

  sub cdio_eject_media_drive :Args(string) :Native(cdio) {}
  sub cdio_close_tray :Args(string, int) :Native(cdio) {}

  say "Gimme a CD!";
  cdio_eject_media_drive undef;

  sleep 1;
  say "Ha! Too slow!";
  cdio_close_tray undef, 0;

  sub fmax :Args(double, double) :Native :Returns(double) {}
  say "fmax(2.0, 3.0) = " . fmax(2.0, 3.0);
  
  # avoid Perl built in also called "abs"
  sub myabs :Args(int) :Native :Returns(int) :Symbol(abs) {}
  say "abs(-3) = " . abs(-3);

DESCRIPTION

Mimics the NativeCall module and interface from Perl 6. Uses FFI::Platypus, by the mighty Graham Ollis, for the actual hard work. Uses inheritance and attributes.

See examples/troll.pl for the example given above in SYNOPSIS.

ATTRIBUTES

Native

If an argument is given, try to load from that library. If none given, use what is already loaded.

Args

A comma-separated list of FFI::Platypus::Types. All types are supported, including closures.

Returns

A single FFI::Platypus::Type.

Symbol

The native symbol name, if different from the Perl sub name.

INSPIRATION

This module is entirely inspired by the article about Perl 6 NativeCall at http://blogs.perl.org/users/zoffix_znet/2016/05/perl-6-nativecall-look-ma-im-a-c-programmer-1.html. All credit for clear explanation to Zoffix. All brickbats to me.