🌻 📖 FFI::Platypus::Constant

NAME

FFI::Platypus::Constant - Define constants in C space for Perl

VERSION

version 2.08

SYNOPSIS

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 2.00;
 use Exporter qw( import );
 
 my $ffi = FFI::Platypus->new( api => 2 );
 # sets constants Foo::FOO and ABC::DEF from C
 $ffi->bundle;
 
 1;

DESCRIPTION

The 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.

set_str
 c->set_str(name, value);

Sets a string constant.

set_sint
 c->set_sint(name, value);

Sets a 64-bit signed integer constant.

set_uint
 c->set_uint(name, value);

Sets a 64-bit unsigned integer constant.

set_double
 c->set_double(name, value);

Sets a double precision floating point constant.

Example

Suppose 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 2.00;
 
 {
   my $ffi = FFI::Platypus->new( api => 2 );
   $ffi->bundle;
 }
 
 1;

AUTHOR

Author: 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 LICENSE

This software is copyright (c) 2015-2022 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.