Raku mechanism for calling code from installed C libraries. The following is a complete example that uses installed C libraries on Windows to open up a message box: use NativeCall; sub MessageBoxA(int32, Str, Str, int32) returns int32 is native('user32') { * } MessageBoxA(0, "We have NativeCall", "ohai", 64);
Raku mechanism for calling code from C libraries. The following examples are from https://docs.perl6.org/language/nativecall.
use NativeCall; sub some_argless_function() is native('something') { * } some_argless_function();
The first line imports various traits and types. The next line looks like a relatively ordinary Perl 6 sub declaration—with a twist. We use the "native" trait in order to specify that the sub is actually defined in a native library. The platform-specific extension (e.g., '.so' or '.dll'), as well as any customary prefixes (e.g., 'lib') will be added for you.
The first time you call "some_argless_function", the "libsomething" will be loaded and the "some_argless_function" will be located in it. A call will then be made. Subsequent calls will be faster, since the symbol handle is retained.
Function that takes and returns typed arguments:
use NativeCall; sub add(int32, int32) returns int32 is native("calculator") { * }
use NativeCall; sub MessageBoxA(int32, Str, Str, int32) returns int32 is native('user32') { * } MessageBoxA(0, "We have NativeCall", "ohai", 64);