NAME

FFI::Me - Turn foreign functions into perl functions or methods without writing XS.

VERSION

This document describes FFI::Me version 0.01

SYNOPSIS

use FFI::Me;

ffi puts => (
    rv  => ffi::int,
    arg => [ffi::str],
);

puts("Say what!");

Uses in an OO package with a method name that differs from the symbol in the library:

ffi cosine => (
    lib => 'libm.so'
    rv  => ffi::double,
    arg => [ffi::double],
    sym => 'cos',
    method => 1,
);

Then later when we have an object:

say $obj->cosine(2.0);

Note: To make your code more portable you could do something like:

lib => $^O eq 'darwin' ? 'libm.dylib' : 'libm.so',

or use something like FFI::CheckLib.

See </TODO>.

DESCRIPTION

libffi (low-level FFI via FFI::Raw in this case) is a really neat way to get access to foreign functions from inside perl.

This module encapsulates the common task of creating the interface via the low-level FFI and then making a higher-level function or method to wrap it.

The simple syntax is the sugary style prefered by most MOPish code.

INTERFACE

ffi

Imported automatically.

Syntax: ffi NAME => (OPTIONS);

Creates a function or method called "NAME" from a foreign function desribed via NAME and OPTIONS.

See "SYNOPSIS" for examples.

The options are as follows:

  • lib

    The library that contains the symbol we are interested in.

    Default: undef (i.e. search in the main program for the foreign function)

  • rv

    The type for the return value of the symbol we are interested in.

    Default: ffi::void

  • arg

    An array ref of types of the arguments taken by the symbol we are interested in.

    Default: empty list

  • sym

    The name of the foreign function symbol in our lib. Useful when you want your perl function or method to be a different name than the foreign function symbol you are interfacing with.

    Default: The NAME passed to ffi().

  • method

    Boolean. If true, it will create the symbol as a method instead of a function.

    Default: false

Types

Each type has a function that will return the type suitable for “rv” and “arg”.

We use the ffi:: name space for these functions to avoid importing them while still keeping them short.

ffi::void

ffi::int

ffi::uint

ffi::short

ffi::ushort

ffi::long

ffi::ulong

ffi::int64

ffi::uint64

ffi::char

ffi::uchar

ffi::float

ffi::double

ffi::str

ffi::ptr

DIAGNOSTICS

Throws no warnings or errors of its own.

The underlying low-level library throws an exception when it can’t do what we’re asking it to.

DEPENDENCIES

FFI::Raw

INCOMPATIBILITIES

None reported.

BUGS AND LIMITATIONS

No bugs have been reported.

Please report any bugs or feature requests to bug-ffi-me@rt.cpan.org, or through the web interface at http://rt.cpan.org.

TODO

Possibly some library lookup ability to allow for things like lib = 'libm'> witout needing to $^O eq 'darwin' ? 'libm.dylib' : 'libm.so'.

AUTHOR

Daniel Muey <http://drmuey.com/cpan_contact.pl>

LICENCE AND COPYRIGHT

Copyright (c) 2015, Daniel Muey <http://drmuey.com/cpan_contact.pl>. All rights reserved.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic.

DISCLAIMER OF WARRANTY

BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.

IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.