Affix
view release on metacpan or search on metacpan
# NAME
Affix - A Foreign Function Interface eXtension
# SYNOPSIS
```perl
use v5.40;
use Affix qw[:all];
# Bind a function and call it natively.
# Here, we use libm which might be in libm.so, msvcrt.dll, etc.
# C: double pow(double x, double y);
affix libm(), 'pow', [ Double, Double ] => Double;
say pow( 2.0, 10.0 ); # 1024
# Working with C structs is easy
# C: typedef struct { int x; int y; } Point;
# void draw_point(Point p);
typedef Point => Struct[ x => Int, y => Int ];
affix $lib, 'draw_point', [ Point() ] => Void;
draw_point( { x => 10, y => 20 } );
affix $lib, 'get_pos', [] => Point();
my $pt = get_pos();
say sprintf 'x: %d, y: %d', $pt->{x}, $pt->{y};
# We can also allocate and manage raw memory and write data to it
my $ptr = Affix::malloc(1024);
$ptr->[0] = ord('t'); # Direct byte-level access
memcpy( $ptr, 'test', 4 );
# We can also do pointer arithmetic to create new references
my $offset_ptr = Affix::ptr_add( $ptr, 12 );
memcpy( $offset_ptr, 'test', 4 );
# Inspect memory with a hex dump to STDOUT
Affix::dump( $ptr, 32 );
# And release the memory. This is automatic when such a scalar falls out of scope
Affix::free($ptr);
```
# DESCRIPTION
Call native code from Perl without XS, compilers, or runtime overhead.
Affix is a high-performance Foreign Function Interface (FFI) for Perl. It bridges Perl to C, Rust, Zig, C++, Go,
Fortran, and more via JIT-compiled trampolines that handle argument marshalling at runtime. No generic dispatch loops
here. The result is near-native call speed with a rich type system covering primitives, structs, unions, enums, SIMD
vectors, and pointers.
Powered by [infix](https://github.com/sanko/infix/), which has been tested on Linux, Windows, macOS, Solaris, BSD, and
across x86\_64, ARM64, and RISC-V.
# EXPORTS
Import types and functions with built-in tags. By default, Affix exports standard types (`Int`, `Double`, etc.) and
core functions (`affix`, `wrap`, `load_library`).
Control what gets imported:
```perl
use Affix qw[:all]; # Import everything
use Affix qw[:lib]; # Library helpers (libc, libm, load_library...)
use Affix qw[:memory]; # malloc, free, memcpy, cast, dump, raw, snapshot, pin, unpin...
use Affix qw[:types]; # Types only (Int, Struct, Pointer...)
```
# CORE API
Bind functions to Perl subroutines and define custom types. These are the primary entry points for interacting with
foreign libraries.
( run in 0.766 second using v1.01-cache-2.11-cpan-ff9377addf4 )