FFI::Platypus::Buffer(3) Convert scalars to C buffers


version 0.43


use FFI::Platypus::Buffer;
my($pointer, $size) = scalar_to_buffer $scalar;
my $scalar2 = buffer_to_scallar $pointer, $size;


A common pattern in C is to pass a ``buffer'' or region of memory into a function with a pair of arguments, an opaque pointer and the size of the memory region. In Perl the equivalent structure is a scalar containing a string of bytes. This module provides portable functions for converting a Perl string or scalar into a buffer and back.

These functions are implemented using pack and unpack and so they should be relatively fast.

Both functions are exported by default, but you can explicitly export one or neither if you so choose.

A better way to do this might be with custom types see FFI::Platypus::API and FFI::Platypus::Type. These functions were taken from the now obsolete FFI::Util module, as they may be useful in some cases.



 my($pointer, $size) = scalar_to_buffer $scalar;

Convert a string scalar into a buffer. Returned in order are a pointer to the start of the string scalar's memory region and the size of the region.


 my $scalar = buffer_to_scalar $pointer, $size;

Convert the buffer region defined by the pointer and size into a string scalar.


Author: Graham Ollis <[email protected]>


Bakkiaraj Murugesan (bakkiaraj)

Dylan Cali (calid)


Zaki Mughal (zmughal)

Fitz Elliott (felliott)

Vickenty Fesunov (vyf)

Gregor Herrmann (gregoa)


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