package FFI::Raw; { $FFI::Raw::VERSION = '0.17'; } use strict; use warnings; require XSLoader; XSLoader::load('FFI::Raw', $FFI::Raw::VERSION); use overload '&{}' => \&_call_deref; sub _call_deref { my $ffi = shift; return sub { $ffi -> call(@_) }; } =head1 NAME FFI::Raw - Perl bindings to the portable FFI library (libffi) =head1 VERSION version 0.17 =head1 SYNOPSIS use FFI::Raw; my $cos = FFI::Raw -> new( 'libm.so', 'cos', FFI::Raw::double, # return value FFI::Raw::double # arg #1 ); say $cos -> call(2.0); =head1 DESCRIPTION B provides a raw foreign function interface for Perl based on L. It can access and call functions exported by shared libraries without the need to write C/XS code. Dynamic symbols can be automatically resolved at runtime so that the only information needed to use B is the name (or path) of the target library, the name of the function to call and its signature (though it is also possible to pass a function pointer). Note that this module has nothing to do with L. =head1 METHODS =head2 new( $library, $function, $return_type [, $arg_type ...] ) Create a new C object. It loads C<$library>, finds the function C<$function> with return type C<$return_type> and creates a calling interface. This function takes also a variable number of types, representing the argument of the wanted function. =head2 new_from_ptr( $function_ptr, $return_type [, $arg_type ...] ) Create a new C object from the C<$function_ptr> function pointer. This function takes also a variable number of types, representing the argument of the wanted function. =head2 call( [$arg ...] ) Execute the C function C<$self>. This function takes also a variable number of arguments, which are passed to the called function. The argument types must match the types passed to C. The C object can be used as a CODE reference as well. Dereferencing the object will work just like call(): $cos -> call(2.0); # normal call() call $cos -> (2.0); # dereference as CODE ref This works becasue FFI::Raw overloads the C<&{}> operator. =head1 SUBROUTINES =head2 memptr( $number ) Create a L. This is a shortcut for Cnew(...)>. =cut sub memptr { FFI::Raw::MemPtr -> new(@_) } =head2 callback( $coderef, $ret_type [, $arg_type ...] ) Create a L. This is a shortcut for Cnew(...)>. =cut sub callback { FFI::Raw::Callback -> new(@_) } =head1 TYPES =head2 FFI::Raw::void Return a C void type. =cut sub void () { ord 'v' } =head2 FFI::Raw::int Return a C integer type. =cut sub int () { ord 'i' } =head2 FFI::Raw::uint Return a C unsigned integer type. =cut sub uint () { ord 'I' } =head2 FFI::Raw::short Return a C short integer type. =cut sub short () { ord 'z' } =head2 FFI::Raw::ushort Return a C unsigned short integer type. =cut sub ushort () { ord 'Z' } =head2 FFI::Raw::char Return a C char type. =cut sub char () { ord 'c' } =head2 FFI::Raw::uchar Return a C unsigned char type. =cut sub uchar () { ord 'C' } =head2 FFI::Raw::float Return a C float type. =cut sub float () { ord 'f' } =head2 FFI::Raw::double Return a C double type. =cut sub double () { ord 'd' } =head2 FFI::Raw::str Return a C string type. =cut sub str () { ord 's' } =head2 FFI::Raw::ptr Return a C pointer type. =cut sub ptr () { ord 'p' } =head1 AUTHOR Alessandro Ghedini =head1 SEE ALSO L, L =head1 LICENSE AND COPYRIGHT Copyright 2012 Alessandro Ghedini. This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License. See http://dev.perl.org/licenses/ for more information. =cut 1; # End of FFI::Raw