closure#
Wrap a Perl coderef as a C-callable function pointer.
Synopsis#
# int (*)(void*) - a progress handler
my $ptr = Peta::FFI::closure('(o)i', sub { my ($ud) = @_; 0 });
Peta::FFI::call($lib, 'sqlite3_progress_handler', '(oioo)v',
$db, 100, $ptr, undef);
...
Peta::FFI::closure_free($ptr);
The signature grammar is call’s: (args)ret over the type codes v i l L d f p P o. The returned value is the raw address of the trampoline, to be passed wherever C wants the function pointer.
Lifetime#
The closure stays alive until closure_free is called with the address this returned. Freeing it while C still holds the pointer leaves that library calling into released memory, so free it only after the C side has been told to stop - for a driver, that means tying it to the handle and releasing it at disconnect.
Errors in the callback#
A callback that dies does NOT propagate: the error would have to unwind through the C caller’s stack frame. $@ is set and the closure returns a zero of its declared return type; check $@ after the C call that triggered it.