NativeCall(3pm)

πŸ“¦
version 0.006 Β· 2 versions
  • 0.006 Β· debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking (latest)
  • 0.006 Β· ubuntu jammy
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– NativeCall πŸ’–

okay bestie, so this is NativeCall β€” the Perl 5 manual page for calling into foreign functions straight from Perl code, no XS required. Like, zero XS. none. nada. ✨


✨ NAME ✨

NativeCall β€” the Perl 5 interface to foreign functions, done entirely from Perl code, with no XS. πŸ¦‹


🌸 SYNOPSIS 🌸

here's the whole vibe, copy-paste ready:

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 πŸŽ€

so basically, this thing mimics the NativeCall module and interface from Perl 6. it does the actual hard work with FFI::Platypus, written by the mighty Graham Ollis β€” king behavior, honestly πŸ‘‘. and it gets all that done using inheritance and attributes. slay.

oh and also, examples/troll.pl has the exact example from the SYNOPSIS above if you wanna go look at it. πŸ“

πŸ’… ATTRIBUTES πŸ’…

  • Native β€” if you hand it an argument, it tries to load from that library. if you give it nothing, it just uses whatever's already loaded. easy.
  • Args β€” a comma-separated list of FFI::Platypus::Types. all types are supported, closures included. yes bestie, closures too. 😭✨
  • Returns β€” a single FFI::Platypus::Type. one. that's it.
  • Symbol β€” the native symbol name, for when it's different from the Perl sub name. iykyk.

🌈 INSPIRATION 🌈

this module is entirely inspired by that article about Perl 6 NativeCall, right here:

http://blogs.perl.org/users/zoffix_znet/2016/05/perl-6-nativecall-look-ma-im-a-c-programmer-1.html

all the credit for explaining it so clearly goes to Zoffix. all the brickbats come straight to me πŸ™ˆ. that's the deal, periodt.


fr fr that's the whole page bestie, go forth and call some foreign functions πŸ’–

this was written up for perl v5.34.0, dated 2022-06-16, and you're reading the NativeCall(3pm) page 🫢

πŸ“¦ where this page comes from