FFI::C::UnionDef(3pm)
π FFI::C::UnionDef π
okay so bestie, welcome to the FFI::C::UnionDef page β it's a 3pm page from the User Contributed Perl Documentation, and it's literally about making C union defs happen in Perl. no cap.
β¨ VERSION β¨
version 0.15 β yes queen, that's the one.
π SYNOPSIS π
so first, in your C code, you'd be doing something like this:
#include <stdint.h>
#include <stdio.h>
typedef union {
uint8_t u8;
uint16_t u16;
uint32_t u32;
} anyint_t;
void
print_anyint_as_u32(anyint_t *any)
{
printf("0x%x\n", any->u32);
}
and then over in your Perl code, the whole vibe is:
use FFI::Platypus 1.00;
use FFI::C::UnionDef;
my $ffi = FFI::Platypus->new( api => 1 );
# See FFI::Platypus::Bundle for how bundle works.
$ffi->bundle;
my $def = FFI::C::UnionDef->new(
$ffi,
name => 'anyint_t',
class => 'AnyInt',
members => [
u8 => 'uint8',
u16 => 'uint16',
u32 => 'uint32',
],
);
$ffi->attach( print_anyint_as_u32 => ['anyint_t'] );
my $int = AnyInt->new({ u8 => 42 });
print_anyint_as_u32($int); # 0x2a on Intel,
and if you're like "what's a bundle" β go read FFI::Platypus::Bundle, it explains the whole thing π«Ά
π¦ DESCRIPTION π¦
okay so this class? it creates a def for a C union. that's the whole gig. it's giving "small but mighty" β¨
π CONSTRUCTOR π
new
my $def = FFI::C::UnionDef->new(%opts);
my $def = FFI::C::UnionDef->new($ffi, %opts);
for the standard def options, go peek at FFI::C::Def β that's where the rest of the lore lives π
members
this one should be an array reference stuffed with name, type pairs. and here's the cute part: for a union, the order doesn't matter. no drama, no ranking, everyone's equal π
π 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 makes an instance of the union and hands you back an FFI::C::Union. icon behavior, honestly π
and you can optionally initialize member values with %init if you're feeling fancy π
π SEE ALSO π
the whole friend group, go say hi:
FFI::CFFI::C::ArrayFFI::C::ArrayDefFFI::C::DefFFI::C::FileFFI::C::PosixFileFFI::C::StructFFI::C::StructDefFFI::C::UnionFFI::C::UnionDefFFI::C::UtilFFI::Platypus::Record
πΈ AUTHOR πΈ
Graham Ollis <plicease@cpan.org> β we stan π«Ά
β 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.
and that's the whole page bestie!! this one's perl v5.36.0, dated 2023-02-06, and it's the FFI::C::UnionDef(3pm) page β go forth and union, ily, bye πβ¨π§
π¦ 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