{"id":22764575,"url":"https://github.com/perlffi/ffi-platypus","last_synced_at":"2025-08-27T03:17:27.365Z","repository":{"id":37008191,"uuid":"18826948","full_name":"PerlFFI/FFI-Platypus","owner":"PerlFFI","description":"Write Perl bindings to non-Perl libraries with FFI.  No XS required.","archived":false,"fork":false,"pushed_at":"2025-01-01T12:07:45.000Z","size":3389,"stargazers_count":93,"open_issues_count":17,"forks_count":23,"subscribers_count":10,"default_branch":"main","last_synced_at":"2025-05-24T10:06:20.084Z","etag":null,"topics":["ffi","perl","perl7"],"latest_commit_sha":null,"homepage":"","language":"Perl","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/PerlFFI.png","metadata":{"files":{"readme":"README.md","changelog":"Changes","contributing":"CONTRIBUTING","funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":"SUPPORT","governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2014-04-16T04:31:10.000Z","updated_at":"2025-02-01T18:53:55.000Z","dependencies_parsed_at":"2025-01-19T00:30:42.227Z","dependency_job_id":null,"html_url":"https://github.com/PerlFFI/FFI-Platypus","commit_stats":{"total_commits":1945,"total_committers":22,"mean_commits":88.4090909090909,"dds":0.2107969151670951,"last_synced_commit":"8d52251e7e7a07a98c4e32f516812131b7e1bd34"},"previous_names":[],"tags_count":220,"template":false,"template_full_name":null,"purl":"pkg:github/PerlFFI/FFI-Platypus","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PerlFFI%2FFFI-Platypus","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PerlFFI%2FFFI-Platypus/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PerlFFI%2FFFI-Platypus/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PerlFFI%2FFFI-Platypus/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PerlFFI","download_url":"https://codeload.github.com/PerlFFI/FFI-Platypus/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PerlFFI%2FFFI-Platypus/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":272284286,"owners_count":24906857,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-27T02:00:09.397Z","response_time":76,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ffi","perl","perl7"],"created_at":"2024-12-11T12:09:16.173Z","updated_at":"2025-08-27T03:17:27.346Z","avatar_url":"https://github.com/PerlFFI.png","language":"Perl","funding_links":[],"categories":[],"sub_categories":[],"readme":"# FFI::Platypus ![static](https://github.com/PerlFFI/FFI-Platypus/workflows/static/badge.svg) ![linux](https://github.com/PerlFFI/FFI-Platypus/workflows/linux/badge.svg) ![windows](https://github.com/PerlFFI/FFI-Platypus/workflows/windows/badge.svg) ![macos](https://github.com/PerlFFI/FFI-Platypus/workflows/macos/badge.svg) ![msys2-mingw](https://github.com/PerlFFI/FFI-Platypus/workflows/msys2-mingw/badge.svg)\n\nWrite Perl bindings to non-Perl libraries with FFI. No XS required.\n\n# SYNOPSIS\n\n```perl\nuse FFI::Platypus 2.00;\n\n# for all new code you should use api =\u003e 2\nmy $ffi = FFI::Platypus-\u003enew(\n  api =\u003e 2,\n  lib =\u003e undef, # search libc\n);\n\n# call dynamically\n$ffi-\u003efunction( puts =\u003e ['string'] =\u003e 'int' )-\u003ecall(\"hello world\");\n\n# attach as a xsub and call (much faster)\n$ffi-\u003eattach( puts =\u003e ['string'] =\u003e 'int' );\nputs(\"hello world\");\n```\n\n# DESCRIPTION\n\nPlatypus is a library for creating interfaces to machine code libraries\nwritten in languages like C, [C++](https://metacpan.org/pod/FFI::Platypus::Lang::CPP),\n[Go](https://metacpan.org/pod/FFI::Platypus::Lang::Go),\n[Fortran](https://metacpan.org/pod/FFI::Platypus::Lang::Fortran),\n[Rust](https://metacpan.org/pod/FFI::Platypus::Lang::Rust),\n[Pascal](https://metacpan.org/pod/FFI::Platypus::Lang::Pascal). Essentially anything that gets\ncompiled into machine code.  This implementation uses [libffi](https://sourceware.org/libffi/) to\naccomplish this task.  [libffi](https://sourceware.org/libffi/) is battle tested by a number of other\nscripting and virtual machine languages, such as Python and Ruby to\nserve a similar role.  There are a number of reasons why you might want\nto write an extension with Platypus instead of XS:\n\n- FFI / Platypus does not require messing with the guts of Perl\n\n    XS is less of an API and more of the guts of perl splayed out to do\n    whatever you want.  That may at times be very powerful, but it can also\n    be a frustrating exercise in hair pulling.\n\n- FFI / Platypus is portable\n\n    Lots of languages have FFI interfaces, and it is subjectively easier to\n    port an extension written in FFI in Perl or another language to FFI in\n    another language or Perl.  One goal of the Platypus Project is to reduce\n    common interface specifications to a common format like JSON that could\n    be shared between different languages.\n\n- FFI / Platypus could be a bridge to Raku\n\n    One of those \"other\" languages could be Raku and Raku already has an\n    FFI interface I am told.\n\n- FFI / Platypus can be reimplemented\n\n    In a bright future with multiple implementations of Perl 5, each\n    interpreter will have its own implementation of Platypus, allowing\n    extensions to be written once and used on multiple platforms, in much\n    the same way that Ruby-FFI extensions can be use in Ruby, JRuby and\n    Rubinius.\n\n- FFI / Platypus is pure perl (sorta)\n\n    One Platypus script or module works on any platform where the libraries\n    it uses are available.  That means you can deploy your Platypus script\n    in a shared filesystem where they may be run on different platforms.  It\n    also means that Platypus modules do not need to be installed in the\n    platform specific Perl library path.\n\n- FFI / Platypus is not C or C++ centric\n\n    XS is implemented primarily as a bunch of C macros, which requires at\n    least some understanding of C, the C pre-processor, and some C++ caveats\n    (since on some platforms Perl is compiled and linked with a C++\n    compiler). Platypus on the other hand could be used to call other\n    compiled languages, like [Fortran](https://metacpan.org/pod/FFI::Platypus::Lang::Fortran),\n    [Go](https://metacpan.org/pod/FFI::Platypus::Lang::Go),\n    [Rust](https://metacpan.org/pod/FFI::Platypus::Lang::Rust),\n    [Pascal](https://metacpan.org/pod/FFI::Platypus::Lang::Pascal), [C++](https://metacpan.org/pod/FFI::Platypus::Lang::CPP),\n    or even [assembly](https://metacpan.org/pod/FFI::Platypus::Lang::ASM), allowing you to focus\n    on your strengths.\n\n- FFI / Platypus does not require a parser\n\n    [Inline](https://metacpan.org/pod/Inline) isolates the extension developer from XS to some extent, but\n    it also requires a parser.  The various [Inline](https://metacpan.org/pod/Inline) language bindings are\n    a great technical achievement, but I think writing a parser for every\n    language that you want to interface with is a bit of an anti-pattern.\n\nThis document consists of an API reference, a set of examples, some\nsupport and development (for contributors) information.  If you are new\nto Platypus or FFI, you may want to skip down to the\n[EXAMPLES](#examples) to get a taste of what you can do with Platypus.\n\nPlatypus has extensive documentation of types at [FFI::Platypus::Type](https://metacpan.org/pod/FFI::Platypus::Type)\nand its custom types API at [FFI::Platypus::API](https://metacpan.org/pod/FFI::Platypus::API).\n\nYou are **strongly** encouraged to use API level 2 for all new code.\nThere are a number of improvements and design fixes that you get\nfor free.  You should even consider updating existing modules to\nuse API level 2 where feasible.  How do I do that you might ask?\nSimply pass in the API level to the platypus constructor.\n\n```perl\nmy $ffi = FFI::Platypus-\u003enew( api =\u003e 2 );\n```\n\nThe Platypus documentation has already been updated to assume API\nlevel 1.\n\n# CONSTRUCTORS\n\n## new\n\n```perl\nmy $ffi = FFI::Platypus-\u003enew( api =\u003e 2, %options);\n```\n\nCreate a new instance of [FFI::Platypus](https://metacpan.org/pod/FFI::Platypus).\n\nAny types defined with this instance will be valid for this instance\nonly, so you do not need to worry about stepping on the toes of other\nCPAN FFI / Platypus Authors.\n\nAny functions found will be out of the list of libraries specified with\nthe [lib](#lib) attribute.\n\n### options\n\n- api\n\n    \\[version 0.91\\]\n\n    Sets the API level.  The recommended value for all new code is `2`.\n    The Platypus documentation assumes API level `2` except for a few\n    places that specifically document older versions.  You should\n    only use a lower value for a legacy code base that cannot be migrated to\n    a newer API level. Legal values are:\n\n    - `0`\n\n        Original API level.  See [FFI::Platypus::TypeParser::Version0](https://metacpan.org/pod/FFI::Platypus::TypeParser::Version0) for details\n        on the differences.\n\n    - `1`\n\n        Enable version 1 API type parser which allows pass-by-value records\n        and type decoration on basic types.\n\n    - `2`\n\n        Enable version 2 API.\n        The Platypus documentation assumes this api level is set.\n\n        API version 2 is identical to version 1, except:\n\n        - Pointer functions that return `NULL` will return `undef` instead of empty list\n\n            This fixes a long standing design bug in Platypus.\n\n        - Array references may be passed to pointer argument types\n\n            This replicates the behavior of array argument types with no size.  So the types `sint8*` and `sint8[]`\n            behave identically when an array reference is passed in.  They differ in that, as before, you can\n            pass a scalar reference into type `sint8*`.\n\n        - The fixed string type can be specified without pointer modifier\n\n            That is you can use `string(10)` instead of `string(10)*` as you were previously able to\n            in API 0.\n\n- lib\n\n    Either a pathname (string) or a list of pathnames (array ref of strings)\n    to pre-populate the [lib](#lib) attribute.  Use `[undef]` to search the\n    current process for symbols.\n\n    0.48\n\n    `undef` (without the array reference) can be used to search the current\n    process for symbols.\n\n- ignore\\_not\\_found\n\n    \\[version 0.15\\]\n\n    Set the [ignore\\_not\\_found](#ignore_not_found) attribute.\n\n- lang\n\n    \\[version 0.18\\]\n\n    Set the [lang](#lang) attribute.\n\n# ATTRIBUTES\n\n## lib\n\n```perl\n$ffi-\u003elib($path1, $path2, ...);\nmy @paths = $ffi-\u003elib;\n```\n\nThe list of libraries to search for symbols in.\n\nThe most portable and reliable way to find dynamic libraries is by using\n[FFI::CheckLib](https://metacpan.org/pod/FFI::CheckLib), like this:\n\n```perl\nuse FFI::CheckLib 0.06;\n$ffi-\u003elib(find_lib_or_die lib =\u003e 'archive');\n  # finds libarchive.so on Linux\n  #       libarchive.bundle on OS X\n  #       libarchive.dll (or archive.dll) on Windows\n  #       cygarchive-13.dll on Cygwin\n  #       ...\n  # and will die if it isn't found\n```\n\n[FFI::CheckLib](https://metacpan.org/pod/FFI::CheckLib) has a number of options, such as checking for specific\nsymbols, etc.  You should consult the documentation for that module.\n\nAs a special case, if you add `undef` as a \"library\" to be searched,\nPlatypus will also search the current process for symbols. This is\nmostly useful for finding functions in the standard C library, without\nhaving to know the name of the standard c library for your platform (as\nit turns out it is different just about everywhere!).\n\nYou may also use the [\"find\\_lib\"](#find_lib) method as a shortcut:\n\n```perl\n$ffi-\u003efind_lib( lib =\u003e 'archive' );\n```\n\n## ignore\\_not\\_found\n\n\\[version 0.15\\]\n\n```perl\n$ffi-\u003eignore_not_found(1);\nmy $ignore_not_found = $ffi-\u003eignore_not_found;\n```\n\nNormally the [attach](#attach) and [function](#function) methods will\nthrow an exception if it cannot find the name of the function you\nprovide it.  This will change the behavior such that\n[function](#function) will return `undef` when the function is not\nfound and [attach](#attach) will ignore functions that are not found.\nThis is useful when you are writing bindings to a library and have many\noptional functions and you do not wish to wrap every call to\n[function](#function) or [attach](#attach) in an `eval`.\n\n## lang\n\n\\[version 0.18\\]\n\n```\n$ffi-\u003elang($language);\n```\n\nSpecifies the foreign language that you will be interfacing with. The\ndefault is C.  The foreign language specified with this attribute\nchanges the default native types (for example, if you specify\n[Rust](https://metacpan.org/pod/FFI::Platypus::Lang::Rust), you will get `i32` as an alias for\n`sint32` instead of `int` as you do with [C](https://metacpan.org/pod/FFI::Platypus::Lang::C)).\n\nIf the foreign language plugin supports it, this will also enable\nPlatypus to find symbols using the demangled names (for example, if you\nspecify [CPP](https://metacpan.org/pod/FFI::Platypus::Lang::CPP) for C++ you can use method names\nlike `Foo::get_bar()` with [\"attach\"](#attach) or [\"function\"](#function).\n\n## api\n\n\\[version 1.11\\]\n\n```perl\nmy $level = $ffi-\u003eapi;\n```\n\nReturns the API level of the Platypus instance.\n\n# METHODS\n\n## type\n\n```perl\n$ffi-\u003etype($typename);\n$ffi-\u003etype($typename =\u003e $alias);\n```\n\nDefine a type.  The first argument is the native or C name of the type.\nThe second argument (optional) is an alias name that you can use to\nrefer to this new type.  See [FFI::Platypus::Type](https://metacpan.org/pod/FFI::Platypus::Type) for legal type\ndefinitions.\n\nExamples:\n\n```perl\n$ffi-\u003etype('sint32');            # only checks to see that sint32 is a valid type\n$ffi-\u003etype('sint32' =\u003e 'myint'); # creates an alias myint for sint32\n$ffi-\u003etype('bogus');             # dies with appropriate diagnostic\n```\n\n## custom\\_type\n\n```perl\n$ffi-\u003ecustom_type($alias =\u003e {\n  native_type         =\u003e $native_type,\n  native_to_perl      =\u003e $coderef,\n  perl_to_native      =\u003e $coderef,\n  perl_to_native_post =\u003e $coderef,\n});\n```\n\nDefine a custom type.  See [FFI::Platypus::Type#Custom-Types](https://metacpan.org/pod/FFI::Platypus::Type#Custom-Types) for details.\n\n## load\\_custom\\_type\n\n```perl\n$ffi-\u003eload_custom_type($name =\u003e $alias, @type_args);\n```\n\nLoad the custom type defined in the module _$name_, and make an alias\n_$alias_. If the custom type requires any arguments, they may be passed\nin as _@type\\_args_. See [FFI::Platypus::Type#Custom-Types](https://metacpan.org/pod/FFI::Platypus::Type#Custom-Types) for\ndetails.\n\nIf _$name_ contains `::` then it will be assumed to be a fully\nqualified package name. If not, then `FFI::Platypus::Type::` will be\nprepended to it.\n\n## types\n\n```perl\nmy @types = $ffi-\u003etypes;\nmy @types = FFI::Platypus-\u003etypes;\n```\n\nReturns the list of types that FFI knows about.  This will include the\nnative `libffi` types (example: `sint32`, `opaque` and `double`) and\nthe normal C types (example: `unsigned int`, `uint32_t`), any types\nthat you have defined using the [type](#type) method, and custom types.\n\nThe list of types that Platypus knows about varies somewhat from\nplatform to platform, [FFI::Platypus::Type](https://metacpan.org/pod/FFI::Platypus::Type) includes a list of the core\ntypes that you can always count on having access to.\n\nIt can also be called as a class method, in which case, no user defined\nor custom types will be included in the list.\n\n## type\\_meta\n\n```perl\nmy $meta = $ffi-\u003etype_meta($type_name);\nmy $meta = FFI::Platypus-\u003etype_meta($type_name);\n```\n\nReturns a hash reference with the meta information for the given type.\n\nIt can also be called as a class method, in which case, you won't be\nable to get meta data on user defined types.\n\nThe format of the meta data is implementation dependent and subject to\nchange.  It may be useful for display or debugging.\n\nExamples:\n\n```perl\nmy $meta = $ffi-\u003etype_meta('int');        # standard int type\nmy $meta = $ffi-\u003etype_meta('int[64]');    # array of 64 ints\n$ffi-\u003etype('int[128]' =\u003e 'myintarray');\nmy $meta = $ffi-\u003etype_meta('myintarray'); # array of 128 ints\n```\n\n## mangler\n\n```\n$ffi-\u003emangler(\\\u0026mangler);\n```\n\nSpecify a customer mangler to be used for symbol lookup.  This is usually useful\nwhen you are writing bindings for a library where all of the functions have the\nsame prefix.  Example:\n\n```perl\n$ffi-\u003emangler(sub {\n  my($symbol) = @_;\n  return \"foo_$symbol\";\n});\n\n$ffi-\u003efunction( get_bar =\u003e [] =\u003e 'int' );  # attaches foo_get_bar\n\nmy $f = $ffi-\u003efunction( set_baz =\u003e ['int'] =\u003e 'void' );\n$f-\u003ecall(22); # calls foo_set_baz\n```\n\n## function\n\n```perl\nmy $function = $ffi-\u003efunction($name =\u003e \\@argument_types =\u003e $return_type);\nmy $function = $ffi-\u003efunction($address =\u003e \\@argument_types =\u003e $return_type);\nmy $function = $ffi-\u003efunction($name =\u003e \\@argument_types =\u003e $return_type, \\\u0026wrapper);\nmy $function = $ffi-\u003efunction($address =\u003e \\@argument_types =\u003e $return_type, \\\u0026wrapper);\n```\n\nReturns an object that is similar to a code reference in that it can be\ncalled like one.\n\nCaveat: many situations require a real code reference, so at the price\nof a performance penalty you can get one like this:\n\n```perl\nmy $function = $ffi-\u003efunction(...);\nmy $coderef = sub { $function-\u003e(@_) };\n```\n\nIt may be better, and faster to create a real Perl function using the\n[attach](#attach) method.\n\nIn addition to looking up a function by name you can provide the address\nof the symbol yourself:\n\n```perl\nmy $address = $ffi-\u003efind_symbol('my_function');\nmy $function = $ffi-\u003efunction($address =\u003e ...);\n```\n\nUnder the covers, [function](#function) uses [find\\_symbol](#find_symbol)\nwhen you provide it with a name, but it is useful to keep this in mind\nas there are alternative ways of obtaining a functions address.\nExample: a C function could return the address of another C function\nthat you might want to call.\n\n\\[version 0.76\\]\n\nIf the last argument is a code reference, then it will be used as a\nwrapper around the function when called.  The first argument to the wrapper\nwill be the inner function, or if it is later attached an xsub.  This can be\nused if you need to verify/modify input/output data.\n\nExamples:\n\n```perl\nmy $function = $ffi-\u003efunction('my_function_name', ['int', 'string'] =\u003e 'string');\nmy $return_string = $function-\u003e(1, \"hi there\");\n```\n\n\\[version 0.91\\]\n\n```perl\nmy $function = $ffi-\u003efunction( $name =\u003e \\@fixed_argument_types =\u003e \\@var_argument_types =\u003e $return_type);\nmy $function = $ffi-\u003efunction( $name =\u003e \\@fixed_argument_types =\u003e \\@var_argument_types =\u003e $return_type, \\\u0026wrapper);\nmy $function = $ffi-\u003efunction( $name =\u003e \\@fixed_argument_types =\u003e \\@var_argument_types);\nmy $function = $ffi-\u003efunction( $name =\u003e \\@fixed_argument_types =\u003e \\@var_argument_types =\u003e \\\u0026wrapper);\n```\n\nVersion 0.91 and later allows you to creat functions for c variadic functions\n(such as printf, scanf, etc) which can take a variable number of arguments.\nThe first set of arguments are the fixed set, the second set are the variable\narguments to bind with.  The variable argument types must be specified in order\nto create a function object, so if you need to call variadic function with\ndifferent set of arguments then you will need to create a new function object\neach time:\n\n```perl\n# int printf(const char *fmt, ...);\n$ffi-\u003efunction( printf =\u003e ['string'] =\u003e ['int'] =\u003e 'int' )\n    -\u003ecall(\"print integer %d\\n\", 42);\n$ffi-\u003efunction( printf =\u003e ['string'] =\u003e ['string'] =\u003e 'int' )\n    -\u003ecall(\"print string %s\\n\", 'platypus');\n```\n\nSome older versions of libffi and possibly some platforms may not support\nvariadic functions.  If you try to create a one, then an exception will be\nthrown.\n\n\\[version 1.26\\]\n\nIf the return type is omitted then `void` will be the assumed return type.\n\n## attach\n\n```perl\n$ffi-\u003eattach($name =\u003e \\@argument_types =\u003e $return_type);\n$ffi-\u003eattach([$c_name =\u003e $perl_name] =\u003e \\@argument_types =\u003e $return_type);\n$ffi-\u003eattach([$address =\u003e $perl_name] =\u003e \\@argument_types =\u003e $return_type);\n$ffi-\u003eattach($name =\u003e \\@argument_types =\u003e $return_type, \\\u0026wrapper);\n$ffi-\u003eattach([$c_name =\u003e $perl_name] =\u003e \\@argument_types =\u003e $return_type, \\\u0026wrapper);\n$ffi-\u003eattach([$address =\u003e $perl_name] =\u003e \\@argument_types =\u003e $return_type, \\\u0026wrapper);\n```\n\nFind and attach a C function as a real live Perl xsub.  The advantage of\nattaching a function over using the [function](#function) method is that\nit is much much much faster since no object resolution needs to be done.\nThe disadvantage is that it locks the function and the [FFI::Platypus](https://metacpan.org/pod/FFI::Platypus)\ninstance into memory permanently, since there is no way to deallocate an\nxsub.\n\nIf just one _$name_ is given, then the function will be attached in\nPerl with the same name as it has in C.  The second form allows you to\ngive the Perl function a different name.  You can also provide an\naddress (the third form), just like with the [function](#function)\nmethod.\n\nExamples:\n\n```perl\n$ffi-\u003eattach('my_function_name', ['int', 'string'] =\u003e 'string');\n$ffi-\u003eattach(['my_c_function_name' =\u003e 'my_perl_function_name'], ['int', 'string'] =\u003e 'string');\nmy $string1 = my_function_name($int);\nmy $string2 = my_perl_function_name($int);\n```\n\n\\[version 0.20\\]\n\nIf the last argument is a code reference, then it will be used as a\nwrapper around the attached xsub.  The first argument to the wrapper\nwill be the inner xsub.  This can be used if you need to verify/modify\ninput/output data.\n\nExamples:\n\n```perl\n$ffi-\u003eattach('my_function', ['int', 'string'] =\u003e 'string', sub {\n  my($my_function_xsub, $integer, $string) = @_;\n  $integer++;\n  $string .= \" and another thing\";\n  my $return_string = $my_function_xsub-\u003e($integer, $string);\n  $return_string =~ s/Belgium//; # HHGG remove profanity\n  $return_string;\n});\n```\n\n\\[version 0.91\\]\n\n```perl\n$ffi-\u003eattach($name =\u003e \\@fixed_argument_types =\u003e \\@var_argument_types, $return_type);\n$ffi-\u003eattach($name =\u003e \\@fixed_argument_types =\u003e \\@var_argument_types, $return_type, \\\u0026wrapper);\n```\n\nAs of version 0.91 you can attach a variadic functions, if it is supported\nby the platform / libffi that you are using.  For details see the `function`\ndocumentation.  If not supported by the implementation then an exception\nwill be thrown.\n\n## closure\n\n```perl\nmy $closure = $ffi-\u003eclosure($coderef);\nmy $closure = FFI::Platypus-\u003eclosure($coderef);\n```\n\nPrepares a code reference so that it can be used as a FFI closure (a\nPerl subroutine that can be called from C code).  For details on\nclosures, see [FFI::Platypus::Type#Closures](https://metacpan.org/pod/FFI::Platypus::Type#Closures) and [FFI::Platypus::Closure](https://metacpan.org/pod/FFI::Platypus::Closure).\n\n## cast\n\n```perl\nmy $converted_value = $ffi-\u003ecast($original_type, $converted_type, $original_value);\n```\n\nThe `cast` function converts an existing _$original\\_value_ of type\n_$original\\_type_ into one of type _$converted\\_type_.  Not all types\nare supported, so care must be taken.  For example, to get the address\nof a string, you can do this:\n\n```perl\nmy $address = $ffi-\u003ecast('string' =\u003e 'opaque', $string_value);\n```\n\nSomething that won't work is trying to cast an array to anything:\n\n```perl\nmy $address = $ffi-\u003ecast('int[10]' =\u003e 'opaque', \\@list);  # WRONG\n```\n\n## attach\\_cast\n\n```perl\n$ffi-\u003eattach_cast(\"cast_name\", $original_type, $converted_type);\n$ffi-\u003eattach_cast(\"cast_name\", $original_type, $converted_type, \\\u0026wrapper);\nmy $converted_value = cast_name($original_value);\n```\n\nThis function attaches a cast as a permanent xsub.  This will make it\nfaster and may be useful if you are calling a particular cast a lot.\n\n\\[version 1.26\\]\n\nA wrapper may be added as the last argument to `attach_cast` and works\njust like the wrapper for `attach` and `function` methods.\n\n## sizeof\n\n```perl\nmy $size = $ffi-\u003esizeof($type);\nmy $size = FFI::Platypus-\u003esizeof($type);\n```\n\nReturns the total size of the given type in bytes.  For example to get\nthe size of an integer:\n\n```perl\nmy $intsize = $ffi-\u003esizeof('int');   # usually 4\nmy $longsize = $ffi-\u003esizeof('long'); # usually 4 or 8 depending on platform\n```\n\nYou can also get the size of arrays\n\n```perl\nmy $intarraysize = $ffi-\u003esizeof('int[64]');  # usually 4*64\nmy $intarraysize = $ffi-\u003esizeof('long[64]'); # usually 4*64 or 8*64\n                                             # depending on platform\n```\n\nKeep in mind that \"pointer\" types will always be the pointer / word size\nfor the platform that you are using.  This includes strings, opaque and\npointers to other types.\n\nThis function is not very fast, so you might want to save this value as\na constant, particularly if you need the size in a loop with many\niterations.\n\n## alignof\n\n\\[version 0.21\\]\n\n```perl\nmy $align = $ffi-\u003ealignof($type);\n```\n\nReturns the alignment of the given type in bytes.\n\n## kindof\n\n\\[version 1.24\\]\n\n```perl\nmy $kind = $ffi-\u003ekindof($type);\n```\n\nReturns the kind of a type.  This is a string with a value of one of\n\n- `void`\n- `scalar`\n- `string`\n- `closure`\n- `record`\n- `record-value`\n- `pointer`\n- `array`\n- `object`\n\n## countof\n\n\\[version 1.24\\]\n\n```perl\nmy $count = $ffi-\u003ecountof($type);\n```\n\nFor array types returns the number of elements in the array (returns 0 for variable length array).\nFor the `void` type returns 0.  Returns 1 for all other types.\n\n## def\n\n\\[version 1.24\\]\n\n```perl\n$ffi-\u003edef($package, $type, $value);\nmy $value = $ff-\u003edef($package, $type);\n```\n\nThis method allows you to store data for types.  If the `$package` is not provided, then the\ncaller's package will be used.  `$type` must be a legal Platypus type for the [FFI::Platypus](https://metacpan.org/pod/FFI::Platypus)\ninstance.\n\n## unitof\n\n\\[version 1.24\\]\n\n```perl\nmy $unittype = $ffi-\u003eunitof($type);\n```\n\nFor array and pointer types, returns the basic type without the array or pointer part.\nIn other words, for `sin16[]` or `sint16*` it will return `sint16`.\n\n## find\\_lib\n\n\\[version 0.20\\]\n\n```perl\n$ffi-\u003efind_lib( lib =\u003e $libname );\n```\n\nThis is just a shortcut for calling [FFI::CheckLib#find\\_lib](https://metacpan.org/pod/FFI::CheckLib#find_lib) and\nupdating the [\"lib\"](#lib) attribute appropriately.  Care should be taken\nthough, as this method simply passes its arguments to\n[FFI::CheckLib#find\\_lib](https://metacpan.org/pod/FFI::CheckLib#find_lib), so if your module or script is depending on a\nspecific feature in [FFI::CheckLib](https://metacpan.org/pod/FFI::CheckLib) then make sure that you update your\nprerequisites appropriately.\n\n## find\\_symbol\n\n```perl\nmy $address = $ffi-\u003efind_symbol($name);\n```\n\nReturn the address of the given symbol (usually function).\n\n## bundle\n\n\\[version 0.96 api = 1+\\]\n\n```\n$ffi-\u003ebundle($package, \\@args);\n$ffi-\u003ebundle(\\@args);\n$ffi-\u003ebundle($package);\n$ffi-\u003ebundle;\n```\n\nThis is an interface for bundling compiled code with your\ndistribution intended to eventually replace the `package` method documented\nabove.  See [FFI::Platypus::Bundle](https://metacpan.org/pod/FFI::Platypus::Bundle) for details on how this works.\n\n## package\n\n\\[version 0.15 api = 0\\]\n\n```perl\n$ffi-\u003epackage($package, $file); # usually __PACKAGE__ and __FILE__ can be used\n$ffi-\u003epackage;                  # autodetect\n```\n\n**Note**: This method is officially discouraged in favor of `bundle`\ndescribed above.\n\nIf you use [FFI::Build](https://metacpan.org/pod/FFI::Build) (or the older deprecated [Module::Build::FFI](https://metacpan.org/pod/Module::Build::FFI)\nto bundle C code with your distribution, you can use this method to tell\nthe [FFI::Platypus](https://metacpan.org/pod/FFI::Platypus) instance to look for symbols that came with the\ndynamic library that was built when your distribution was installed.\n\n## abis\n\n```perl\nmy $href = $ffi-\u003eabis;\nmy $href = FFI::Platypus-\u003eabis;\n```\n\nGet the legal ABIs supported by your platform and underlying\nimplementation.  What is supported can vary a lot by CPU and by\nplatform, or even between 32 and 64 bit on the same CPU and platform.\nThey keys are the \"ABI\" names, also known as \"calling conventions\".  The\nvalues are integers used internally by the implementation to represent\nthose ABIs.\n\n## abi\n\n```\n$ffi-\u003eabi($name);\n```\n\nSet the ABI or calling convention for use in subsequent calls to\n[\"function\"](#function) or [\"attach\"](#attach).  May be either a string name or integer\nvalue from the [\"abis\"](#abis) method above.\n\n# EXAMPLES\n\nHere are some examples.  These examples are provided in full with the\nPlatypus distribution in the \"examples\" directory.  There are also some\nmore examples in [FFI::Platypus::Type](https://metacpan.org/pod/FFI::Platypus::Type) that are related to types.\n\n## Passing and Returning Integers\n\n### C Source\n\n```\nint add(int a, int b) {\n  return a+b;\n}\n```\n\n### Perl Source\n\n```perl\nuse FFI::Platypus 2.00;\nuse FFI::CheckLib qw( find_lib_or_die );\nuse File::Basename qw( dirname );\n\nmy $ffi = FFI::Platypus-\u003enew( api =\u003e 2, lib =\u003e './add.so' );\n$ffi-\u003eattach( add =\u003e ['int', 'int'] =\u003e 'int' );\n\nprint add(1,2), \"\\n\";  # prints 3\n```\n\n### Execute\n\n```\n$ cc -shared -o add.so add.c\n$ perl add.pl\n3\n```\n\n### Discussion\n\nBasic types like integers and floating points are the easiest to pass\nacross the FFI boundary.  Because they are values that are passed on\nthe stack (or through registers) you don't need to worry about memory\nallocations or ownership.\n\nHere we are building our own C dynamic library using the native C\ncompiler on a Unix like platform.  The exact incantation that you\nwill use to do this would unfortunately depend on your platform and\nC compiler.\n\nBy default, Platypus uses the\n[Platypus C language plugin](https://metacpan.org/pod/FFI::Platypus::Lang::C), which gives you\neasy access to many of the basic types used by C APIs.  (for example\n`int`, `unsigned long`, `double`, `size_t` and others).\n\nIf you are working with another language like\n[Fortran](https://metacpan.org/pod/FFI::Platypus::Lang::Fortran#Passing-and-Returning-Integers),\n[Go](https://metacpan.org/pod/FFI::Platypus::Lang::Go#Passing-and-Returning-Integers),\n[Rust](https://metacpan.org/pod/FFI::Platypus::Lang::Rust#Passing-and-Returning-Integers) or\n[Zig](https://metacpan.org/pod/FFI::Platypus::Lang::Zig#Passing-and-Returning-Integers),\nyou will find similar examples where you can use the Platypus language\nplugin for that language and use the native types.\n\n## String Arguments (with puts)\n\n### C API\n\n[cppreference - puts](https://en.cppreference.com/w/c/io/puts)\n\n### Perl Source\n\n```perl\nuse FFI::Platypus 2.00;\n\nmy $ffi = FFI::Platypus-\u003enew( api =\u003e 2, lib =\u003e undef );\n$ffi-\u003eattach( puts =\u003e ['string'] =\u003e 'int' );\n\nputs(\"hello world\");\n```\n\n### Execute\n\n```\n$ perl puts.pl\nhello world\n```\n\n### Discussion\n\nPassing strings into a C function as an argument is also pretty easy\nusing Platypus.  Just use the `string` type, which is equivalent to\nthe C \u0026lt;char \\*\u003e or `const char *` types.\n\nIn this example we are using the C Standard Library's `puts` function,\nso we don't need to build our own C code.  We do still need to tell\nPlatypus where to look for the `puts` symbol though, which is why\nwe set `lib` to `undef`.  This is a special value which tells\nPlatypus to search the Perl runtime executable itself (including any\ndynamic libraries) for symbols.  That helpfully includes the C Standard\nLibrary.\n\n## Returning Strings\n\n### C Source\n\n```\n#include \u003cstring.h\u003e\n#include \u003cstdlib.h\u003e\n\nconst char *\nstring_reverse(const char *input)\n{\n  static char *output = NULL;\n  int i, len;\n\n  if(output != NULL)\n    free(output);\n\n  if(input == NULL)\n    return NULL;\n\n  len = strlen(input);\n  output = malloc(len+1);\n\n  for(i=0; input[i]; i++)\n    output[len-i-1] = input[i];\n  output[len] = '\\0';\n\n  return output;\n}\n```\n\n### Perl Source\n\n```perl\nuse FFI::Platypus 2.00;\n\nmy $ffi = FFI::Platypus-\u003enew(\n  api =\u003e 2,\n  lib =\u003e './string_reverse.so',\n);\n\n$ffi-\u003eattach( string_reverse =\u003e ['string'] =\u003e 'string' );\n\nprint string_reverse(\"\\nHello world\");\n\nstring_reverse(undef);\n```\n\n### Execute\n\n```\n$ cc -shared -o string_reverse.so string_reverse.c\n$ perl string_reverse.pl\ndlrow olleH\n```\n\n### Discussion\n\nThe C code here takes an input ASCII string and reverses it, returning\nthe result.  Note that it retains ownership of the string, the caller\nis expected to use it before the next call to `reverse_string`, or\ncopy it.\n\nThe Perl code simply declares the return value as `string` and is very\nsimple.  This does bring up an inconsistency though, strings passed in\nto a function as arguments are passed by reference, whereas the return\nvalue is copied!  This is usually what you want because C APIs usually\nfollow this pattern where you are expected to make your own copy of\nthe string.\n\nAt the end of the program we call `reverse_string` with `undef`, which\ngets translated to C as `NULL`.  This allows it to free the output buffer\nso that the memory will not leak.\n\n## Returning and Freeing Strings with Embedded NULLs\n\n### C Source\n\n```\n#include \u003cstring.h\u003e\n#include \u003cstdlib.h\u003e\n\nchar *\nstring_crypt(const char *input, int len, const char *key)\n{\n  char *output;\n  int i, n;\n\n  if(input == NULL)\n    return NULL;\n\n  output = malloc(len+1);\n  output[len] = '\\0';\n\n  for(i=0, n=0; i\u003clen; i++, n++) {\n    if(key[n] == '\\0')\n      n = 0;\n    output[i] = input[i] ^ key[n];\n  }\n\n  return output;\n}\n\nvoid\nstring_crypt_free(char *output)\n{\n  if(output != NULL)\n    free(output);\n}\n```\n\n### Perl Source\n\n```perl\nuse FFI::Platypus 2.00;\nuse FFI::Platypus::Buffer qw( buffer_to_scalar );\nuse YAML ();\n\nmy $ffi = FFI::Platypus-\u003enew(\n  api =\u003e 2,\n  lib =\u003e './xor_cipher.so',\n);\n\n$ffi-\u003eattach( string_crypt_free =\u003e ['opaque'] );\n\n$ffi-\u003eattach( string_crypt =\u003e ['string','int','string'] =\u003e 'opaque' =\u003e sub{\n  my($xsub, $input, $key) = @_;\n  my $ptr = $xsub-\u003e($input, length($input), $key);\n  my $output = buffer_to_scalar $ptr, length($input);\n  string_crypt_free($ptr);\n  return $output;\n});\n\nmy $orig = \"hello world\";\nmy $key  = \"foobar\";\n\nprint YAML::Dump($orig);\nmy $encrypted = string_crypt($orig, $key);\nprint YAML::Dump($encrypted);\nmy $decrypted = string_crypt($encrypted, $key);\nprint YAML::Dump($decrypted);\n```\n\n### Execute\n\n```\n$ cc -shared -o xor_cipher.so xor_cipher.c\n$ perl xor_cipher.pl\n--- hello world\n--- \"\\x0e\\n\\x03\\x0e\\x0eR\\x11\\0\\x1d\\x0e\\x05\"\n--- hello world\n```\n\n### Discussion\n\nThe C code here also returns a string, but it has some different expectations,\nso we can't just use the `string` type like we did in the previous example\nand copy the string.\n\nThis C code implements a simple XOR cipher.  Given an input string and a key\nit returns an encrypted or decrypted output string where the characters are\nXORd with the key.  There are some challenges here though.  First the input\nand output strings can have embedded `NULL`s in them.  For the string passed\nin, we can provide the length of the input string.  For the output, the\n`string` type expects a `NULL` terminated string, so we can't use that.  So\ninstead we get a pointer to the output using the `opaque` type.  Because we\nknow that the output string is the same length as the input string we can\nconvert the pointer to a regular Perl string using the `buffer_to_scalar`\nfunction.  (For more details about working with buffers and strings see\n[FFI::Platypus::Buffer](https://metacpan.org/pod/FFI::Platypus::Buffer)).\n\nNext, the C code here does not keep the pointer to the output string, as in\nthe previous example.  We are expected to call `string_encrypt_free` when\nwe are done.  Since we are getting the pointer back from the C code instead\nof copying the string that is easy to do.\n\nFinally, we are using a wrapper to hide a lot of this complexity from our\ncaller.  The last argument to the `attach` call is a code reference which will\nwrap around the C function, which is passed in as the first argument of\nthe wrapper.  This is a good practice when writing modules, to hide the\ncomplexity of C.\n\n## Pointers\n\n### C Source\n\n```\nvoid\nswap(int *a, int *b)\n{\n  int tmp = *b;\n  *b = *a;\n  *a = tmp;\n}\n```\n\n### Perl Source\n\n```perl\nuse FFI::Platypus 2.00;\n\nmy $ffi = FFI::Platypus-\u003enew(\n  api =\u003e 2,\n  lib =\u003e './swap.so',\n);\n\n$ffi-\u003eattach( swap =\u003e ['int*','int*'] );\n\nmy $a = 1;\nmy $b = 2;\n\nprint \"[a,b] = [$a,$b]\\n\";\n\nswap( \\$a, \\$b );\n\nprint \"[a,b] = [$a,$b]\\n\";\n```\n\n### Execute\n\n```\n$ cc -shared -o swap.so swap.c\n$ perl swap.pl\n[a,b] = [1,2]\n[a,b] = [2,1]\n```\n\n### Discussion\n\nPointers are often use in C APIs to return simple values like this.  Platypus\nprovides access to pointers to primitive types by appending `*` to the\nprimitive type.  Here for example we are using `int*` to create a function\nthat takes two pointers to integers and swaps their values.\n\nWhen calling the function from Perl we pass in a reference to a scalar.\nStrictly speaking Perl allows modifying the argument values to subroutines, so\nwe could have allowed just passing in a scalar, but in the design of Platypus\nwe decided that forcing the use of a reference here emphasizes that you are\npassing a reference to the variable, not just the value.\n\nNot pictured in this example, but you can also pass in `undef` for a pointer\nvalue and that will be translated into `NULL` on the C side.  You can also\nreturn a pointer to a primitive type from a function, again this will be\nreturned to Perl as a reference to a scalar.  Platypus also supports string\npointers (`string*`).  (Though the C equivalent to a `string*` is a double\npointer to char `char**`).\n\n## Opaque Pointers (objects)\n\n### C Source\n\n```\n#include \u003cstring.h\u003e\n#include \u003cstdlib.h\u003e\n\ntypedef struct person_t {\n  char *name;\n  unsigned int age;\n} person_t;\n\nperson_t *\nperson_new(const char *name, unsigned int age) {\n  person_t *self = malloc(sizeof(person_t));\n  self-\u003ename = strdup(name);\n  self-\u003eage  = age;\n}\n\nconst char *\nperson_name(person_t *self) {\n  return self-\u003ename;\n}\n\nunsigned int\nperson_age(person_t *self) {\n  return self-\u003eage;\n}\n\nvoid\nperson_free(person_t *self) {\n  free(self-\u003ename);\n  free(self);\n}\n```\n\n### Perl Source\n\n```perl\nuse FFI::Platypus 2.00;\n\nmy $ffi = FFI::Platypus-\u003enew(\n  api =\u003e 2,\n  lib =\u003e './person.so',\n);\n\n$ffi-\u003etype( 'opaque' =\u003e 'person_t' );\n\n$ffi-\u003eattach( person_new =\u003e  ['string','unsigned int'] =\u003e 'person_t'       );\n$ffi-\u003eattach( person_name =\u003e ['person_t']              =\u003e 'string'       );\n$ffi-\u003eattach( person_age =\u003e  ['person_t']              =\u003e 'unsigned int' );\n$ffi-\u003eattach( person_free =\u003e ['person_t']                                  );\n\nmy $person = person_new( 'Roger Frooble Bits', 35 );\n\nprint \"name = \", person_name($person), \"\\n\";\nprint \"age  = \", person_age($person),  \"\\n\";\n\nperson_free($person);\n```\n\n### Execute\n\n```\n$ cc -shared -o person.so person.c\n$ perl person.pl\nname = Roger Frooble Bits\nage  = 35\n```\n\n### Discussion\n\nAn opaque pointer is a pointer (memory address) that is pointing to _something_\nbut you do not know the structure of that something.  In C this is usually a\n`void*`, but it could also be a pointer to a `struct` without a defined body.\n\nThis is often used to as an abstraction around objects in C.  Here in the C\ncode we have a `person_t` struct with functions to create (a constructor), free\n(a destructor) and query it (methods).\n\nThe Perl code can then use the constructor, methods and destructors without having\nto understand the internals.  The `person_t` internals can also be changed\nwithout having to modify the calling code.\n\nWe use the Platypus [type method](#type) to create an alias of `opaque` called\n`person_t`.  While this is not necessary, it does make the Perl code easier\nto understand.\n\nIn later examples we will see how to hide the use of `opaque` types further\nusing the `object` type, but for some code direct use of `opaque` is\nappropriate.\n\n## Opaque Pointers (buffers and strings)\n\n### C API\n\n- [cppreference - free](https://en.cppreference.com/w/c/memory/free)\n- [cppreference - malloc](https://en.cppreference.com/w/c/memory/malloc)\n- [cppreference - memcpy](https://en.cppreference.com/w/c/string/byte/memcpy)\n- [cppreference - strdup](https://en.cppreference.com/w/c/string/byte/strdup)\n\n### Perl Source\n\n```perl\nuse FFI::Platypus 2.00;\nuse FFI::Platypus::Memory qw( malloc free memcpy strdup );\n\nmy $ffi = FFI::Platypus-\u003enew( api =\u003e 2 );\nmy $buffer = malloc 14;\nmy $ptr_string = strdup(\"hello there!!\\n\");\n\nmemcpy $buffer, $ptr_string, 15;\n\nprint $ffi-\u003ecast('opaque' =\u003e 'string', $buffer);\n\nfree $ptr_string;\nfree $buffer;\n```\n\n### Execute\n\n```\n$ perl malloc.pl\nhello there!!\n```\n\n### Discussion\n\nAnother useful application of the `opaque` type is for dealing with buffers,\nand C strings that you do not immediately need to convert into Perl strings.\nThis example is completely contrived, but we are using `malloc` to create a\nbuffer of 14 bytes.  We create a C string using `strdup`, and then copy it\ninto the buffer using `memcpy`.  When we are done with the `opaque` pointers\nwe can free them using `free` since they. (This is generally only okay when\nfreeing memory that was allocated by `malloc`, which is the case for `strdup`).\n\nThese memory tools, along with others are provided by the [FFI::Platypus::Memory](https://metacpan.org/pod/FFI::Platypus::Memory)\nmodule, which is worth reviewing when you need to manipulate memory from\nPerl when writing your FFI code.\n\nJust to verify that the `memcpy` did the right thing we convert the\nbuffer into a Perl string and print it out using the Platypus [cast method](#cast).\n\n## Arrays\n\n### C Source\n\n```\nvoid\narray_reverse(int a[], int len) {\n  int tmp, i;\n\n  for(i=0; i \u003c len/2; i++) {\n    tmp = a[i];\n    a[i] = a[len-i-1];\n    a[len-i-1] = tmp;\n  }\n}\n\nvoid\narray_reverse10(int a[10]) {\n  array_reverse(a, 10);\n}\n```\n\n### Perl Source\n\n```perl\nuse FFI::Platypus 2.00;\n\nmy $ffi = FFI::Platypus-\u003enew(\n  api =\u003e 2,\n  lib =\u003e './array_reverse.so',\n);\n\n$ffi-\u003eattach( array_reverse   =\u003e ['int[]','int'] );\n$ffi-\u003eattach( array_reverse10 =\u003e ['int[10]'] );\n\nmy @a = (1..10);\narray_reverse10( \\@a );\nprint \"$_ \" for @a;\nprint \"\\n\";\n\n@a = (1..20);\narray_reverse( \\@a, 20 );\nprint \"$_ \" for @a;\nprint \"\\n\";\n```\n\n### Execute\n\n```\n$ cc -shared -o array_reverse.so array_reverse.c\n$ perl array_reverse.pl\n10 9 8 7 6 5 4 3 2 1\n20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1\n```\n\n### Discussion\n\nArrays in C are passed as pointers, so the C code here reverses the array\nin place, rather than returning it.  Arrays can also be fixed or variable\nlength.  If the array is variable length the length of the array must be\nprovided in some way.  In this case we explicitly pass in a length.  Another\nway might be to end the array with `0`, if you don't otherwise expect any\n`0` to appear in your data.  For this reason, Platypus adds a zero (or\n`NULL` in the case of pointers) element at the end of the array when passing\nit into a variable length array type, although we do not use it here.\n\nWith Platypus you can declare an array type as being either fixed or variable\nlength.  Because Perl stores arrays in completely differently than C, a\ntemporary array is created by Platypus, passed into the C function as a pointer.\nWhen the function returns the array is re-read by Platypus and the Perl array\nis updated with the new values.  The temporary array is then freed.\n\nYou can use any primitive type for arrays, even `string`.  You can also\nreturn an array from a function.  As in our discussion about strings, when\nyou return an array the value is copied, which is usually what you want.\n\n## Pointers as Arrays\n\n### C Source\n\n```\n#include \u003cstdlib.h\u003e\n\nint\narray_sum(const int *a) {\n  int i, sum;\n  if(a == NULL)\n    return -1;\n  for(i=0, sum=0; a[i] != 0; i++)\n    sum += a[i];\n  return sum;\n}\n```\n\n### Perl Source\n\n```perl\nuse FFI::Platypus 2.00;\n\nmy $ffi = FFI::Platypus-\u003enew(\n  api =\u003e 2,\n  lib =\u003e './array_sum.so',\n);\n\n$ffi-\u003eattach( array_sum =\u003e ['int*'] =\u003e 'int' );\n\nprint array_sum(undef), \"\\n\";     # -1\nprint array_sum([0]), \"\\n\";       # 0\nprint array_sum([1,2,3,0]), \"\\n\"; # 6\n```\n\n### Execute\n\n```\n$ cc -shared -o array_sum.so array_sum.c\n$ perl array_sum.pl\n-1\n0\n6\n```\n\n### Discussion\n\nStarting with the Platypus version 2 API, you can also pass an array reference\nin to a pointer argument.\n\nIn C pointer and array arguments are often used somewhat interchangeably.  In\nthis example we have an `array_sum` function that takes a zero terminated\narray of integers and computes the sum.  If the pointer to the array is zero\n(`0`) then we return `-1` to indicate an error.\n\nThis is the main advantage from Perl for using pointer argument rather than\nan array one: the array argument will not let you pass in `undef` / `NULL`.\n\n## Sending Strings to GUI on Unix with libnotify\n\n### C API\n\n[Libnotify Reference Manual](https://developer-old.gnome.org/libnotify/unstable)\n\n### Perl Source\n\n```perl\nuse FFI::CheckLib;\nuse FFI::Platypus 2.00;\n\nmy $ffi = FFI::Platypus-\u003enew(\n  api =\u003e 2,\n  lib =\u003e find_lib_or_die(lib =\u003e 'notify'),\n);\n\n$ffi-\u003eattach( notify_init              =\u003e ['string']                                  );\n$ffi-\u003eattach( notify_uninit            =\u003e []                                          );\n$ffi-\u003eattach( notify_notification_new  =\u003e ['string', 'string', 'string']  =\u003e 'opaque' );\n$ffi-\u003eattach( notify_notification_show =\u003e ['opaque', 'opaque']                        );\n\nmy $message = join \"\\n\",\n  \"Hello from Platypus!\",\n  \"Welcome to the fun\",\n  \"world of FFI\";\n\nnotify_init('Platypus Hello');\nmy $n = notify_notification_new('Platypus Hello World', $message, 'dialog-information');\nnotify_notification_show($n, undef);\nnotify_uninit();\n```\n\n### Execute\n\n```\n$ perl notify.pl\n```\n\n\u003cdiv\u003e\n    \u003cp\u003eAnd this is what it will look like:\u003c/p\u003e\n    \u003cdiv style=\"display: flex\"\u003e\n    \u003cdiv style=\"margin: 3px; flex: 1 1 50%\"\u003e\n    \u003cimg alt=\"Test\" src=\"/examples//notify.png\"\u003e\n    \u003c/div\u003e\n    \u003c/div\u003e\n\u003c/div\u003e\n\n### Discussion\n\nThe GNOME project provides an API to send notifications to its desktop environment.\nNothing here is particularly new: all of the types and techniques are ones that we\nhave seen before, except we are using a third party library, instead of using our\nown C code or the standard C library functions.\n\nWhen using a third party library you have to know the name or location of it, which\nis not typically portable, so here we use [FFI::CheckLib](https://metacpan.org/pod/FFI::CheckLib)'s\n[find\\_lib\\_or\\_die function](https://metacpan.org/pod/FFI::CheckLib#find_lib_or_die).  If the library is not\nfound the script will die with a useful diagnostic.  [FFI::CheckLib](https://metacpan.org/pod/FFI::CheckLib) has a number\nof useful features and will integrate nicely with [Alien::Build](https://metacpan.org/pod/Alien::Build) based [Alien](https://metacpan.org/pod/Alien)s.\n\n## The Win32 API with MessageBoxW\n\n### Win32 API\n\n[MessageBoxW function (winuser.h)](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-messageboxw)\n\n### Perl Source\n\n```perl\nuse utf8;\nuse FFI::Platypus 2.00;\n\nmy $ffi = FFI::Platypus-\u003enew(\n  api  =\u003e 2,\n  lib  =\u003e [undef],\n);\n\n# see FFI::Platypus::Lang::Win32\n$ffi-\u003elang('Win32');\n\n# Send a Unicode string to the Windows API MessageBoxW function.\nuse constant MB_OK                   =\u003e 0x00000000;\nuse constant MB_DEFAULT_DESKTOP_ONLY =\u003e 0x00020000;\n$ffi-\u003eattach( [MessageBoxW =\u003e 'MessageBox'] =\u003e [ 'HWND', 'LPCWSTR', 'LPCWSTR', 'UINT'] =\u003e 'int' );\nMessageBox(undef, \"I ❤️ Platypus\", \"Confession\", MB_OK|MB_DEFAULT_DESKTOP_ONLY);\n```\n\n### Execute\n\n```\n$ perl win32_messagebox.pl\n```\n\n\u003cdiv\u003e\n    \u003cp\u003eAnd this is what it will look like:\u003c/p\u003e\n    \u003cdiv style=\"display: flex\"\u003e\n    \u003cdiv style=\"margin: 3px; flex: 1 1 50%\"\u003e\n    \u003cimg alt=\"Test\" src=\"/examples/win32_messagebox.png\"\u003e\n    \u003c/div\u003e\n    \u003c/div\u003e\n\u003c/div\u003e\n\n### Discussion\n\nThe API used by Microsoft Windows presents some unique\nchallenges.  On 32 bit systems a different ABI is used than what\nis used by the standard C library.  It also provides a rats nest of\ntype aliases.  Finally if you want to talk Unicode to any of the\nWindows API you will need to use `UTF-16LE` instead of `UTF-8`\nwhich is native to Perl.  (The Win32 API refers to these as\n`LPWSTR` and `LPCWSTR` types).  As much as possible the Win32\n\"language\" plugin attempts to handle these challenges transparently.\nFor more details see [FFI::Platypus::Lang::Win32](https://metacpan.org/pod/FFI::Platypus::Lang::Win32).\n\n### Discussion\n\nThe libnotify library is a desktop GUI notification system for the\nGNOME Desktop environment. This script sends a notification event that\nshould show up as a balloon, for me it did so in the upper right hand\ncorner of my screen.\n\n## Structured Data Records (by pointer or by reference)\n\n### C API\n\n[cppreference - localtime](https://en.cppreference.com/w/c/chrono/localtime)\n\n### Perl Source\n\n```perl\nuse FFI::Platypus 2.00;\nuse FFI::C;\n\nmy $ffi = FFI::Platypus-\u003enew(\n  api =\u003e 2,\n  lib =\u003e [undef],\n);\nFFI::C-\u003effi($ffi);\n\npackage Unix::TimeStruct {\n\n  FFI::C-\u003estruct(tm =\u003e [\n    tm_sec    =\u003e 'int',\n    tm_min    =\u003e 'int',\n    tm_hour   =\u003e 'int',\n    tm_mday   =\u003e 'int',\n    tm_mon    =\u003e 'int',\n    tm_year   =\u003e 'int',\n    tm_wday   =\u003e 'int',\n    tm_yday   =\u003e 'int',\n    tm_isdst  =\u003e 'int',\n    tm_gmtoff =\u003e 'long',\n    _tm_zone  =\u003e 'opaque',\n  ]);\n\n  # For now 'string' is unsupported by FFI::C, but we\n  # can cast the time zone from an opaque pointer to\n  # string.\n  sub tm_zone {\n    my $self = shift;\n    $ffi-\u003ecast('opaque', 'string', $self-\u003e_tm_zone);\n  }\n\n  # attach the C localtime function\n  $ffi-\u003eattach( localtime =\u003e ['time_t*'] =\u003e 'tm', sub {\n    my($inner, $class, $time) = @_;\n    $time = time unless defined $time;\n    $inner-\u003e(\\$time);\n  });\n}\n\n# now we can actually use our Unix::TimeStruct class\nmy $time = Unix::TimeStruct-\u003elocaltime;\nprintf \"time is %d:%d:%d %s\\n\",\n  $time-\u003etm_hour,\n  $time-\u003etm_min,\n  $time-\u003etm_sec,\n  $time-\u003etm_zone;\n```\n\n### Execute\n\n```\n$ perl time_struct.pl\ntime is 3:48:19 MDT\n```\n\n### Discussion\n\nC and other machine code languages frequently provide interfaces that\ninclude structured data records (defined using the `struct` keyword\nin C).  Some libraries will provide an API which you are expected to read\nor write before and/or after passing them along to the library.\n\nFor C pointers to `strict`, `union`, nested `struct` and nested\n`union` structures, the easiest interface to use is via [FFI::C](https://metacpan.org/pod/FFI::C).\nIf you are working with a `struct` that must be passed by value\n(not pointers), then you will want to use [FFI::Platypus::Record](https://metacpan.org/pod/FFI::Platypus::Record)\nclass instead.  We will discuss an example of that next.\n\nThe C `localtime` function takes a pointer to a C struct.  We simply define\nthe members of the struct using the [FFI::C](https://metacpan.org/pod/FFI::C) `struct` method.  Because\nwe used the `ffi` method to tell [FFI::C](https://metacpan.org/pod/FFI::C) to use our local instance of\n[FFI::Platypus](https://metacpan.org/pod/FFI::Platypus) it registers the `tm` type for us, and we can just start\nusing it as a return type!\n\n## Structured Data Records (on stack or by value)\n\n### C Source\n\n```\n#include \u003cstdint.h\u003e\n#include \u003cstring.h\u003e\n\ntypedef struct color_t {\n   char    name[8];\n   uint8_t red;\n   uint8_t green;\n   uint8_t blue;\n} color_t;\n\ncolor_t\ncolor_increase_red(color_t color, uint8_t amount)\n{\n  strcpy(color.name, \"reddish\");\n  color.red += amount;\n  return color;\n}\n```\n\n### Perl Source\n\n```perl\nuse FFI::Platypus 2.00;\n\nmy $ffi = FFI::Platypus-\u003enew(\n  api =\u003e 2,\n  lib =\u003e './color.so'\n);\n\npackage Color {\n\n  use FFI::Platypus::Record;\n  use overload\n    '\"\"' =\u003e sub { shift-\u003eas_string },\n    bool =\u003e sub { 1 }, fallback =\u003e 1;\n\n  record_layout_1($ffi,\n    'string(8)' =\u003e 'name', qw(\n    uint8     red\n    uint8     green\n    uint8     blue\n  ));\n\n  sub as_string {\n    my($self) = @_;\n    sprintf \"%s: [red:%02x green:%02x blue:%02x]\",\n      $self-\u003ename, $self-\u003ered, $self-\u003egreen, $self-\u003eblue;\n  }\n\n}\n\n$ffi-\u003etype('record(Color)' =\u003e 'color_t');\n$ffi-\u003eattach( color_increase_red =\u003e ['color_t','uint8'] =\u003e 'color_t' );\n\nmy $gray = Color-\u003enew(\n  name  =\u003e 'gray',\n  red   =\u003e 0xDC,\n  green =\u003e 0xDC,\n  blue  =\u003e 0xDC,\n);\n\nmy $slightly_red = color_increase_red($gray, 20);\n\nprint \"$gray\\n\";\nprint \"$slightly_red\\n\";\n```\n\n### Execute\n\n```\n$ cc -shared -o color.so color.c\n$ perl color.pl\ngray: [red:dc green:dc blue:dc]\nreddish: [red:f0 green:dc blue:dc]\n```\n\n### Discussion\n\nIn the C source of this example, we pass a C `struct` by value by\ncopying it onto the stack.  On the Perl side we create a `Color` class\nusing [FFI::Platypus::Record](https://metacpan.org/pod/FFI::Platypus::Record), which allows us to pass the structure\nthe way the C source wants us to.\n\nGenerally you should only reach for [FFI::Platypus::Record](https://metacpan.org/pod/FFI::Platypus::Record) if you\nneed to pass small records on the stack like this.  For more complicated\n(including nested) data you want to use [FFI::C](https://metacpan.org/pod/FFI::C) using pointers.\n\n## Avoiding Copy Using Memory Windows (with libzmq3)\n\n### C API\n\n[ØMQ/3.2.6 API Reference](http://api.zeromq.org/3-2:_start)\n\n### Perl Source\n\n```perl\nuse constant ZMQ_IO_THREADS  =\u003e 1;\nuse constant ZMQ_MAX_SOCKETS =\u003e 2;\nuse constant ZMQ_REQ =\u003e 3;\nuse constant ZMQ_REP =\u003e 4;\nuse FFI::CheckLib qw( find_lib_or_die );\nuse FFI::Platypus 2.00;\nuse FFI::Platypus::Memory qw( malloc );\nuse FFI::Platypus::Buffer qw( scalar_to_buffer window );\n\nmy $endpoint = \"ipc://zmq-ffi-$$\";\nmy $ffi = FFI::Platypus-\u003enew(\n  api =\u003e 2,\n  lib =\u003e find_lib_or_die lib =\u003e 'zmq',\n);\n\n$ffi-\u003eattach(zmq_version =\u003e ['int*', 'int*', 'int*'] =\u003e 'void');\n\nmy($major,$minor,$patch);\nzmq_version(\\$major, \\$minor, \\$patch);\nprint \"libzmq version $major.$minor.$patch\\n\";\ndie \"this script only works with libzmq 3 or better\" unless $major \u003e= 3;\n\n$ffi-\u003etype('opaque'       =\u003e 'zmq_context');\n$ffi-\u003etype('opaque'       =\u003e 'zmq_socket');\n$ffi-\u003etype('opaque'       =\u003e 'zmq_msg_t');\n$ffi-\u003eattach(zmq_ctx_new  =\u003e [] =\u003e 'zmq_context');\n$ffi-\u003eattach(zmq_ctx_set  =\u003e ['zmq_context', 'int', 'int'] =\u003e 'int');\n$ffi-\u003eattach(zmq_socket   =\u003e ['zmq_context', 'int'] =\u003e 'zmq_socket');\n$ffi-\u003eattach(zmq_connect  =\u003e ['opaque', 'string'] =\u003e 'int');\n$ffi-\u003eattach(zmq_bind     =\u003e ['zmq_socket', 'string'] =\u003e 'int');\n$ffi-\u003eattach(zmq_send     =\u003e ['zmq_socket', 'opaque', 'size_t', 'int'] =\u003e 'int');\n$ffi-\u003eattach(zmq_msg_init =\u003e ['zmq_msg_t'] =\u003e 'int');\n$ffi-\u003eattach(zmq_msg_recv =\u003e ['zmq_msg_t', 'zmq_socket', 'int'] =\u003e 'int');\n$ffi-\u003eattach(zmq_msg_data =\u003e ['zmq_msg_t'] =\u003e 'opaque');\n$ffi-\u003eattach(zmq_errno    =\u003e [] =\u003e 'int');\n$ffi-\u003eattach(zmq_strerror =\u003e ['int'] =\u003e 'string');\n\nmy $context = zmq_ctx_new();\nzmq_ctx_set($context, ZMQ_IO_THREADS, 1);\n\nmy $socket1 = zmq_socket($context, ZMQ_REQ);\nzmq_connect($socket1, $endpoint);\n\nmy $socket2 = zmq_socket($context, ZMQ_REP);\nzmq_bind($socket2, $endpoint);\n\n{ # send\n  our $sent_message = \"hello there\";\n  my($pointer, $size) = scalar_to_buffer $sent_message;\n  my $r = zmq_send($socket1, $pointer, $size, 0);\n  die zmq_strerror(zmq_errno()) if $r == -1;\n}\n\n{ # recv\n  my $msg_ptr  = malloc 100;\n  zmq_msg_init($msg_ptr);\n  my $size     = zmq_msg_recv($msg_ptr, $socket2, 0);\n  die zmq_strerror(zmq_errno()) if $size == -1;\n  my $data_ptr = zmq_msg_data($msg_ptr);\n  window(my $recv_message, $data_ptr, $size);\n  print \"recv_message = $recv_message\\n\";\n}\n```\n\n### Execute\n\n```\n$ perl zmq3.pl\nlibzmq version 4.3.4\nrecv_message = hello there\n```\n\n### Discussion\n\nØMQ is a high-performance asynchronous messaging library. There are a\nfew things to note here.\n\nFirstly, sometimes there may be multiple versions of a library in the\nwild and you may need to verify that the library on a system meets your\nneeds (alternatively you could support multiple versions and configure\nyour bindings dynamically).  Here we use `zmq_version` to ask libzmq\nwhich version it is.\n\n`zmq_version` returns the version number via three integer pointer\narguments, so we use the pointer to integer type: `int *`.  In order to\npass pointer types, we pass a reference. In this case it is a reference\nto an undefined value, because zmq\\_version will write into the pointers\nthe output values, but you can also pass in references to integers,\nfloating point values and opaque pointer types.  When the function\nreturns the `$major` variable (and the others) has been updated and we\ncan use it to verify that it supports the API that we require.\n\nFinally we attach the necessary functions, send and receive a message.\nWhen we receive we use the [FFI::Platypus::Buffer](https://metacpan.org/pod/FFI::Platypus::Buffer) function `window`\ninstead of `buffer_to_scalar`.  They have a similar effect in that\nthe provide a scalar from a region of memory, but `window` doesn't\nhave to copy any data, so it is cheaper to call.  The only downside\nis that a windowed scalar like this is read-only.\n\n## libarchive\n\n### C Documentation\n\n[https://www.libarchive.org/](https://www.libarchive.org/)\n\n### Perl Source\n\n```perl\nuse FFI::Platypus 2.00;\nuse FFI::CheckLib qw( find_lib_or_die );\n\n# This example uses FreeBSD's libarchive to list the contents of any\n# archive format that it suppors.  We've also filled out a part of\n# the ArchiveWrite class that could be used for writing archive formats\n# supported by libarchive\n\nmy $ffi = FFI::Platypus-\u003enew(\n  api =\u003e 2,\n  lib =\u003e find_lib_or_die(lib =\u003e 'archive'),\n);\n$ffi-\u003etype('object(Archive)'      =\u003e 'archive_t');\n$ffi-\u003etype('object(ArchiveRead)'  =\u003e 'archive_read_t');\n$ffi-\u003etype('object(ArchiveWrite)' =\u003e 'archive_write_t');\n$ffi-\u003etype('object(ArchiveEntry)' =\u003e 'archive_entry_t');\n\npackage Archive {\n  # base class is \"abstract\" having no constructor or destructor\n\n  $ffi-\u003emangler(sub {\n    my($name) = @_;\n    \"archive_$name\";\n  });\n  $ffi-\u003eattach( error_string =\u003e ['archive_t'] =\u003e 'string' );\n}\n\npackage ArchiveRead {\n  our @ISA = qw( Archive );\n\n  $ffi-\u003emangler(sub {\n    my($name) = @_;\n    \"archive_read_$name\";\n  });\n\n  $ffi-\u003eattach( new                   =\u003e ['string']                        =\u003e 'archive_read_t' );\n  $ffi-\u003eattach( [ free =\u003e 'DESTROY' ] =\u003e ['archive_t']                                         );\n  $ffi-\u003eattach( support_filter_all    =\u003e ['archive_t']                     =\u003e 'int'            );\n  $ffi-\u003eattach( support_format_all    =\u003e ['archive_t']                     =\u003e 'int'            );\n  $ffi-\u003eattach( open_filename         =\u003e ['archive_t','string','size_t']   =\u003e 'int'            );\n  $ffi-\u003eattach( next_header2          =\u003e ['archive_t', 'archive_entry_t' ] =\u003e 'int'            );\n  $ffi-\u003eattach( data_skip             =\u003e ['archive_t']                     =\u003e 'int'            );\n  # ... define additional read methods\n}\n\npackage ArchiveWrite {\n\n  our @ISA = qw( Archive );\n\n  $ffi-\u003emangler(sub {\n    my($name) = @_;\n    \"archive_write_$name\";\n  });\n\n  $ffi-\u003eattach( new                   =\u003e ['string'] =\u003e 'archive_write_t' );\n  $ffi-\u003eattach( [ free =\u003e 'DESTROY' ] =\u003e ['archive_write_t'] );\n  # ... define additional write methods\n}\n\npackage ArchiveEntry {\n\n  $ffi-\u003emangler(sub {\n    my($name) = @_;\n    \"archive_entry_$name\";\n  });\n\n  $ffi-\u003eattach( new =\u003e ['string']     =\u003e 'archive_entry_t' );\n  $ffi-\u003eattach( [ free =\u003e 'DESTROY' ] =\u003e ['archive_entry_t'] );\n  $ffi-\u003eattach( pathname              =\u003e ['archive_entry_t'] =\u003e 'string' );\n  # ... define additional entry methods\n}\n\nuse constant ARCHIVE_OK =\u003e 0;\n\n# this is a Perl version of the C code here:\n# https://github.com/libarchive/libarchive/wiki/Examples#List_contents_of_Archive_stored_in_File\n\nmy $archive_filename = shift @ARGV;\nunless(defined $archive_filename)\n{\n  print \"usage: $0 archive.tar\\n\";\n  exit;\n}\n\nmy $archive = ArchiveRead-\u003enew;\n$archive-\u003esupport_filter_all;\n$archive-\u003esupport_format_all;\n\nmy $r = $archive-\u003eopen_filename($archive_filename, 1024);\ndie \"error opening $archive_filename: \", $archive-\u003eerror_string\n  unless $r == ARCHIVE_OK;\n\nmy $entry = ArchiveEntry-\u003enew;\n\nwhile($archive-\u003enext_header2($entry) == ARCHIVE_OK)\n{\n  print $entry-\u003epathname, \"\\n\";\n  $archive-\u003edata_skip;\n}\n```\n\n### Execute\n\n```\n$ perl archive_object.pl archive.tar\narchive.pl\narchive_object.pl\n```\n\n### Discussion\n\nlibarchive is the implementation of `tar` for FreeBSD provided as a\nlibrary and available on a number of platforms.\n\nOne interesting thing about libarchive is that it provides a kind of\nobject oriented interface via opaque pointers.  This example creates an\nabstract class `Archive`, and concrete classes `ArchiveWrite`,\n`ArchiveRead` and `ArchiveEntry`.  The concrete classes can even be\ninherited from and extended just like any Perl classes because of the\nway the custom types are implemented.  We use Platypus's `object`\ntype for this implementation, which is a wrapper around an `opaque`\n(can also be an integer) type that is blessed into a particular class.\n\nAnother advanced feature of this example is that we define a mangler\nto modify the symbol resolution for each class.  This means we can do\nthis when we define a method for Archive:\n\n```perl\n$ffi-\u003eattach( support_filter_all =\u003e ['archive_t'] =\u003e 'int' );\n```\n\nRather than this:\n\n```perl\n$ffi-\u003eattach(\n  [ archive_read_support_filter_all =\u003e 'support_read_filter_all' ] =\u003e\n  ['archive_t'] =\u003e 'int' );\n);\n```\n\nAs nice as `libarchive` is, note that we have to shoehorn then\n`archive_free` function name into the Perl convention of using\n`DESTROY` as the destructor.  We can easily do that for just this\none function with:\n\n```perl\n$ffi-\u003eattach( [ free =\u003e 'DESTROY' ] =\u003e ['archive_t'] );\n```\n\nThe `libarchive` is a large library with hundreds of methods.\nFor comprehensive FFI bindings for `libarchive` see [Archive::Libarchive](https://metacpan.org/pod/Archive::Libarchive).\n\n## unix open\n\n### C API\n\n[Input-output system calls in C](https://www.geeksforgeeks.org/input-output-system-calls-c-create-open-close-read-write/)\n\n### Perl Source\n\n```perl\nuse FFI::Platypus 2.00;\n\n{\n  package FD;\n\n  use constant O_RDONLY =\u003e 0;\n  use constant O_WRONLY =\u003e 1;\n  use constant O_RDWR   =\u003e 2;\n\n  use constant IN  =\u003e bless \\do { my $in=0  }, __PACKAGE__;\n  use constant OUT =\u003e bless \\do { my $out=1 }, __PACKAGE__;\n  use constant ERR =\u003e bless \\do { my $err=2 }, __PACKAGE__;\n\n  my $ffi = FFI::Platypus-\u003enew( api =\u003e 2, lib =\u003e [undef]);\n\n  $ffi-\u003etype('object(FD,int)' =\u003e 'fd');\n\n  $ffi-\u003eattach( [ 'open' =\u003e 'new' ] =\u003e [ 'string', 'int', 'mode_t' ] =\u003e 'fd' =\u003e sub {\n    my($xsub, $class, $fn, @rest) = @_;\n    my $fd = $xsub-\u003e($fn, @rest);\n    die \"error opening $fn $!\" if $$fd == -1;\n    $fd;\n  });\n\n  $ffi-\u003eattach( write =\u003e ['fd', 'string', 'size_t' ] =\u003e 'ssize_t' );\n  $ffi-\u003eattach( read  =\u003e ['fd', 'string', 'size_t' ] =\u003e 'ssize_t' );\n  $ffi-\u003eattach( close =\u003e ['fd'] =\u003e 'int' );\n}\n\nmy $fd = FD-\u003enew(\"file_handle.txt\", FD::O_RDONLY);\n\nmy $buffer = \"\\0\" x 10;\n\nwhile(my $br = $fd-\u003eread($buffer, 10))\n{\n  FD::OUT-\u003ewrite($buffer, $br);\n}\n\n$fd-\u003eclose;\n```\n\n### Execute\n\n```\n$ perl file_handle.pl\nHello World\n```\n\n### Discussion\n\nThe Unix file system calls use an integer handle for each open file.\nWe can use the same `object` type that we used for libarchive above,\nexcept we let platypus know that the underlying type is `int` instead\nof `opaque` (the latter being the default for the `object` type).\nMainly just for demonstration since Perl has much better IO libraries,\nbut now we have an OO interface to the Unix IO functions.\n\n## Varadic Functions (with libcurl)\n\n### C API\n\n- [curl\\_easy\\_init](https://curl.se/libcurl/c/curl_easy_init.html)\n- [curl\\_easy\\_setopt](https://curl.se/libcurl/c/curl_easy_setopt.html)\n- [curl\\_easy\\_perform](https://curl.se/libcurl/c/curl_easy_perform.html)\n- [curl\\_easy\\_cleanup](https://curl.se/libcurl/c/curl_easy_cleanup.html)\n- [CURLOPT\\_URL](https://curl.se/libcurl/c/CURLOPT_URL.html)\n\n### Perl Source\n\n```perl\nuse FFI::Platypus 2.00;\nuse FFI::CheckLib qw( find_lib_or_die );\nuse constant CURLOPT_URL =\u003e 10002;\n\nmy $ffi = FFI::Platypus-\u003enew(\n  api =\u003e 2,\n  lib =\u003e find_lib_or_die(lib =\u003e 'curl'),\n);\n\nmy $curl_handle = $ffi-\u003efunction( 'curl_easy_init' =\u003e [] =\u003e 'opaque' )\n                      -\u003ecall;\n\n$ffi-\u003efunction( 'curl_easy_setopt' =\u003e ['opaque', 'enum' ] =\u003e ['string'] )\n    -\u003ecall($curl_handle, CURLOPT_URL, \"https://pl.atypus.org\" );\n\n$ffi-\u003efunction( 'curl_easy_perform' =\u003e ['opaque' ] =\u003e 'enum' )\n    -\u003ecall($curl_handle);\n\n$ffi-\u003efunction( 'curl_easy_cleanup' =\u003e ['opaque' ] )\n    -\u003ecall($curl_handle);\n```\n\n### Execute\n\n```\n$ perl curl.pl\n\u003c!doctype html\u003e\n\u003chtml lang=\"en\"\u003e\n  \u003chead\u003e\n    \u003cmeta charset=\"utf-8\" /\u003e\n    \u003ctitle\u003epl.atypus.org - Home for the Perl Platypus Project\u003c/title\u003e\n...\n```\n\n### Discussion\n\nThe [libcurl](https://curl.se/) library makes extensive use of \"varadic\" functions.\n\nThe C programming language and ABI have the concept of \"varadic\" functions\nthat can take a variable number and variable type of arguments.  Assuming\nyou have a `libffi` that supports it (and most modern systems should),\nthen you can create bindings to a varadic function by providing two sets\nof array references, one for the fixed arguments (for reasons, C varadic\nfunctions must have at least one) and one for variable arguments.  In\nthis example we call `curl_easy_setopt` as a varadic function.\n\nFor functions that have a large or infinite number of possible signatures\nit may be impracticable or impossible to attach them all.  You can instead\ndo as we did in this example, create a function object using the\n[function method](#function) and call it immediately.  This is not as\nperformant either when you create or call as using the [attach method](#attach),\nbut in some cases the performance penalty may be worth it or unavoidable.\n\n## Callbacks (with libcurl)\n\n### C API\n\n- [curl\\_easy\\_init](https://curl.se/libcurl/c/curl_easy_init.html)\n- [curl\\_easy\\_setopt](https://curl.se/libcurl/c/curl_easy_setopt.html)\n- [curl\\_easy\\_perform](https://curl.se/libcurl/c/curl_easy_perform.html)\n- [curl\\_easy\\_cleanup](https://curl.se/libcurl/c/curl_easy_cleanup.html)\n- [CURLOPT\\_URL](https://curl.se/libcurl/c/CURLOPT_URL.html)\n- [CURLOPT\\_WRITEFUNCTION](https://curl.se/libcurl/c/CURLOPT_WRITEFUNCTION.html)\n\n### Perl Source\n\n```perl\nuse FFI::Platypus 2.00;\nuse FFI::CheckLib qw( find_lib_or_die );\nuse FFI::Platypus::Buffer qw( window );\nuse constant CURLOPT_URL           =\u003e 10002;\nuse constant CURLOPT_WRITEFUNCTION =\u003e 20011;\n\nmy $ffi = FFI::Platypus-\u003enew(\n  api =\u003e 2,\n  lib =\u003e find_lib_or_die(lib =\u003e 'curl'),\n);\n\nmy $curl_handle = $ffi-\u003efunction( 'curl_easy_init' =\u003e [] =\u003e 'opaque' )\n                      -\u003ecall;\n\n$ffi-\u003efunction( 'curl_easy_setopt' =\u003e [ 'opaque', 'enum' ] =\u003e ['string'] )\n    -\u003ecall($curl_handle, CURLOPT_URL, \"https://pl.atypus.org\" );\n\nmy $html;\n\nmy $closure = $ffi-\u003eclosure(sub {\n  my($ptr, $len, $num, $user) = @_;\n  window(my $buf, $ptr, $len*$num);\n  $html .= $buf;\n  return $len*$num;\n});\n\n$ffi-\u003efunction( 'curl_easy_setopt' =\u003e [ 'opaque', 'enum' ] =\u003e ['(opaque,size_t,size_t,opaque)-\u003esize_t'] =\u003e 'enum' )\n    -\u003ecall($curl_handle, CURLOPT_WRITEFUNCTION, $closure);\n\n$ffi-\u003efunction( 'curl_easy_perform' =\u003e [ 'opaque' ] =\u003e 'enum' )\n    -\u003ecall($curl_handle);\n\n$ffi-\u003efunction( 'curl_easy_cleanup' =\u003e [ 'opaque' ] )\n    -\u003ecall($curl_handle);\n\nif($html =~ /\u003ctitle\u003e(.*?)\u003c\\/title\u003e/) {\n  print \"$1\\n\";\n}\n```\n\n### Execute\n\n```\n$ perl curl_callback.pl\npl.atypus.org - Home for the Perl Platypus Project\n```\n\n### Discussion\n\nThis example is similar to the previous one, except instead of letting\n[libcurl](https://curl.se) write the content body to `STDOUT`, we give\nit a callback to send the data to instead.  The [closure method](#closure)\ncan be used to create a callback function pointer that can be called from\nC.  The type for the callback is in the form `(arg_type,arg_type,etc)-\u003ereturn_type`\nwhere the argument types are in parentheticals with an arrow between the\nargument types and the return type.\n\nInside the closure or callback we use the [window function](https://metacpan.org/pod/FFI::Platypus::Buffer#window)\nfrom [FFI::Platypus::Buffer](https://metacpan.org/pod/FFI::Platypus::Buffer) again to avoid an _extra_ copy.  We still\nhave to copy the buffer to append it to `$hmtl` but it is at least one\nless copy.\n\n## bundle your own code\n\n### C Source\n\n`ffi/foo.c`:\n\n```\n#include \u003cffi_platypus_bundle.h\u003e\n#include \u003cstring.h\u003e\n\ntypedef struct {\n  char *name;\n  int value;\n} foo_t;\n\nfoo_t*\nfoo__new(const char *class_name, const char *name, int value) {\n  (void)class_name;\n  foo_t *self = malloc( sizeof( foo_t ) );\n  self-\u003ename = strdup(name);\n  self-\u003evalue = value;\n  return self;\n}\n\nconst char *\nfoo__name(foo_t *self) {\n  return self-\u003ename;\n}\n\nint\nfoo__value(foo_t *self) {\n  return self-\u003evalue;\n}\n\nvoid\nfoo__DESTROY(foo_t *self) {\n  free(self-\u003ename);\n  free(self);\n}\n```\n\n### Perl Source\n\n`lib/Foo.pm`:\n\n```perl\npackage Foo;\n\nuse strict;\nuse warnings;\nuse FFI::Platypus 2.00;\n\nmy $ffi = FFI::Platypus-\u003enew( api =\u003e 2 );\n\n$ffi-\u003etype('object(Foo)' =\u003e 'foo_t');\n$ffi-\u003emangler(sub {\n  my $name = shift;\n  $name =~ s/^/foo__/;\n  $name;\n});\n\n$ffi-\u003ebundle;\n\n$ffi-\u003eattach( new =\u003e     [ 'string', 'string', 'int' ] =\u003e 'foo_t'  );\n$ffi-\u003eattach( name =\u003e    [ 'foo_t' ]                   =\u003e 'string' );\n$ffi-\u003eattach( value =\u003e   [ 'foo_t' ]                   =\u003e 'int'    );\n$ffi-\u003eattach( DESTROY =\u003e [ 'foo_t' ]                   =\u003e 'void'   );\n\n1;\n```\n\n`t/foo.t`:\n\n```perl\nuse Test2::V0;\nuse Foo;\n\nmy $foo = Foo-\u003enew(\"platypus\", 10);\nisa_ok $foo, 'Foo';\nis $foo-\u003ename, \"platypus\";\nis $foo-\u003evalue, 10;\n\ndone_testing;\n```\n\n`Makefile.PL`:\n\n```perl\nuse ExtUtils::MakeMaker;\nuse FFI::Build::MM;\nmy $fbmm = FFI::Build::MM-\u003enew;\nWriteMakefile(\n  $fbmm-\u003emm_args(\n    NAME     =\u003e 'Foo',\n    DISTNAME =\u003e 'Foo',\n    VERSION  =\u003e '1.00',\n    # ...\n  )\n);\n\nsub MY::postamble\n{\n  $fbmm-\u003emm_postamble;\n}\n```\n\n### Execute\n\nWith prove:\n\n```\n$ prove -lvm\nt/foo.t ..\n# Seeded srand with seed '20221105' from local date.\nok 1 - Foo=SCALAR-\u003eisa('Foo')\nok 2\nok 3\n1..3\nok\nAll tests successful.\nFiles=1, Tests=3,  0 wallclock secs ( 0.00 usr  0.00 sys +  0.10 cusr  0.00 csys =  0.10 CPU)\nResult: PASS\n```\n\nWith [ExtUtils::MakeMaker](https://metacpan.org/pod/ExtUtils::MakeMaker):\n\n```\n$ perl Makefile.PL\nGenerating a Unix-style Makefile\nWriting Makefile for Foo\nWriting MYMETA.yml and MYMETA.json\n$ make\ncp lib/Foo.pm blib/lib/Foo.pm\n\"/home/ollisg/opt/perl/5.37.5/bin/perl5.37.5\" -MFFI::Build::MM=cmd -e fbx_build\nCC ffi/foo.c\nLD blib/lib/auto/share/dist/Foo/lib/libFoo.so\n$ make test\n\"/home/ollisg/opt/perl/5.37.5/bin/perl5.37.5\" -MFFI::Build::MM=cmd -e fbx_build\n\"/home/ollisg/opt/perl/5.37.5/bin/perl5.37.5\" -MFFI::Build::MM=cmd -e fbx_test\nPERL_DL_NONLAZY=1 \"/home/ollisg/opt/perl/5.37.5/bin/perl5.37.5\" \"-MExtUtils::Command::MM\" \"-MTest::Harness\" \"-e\" \"undef *Test::Harness::Switches; test_harness(0, 'blib/lib', 'blib/arch')\" t/*.t\nt/foo.t .. ok\nAll tests successful.\nFiles=1, Tests=3,  1 wallclock secs ( 0.00 usr  0.00 sys +  0.03 cusr  0.00 csys =  0.03 CPU)\nResult: PASS\n```\n\n### Discussion\n\nYou can bundle your own C code with your Perl extension.  There are a number\nof reasons you might want to do this  Sometimes you need to optimize a\ntight loop for speed.  Or you might need a little bit of glue code for your\nbindings to a library that isn't inherently FFI friendly.  Either way\nwhat you want is the [FFI::Build](https://metacpan.org/pod/FFI::Build) system on the install step and the\n[FFI::Platypus::Bundle](https://metacpan.org/pod/FFI::Platypus::Bundle) interface on the runtime step.  If you are using\n[Dist::Zilla](https://metacpan.org/pod/Dist::Zilla) for your distribution, you will also want to check out the\n[Dist::Zilla::Plugin::FFI::Build](https://metacpan.org/pod/Dist::Zilla::Plugin::FFI::Build) plugin to make this as painless as possible.\n\nOne of the nice things about the bundle interface is that it is smart enough to\nwork with either [App::Prove](https://metacpan.org/pod/App::Prove) or [ExtUtils::MakeMaker](https://metacpan.org/pod/ExtUtils::MakeMaker).  This means, unlike\nXS, you do not need to explicitly compile your C code in development mode, that\nwill be done for you when you call `$ffi-\u003ebundle`\n\n# FAQ\n\n## How do I get constants defined as macros in C header files\n\nThis turns out to be a challenge for any language calling into C, which\nfrequently uses `#define` macros to define constants like so:\n\n```\n#define FOO_STATIC  1\n#define FOO_DYNAMIC 2\n#define FOO_OTHER   3\n```\n\nAs macros are expanded and their definitions are thrown away by the C pre-processor\nthere isn't any way to get the name/value mappings from the compiled dynamic\nlibrary.\n\nYou can manually create equivalent constants in your Perl source:\n\n```perl\nuse constant FOO_STATIC  =\u003e 1;\nuse constant FOO_DYNAMIC =\u003e 2;\nuse constant FOO_OTHER   =\u003e 3;\n```\n\nIf there are a lot of these types of constants you might want to consider using\na tool ([Convert::Binary::C](https://metacpan.org/pod/Convert::Binary::C) can do this) that can extract the constants for you.\n\nSee also the \"Integer constants\" example in [FFI::Platypus::Type](https://metacpan.org/pod/FFI::Platypus::Type).\n\nYou can also use the new Platypus bundle interface to define Perl constants\nfrom C space.  This is more reliable, but does require a compiler at install\ntime.  It is recommended mainly for writing bindings against libraries that\nhave constants that can vary widely from platform to platform.  See\n[FFI::Platypus::Constant](https://metacpan.org/pod/FFI::Platypus::Constant) for details.\n\n## What about enums?\n\nThe C enum types are integers.  The underlying type is up to the platform, so\nPlatypus provides `enum` and `senum` types for unsigned and singed enums\nrespectively.  At least some compilers treat signed and unsigned enums as\ndifferent types.  The enum _values_ are essentially the same as macro constants\ndescribed above from an FFI perspective.  Thus the process of defining enum values\nis identical to the process of defining macro constants in Perl.\n\nFor more details on enumerated types see [\"Enum types\" in FFI::Platypus::Type](https://metacpan.org/pod/FFI::Platypus::Type#Enum-types).\n\nThere is also a type plugin ([FFI::Platypus::Type::Enum](https://metacpan.org/pod/FFI::Platypus::Type::Enum)) that can be helpful\nin writing interfaces that use enums.\n\n## Memory leaks\n\nThere are a couple places where memory is allocated, but never deallocated that may\nlook like memory leaks by tools designed to find memory leaks like valgrind.  This\nmemory is intended to be used for the lifetime of the perl process so there normally\nthis isn't a problem unless you are embedding a Perl interpreter which doesn't closely\nmatch the lifetime of your overall application.\n\nSpecifically:\n\n- type cache\n\n    some types are cached and not freed.  These are needed as long as there are FFI\n    functions that could be called.\n\n- attached functions\n\n    Attaching a function as an xsub will definitely allocate memory that won't be freed\n    because the xsub could be called at any time, including in `END` blocks.\n\nThe Platypus team plans on adding a hook to free some of this \"leaked\" memory\nfor use cases where Perl and Platypus are embedded in a larger application\nwhere the lifetime of the Perl process is significantly smaller than the\noverall lifetime of the whole process.\n\n## I get seg faults on some platforms but not others with a library using pthreads.\n\nOn some platforms, Perl isn't linked with `libpthreads` if Perl threads are not\nenabled.  On some platforms this doesn't seem to matter, `libpthreads` can be\nloaded at runtime without much ill-effect.  (Linux from my experience doesn't seem\nto mind one way or the other).  Some platforms are not happy about this, and about\nthe only thing that you can do about it is to build Perl such that it links with\n`libpthreads` even if it isn't a threaded Perl.\n\nThis is not really an FFI issue, but a Perl issue, as you will have the same\nproblem writing XS code for the such libraries.\n\n## Doesn't work on Perl 5.10.0.\n\nThe first point release of Perl 5.10 was buggy, and is not supported by Platypus.\nPlease upgrade to a newer Perl.\n\n# CAVEATS\n\nPlatypus and Native Interfaces like libffi rely on the availability of\ndynamic libraries.  Things not supported include:\n\n- Systems that lack dynamic library support\n\n    Like MS-DOS\n\n- Systems that are not supported by libffi\n\n    Like OpenVMS\n\n- Languages that do not support using dynamic libraries from other languages\n\n    This used to be the case with Google's Go, but is no longer the case.  This is\n    a problem for C / XS code as well.\n\n- Languages that do not compile to machine code\n\n    Like .NET based languages and Java.\n\nThe documentation has a bias toward using FFI / Platypus with C.  This\nis my fault, as my background mainly in C/C++ programmer (when I am\nnot writing Perl).  In many places I use \"C\" as a short form for \"any\nlanguage that can generate machine code and is callable from C\".  I\nwelcome pull requests to the Platypus core to address this issue.  In an\nattempt to ease usage of Platypus by non C programmers, I have written a\nnumber of foreign language plugins for various popular languages (see\nthe SEE ALSO below).  These plugins come with examples specific to those\nlanguages, and documentation on common issues related to using those\nlanguages with FFI.  In most cases these are available for easy adoption\nfor those with the know-how or the willingness to learn.  If your\nlanguage doesn't have a plugin YET, that is just because you haven't\nwritten it yet.\n\n# SUPPORT\n\nThe intent of the `FFI-Platypus` team is to support the same versions of\nPerl that are supported by the Perl toolchain.  As of this writing that\nmeans 5.16 and better.\n\nIRC: #native on irc.perl.org\n\n[(click for instant chat room login)](http://chat.mibbit.com/#native@irc.perl.org)\n\nIf something does not work the way you think it should, or if you have a\nfeature request, please open an issue on this project's GitHub Issue\ntracker:\n\n[https://github.com/perlFFI/FFI-Platypus/issues](https://github.com/perlFFI/FFI-Platypus/issues)\n\n# CONTRIBUTING\n\nIf you have implemented a new feature or fixed a bug then you may make a\npull request on this project's GitHub repository:\n\n[https://github.com/PerlFFI/FFI-Platypus/pulls](https://github.com/PerlFFI/FFI-Platypus/pulls)\n\nThis project is developed using [Dist::Zilla](https://metacpan.org/pod/Dist::Zilla).  The project's git\nrepository also comes with the `Makefile.PL` file necessary\nfor building, testing (and even installing if necessary) without\n[Dist::Zilla](https://metacpan.org/pod/Dist::Zilla).  Please keep in mind though that these files are\ngenerated so if changes need to be made to those files they should be\ndone through the project's `dist.ini` file.  If you do use\n[Dist::Zilla](https://metacpan.org/pod/Dist::Zilla) and already have the necessary plugins installed, then I\nencourage you to run `dzil test` before making any pull requests.  This\nis not a requirement, however, I am happy to integrate especially\nsmaller patches that need tweaking to fit the project standards.  I may\npush back and ask you to write a test case or alter the formatting of a\npatch depending on the amount of time I have and the amount of code that\nyour patch touches.\n\nThis project's GitHub issue tracker listed above is not Write-Only.  If\nyou want to contribute then feel free to browse through the existing\nissues and see if there is something you feel you might be good at and\ntake a whack at the problem.  I frequently open issues myself that I\nhope will be accomplished by someone in the future but do not have time\nto immediately implement myself.\n\nAnother good area to help out in is documentation.  I try to make sure\nthat there is good document coverage, that is there should be\ndocumentation describing all the public features and warnings about\ncommon pitfalls, but an outsider's or alternate view point on such\nthings would be welcome; if you see something confusing or lacks\nsufficient detail I encourage documentation only pull requests to\nimprove things.\n\nThe Platypus distribution comes with a test library named `libtest`\nthat is normally automatically built by `./Build test`.  If you prefer\nto use `prove` or run tests directly, you can use the `./Build\nlibtest` command to build it.  Example:\n\n```\n% perl Makefile.PL\n% make\n% make ffi-test\n% prove -bv t\n# or an individual test\n% perl -Mblib t/ffi_platypus_memory.t\n```\n\nThe build process also respects these environment variables:\n\n- FFI\\_PLATYPUS\\_DEBUG\\_FAKE32\n\n    When building Platypus on 32 bit Perls, it will use the [Math::Int64](https://metacpan.org/pod/Math::Int64) C\n    API and make [Math::Int64](https://metacpan.org/pod/Math::Int64) a prerequisite.  Setting this environment\n    variable will force Platypus to build with both of those options on a 64\n    bit Perl as well.\n\n    ```\n    % env FFI_PLATYPUS_DEBUG_FAKE32=1 perl Makefile.PL\n    DEBUG_FAKE32:\n      + making Math::Int64 a prereq\n      + Using Math::Int64's C API to manipulate 64 bit values\n    Generating a Unix-style Makefile\n    Writing Makefile for FFI::Platypus\n    Writing MYMETA.yml and MYMETA.json\n    %\n    ```\n\n- FFI\\_PLATYPUS\\_NO\\_ALLOCA\n\n    Platypus uses the non-standard and somewhat controversial C function\n    `alloca` by default on platforms that support it.  I believe that\n    Platypus uses it responsibly to allocate small amounts of memory for\n    argument type parameters, and does not use it to allocate large\n    structures like arrays or buffers.  If you prefer not to use `alloca`\n    despite these precautions, then you can turn its use off by setting this\n    environment variable when you run `Makefile.PL`:\n\n    ```perl\n    helix% env FFI_PLATYPUS_NO_ALLOCA=1 perl Makefile.PL\n    NO_ALLOCA:\n      + alloca() will not be used, even if your platform supports it.\n    Generating a Unix-style Makefile\n    Writing Makefile for FFI::Platypus\n    Writing MYMETA.yml and MYMETA.json\n    ```\n\n- V\n\n    When building platypus may hide some of the excessive output when\n    probing and building, unless you set `V` to a true value.\n\n    ```\n    % env V=1 perl Makefile.PL\n    % make V=1\n    ...\n    ```\n\n## Coding Guidelines\n\n- Do not hesitate to make code contribution.  Making useful contributions\nis more important than following byzantine bureaucratic coding\nregulations.  We can always tweak things later.\n- Please make an effort to follow existing coding style when making pull\nrequests.\n- The intent of the `FFI-Platypus` team is to support the same versions of\nPerl that are supported by the Perl toolchain.  As of this writing that\nmeans 5.16 and better.  As such, please do not include any code that\nrequires a newer version of Perl.\n\n## Performance Testing\n\nAs Mark Twain was fond of saying there are four types of lies: lies,\ndamn lies, statistics and benchmarks.  That being said, it can sometimes\nbe helpful to compare the runtime performance of Platypus if you are\nmaking significant changes to the Platypus Core.  For that I use\n\\`FFI-Performance\\`, which can be found in my GitHub repository here:\n\n- [https://github.com/Perl5-FFI/FFI-Performance](https://github.com/Perl5-FFI/FFI-Performance)\n\n## System integrators\n\nThis distribution uses [Alien::FFI](https://metacpan.org/pod/Alien::FFI) in fallback mode, meaning if\nthe system doesn't provide `pkg-config` and `libffi` it will attempt\nto download `libffi` and build it from source.  If you are including\nPlatypus in a larger system (for example a Linux distribution) you\nonly need to make sure to declare `pkg-config` or `pkgconf` and\nthe development package for `libffi` as prereqs for this module.\n\n# SEE ALSO\n\n## Extending Platypus\n\n- [FFI::Platypus::Type](https://metacpan.org/pod/FFI::Platypus::Type)\n\n    Type definitions for Platypus.\n\n- [FFI::C](https://metacpan.org/pod/FFI::C)\n\n    Interface for defining structured data records for use with\n    Platypus.  It supports C `struct`, `union`, nested structures\n    and arrays of all of those.  It only supports passing these\n    types by reference or pointer, so if you need to pass structured\n    data by value see [FFI::Platypus::Record](https://metacpan.org/pod/FFI::Platypus::Record) below.\n\n- [FFI::Platypus::Record](https://metacpan.org/pod/FFI::Platypus::Record)\n\n    Interface for defining structured data records for use with\n    Platypus.  Included in the Platypus core.  Supports pass by\n    value which is uncommon in C, but frequently used in languages\n    like Rust and Go.  Consider using [FFI::C](https://metacpan.org/pod/FFI::C) instead if you\n    don't need to pass by value.\n\n- [FFI::Platypus::API](https://metacpan.org/pod/FFI::Platypus::API)\n\n    The custom types API for Platypus.\n\n- [FFI::Platypus::Memory](https://metacpan.org/pod/FFI::Platypus::Memory)\n\n    Memory functions for FFI.\n\n## Languages\n\n- [FFI::Platypus::Lang::C](https://metacpan.org/pod/FFI::Platypus::Lang::C)\n\n    Documentation and tools for using Platypus with the C programming\n    language\n\n- [FFI::Platypus::Lang::CPP](https://metacpan.org/pod/FFI::Platypus::Lang::CPP)\n\n    Documentation and tools for using Platypus with the C++ programming\n    language\n\n- [FFI::Platypus::Lang::Fortran](https://metacpan.org/pod/FFI::Platypus::Lang::Fortran)\n\n    Documentation and tools for using Platypus with Fortran\n\n- [FFI::Platypus::Lang::Go](https://metacpan.org/pod/FFI::Platypus::Lang::Go)\n\n    Documentation and tools for using Platypus with Go\n\n- [FFI::Platypus::Lang::Pascal](https://metacpan.org/pod/FFI::Platypus::Lang::Pascal)\n\n    Documentation and tools for using Platypus with Free Pascal\n\n- [FFI::Platypus::Lang::Rust](https://metacpan.org/pod/FFI::Platypus::Lang::Rust)\n\n    Documentation and tools for using Platypus with the Rust programming\n    language\n\n- [FFI::Platypus::Lang::ASM](https://metacpan.org/pod/FFI::Platypus::Lang::ASM)\n\n    Documentation and tools for using Platypus with the Assembly\n\n- [FFI::Platypus::Lang::Win32](https://metacpan.org/pod/FFI::Platypus::Lang::Win32)\n\n    Documentation and tools for using Platypus with the Win32 API.\n\n- [FFI::Platypus::Lang::Zig](https://metacpan.org/pod/FFI::Platypus::Lang::Zig)\n\n    Documentation and tools for using Platypus with the Zig programming\n    language\n\n- [Wasm](https://metacpan.org/pod/Wasm) and [Wasm::Wasmtime](https://metacpan.org/pod/Wasm::Wasmtime)\n\n    Modules for writing WebAssembly bindings in Perl.  This allows you to call\n    functions written in any language supported by WebAssembly.  These modules\n    are also implemented using Platypus.\n\n## Other Tools Related Tools Useful for FFI\n\n- [FFI::CheckLib](https://metacpan.org/pod/FFI::CheckLib)\n\n    Find dynamic libraries in a portable way.\n\n- [Convert::Binary::C](https://metacpan.org/pod/Convert::Binary::C)\n\n    A great interface for decoding C data structures, including `struct`s,\n    `enum`s, `#define`s and more.\n\n- [pack and unpack](https://metacpan.org/pod/perlpacktut)\n\n    Native to Perl functions that can be used to decode C `struct` types.\n\n- [C::Scan](https://metacpan.org/pod/C::Scan)\n\n    This module can extract constants and other useful objects from C header\n    files that may be relevant to an FFI application.  One downside is that\n    its use may require development packages to be installed.\n\n## Other Foreign Function Interfaces\n\n- [Dyn](https://metacpan.org/pod/Dyn)\n\n    A wrapper around [dyncall](https://dyncall.org), which is itself an alternative to\n    [libffi](https://sourceware.org/libffi/).\n\n- [NativeCall](https://metacpan.org/pod/NativeCall)\n\n    Promising interface to Platypus inspired by Raku.\n\n- [Win32::API](https://metacpan.org/pod/Win32::API)\n\n    Microsoft Windows specific FFI style interface.\n\n- [FFI](https://metacpan.org/pod/FFI)\n\n    Older, simpler, less featureful FFI.  It used to be implemented\n    using FSF's `ffcall`.  Because `ffcall` has been unsupported for\n    some time, I reimplemented this module using [FFI::Platypus](https://metacpan.org/pod/FFI::Platypus).\n\n- [C::DynaLib](https://metacpan.org/pod/C::DynaLib)\n\n    Another FFI for Perl that doesn't appear to have worked for a long time.\n\n- [C::Blocks](https://metacpan.org/pod/C::Blocks)\n\n    Embed a tiny C compiler into your Perl scripts.\n\n- [P5NCI](https://metacpan.org/pod/P5NCI)\n\n    Yet another FFI like interface that does not appear to be supported or\n    under development anymore.\n\n## Other\n\n- [Alien::FFI](https://metacpan.org/pod/Alien::FFI)\n\n    Provides libffi for Platypus during its configuration and build stages.\n\n# ACKNOWLEDGMENTS\n\nIn addition to the contributors mentioned below, I would like to\nacknowledge Brock Wilcox (AWWAIID) and Meredith Howard (MHOWARD) whose\nwork on `FFI::Sweet` not only helped me get started with FFI but\nsignificantly influenced the design of Platypus.\n\nDan Book, who goes by Grinnz on IRC for answering user questions about\nFFI and Platypus.\n\nIn addition I'd like to thank Alessandro Ghedini (ALEXBIO) whose work\non another Perl FFI library helped drive some of the development ideas\nfor [FFI::Platypus](https://metacpan.org/pod/FFI::Platypus).\n\n# AUTHOR\n\nAuthor: Graham Ollis \u003cplicease@cpan.org\u003e\n\nContributors:\n\nBakkiaraj Murugesan (bakkiaraj)\n\nDylan Cali (calid)\n\npipcet\n\nZaki Mughal (zmughal)\n\nFitz Elliott (felliott)\n\nVickenty Fesunov (vyf)\n\nGregor Herrmann (gregoa)\n\nShlomi Fish (shlomif)\n\nDamyan Ivanov\n\nIlya Pavlov (Ilya33)\n\nPetr Písař (ppisar)\n\nMohammad S Anwar (MANWAR)\n\nHåkon Hægland (hakonhagland, HAKONH)\n\nMeredith (merrilymeredith, MHOWARD)\n\nDiab Jerius (DJERIUS)\n\nEric Brine (IKEGAMI)\n\nszTheory\n\nJosé Joaquín Atria (JJATRIA)\n\nPete Houston (openstrike, HOUSTON)\n\nLukas Mai (MAUKE)\n\n# COPYRIGHT AND LICENSE\n\nThis software is copyright (c) 2015-2022 by Graham Ollis.\n\nThis is free software; you can redistribute it and/or modify it under\nthe same terms as the Perl 5 programming language system itself.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fperlffi%2Fffi-platypus","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fperlffi%2Fffi-platypus","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fperlffi%2Fffi-platypus/lists"}