FFI::C::Struct(3pm)
π FFI::C::Struct β structured data instance for FFI π
okay bestie, welcome to FFI::C::Struct β it's the Perl module that lets you vibe with an actual C struct from inside Perl. it lives in the User Contributed Perl Documentation corner of your perl man pages, and we're on version 0.15. no cap, this is the good stuff. β¨
πΈ VERSION πΈ
version 0.15 β that's the one, that's the era we're living in π
β¨ SYNOPSIS β¨
here's the whole vibe in one go, literally just read it top to bottom and you'll get it:
use FFI::C::StructDef;
my $def = FFI::C::StructDef->new(
name => 'color_t',
class => 'Color',
members => [
red => 'uint8',
green => 'uint8',
blue => 'uint8',
],
);
my $red = $def->create({ red => 255 }); # creates a FFI::C::Stuct
printf "[%02x %02x %02x]\n", $red->red, $red->green, $red->blue; # [ff 00 00]
# that red is too bright!
$red->red(200);
printf "[%02x %02x %02x]\n", $red->red, $red->green, $red->blue; # [c8 00 00]
my $green = Color->new({ green => 255 }); # creates a FFI::C::Stuct
printf "[%02x %02x %02x]\n", $green->red, $green->green, $green->blue; # [00 ff 00]
so yeah β you define a struct, you create one with whatever starting values you want, and then you poke at the members like they're just lil Perl accessors. main character energy fr fr π«Ά
π¦ DESCRIPTION π¦
okay so, this class? it represents an instance of a C struct. that's the whole thing. it's the living, breathing (well, living, breathing, and memory-mapped) instance.
how do you get one, you ask? two iconic ways:
- use
newon the generated class β but only if you actually specified that class when you set up theFFI::C::StructDefπ - or call the
createmethod onFFI::C::StructDefand let it hand you one π
and here's the slay: for every single member you defined in the FFI::C::StructDef, there's a matching accessor on the FFI::C::Struct instance. every one. no member left behind. π
π CONSTRUCTOR π
π new π
FFI::C::StructDef->new( class => 'User::Struct::Class', ... );
my $instance = User::Struct::Class->new;
creates a new instance of the struct. that's it, that's the job, and it does it beautifully. β¨
heads up bestie: that first line up there is you setting up theFFI::C::StructDefwith aclassso the generated class exists in the first place β thennewon that class gives you your instance. order matters, iykyk π
π 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> β king behavior, honestly π
π 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. so go off, remix it, redistribute it, it's yours to play with π
and that's the whole page, bestie!! this one came straight from the User Contributed Perl Documentation collection, shipping as FFI::C::Struct(3pm) with perl v5.36.0, dated 2023-02-06. mwah, go write some structs π
β¨π¦
π¦ 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