perlxstypemap(3)

πŸ“¦
version 3.61 Β· 4 versions
, in fedora 44

πŸ’– perlxstypemap πŸ’–

okay bestie, welcome to perlxstypemap β€” the Perl XS C/Perl type mapping page, aka the group chat where C and Perl finally learn to speak each other's language 🫢


✨ DESCRIPTION ✨

ngl, the more you actually think about gluing two programming languages together, the more you realize that most of the actual work is just... converting data structures back and forth. Like, that's the whole job. It absolutely dwarfs stuff like differing calling conventions, because the problem space is SO much bigger. There are simply way more ways to shove data into memory than there are ways to implement a function call. fr fr. πŸ’€

Perl XS' solution to this whole situation is the concept of typemaps. At an abstract level, a Perl XS typemap is literally nothing but a recipe for converting from a certain Perl data structure to a certain C data structure and vice versa. And because there can be C types that are similar enough to each other that you'd want to convert them with the same logic, XS typemaps are represented by a unique identifier, which this document calls an XS type. Then you get to tell the XS compiler that multiple C types should all be mapped with that same XS typemap. iconic.

So in your XS code: when you define an argument with a C type, or when you're using a CODE: and an OUTPUT: section together with a C return type on your XSUB, it's the typemapping mechanism that makes all of that easy. πŸ’…

🧚 Anatomy of a typemap

Getting practical now: the typemap is a collection of code fragments that the xsubpp compiler uses to map C function parameters and values to Perl values. The typemap file may consist of three sections labelled TYPEMAP, INPUT, and OUTPUT. If there's an unlabelled initial section, it's assumed to be a TYPEMAP section. Here's who does what:

  • The INPUT section tells the compiler how to translate Perl values into variables of certain C types.
  • The OUTPUT section tells the compiler how to translate the values from certain C types into values Perl can understand.
  • The TYPEMAP section tells the compiler which of the INPUT and OUTPUT code fragments should be used to map a given C type to a Perl value.
Heads up: the section labels TYPEMAP, INPUT, or OUTPUT must begin in the first column, on a line by themselves, and must be in UPPERCASE. No sneaky leading spaces, bestie. 🚨

Each type of section can appear an arbitrary number of times and doesn't have to appear at all. For example, a typemap may commonly lack INPUT and OUTPUT sections if all it needs to do is associate additional C types with core XS types like T_PTROBJ. Lines that start with a hash # are considered comments and ignored in the TYPEMAP section β€” but they are considered significant in INPUT and OUTPUT. Blank lines are generally ignored.

Traditionally, typemaps needed to be written to a separate file, conventionally called typemap in a CPAN distribution. But with ExtUtils::ParseXS (the XS compiler) version 3.12 or better, which comes with perl 5.16, typemaps can also be embedded directly into XS code using a HERE-doc like syntax:

TYPEMAP: <<HERE
...
HERE

where HERE can be replaced by other identifiers like with normal Perl HERE-docs. All details below about the typemap textual format remain valid. πŸ’―

The TYPEMAP section should contain one pair of C type and XS type per line, like this. An example from the core typemap file:

TYPEMAP
# all variants of char* is handled by the T_PV typemap
char *          T_PV
const char *    T_PV
unsigned char * T_PV
...

The INPUT and OUTPUT sections have identical formats, that is, each unindented line starts a new in- or output map respectively. A new in- or output map must start with the name of the XS type to map on a line by itself, followed by the code that implements it indented on the following lines. Example:

INPUT
T_PV
  $var = ($type)SvPV_nolen($arg)
T_PTR
  $var = INT2PTR($type,SvIV($arg))

We'll get to the meaning of those Perlish-looking variables in a little bit, don't worry. πŸ™ˆ

Finally, here's an example of the full typemap file for mapping C strings of the char * type to Perl scalars/strings:

TYPEMAP
char *  T_PV

INPUT
T_PV
  $var = ($type)SvPV_nolen($arg)

OUTPUT
T_PV
  sv_setpv((SV*)$arg, $var);

Here's a more complicated example: suppose that you wanted struct netconfig to be blessed into the class Net::Config. One way to do this is to use underscores (_) to separate package names, as follows:

typedef struct netconfig * Net_Config;

And then provide a typemap entry T_PTROBJ_SPECIAL that maps underscores to double-colons (::), and declare Net_Config to be of that type:

TYPEMAP
Net_Config      T_PTROBJ_SPECIAL

INPUT
T_PTROBJ_SPECIAL
  if (sv_derived_from($arg, \"${(my $ntt=$ntype)=~s/_/::/g;\$ntt}\")){
    IV tmp = SvIV((SV*)SvRV($arg));
    $var = INT2PTR($type, tmp);
  }
  else
    croak(\"$var is not of type ${(my $ntt=$ntype)=~s/_/::/g;\$ntt}\")

OUTPUT
T_PTROBJ_SPECIAL
  sv_setref_pv($arg, \"${(my $ntt=$ntype)=~s/_/::/g;\$ntt}\",
               (void*)$var);

The INPUT and OUTPUT sections substitute underscores for double-colons on the fly, giving the desired effect. This example demonstrates some of the power and versatility of the typemap facility. ate, no crumbs. πŸ”₯

The INT2PTR macro (defined in perl.h) casts an integer to a pointer of a given type, taking care of the possible different size of integers and pointers. There are also PTR2IV, PTR2UV, PTR2NV macros, to map the other way, which may be useful in OUTPUT sections. 🌈

πŸŽ€ The Role of the typemap File in Your Distribution

The default typemap in the lib/ExtUtils directory of the Perl source contains many useful types which can be used by Perl extensions. Some extensions define additional typemaps which they keep in their own directory. These additional typemaps may reference INPUT and OUTPUT maps in the main typemap. The xsubpp compiler will allow the extension's own typemap to override any mappings which are in the default typemap. Instead of using an additional typemap file, typemaps may be embedded verbatim in XS with a heredoc-like syntax. See the documentation on the TYPEMAP: XS keyword.

For CPAN distributions, you can assume that the XS types defined by the perl core are already available. Additionally, the core typemap has default XS types for a large number of C types. For example, if you simply return a char * from your XSUB, the core typemap will have this C type associated with the T_PV XS type. That means your C string will be copied into the PV (pointer value) slot of a new scalar that will be returned from your XSUB to Perl. cute, right? 🌸

If you're developing a CPAN distribution using XS, you may add your own file called typemap to the distribution. That file may contain typemaps that either map types that are specific to your code or that override the core typemap file's mappings for common C types.

πŸ¦‹ Sharing typemaps Between CPAN Distributions

Starting with ExtUtils::ParseXS version 3.13_01 (comes with perl 5.16 and better), it is rather easy to share typemap code between multiple CPAN distributions. The general idea is to share it as a module that offers a certain API and have the dependent modules declare that as a built-time requirement and import the typemap into the XS. An example of such a typemap-sharing module on CPAN is ExtUtils::Typemaps::Basic. Two steps to getting that module's typemaps available in your code:

  • Declare ExtUtils::Typemaps::Basic as a build-time dependency in Makefile.PL (use BUILD_REQUIRES), or in your Build.PL (use build_requires).
  • Include the following line in the XS section of your XS file: (don't break the line)
INCLUDE_COMMAND: $^X -MExtUtils::Typemaps::Cmd
                 -e "print embeddable_typemap(q{Basic})"

πŸ’œ Writing typemap Entries

Each INPUT or OUTPUT typemap entry is a double-quoted Perl string that will be evaluated in the presence of certain variables to get the final C code for mapping a certain C type.

This means that you can embed Perl code in your typemap (C) code using constructs such as ${ perl code that evaluates to scalar reference here }. A common use case is to generate error messages that refer to the true function name even when using the ALIAS XS feature:

${ $ALIAS ? \q[GvNAME(CvGV(cv))] : \qq[\"$pname\"] }

For many typemap examples, refer to the core typemap file that can be found in the perl source tree at lib/ExtUtils/typemap.

The Perl variables that are available for interpolation into typemaps are the following:

  • $var - the name of the input or output variable, eg. RETVAL for return values.
  • $type - the raw C type of the parameter, any : replaced with _. e.g. for a type of Foo::Bar, $type is Foo__Bar
  • $ntype - the supplied type with * replaced with Ptr. e.g. for a type of Foo*, $ntype is FooPtr
  • $arg - the stack entry, that the parameter is input from or output to, e.g. ST(0)
  • $argoff - the argument stack offset of the argument. ie. 0 for the first argument, etc.
  • $pname - the full name of the XSUB, with including the PACKAGE name, with any PREFIX stripped. This is the non-ALIAS name.
  • $Package - the package specified by the most recent PACKAGE keyword.
  • $ALIAS - non-zero if the current XSUB has any aliases declared with ALIAS.

πŸ‘‘ Full Listing of Core Typemaps

Each C type is represented by an entry in the typemap file that is responsible for converting perl variables (SV, AV, HV, CV, etc.) to and from that type. The following sections list all XS types that come with perl by default. bestie, this is the whole menu, no skips. πŸ“

⭐ T_SV

This simply passes the C representation of the Perl variable (an SV*) in and out of the XS layer. This can be used if the C code wants to deal directly with the Perl variable.

⭐ T_SVREF

Used to pass in and return a reference to an SV.

Note that this typemap does not decrement the reference count when returning the reference to an SV*. See also: T_SVREF_REFCOUNT_FIXED

⭐ T_SVREF_FIXED

Used to pass in and return a reference to an SV. This is a fixed variant of T_SVREF that decrements the refcount appropriately when returning a reference to an SV*. Introduced in perl 5.15.4.

⭐ T_AVREF

From the perl level this is a reference to a perl array. From the C level this is a pointer to an AV.

Note that this typemap does not decrement the reference count when returning an AV*. See also: T_AVREF_REFCOUNT_FIXED

T_AVREF_REFCOUNT_FIXED

From the perl side this is a reference to a perl array, from the C side it's a pointer to an AV. It's the fixed variant of T_AVREF β€” the one that actually decrements the refcount like it's supposed to when it hands back an AV*. Introduced in perl 5.15.4. ✨


T_HVREF

Perl side: a reference to a perl hash. C side: a pointer to an HV. Simple. Iconic. πŸ’…

Heads up bestie β€” this typemap does not decrement the reference count when returning an HV*. See also: T_HVREF_REFCOUNT_FIXED if that's the behaviour you're after. 😭

T_HVREF_REFCOUNT_FIXED

Perl side: a reference to a perl hash. C side: a pointer to an HV. This is the fixed variant of T_HVREF that decrements the refcount appropriately when returning an HV*. Introduced in perl 5.15.4. 🫢


T_CVREF

From perl, this is a reference to a perl subroutine β€” like $sub = sub { 1 };. From C, it's a pointer to a CV. 🌸

Okay so the docs here literally say: "Note that this typemap does not decrement the reference count when returning an HV*. See also: T_HVREF_REFCOUNT_FIXED". Yes, HV*, in the CV entry. That's what it says, I'm just the messenger. 🫠

T_CVREF_REFCOUNT_FIXED

From perl, a reference to a perl subroutine β€” e.g. $sub = sub { 1 };. From C, a pointer to a CV.

This is a fixed variant of T_HVREF that decrements the refcount appropriately when returning an HV*. Introduced in perl 5.15.4. (And yes, the original really does say T_HVREF and HV* in the CV entry β€” keeping it exactly as written, no cap. πŸ™ˆ)


T_SYSRET

T_SYSRET is the typemap that handles return values coming back from system calls. It only makes sense going C β†’ perl β€” there's literally no concept of passing a system return value from Perl to C, so don't even try. 🚫

System calls return -1 on error (and they set ERRNO with the reason why), and (usually) 0 on success. So:

  • If the return value is -1, this typemap gives you back undef. πŸ’€
  • If it's not -1, it takes that 0 (which is perl-false) and translates it into "0 but true" (which is perl-true), or it just returns the value itself β€” either way, the vibe is "the command succeeded". πŸ‘‘

The POSIX module makes extensive use of this type. 🌈


T_UV

An unsigned integer. πŸ“


T_IV

A signed integer. Gets cast to whatever integer type is required on the way into C, and converted to an IV on the way back to Perl. πŸ”


T_INT

A signed integer. This one converts the Perl value to a native integer type β€” that's the int type on whatever platform you're on. When it heads back to perl it gets processed the exact same way as T_IV.

Its behaviour is identical to just using an int type in XS with T_IV. Same energy, different name. ✨


T_ENUM

An enum value. Used to transfer an enum component from C. There's genuinely no reason to pass an enum value to C, because inside perl it's just stored as an IV. 🀷


T_BOOL

A boolean type. Use it to pass true and false values both to and from C. πŸŽ€


T_U_INT

This one's for unsigned integers. It's equivalent to using T_UV, except it explicitly casts the variable to type unsigned int. The default type for unsigned int is T_UV. πŸ’œ


T_SHORT

Short integers. Equivalent to T_IV, but it explicitly casts the return to type short. The default typemap for short is T_IV. 🩳


T_U_SHORT

Unsigned short integers. Equivalent to T_UV, but it explicitly casts the return to type unsigned short. The default typemap for unsigned short is T_UV.

T_U_SHORT is used for type U16 in the standard typemap. πŸ”’


T_LONG

Long integers. Equivalent to T_IV, but it explicitly casts the return to type long. The default typemap for long is T_IV. πŸ“


T_U_LONG

Unsigned long integers. Equivalent to T_UV, but it explicitly casts the return to type unsigned long. The default typemap for unsigned long is T_UV.

T_U_LONG is used for type U32 in the standard typemap. πŸ”’


T_CHAR

Single 8-bit characters. 🧚


T_U_CHAR

An unsigned byte. πŸ¦‹


T_FLOAT

A floating point number. This typemap guarantees to return a variable cast to a float. 🌊


T_NV

A Perl floating point number. Similar to T_IV and T_UV in that the return type gets cast to the requested numeric type rather than to one specific type. Flexible queen. πŸ‘‘


T_DOUBLE

A double precision floating point number. This typemap guarantees to return a variable cast to a double. ✌️


T_PV

A string (char *). πŸ“


T_PTR

A memory address β€” a pointer. Typically it's associated with a void * type. πŸ“


T_PTRREF

Basically T_PTR but with ✨secrecy✨ β€” the pointer gets stored in a scalar, and the reference to that scalar is what's returned to the caller. Super useful for hiding the actual pointer value from the programmer, since you usually don't need it directly from inside perl anyway.

The typemap also checks that a scalar reference is what got passed from perl into XS. βœ…


T_PTROBJ

Like T_PTRREF, except the reference is blessed into a class. This lets the pointer be used as an object β€” most commonly when you're dealing with C structs. The typemap checks that the perl object you passed into the XS routine is of the correct class (or part of a subclass). πŸ‘―

The pointer gets blessed into a class derived from the name of the type of the pointer, but with every '*' in that name replaced with 'Ptr'. βœ‚οΈ

For DESTROY XSUBs only, a T_PTROBJ is optimized down to a T_PTRREF. Which means the class check gets skipped entirely. πŸƒ


T_REF_IV_REF

NOT YET 🚧


T_REF_IV_PTR

Similar to T_PTROBJ in that the pointer gets blessed into a scalar object. The difference? When the object gets passed back into XS it has to be of the correct type β€” inheritance is not supported here β€” whereas T_PTROBJ does support inheritance. πŸ’…

The pointer gets blessed into a class derived from the name of the type of the pointer, but with every '*' in that name replaced with 'Ptr'. βœ‚οΈ

For DESTROY XSUBs only, a T_REF_IV_PTR is optimized down to a T_PTRREF. So yes, the class check gets skipped. πŸƒ


T_PTRDESC

NOT YET 🚧


T_REFREF

Similar to T_PTRREF, except the pointer stored in the referenced scalar gets dereferenced and copied to the output variable. Which means T_REFREF is to T_PTRREF what T_OPAQUE is to T_OPAQUEPTR. All clear? 😭 (bestie no, it's not, but we move)

Only the INPUT part of this is implemented (that's Perl β†’ XSUB), and there are no known users in core or on CPAN. πŸ‘»


T_REFOBJ

Like T_REFREF, except it does strict type checking β€” and nope, inheritance is not supported. 🚫

For DESTROY XSUBs only, a T_REFOBJ is optimized down to a T_REFREF. Which means the class check gets skipped. πŸƒ


T_OPAQUEPTR

This one lets you store bytes in the string component of the SV. Here the representation of the data is completely irrelevant to perl β€” the bytes themselves just get stored in the SV. It assumes the C variable is a pointer (the bytes get copied from that memory location). So if the pointer is pointing at something represented by 8 bytes, those 8 bytes get stored in the SV (and length() will report a value of 8). This entry is similar to T_OPAQUE. 🫧

In principle, the unpack() command can be used to convert those bytes back into a number β€” if the underlying type is known to be a number.

This entry can also store a C structure (the number of bytes to copy gets calculated with the C sizeof function) and works as an alternative to T_PTRREF without you having to worry about a memory leak, since Perl will clean up the SV for you. 🧼


T_OPAQUE

This one stores data from non-pointer types in the string part of an SV. It's similar to T_OPAQUEPTR, except the typemap retrieves the pointer directly instead of assuming one is being supplied. For example, if an integer gets imported into Perl using T_OPAQUE rather than T_IV, the underlying bytes representing that integer get stored in the SV β€” but the actual integer value will not be available. i.e. the data is opaque to perl. 🌫️

You can retrieve the data using the unpack function, if the underlying type of the byte stream is known.

T_OPAQUE supports both input and output of simple types. T_OPAQUEPTR can be used to pass those bytes back into C, if a pointer is acceptable. πŸ”


Implicit array

xsubpp supports a special syntax for returning packed C arrays to perl. If the XS return type is given as

array(type, nelem)

then xsubpp will copy the contents of nelem * sizeof(type) bytes from RETVAL to an SV and push it onto the stack. This is only really useful if the number of items to be returned is known at compile time, and you genuinely don't mind having a string of bytes sitting in your SV. Use T_ARRAY to push a variable number of arguments onto the return stack instead (they won't be packed as a single string though). πŸ“¦

This is similar to using T_OPAQUEPTR, but it can be used to process more than one element. ✨


T_PACKED

This calls user-supplied functions for conversion. For OUTPUT (that's XSUB β†’ Perl), a function named XS_pack_$ntype gets called with the output Perl scalar and the C variable to convert from. Here $ntype is the normalized C type that's being mapped to Perl. "Normalized" means all "*" get replaced by the string "Ptr". The return value of the function is ignored. πŸ™ˆ

Conversely, for INPUT mapping (Perl β†’ XSUB), the function named XS_unpack_$ntype gets called with the input Perl scalar as its argument, and the return value gets cast to the mapped C type and assigned to the output C variable.

An example conversion function for a typemapped struct foo_t * might be:

static void
XS_pack_foo_tPtr(SV *out, foo_t *in)
{
  dTHX; /* alas, signature does not include pTHX_ */
  HV* hash = newHV();
  hv_stores(hash, "int_member", newSViv(in->int_member));
  hv_stores(hash, "float_member", newSVnv(in->float_member));
  /* ... */

  /* mortalize as thy stack is not refcounted */
  sv_setsv(out, sv_2mortal(newRV_noinc((SV*)hash)));
}

The conversion from Perl to C is left as an exercise to the reader 😭, but the prototype would be:

static foo_t *
XS_unpack_foo_tPtr(SV *in);

Instead of an actual C function that has to fetch the thread context using dTHX, you can define macros of the same name and skip that overhead entirely. Also β€” keep in mind that you might need to free the memory allocated by XS_unpack_foo_tPtr. 🧹

T_PACKEDARRAY πŸ’…

okay so T_PACKEDARRAY is basically T_PACKED's sibling, fr fr. The "INPUT" typemap (Perl to XSUB) is literally identical. But the "OUTPUT" typemap? It passes an extra argument to the XS_pack_$ntype function. This third parameter is like "hey here's how many elements are in the output" so the function can handle C arrays sanely. The variable has to be declared by you, the user, and it must be named count_$ntype where $ntype is the normalized C type name as explained above. The signature would look like this for the example above and "foo_t **":

static void
XS_pack_foo_tPtrPtr(SV *out, foo_t *in, UV count_foo_tPtrPtr);

The type of that third parameter is arbitrary as far as the typemap cares. It just has to be in line with the declared variable. periodt. ✨

Now, unless you know the number of elements in the "sometype **" C array inside your XSUB, the return value from foo_t ** XS_unpack_foo_tPtrPtr(...) is gonna be hard to decipher, no cap. Since all the details are up to the XS author (that's you, the typemap user), there are several solutions, and none of them are particularly elegant. The most commonly seen solution? Allocate memory for N+1 pointers and assign NULL to the (N+1)th one so iteration is easier.

Alternatively, using a customized typemap for your purposes from the start is probably preferable. πŸ’–

T_DATAUNIT

NOT YET πŸ’€

T_CALLBACK

NOT YET πŸ’€

T_ARRAY 🌸

This one is used to convert the perl argument list to a C array, and for pushing the contents of a C array onto the perl argument stack. The usual calling signature is:

@out = array_func( @in );

Any number of arguments can occur in the list before the array, but the input and output arrays must be the last elements in the list.

When you're passing a perl list to C, the XS writer must provide a function (named after the array type but with 'Ptr' substituted for '*') to allocate the memory needed to hold the list. A pointer should be returned. It's up to the XS writer to free that memory when exiting the function. The variable ix_$var is set to the number of elements in the new array.

When returning a C array to Perl, the XS writer must provide an integer variable called size_$var containing the number of elements in the array. This is used to figure out how many elements should be pushed onto the return argument stack. This is not required on input since Perl knows how many arguments are on the stack when the routine is called. Ordinarily this variable would be called size_RETVAL.

Additionally, the type of each element is determined from the type of the array. If the array uses type intArray *, xsubpp will automatically work out that it contains variables of type int and use that typemap entry to perform the copy of each element. All pointer '*' and 'Array' tags are removed from the name to determine the subtype. πŸ‘‘

T_STDIO

This is used for passing perl filehandles to and from C using FILE * structures. Simple as that. ✨

T_INOUT πŸ¦‹

This is used for passing perl filehandles to and from C using PerlIO * structures. The file handle can be used for reading and writing. This corresponds to the "+<" mode, see also T_IN and T_OUT.

See perliol for more information on the Perl IO abstraction layer. Perl must have been built with -Duseperlio.

There is no check to assert that the filehandle passed from Perl to C was created with the right open() mode.

Hint: The perlxstut tutorial covers the T_INOUT, T_IN, and T_OUT XS types nicely.

T_IN

Same as T_INOUT, but the filehandle that is returned from C to Perl can only be used for reading (mode "<").

T_OUT

Same as T_INOUT, but the filehandle that is returned from C to Perl is set to use the open mode "+>".


and that's the whole vibe, bestie 🫢 this was perl v5.42.0, dated 2026-01-17, from perlxstypemap(3). stay iconic πŸ‘‘πŸ’–

πŸ“¦ where this page comes from