FFI::P...onstant(3pm)
π NAME π
FFI::Platypus::Constant β bestie, this is how you define constants in C space for Perl β¨
Oh and hiii, this is the manpage for FFI::P...onstant(3pm) from the User Contributed Perl Documentation, in case you were wondering which page you just opened π«Ά
π VERSION π
version 2.11
π¦ SYNOPSIS π¦
Okay so picture this: you got a C file, "ffi/foo.c", and it looks like this π
#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 */
}
And then your Perl side, "lib/Foo.pm", is literally just vibes and a bundle call π
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;
No cap, that's the whole thing π
β¨ DESCRIPTION β¨
So the Platypus bundle interface (peep FFI::Platypus::Bundle if you wanna know more, iykyk) has this entry point called ffi_pl_bundle_constant, and it lets you define constants in Perl space straight 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 full of function pointers that lets you define constants of different types. For every one of those functions, the first argument is the name of the constant and the second is the value. Here's the tea tho: if the constant name has :: in it, it gets defined in that package space. If it doesn't, then the constant lands in whichever package called bundle. Periodt π
πΈ 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 π
Okay bestie, let's say you have a header file "myheader.h" that's giving very much "defines and an enum":
#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 all these constants from C, like so π
#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);
}
And your Perl code? Bestie, it literally doesn't have to do anything extra when calling bundle. Iconic behaviour, honestly π
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 (the whole squad, fr fr π«Ά):
- 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)
- Lukas Mai (MAUKE)
π 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 π
That's the whole page, bestie! π Run it, love it, define your constants. Slay π¦
Okay and for the little sign-off corner π this version is perl v5.42.3, dated 2026-08-20, and you just read FFI::P...onstant(3pm). Mwah, bye πβ¨
π¦ where this page comes from
- ubuntu stonking: libffi-platypus-perl 2.11-1build1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms