.\" Automatically generated by Pod::Man 4.11 (Pod::Simple 3.35) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "FFI::Platypus::Declare 3pm" .TH FFI::Platypus::Declare 3pm "2020-09-25" "perl v5.30.3" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" FFI::Platypus::Declare \- Declarative interface to FFI::Platypus .SH "VERSION" .IX Header "VERSION" version 1.32 .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use FFI::Platypus::Declare \*(Aqstring\*(Aq, \*(Aqint\*(Aq; \& \& lib undef; # use libc \& attach puts => [string] => int; \& \& puts("hello world"); .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" This module is officially \fBdiscouraged\fR. The idea was to provide a simpler declarative interface without the need of (directly) creating an FFI::Platypus instance. In practice it is almost as complicated and makes it difficult to upgrade to the proper \s-1OO\s0 interface if the need arises. I have stopped using it mainly for this reason. It will remain as part of the Platypus core distribution to keep old code working, but you are encouraged to write new code using the \s-1OO\s0 interface. Alternatively, you can try the Perl 6 inspired NativeCall, which provides most of the goals this module was intended for (that is a simple interface at the cost of some power), without much of the complexity. The remainder of this document describes the interface. .PP This module provides a declarative interface to FFI::Platypus. It provides a more concise interface at the cost of a little less power, and a little more namespace pollution. .PP Any strings passed into the \f(CW\*(C`use\*(C'\fR line will be declared as types and exported as constants into your namespace, so that you can use them without quotation marks. .PP Aliases can be declared using a list reference: .PP .Vb 1 \& use FFI::Platypus [ \*(Aqint[48]\*(Aq => \*(Aqmy_integer_array\*(Aq ]; .Ve .PP Custom types can also be declared as a list reference (the type name must include a ::): .PP .Vb 2 \& use FFI::Platypus [ \*(Aq::StringPointer\*(Aq => \*(Aqmy_string_pointer\*(Aq ]; \& # short for FFI::Platypus::Type::StringPointer .Ve .SH "FUNCTIONS" .IX Header "FUNCTIONS" All functions are exported into your namespace. If you do not want that, then use the \s-1OO\s0 interface (see FFI::Platypus). .SS "lib" .IX Subsection "lib" .Vb 1 \& lib $libpath; .Ve .PP Specify one or more dynamic libraries to search for symbols. If you are unsure of the location / version of the library then you can use FFI::CheckLib#find_lib. .SS "type" .IX Subsection "type" .Vb 2 \& type $type; \& type $type = $alias; .Ve .PP Declare the given type. .PP Examples: .PP .Vb 2 \& type \*(Aquint8\*(Aq; # only really checks that uint8 is a valid type \& type \*(Aquint8\*(Aq => \*(Aqmy_unsigned_int_8\*(Aq; .Ve .SS "custom_type" .IX Subsection "custom_type" .Vb 1 \& custom_type $alias => \e%args; .Ve .PP Declare the given custom type. See FFI::Platypus::Type#Custom\-Types for details. .SS "load_custom_type" .IX Subsection "load_custom_type" .Vb 1 \& load_custom_type $name => $alias, @type_args; .Ve .PP Load the custom type defined in the module \fI\f(CI$name\fI\fR, and make an alias with the name \fI\f(CI$alias\fI\fR. If the custom type requires any arguments, they may be passed in as \fI\f(CI@type_args\fI\fR. See FFI::Platypus::Type#Custom\-Types for details. .PP If \fI\f(CI$name\fI\fR contains \f(CW\*(C`::\*(C'\fR then it will be assumed to be a fully qualified package name. If not, then \f(CW\*(C`FFI::Platypus::Type::\*(C'\fR will be prepended to it. .SS "type_meta" .IX Subsection "type_meta" .Vb 1 \& my $meta = type_meta $type; .Ve .PP Get the type meta data for the given type. .PP Example: .PP .Vb 1 \& my $meta = type_meta \*(Aqint\*(Aq; .Ve .SS "attach" .IX Subsection "attach" .Vb 3 \& attach $name => \e@argument_types => $return_type; \& attach [$c_name => $perl_name] => \e@argument_types => $return_type; \& attach [$address => $perl_name] => \e@argument_types => $return_type; .Ve .PP Find and attach a C function as a Perl function as a real live xsub. .PP If just one \fI\f(CI$name\fI\fR is given, then the function will be attached in Perl with the same name as it has in C. The second form allows you to give the Perl function a different name. You can also provide a memory address (the third form) of a function to attach. .PP Examples: .PP .Vb 4 \& attach \*(Aqmy_function\*(Aq, [\*(Aquint8\*(Aq] => \*(Aqstring\*(Aq; \& attach [\*(Aqmy_c_function_name\*(Aq => \*(Aqmy_perl_function_name\*(Aq], [\*(Aquint8\*(Aq] => \*(Aqstring\*(Aq; \& my $string1 = my_function($int); \& my $string2 = my_perl_function_name($int); .Ve .SS "closure" .IX Subsection "closure" .Vb 1 \& my $closure = closure $codeblock; .Ve .PP Create a closure that can be passed into a C function. For details on closures, see FFI::Platypus::Type#Closures. .PP Example: .PP .Vb 2 \& my $closure1 = closure { return $_[0] * 2 }; \& my $closure2 = closure sub { return $_[0] * 4 }; .Ve .SS "sticky" .IX Subsection "sticky" .Vb 1 \& my $closure = sticky closure $codeblock; .Ve .PP Keyword to indicate the closure should not be deallocated for the life of the current process. .PP If you pass a closure into a C function without saving a reference to it like this: .PP .Vb 1 \& foo(closure { ... }); # BAD .Ve .PP Perl will not see any references to it and try to free it immediately. (this has to do with the way Perl and C handle responsibilities for memory allocation differently). One fix for this is to make sure the closure remains in scope using either \f(CW\*(C`my\*(C'\fR or \f(CW\*(C`our\*(C'\fR. If you know the closure will need to remain in existence for the life of the process (or if you do not care about leaking memory), then you can add the sticky keyword to tell FFI::Platypus to keep the thing in memory. .PP .Vb 1 \& foo(sticky closure { ... }); # OKAY .Ve .SS "cast" .IX Subsection "cast" .Vb 1 \& my $converted_value = cast $original_type, $converted_type, $original_value; .Ve .PP The \f(CW\*(C`cast\*(C'\fR function converts an existing \fI\f(CI$original_value\fI\fR of type \&\fI\f(CI$original_type\fI\fR into one of type \fI\f(CI$converted_type\fI\fR. Not all types are supported, so care must be taken. For example, to get the address of a string, you can do this: .PP .Vb 1 \& my $address = cast \*(Aqstring\*(Aq => \*(Aqopaque\*(Aq, $string_value; .Ve .SS "attach_cast" .IX Subsection "attach_cast" .Vb 2 \& attach_cast "cast_name", $original_type, $converted_type; \& my $converted_value = cast_name($original_value); .Ve .PP This function creates a subroutine which can be used to convert variables just like the cast function above. The above synopsis is roughly equivalent to this: .PP .Vb 2 \& sub cast_name { cast($original_type, $converted_type, $_[0]) } \& my $converted_value = cast_name($original_value); .Ve .PP Except that the attach_cast variant will be much faster if called multiple times since the cast does not need to be dynamically allocated on each instance. .SS "sizeof" .IX Subsection "sizeof" .Vb 1 \& my $size = sizeof $type; .Ve .PP Returns the total size of the given type. For example to get the size of an integer: .PP .Vb 1 \& my $intsize = sizeof \*(Aqint\*(Aq; # usually 4 or 8 depending on platform .Ve .PP You can also get the size of arrays .PP .Vb 1 \& my $intarraysize = sizeof \*(Aqint[64]\*(Aq; .Ve .PP Keep in mind that \*(L"pointer\*(R" types will always be the pointer / word size for the platform that you are using. This includes strings, opaque and pointers to other types. .PP This function is not very fast, so you might want to save this value as a constant, particularly if you need the size in a loop with many iterations. .SS "lang" .IX Subsection "lang" .Vb 1 \& lang $language; .Ve .PP Specifies the foreign language that you will be interfacing with. The default is C. The foreign language specified with this attribute changes the default native types (for example, if you specify Rust, you will get \f(CW\*(C`i32\*(C'\fR as an alias for \&\f(CW\*(C`sint32\*(C'\fR instead of \f(CW\*(C`int\*(C'\fR as you do with C). .PP In the future this may attribute may offer hints when doing demangling of languages that require it like \*(C+. .SS "abi" .IX Subsection "abi" .Vb 1 \& abi $abi; .Ve .PP Set the \s-1ABI\s0 or calling convention for use in subsequent calls to \*(L"attach\*(R". May be either a string name or integer value from FFI::Platypus#abis. .SH "SEE ALSO" .IX Header "SEE ALSO" .IP "FFI::Platypus" 4 .IX Item "FFI::Platypus" Object oriented interface to Platypus. .IP "FFI::Platypus::Type" 4 .IX Item "FFI::Platypus::Type" Type definitions for Platypus. .IP "FFI::Platypus::API" 4 .IX Item "FFI::Platypus::API" Custom types \s-1API\s0 for Platypus. .IP "FFI::Platypus::Memory" 4 .IX Item "FFI::Platypus::Memory" memory functions for \s-1FFI.\s0 .IP "FFI::CheckLib" 4 .IX Item "FFI::CheckLib" Find dynamic libraries in a portable way. .IP "FFI::TinyCC" 4 .IX Item "FFI::TinyCC" \&\s-1JIT\s0 compiler for \s-1FFI.\s0 .SH "AUTHOR" .IX Header "AUTHOR" Author: Graham Ollis .PP Contributors: .PP Bakkiaraj Murugesan (bakkiaraj) .PP Dylan Cali (calid) .PP pipcet .PP Zaki Mughal (zmughal) .PP Fitz Elliott (felliott) .PP Vickenty Fesunov (vyf) .PP Gregor Herrmann (gregoa) .PP Shlomi Fish (shlomif) .PP Damyan Ivanov .PP Ilya Pavlov (Ilya33) .PP Petr Pisar (ppisar) .PP Mohammad S Anwar (\s-1MANWAR\s0) .PP Håkon Hægland (hakonhagland, \s-1HAKONH\s0) .PP Meredith (merrilymeredith, \s-1MHOWARD\s0) .PP Diab Jerius (\s-1DJERIUS\s0) .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" This software is copyright (c) 2015,2016,2017,2018,2019,2020 by Graham Ollis. .PP This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.