++ed by:
TSIBLEY JOHNYJH JERRYV PABLROD SSCAFFIDI

27 PAUSE users
22 non-PAUSE users.

Graham Ollis 🔥🐉
and 13 contributors

NAME

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

VERSION

version 0.96

SYNOPSIS

 #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        */
 }

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;
 
 {
   my $ffi = FFI::Platypus->new( api => 1 );
   $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 Pisar (ppisar)

Mohammad S Anwar (MANWAR)

Håkon Hægland (hakonhagland, HAKONH)

COPYRIGHT AND LICENSE

This software is copyright (c) 2015,2016,2017,2018,2019 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.