FFI::P...:Record(3pm)

okay bestie, this is the User Contributed Perl Documentation for FFI::Platypus::Record β€” yes, the FFI::P...:Record(3pm) header energy, folded in right here because we're cute like that. it's giving structured-data slay, no cap. πŸ¦‹πŸ’…


πŸ’– NAME πŸ’–

FFI::Platypus::Record β€” FFI support for structured records data. that's the whole name-and-tagline situation, periodt.

✨ VERSION ✨

version 2.11 β€” tiny number, big struct energy.

πŸ¦‹ SYNOPSIS πŸ¦‹

C first, because obviously:

struct my_person {
  int         age;
  const char  title[3];
  const char *name
};

void process_person(struct my_person *person)
{
  /* ... */
}

Perl side:

package MyPerson;

use FFI::Platypus::Record;

record_layout_1(
  'int'       => 'age',
  'string(3)' => 'title',
  'string rw' => 'name',
);

package main;

use FFI::Platypus 2.00;

my $ffi = FFI::Platypus->new( api => 2 );
$ffi->lib("myperson.so");
$ffi->type("record(MyPerson)" => 'MyPerson');

my $person = MyPerson->new(
  age   => 40,
  title => "Mr.",
  name  => "John Smith",
);

$ffi->attach( process_person => [ 'MyPerson*' ] => 'void' );

process_person($person);

$person->age($person->age + 1); # another year older

process_person($person);

🌈 DESCRIPTION 🌈

[version 0.21] β€” hello, yes, that's the version tag sitting right at the top of this section, bestie.

This module gives you a whole mechanism for building classes you can use to manage structured data records β€” the things C calls "structs" and some other languages call "records". a structured record is a series of bytes that have structure understood by the C or other foreign language library that you're interfacing with. it's designed for use with FFI and FFI::Platypus, though it may have other applications too. iykyk.

Before you get too deep into using this class, you should also consider FFI::C, which provides some overlapping functionality. briefly, it comes down to this:

the tl;dr is: use this class when you need to pass by value, since FFI::C does not support pass by value. use FFI::C in all other circumstances.

FFI::Platypus::Record

Supports:

  • C pointers to "struct" types
  • Passing C "struct"s by-value.

Does not support:

  • C "union" types.
  • C arrays of "struct" and "union" types.

FFI::C

Supports:

  • C "struct" and "union" types
  • C arrays of "struct" and "union" types.

Does not support:

  • Passing C "struct"s by-value.

String members are as of this writing a TODO for FFI::C, but should be coming soon! πŸŽ€

πŸ› οΈ FUNCTIONS πŸ› οΈ

🧚 record_layout_1 🧚

the synopsis, fresh out the group chat:

record_layout_1($ffi, $type => $name, ... );
record_layout_1(\@ffi_args, $type => $name, ... );
record_layout_1($type => $name, ... );

Define the layout of the record. you may optionally provide an instance of FFI::Platypus as the first argument in order to use its type aliases. alternatively, you may provide constructor arguments that will be passed to the internal platypus instance. so this is the same:

my $ffi = FFI::Platypus->new( lang => 'Rust', api => 2 );
record_layout_1( $ffi, ... );
# same as:
record_layout_1( [ lang => 'Rust' ], ... );

and this is the same:

my $ffi = FFI::Platypus->new( api => 2 );
record_layout_1( $ffi, ... );
# same as:
record_layout_1( ... );

Then you provide members as type/name pairs. for each member you declare, record_layout_1 will create an accessor which can be used to read and write its value. for example, imagine a class Foo:

package Foo;

use FFI::Platypus::Record;

record_layout_1(
  int          => 'bar',  #  int bar;
  'string(10)' => 'baz',  #  char baz[10];
);

You can get and set its fields with like-named bar and baz accessors:

my $foo = Foo->new;

$foo->bar(22);
my $value = $foo->bar;

$foo->baz("grimlock\0\0"); # should be 10 characters long
my $string_value = $foo->baz; # includes the trailing \0\0

You can also pass initial values into the constructor, either passing as a list of key-value pairs or by passing a hash reference:

$foo = Foo->new(
  bar => 22,
  baz => "grimlock\0\0",
);

# same as:

$foo = Foo->new( {
  bar => 22,
  baz => "grimlock\0\0",
} );

If there are members of a record that you need to account for in terms of size and alignment, but you do not want to have an accessor for them, you can use ":" as a placeholder for the name:

record_layout_1(
  'int'        => ':',
  'string(10)' => 'baz',
);

πŸŽ€ strings πŸŽ€

So far I've shown fixed-length strings. these are declared with the word string followed by the length of the string in parentheticals. fixed-length strings are included inside the record itself and do not need to be allocated or deallocated separately from the record. variable-length strings must be allocated on the heap, and thus require a sense of "ownership" β€” that is, whomever allocates variable-length strings should be responsible for also free'ing them. to handle this, you can add a ro or rw trait to a string field. the default is ro, which means you can get, but not set, its value:

package Foo;

record_layout_1(
  'string ro' => 'bar',  # same type as 'string' and 'string_ro'
);

package main;

my $foo = Foo->new;

my $string = $foo->bar;  # GOOD
$foo->bar("starscream"); # BAD

If you specify a field is rw, then you can set its value:

package Foo;

record_layout_1(
  'string rw' => 'bar',  # same type as 'string_rw'
);

package main;

my $foo = Foo->new;

my $string = $foo->bar;  # GOOD
$foo->bar("starscream"); # GOOD
Any string value that is pointed to by the record will be free'd when it falls out of scope, so you must be very careful that any string rw fields are not set or modified by C code. you should also take care not to copy any record that has an rw string in it because its values will be free'd twice! πŸ˜­πŸ’€
use Clone qw( clone );

my $foo2 = clone $foo;  # BAD  bar will be free'd twice

πŸ“ arrays πŸ“

Arrays of integer, floating points and opaque pointers are supported.

package Foo;

record_layout_1(
  'int[10]' => 'bar',
);

my $foo = Foo->new;

$foo->bar([1,2,3,4,5,6,7,8,9,10]); # sets the values for the array
my $list = $foo->bar;  # returns a list reference

$foo->bar(5, -6); # sets the 5th element in the array to -6
my $item = $foo->bar(5); gets the 5th element in the array

🧚 record_layout 🧚

record_layout($ffi, $type => $name, ... );
record_layout(\@ffi_args, $type => $name, ... );
record_layout($type => $name, ... );

The page says this function works like record_layout β€” yes bestie, that's literally the wording, a little self-referential goblin β€” except that api => 0 is used instead of api => 1. all new code should use record_layout_1 instead. πŸ‘‘

πŸ’€ CAVEATS πŸ’€

These useful features (and probably more) are missing, and unlikely to be added:

  • Unions
  • Nested records

If you need these features, consider using FFI::C instead.

πŸ‘‘ SEE ALSO πŸ‘‘

  • FFI::Platypus β€” the main platypus documentation.
  • FFI::C β€” another interface for constructing structured data. it includes support for union and array types (which this module does not), but lacks support for passing records by-value.
  • FFI::Platypus::Record::TieArray β€” tied array interface for record array members.
  • Convert::Binary::C β€” another method for constructing and dissecting structured data records.
  • pack and unpack β€” built-in Perl functions for constructing and dissecting structured data records.

🫢 AUTHOR 🫢

Author: Graham Ollis <plicease@cpan.org>

Contributors:

  • Bakkiaraj Murugesan (bakkiaraj)
  • Dylan Cali (calid)
  • pipcet
  • Zaki Mughal (zmughal)
  • Fitz Elliott (felliott)
  • Vickenty Fesunov (vyf)
  • Gregor Herrmann (gregoa)
  • Shlomi Fish (shlomif)
  • Damyan Ivanov
  • Ilya Pavlov (Ilya33)
  • Petr PΓ­saΕ™ (ppisar)
  • Mohammad S Anwar (MANWAR)
  • HΓ₯kon HΓ¦gland (hakonhagland, HAKONH)
  • Meredith (merrilymeredith, MHOWARD)
  • Diab Jerius (DJERIUS)
  • Eric Brine (IKEGAMI)
  • szTheory
  • JosΓ© JoaquΓ­n Atria (JJATRIA)
  • Pete Houston (openstrike, HOUSTON)
  • Lukas Mai (MAUKE)

This software is copyright (c) 2015-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.


okay that's the whole page, bestie β€” go build your little C struct with main character energy πŸ’…βœ¨. sign-off tea: this footer vibe came from perl v5.42.3, dated 2026-08-20, under the FFI::P...:Record(3pm) name. bye πŸ‘‹πŸ’–

πŸ“¦ where this page comes from