FFI::C...ructDef(3pm)
π¦ FFI::C::StructDef π¦
okay so bestie, welcome to FFI::C::StructDef β it's the User Contributed Perl Documentation page for it, and this version is version 0.15. it's giving "define a C struct, but make it Perl" and honestly? iconic.
β¨ NAME β¨
FFI::C::StructDef β structured data definition for FFI π
πΈ VERSION πΈ
version 0.15
π SYNOPSIS π
so first, the C side of things. in your C code:
#include <stdint.h>
#include <stdio.h>
typedef struct {
uint8_t red;
uint8_t green;
uint8_t blue;
} color_t;
void
print_color(color_t *c)
{
printf("[%02x %02x %02x]\n",
c->red,
c->green,
c->blue
);
}
and then, over in your Perl code:
use FFI::Platypus 1.00;
use FFI::C::StructDef;
my $ffi = FFI::Platypus->new( api => 1 );
# See FFI::Platypus::Bundle for how bundle works.
$ffi->bundle;
my $def = FFI::C::StructDef->new(
$ffi,
name => 'color_t',
class => 'Color',
members => [
red => 'uint8',
green => 'uint8',
blue => 'uint8',
],
);
my $red = Color->new({ red => 255 });
my $green = Color->new({ green => 255 });
$ffi->attach( print_color => ['color_t'] );
print_color($red); # [ff 00 00]
print_color($green); # [00 ff 00]
# that red is a tad bright!
$red->red( 200 );
print_color($red); # [c8 00 00]
periodt. you just made a whole struct and passed it into C. main character energy fr fr. π«Ά
π DESCRIPTION π
this class creates a def for a C "struct". that's it, that's the vibe. β¨
π₯ CONSTRUCTOR π₯
new
my $def = FFI::C::StructDef->new(%opts);
my $def = FFI::C::StructDef->new($ffi, %opts);
for the standard def options, go peek at FFI::C::Def β this class inherits all that goodness. π
members
this one should be an array reference holding name, type pairs, listed in the order they'll actually be stored inside the struct. order matters, bestie, the struct is not gonna reshuffle itself for you. π
trim_string
if this is true, fixed-length strings get treated as null terminated strings, and they get trimmed. lowkey the tidy-up option. π§
β METHODS β
create
my $instance = $def->create;
my $instance = $def->class->new; # if class was specified
my $instance = $def->create(\%init);
my $instance = $def->class->new(\%init); # if class was specified
this creates an instance of the "struct", and it hands you back an FFI::C::Struct. you can optionally initialize member values using %init if you wanna come in hot with values already set. π
trim_string
my $bool = $def->trim_string;
returns true if fixed-length strings should be treated as null terminated strings and be trimmed. yes it's the same name as the constructor option, no that's not a glitch, it's just how it is. π
π SEE ALSO π
FFI::CFFI::C::ArrayFFI::C::ArrayDefFFI::C::DefFFI::C::FileFFI::C::PosixFileFFI::C::StructFFI::C::StructDefFFI::C::UnionFFI::C::UnionDefFFI::C::UtilFFI::Platypus::Record
go read them all, they're the friend group. π¦
πΈ AUTHOR πΈ
Graham Ollis <plicease@cpan.org>
π COPYRIGHT AND LICENSE π
this software is copyright (c) 2020-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. slay, legally speaking. π
and that's the whole page bestie β¨ β built forperl v5.36.0, dated2023-02-06, and it's theFFI::C...ructDef(3pm)manual page, no cap. π«Άπ
π¦ where this page comes from
- debian bookworm: libffi-c-perl 0.15-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libffi-c-perl 0.15-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libffi-c-perl 0.15-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libffi-c-perl 0.15-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libffi-c-perl 0.15-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms