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)

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