|
NAMEFFI::Platypus::Constant - Define constants in C space for PerlVERSIONversion 1.56SYNOPSIS"ffi/foo.c":#include <ffi_platypus_bundle.h> void ffi_pl_bundle_constant(const char *package, ffi_platypus_constant_t *c) { c->set_str("FOO", "BAR"); /* sets $package::FOO to "BAR" */ c->set_str("ABC::DEF", "GHI"); /* sets ABC::DEF to GHI */ } "lib/Foo.pm": package Foo; use strict; use warnings; use FFI::Platypus 1.00; use Exporter qw( import ); my $ffi = FFI::Platypus->new( api => 1 ); # sets constants Foo::FOO and ABC::DEF from C $ffi->bundle; 1; DESCRIPTIONThe Platypus bundle interface (see FFI::Platypus::Bundle) has an entry point "ffi_pl_bundle_constant" that lets you define constants in Perl space from C.void ffi_pl_bundle_constant(const char *package, ffi_platypus_constant_t *c); The first argument "package" is the name of the Perl package. The second argument "c" is a struct with function pointers that lets you define constants of different types. The first argument for each function is the name of the constant and the second is the value. If "::" is included in the constant name then it will be defined in that package space. If it isn't then the constant will be defined in whichever package called "bundle".
ExampleSuppose you have a header file "myheader.h":#ifndef MYHEADER_H #define MYHEADER_H #define MYVERSION_STRING "1.2.3" #define MYVERSION_MAJOR 1 #define MYVERSION_MINOR 2 #define MYVERSION_PATCH 3 enum { MYBAD = -1, MYOK = 1 }; #define MYPI 3.14 #endif You can define these constants from C: #include <ffi_platypus_bundle.h> #include "myheader.h" void ffi_pl_bundle_constant(const char *package, ffi_platypus_constant_t *c) { c->set_str("MYVERSION_STRING", MYVERSION_STRING); c->set_uint("MYVERSION_MAJOR", MYVERSION_MAJOR); c->set_uint("MYVERSION_MINOR", MYVERSION_MINOR); c->set_uint("MYVERSION_PATCH", MYVERSION_PATCH); c->set_sint("MYBAD", MYBAD); c->set_sint("MYOK", MYOK); c->set_double("MYPI", MYPI); } Your Perl code doesn't have to do anything when calling bundle: package Const; use strict; use warnings; use FFI::Platypus 1.00; { my $ffi = FFI::Platypus->new( api => 1 ); $ffi->bundle; } 1; AUTHORAuthor: Graham Ollis <plicease@cpan.org>Contributors: Bakkiaraj Murugesan (bakkiaraj) Dylan Cali (calid) pipcet Zaki Mughal (zmughal) Fitz Elliott (felliott) Vickenty Fesunov (vyf) Gregor Herrmann (gregoa) Shlomi Fish (shlomif) Damyan Ivanov Ilya Pavlov (Ilya33) Petr Písař (ppisar) Mohammad S Anwar (MANWAR) Håkon Hægland (hakonhagland, HAKONH) Meredith (merrilymeredith, MHOWARD) Diab Jerius (DJERIUS) Eric Brine (IKEGAMI) szTheory José Joaquín Atria (JJATRIA) Pete Houston (openstrike, HOUSTON) COPYRIGHT AND LICENSEThis software is copyright (c) 2015,2016,2017,2018,2019,2020 by Graham Ollis.This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
Visit the GSP FreeBSD Man Page Interface. |