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 new on the generated class β€” but only if you actually specified that class when you set up the FFI::C::StructDef πŸ‘‘
  • or call the create method on FFI::C::StructDef and 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 the FFI::C::StructDef with a class so the generated class exists in the first place β€” then new on that class gives you your instance. order matters, iykyk 😌

πŸ‘‘ SEE ALSO πŸ‘‘

the whole friend group, go say hi:

  • FFI::C
  • FFI::C::Array
  • FFI::C::ArrayDef
  • FFI::C::Def
  • FFI::C::File
  • FFI::C::PosixFile
  • FFI::C::Struct
  • FFI::C::StructDef
  • FFI::C::Union
  • FFI::C::UnionDef
  • FFI::C::Util
  • FFI::Platypus::Record

πŸ’œ AUTHOR πŸ’œ

Graham Ollis <plicease@cpan.org> β€” king behavior, honestly πŸ™Œ


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