FFI::C::ArrayDef(3pm)
π FFI::C::ArrayDef π
okay bestie, so this is FFI::C::ArrayDef β a User Contributed Perl Documentation page, and it's literally the glow-up your C arrays of structs have been waiting for. pull up a chair, we're doing this π«Ά
β¨ NAME β¨
FFI::C::ArrayDef β array data definition for FFI. that's it, that's the name, slay.
πΈ VERSION πΈ
version 0.15 β yes, we're on that version, iconic behavior.
π SYNOPSIS π
first up, your C code, and yes we're keeping it exactly as-is because it's giving β¨
#include <stdio.h>
typedef struct {
double x, y;
} point_t;
void
print_rectangle(point_t rec[2])
{
printf("[[%g %g] [%g %g]]\n",
rec[0].x, rec[0].y,
rec[1].x, rec[1].y
);
}
and now the Perl side, where the magic actually happens π
use FFI::Platypus 1.00;
use FFI::C::ArrayDef;
use FFI::C::StructDef;
my $ffi = FFI::Platypus->new( api => 1 );
# See FFI::Platypus::Bundle for how bundle works.
$ffi->bundle;
my $point_def = FFI::C::StructDef->new(
$ffi,
name => 'point_t',
class => 'Point',
members => [
x => 'double',
y => 'double',
],
);
my $rect_def = FFI::C::ArrayDef->new(
$ffi,
name => 'rectangle_t',
class => 'Rectangle',
members => [
$point_def, 2,
]
);
$ffi->attach( print_rectangle => ['rectangle_t'] );
my $rect = Rectangle->new([
{ x => 1.5, y => 2.0 },
{ x => 3.14, y => 11.0 },
]);
print_rectangle($rect); # [[1.5 2] [3.14 11]]
# move rectangle on the y axis
$rect->[$_]->y( $rect->[$_]->y + 1.0 ) for 0..1;
print_rectangle($rect); # [[1.5 3] [3.14 12]]
like?? we built a rectangle, printed it, moved it up the y axis, printed it again. main character energy fr fr π
π DESCRIPTION π
so what does this class actually do? it creates a def for a C array of structured data. usually that def is holding a FFI::C::StructDef or a FFI::C::UnionDef, and optionally a number of elements. that's the whole vibe. no cap.
π¦ CONSTRUCTOR π¦
new
my $def = FFI::C::ArrayDef->new(%opts);
my $def = FFI::C::ArrayDef->new($ffi, %opts);
for the standard def options, go peek at FFI::C::Def β iykyk.
members
this one should be an array reference: the member type first, and optionally the number of elements after it. examples, bestie:
my $struct = FFI::C::StructDef->new(...);
my $fixed = FFI::C::ArrayDef->new(
members => [ $struct, 10 ],
);
my $var = FFI::C::ArrayDef->new(
members => [ $struct ],
);
so $fixed is your locked-in-count situation, and $var is the flexible girl who doesn't commit to a number yet. both valid. both slay. π
π METHODS π
create
my $instance = $def->create;
my $instance = $def->class->new; # if class was specified
my $instance = $def->create($count);
my $instance = $def->class->new($count); # 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 array. if $count is given, that's what gets used for the element count β and it can possibly override whatever was specified back when the def was created. and listen, important: if the def doesn't have an element count specified, then you MUST provide it here. no skipping, no shortcuts, it's required, periodt.
returns a FFI::C::Array.
and you can optionally initialize member values using @init. cute, right? π
β 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, bestie, they're all part of the same little friend group π«Ά
π AUTHOR π
Graham Ollis <plicease@cpan.org> β the legend himself, we love to see it π
π 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 yeah, free software, do your thing, just respect the terms okay? π
and that's the whole page, bestie π this was FFI::C::ArrayDef, perl v5.36.0, dated 2023-02-06, and it came to you straight from the User Contributed Perl Documentation corner of the universe. mwah, go write some arrays π
β¨πΈ
π¦ 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