FFI::C::Array(3pm)
π FFI::C::Array β the vibe β¨
okay bestie, listen up. this is FFI::C::Array, version 0.15, and it's the whole "array instance for FFI" situation. it lives in the User Contributed Perl Documentation section of your man pages, and it's about to become your new hyperfixation. no cap. π«Ά
πΈ NAME πΈ
FFI::C::Array β Array instance for FFI. that's it. that's the whole name. iconic. π
β VERSION β
version 0.15 π
π¦ SYNOPSIS π¦
okay so here's the whole flow, verbatim, don't skip a line because it all matters:
use FFI::C::ArrayDef;
use FFI::C::StructDef;
my $point_def = FFI::C::StructDef->new(
name => 'point_t',
class => 'Point',
members => [
x => 'double',
y => 'double',
],
);
my $rect_def = FFI::C::ArrayDef->new(
name => 'rectangle_t',
class => 'Rectangle',
members => [
$point_def, 2,
]
);
# create a rectangle using the def's create method
my $square = $rect_def->create([
{ x => 1.0, y => 1.0 },
{ x => 2.0, y => 2.0 },
]);
printf "[[%d %d][%d %d]]\n",
$square->[0]->x, $square->[0]->y,
$square->[1]->x, $square->[1]->y; # [[1 1][2 2]]
# move square by 1 on the x axis
$square->[$_]->x( $square->[$_]->x + 1 ) for 0..1;
printf "[[%d %d][%d %d]]\n",
$square->[0]->x, $square->[0]->y,
$square->[1]->x, $square->[1]->y; # [[2 1][3 2]]
# Create a rectangle using the generated class
my $rect = Rectangle->new;
$rect->[0]->x(1.0);
$rect->[0]->y(1.0);
$rect->[1]->x(2.0);
$rect->[1]->y(3.0);
look at that. look at how clean it is. we are thriving. π
π DESCRIPTION π
so this class represents an instance of a C array. literally what it says on the tin, bestie.
how do you even get one of these? two ways, and both are valid:
- you can create it using
newon the generated class β but only if that class was specified for theFFI::C::ArrayDefπ - or you use the
createmethod onFFI::C::ArrayDef
and then, here's the fun part: you can grab each element of the array with the get method (it's down below, don't worry), OR you can just treat the object like an array reference, because β¨ magical Perl ties β¨. that's the good stuff. the way Perl just does that? obsessed. π
π CONSTRUCTOR π
new
FFI::C::ArrayDef->new( class => 'User::Array::Class', ... );
my $instance = User::Array::Class->new;
my $instance = User::Array::Class->new($count);
creates a shiny new instance of the array. β¨
now here's the tea about $count:
- if you pass
$count, that's what gets used as the element count β it overrides the count that the def defined - if the def didn't specify a count, then you MUST provide a count. like, mandatory. non-negotiable. the code will simply not be doing that for you. π«
β¨ METHODS β¨
get
my $element = $instance->get($index);
my $element = $instance->[$index];
gets the element sitting at the given $index. two syntaxes, same energy, pick your fave. π
count
my $count = $instance->count;
returns the number of elements in the array β if known. note that "if known" bestie, because sometimes it is simply not known and that's just how it is. π
tie
my $arrayref = $instance->tie;
returns a Perl array reference that's tied to the C array. this is the plumbing behind the magic. π§
π SEE ALSO π
your reading list, your syllabus, your curated little ecosystem:
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. i'll wait. π
πΈ AUTHOR πΈ
Graham Ollis <plicease@cpan.org> β thank you for your service, king. π«Ά
π 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.
bestie if you got all the way here you are officially a C-array-in-Perl main character. periodt. π
and that's a wrap on the perl v5.36.0 build from 2023-02-06 β go forth and slay some FFI. π¦β¨
π¦ 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