NativeCall(3pm)
π 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 ofFFI::Platypus::Types. all types are supported, closures included. yes bestie, closures too. πβ¨Returnsβ a singleFFI::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
- debian bookworm: libnativecall-perl 0.006-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libnativecall-perl 0.006-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libnativecall-perl 0.006-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libnativecall-perl 0.006-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libnativecall-perl 0.006-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms