PERLXS(1)

πŸ’– NAME πŸ’–

perlxs β€” XS language reference manual. Bestie, this is literally the manual for the XS language, and we're about to get into it. πŸŽ€

🌸 DESCRIPTION 🌸

✨ Introduction ✨

Okay so, XS is an interface description file format used to create an extension interface between Perl and C code (or a C library) that you want to use with Perl. The XS interface gets combined with the library to create a new library, and that new library can then be either dynamically loaded or statically linked into perl. The XS interface description is written in the XS language, and it is the core component of the Perl extension interface. Periodt. πŸ’…

This documents the XS language, but first β€” and this is important β€” XS code has full access to system calls, including C library functions. It can therefore interfere with things that the Perl core or other modules have set up, like signal handlers or file handles. It could mess with memory, or do any number of harmful things. Don't. πŸ’€ Further detail is in perlclib, which you should read before actually writing any production XS. No cap.

An XSUB forms the basic unit of the XS interface. After compilation by the xsubpp compiler, each XSUB amounts to a C function definition which provides the glue between Perl calling conventions and C calling conventions. It's giving matchmaker energy. 🫢

The glue code pulls the arguments from the Perl stack, converts these Perl values to the formats expected by a C function, calls this C function, and then transfers the return values of the C function back to Perl. Return values here may be a conventional C return value, or any C function arguments that may serve as output parameters. These return values may be passed back to Perl either by putting them on the Perl stack, or by modifying the arguments supplied from the Perl side. 🌈

The above is a somewhat simplified view of what really happens. Since Perl allows more flexible calling conventions than C, XSUBs may do much more in practice β€” like checking input parameters for validity, throwing exceptions (or returning undef/empty list) if the return value from the C function indicates failure, calling different C functions based on numbers and types of the arguments, providing an object-oriented interface, etc. Main character energy, honestly. πŸ‘‘

Of course, one could write such glue code directly in C. However, this would be a tedious task, especially if one needs to write glue for multiple C functions, and/or one is not familiar enough with the Perl stack discipline and other such arcana. XS comes to the rescue here: instead of writing this glue C code in long-hand, one can write a more concise short-hand description of what should be done by the glue, and let the XS compiler xsubpp handle the rest. Slay. ✨

The XS language allows one to describe the mapping between how the C routine is used and how the corresponding Perl routine is used. It also allows creation of Perl routines which are directly translated to C code and which are not related to a pre-existing C function. In cases when the C interface coincides with the Perl interface, the XSUB declaration is almost identical to a declaration of a C function (in K&R style). In such circumstances, there is another tool called h2xs that is able to translate an entire C header file into a corresponding XS file that will provide glue to the functions/macros described in the header file. Obsessed with the convenience. πŸ¦‹

The XS compiler is called xsubpp. This compiler creates the constructs necessary to let an XSUB manipulate Perl values, and creates the glue necessary to let Perl call the XSUB. The compiler uses typemaps to determine how to map C function parameters and output values to Perl values and back. The default typemap (which comes with Perl) handles many common C types. A supplementary typemap may also be needed to handle any special structures and types for the library being linked. For more information on typemaps, see perlxstypemap.

A file in XS format starts with a C language section which goes until the first MODULE = directive. Other XS directives and XSUB definitions may follow this line. The "language" used in this part of the file is usually referred to as the XS language. xsubpp recognizes and skips POD (see perlpod) in both the C and XS language sections, which allows the XS file to contain embedded documentation. Cute AND functional. πŸŽ€

See perlxstut for a tutorial on the whole extension creation process.

Note: For some extensions, Dave Beazley's SWIG system may provide a significantly more convenient mechanism for creating the extension glue code. See <http://www.swig.org/> for more information.

For simple bindings to C libraries as well as other machine code libraries, consider instead using the much simpler libffi <http://sourceware.org/libffi/> interface via CPAN modules like FFI::Platypus or FFI::Raw. Options are iconic. 🌸

πŸš— On The Road πŸš—

Many of the examples which follow will concentrate on creating an interface between Perl and the ONC+ RPC bind library functions. The rpcb_gettime() function is used to demonstrate many features of the XS language. This function has two parameters; the first is an input parameter and the second is an output parameter. The function also returns a status value.

bool_t rpcb_gettime(const char *host, time_t *timep);

From C this function will be called with the following statements.

#include <rpc/rpc.h>
bool_t status;
time_t timep;
status = rpcb_gettime( "localhost", &timep );

If an XSUB is created to offer a direct translation between this function and Perl, then this XSUB will be used from Perl with the following code. The $status and $timep variables will contain the output of the function. πŸ’–

use RPC;
$status = rpcb_gettime( "localhost", $timep );

The following XS file shows an XS subroutine, or XSUB, which demonstrates one possible interface to the rpcb_gettime() function. This XSUB represents a direct translation between C and Perl and so preserves the interface even from Perl. This XSUB will be invoked from Perl with the usage shown above. Note that the first three #include statements, for "EXTERN.h", "perl.h", and "XSUB.h", will always be present at the beginning of an XS file. This approach and others will be expanded later in this document. A #define for "PERL_NO_GET_CONTEXT" should be present to fetch the interpreter context more efficiently, see perlguts for details.

#define PERL_NO_GET_CONTEXT
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include <rpc/rpc.h>

MODULE = RPC  PACKAGE = RPC

bool_t
rpcb_gettime(host,timep)
     char *host
     time_t &timep
   OUTPUT:
     timep

Any extension to Perl, including those containing XSUBs, should have a Perl module to serve as the bootstrap which pulls the extension into Perl. This module will export the extension's functions and variables to the Perl program and will cause the extension's XSUBs to be linked into Perl. The following module will be used for most of the examples in this document and should be used from Perl with the use command as shown earlier. Perl modules are explained in more detail later in this document.

package RPC;

require Exporter;
require DynaLoader;
@ISA = qw(Exporter DynaLoader);
@EXPORT = qw( rpcb_gettime );

bootstrap RPC;
1;

Throughout this document a variety of interfaces to the rpcb_gettime() XSUB will be explored. The XSUBs will take their parameters in different orders or will take different numbers of parameters. In each case the XSUB is an abstraction between Perl and the real C rpcb_gettime() function, and the XSUB must always ensure that the real rpcb_gettime() function is called with the correct parameters. This abstraction will allow the programmer to create a more Perl-like interface to the C function. Bestie, that's the whole vibe. πŸ’…

πŸ«€ The Anatomy of an XSUB πŸ«€

The simplest XSUBs consist of 3 parts: a description of the return value, the name of the XSUB routine and the names of its arguments, and a description of types or formats of the arguments.

The following XSUB allows a Perl program to access a C library function called sin(). The XSUB will imitate the C function which takes a single argument and returns a single value.

double
sin(x)
  double x

Optionally, one can merge the description of types and the list of argument names, rewriting this as

double
sin(double x)

This makes this XSUB look similar to an ANSI C declaration. An optional semicolon is allowed after the argument list, as in

double
sin(double x);

Parameters with C pointer types can have different semantic: C functions with similar declarations

bool string_looks_as_a_number(char *s);
bool make_char_uppercase(char *c);

are used in absolutely incompatible manner. Parameters to these functions could be described to xsubpp like this:

char *  s
char    &c

Both these XS declarations correspond to the char* C type, but they have different semantics, see "The & Unary Operator".

It is convenient to think that the indirection operator * should be considered as a part of the type and the address operator & should be considered part of the variable. See perlxstypemap for more info about handling qualifiers and unary operators in C types. 🧚

The function name and the return type must be placed on separate lines and should be flush left-adjusted.

INCORRECT                        CORRECT

double sin(x)                    double
  double x                       sin(x)
                                   double x

The rest of the function description may be indented or left-adjusted. The following example shows a function with its body left-adjusted. Most examples in this document will indent the body for better readability.

CORRECT

double
sin(x)
double x

More complicated XSUBs may contain many other sections. Each section of an XSUB starts with the corresponding keyword, such as INIT: or CLEANUP:. However, the first two lines of an XSUB always contain the same data: descriptions of the return type and the names of the function and its parameters. Whatever immediately follows these is considered to be an INPUT: section unless explicitly marked with another keyword. (See "The INPUT: Keyword".)

An XSUB section continues until another section-start keyword is found. πŸŽ€

✨ The Argument Stack ✨

Okay so bestie, picture this: there's a Perl argument stack, right? It's where the values get parked that are being sent as parameters to the XSUB, and it's also where the XSUB's return value(s) go. And like β€” in reality all Perl functions (yes, even the non-XSUB ones, no cap) keep their values on this same stack at all the same time, each one limited to its own little range of positions on the stack. πŸ’…

In this doc, the first position on that stack which belongs to the active function? We just call that position 0 for that function. Simple.

XSUBs point at their stack arguments with the macro ST(x), where x is a position in this XSUB's own slice of the stack. So position 0 for the function is known to the XSUB as ST(0). The XSUB's incoming parameters and its outgoing return values always begin at ST(0). For many simple cases the xsubpp compiler will generate the code needed to handle the argument stack by embedding code fragments it finds in the typemaps. For the messier cases? The programmer has to supply the code themselves. 😭

πŸŽ€ The RETVAL Variable πŸŽ€

RETVAL is a special C variable that gets declared automatically for you β€” cute, right? The C type of RETVAL matches the return type of the C library function. The xsubpp compiler will declare this variable in each XSUB with a non-void return type. By default the generated C function uses RETVAL to hold the return value of the C library function being called. And in simple cases the value of RETVAL gets placed in ST(0) of the argument stack, where Perl can receive it as the return value of the XSUB. 🫢

If the XSUB has a return type of void then the compiler will not declare a RETVAL variable for that function. When you're using a PPCODE: section, no manipulation of the RETVAL variable is required β€” the section may use direct stack manipulation to place output values on the stack.

If the PPCODE: directive is not used, void return value should be used only for subroutines which do not return a value β€” even if the CODE: directive is used and sets ST(0) explicitly.

⚠️ Deprecated alert ⚠️ Older versions of this document recommended using void return value in such cases. It was discovered that this could lead to segfaults in cases when the XSUB was truly void. This practice is now deprecated, and may be not supported at some future version. Use the return value SV * in such cases instead. (Currently xsubpp contains some heuristic code which tries to disambiguate between "truly-void" and "old-practice-declared-as-void" functions. Hence your code is at the mercy of these heuristics unless you use SV * as return value. 😬)

πŸ’– Returning SVs, AVs and HVs through RETVAL πŸ’–

So when you're using RETVAL to return an SV *, there's some behind-the-scenes magic going on that honestly deserves a mention. When you're manipulating the argument stack with the ST(x) macro, for example, you usually have to pay special attention to reference counts. (For more about reference counts, see perlguts.) To make your life easier, the typemap file automatically makes RETVAL mortal when you're returning an SV *. 🧚

So these two XSUBs are more or less equivalent:

void
alpha()
    PPCODE:
        ST(0) = newSVpv("Hello World",0);
        sv_2mortal(ST(0));
        XSRETURN(1);

SV *
beta()
    CODE:
        RETVAL = newSVpv("Hello World",0);
    OUTPUT:
        RETVAL

This is quite useful as it usually improves readability. Slay. But while this works fine for an SV *, it's unfortunately not as easy to have AV * or HV * as a return value. In theory you should be able to write:

AV *
array()
    CODE:
        RETVAL = newAV();
        /* do something with RETVAL */
    OUTPUT:
        RETVAL

But due to an unfixable bug (fixing it would break lots of existing CPAN modules) in the typemap file, the reference count of the AV * is not properly decremented. πŸ’€ So the above XSUB would leak memory whenever it is being called. The same problem exists for HV *, CV *, and SVREF (which indicates a scalar reference, not a general SV *).

In XS code on perls starting with perl 5.16, you can override the typemaps for any of these types with a version that has proper handling of refcounts. In your TYPEMAP section, do:

AV*   T_AVREF_REFCOUNT_FIXED

...to get the repaired variant. ✨

For backward compatibility with older versions of perl, you can instead decrement the reference count manually when you're returning one of the aforementioned types, using sv_2mortal:

AV *
array()
    CODE:
        RETVAL = newAV();
        sv_2mortal((SV*)RETVAL);
        /* do something with RETVAL */
    OUTPUT:
        RETVAL

Remember that you don't have to do this for an SV *. And the reference documentation for all core typemaps can be found in perlxstypemap. πŸ“š

πŸ¦‹ The MODULE Keyword πŸ¦‹

The MODULE keyword is used to start the XS code and to specify the package of the functions which are being defined. Like, it's giving opening credits energy.

All text preceding the first MODULE keyword is considered C code and is passed through to the output with POD stripped, but otherwise untouched. Every XS module will have a bootstrap function which is used to hook the XSUBs into Perl. The package name of this bootstrap function will match the value of the last MODULE statement in the XS source files. The value of MODULE should always remain constant within the same XS file, though this is not required.

The following example will start the XS code and will place all functions in a package named RPC.

MODULE = RPC

🌸 The PACKAGE Keyword 🌸

When functions within an XS source file must be separated into packages, the PACKAGE keyword is your girl. This keyword is used with the MODULE keyword and must follow immediately after it when used. Periodt.

MODULE = RPC  PACKAGE = RPC

[ XS code in package RPC ]

MODULE = RPC  PACKAGE = RPCB

[ XS code in package RPCB ]

MODULE = RPC  PACKAGE = RPC

[ XS code in package RPC ]

The same package name can be used more than once, allowing for non-contiguous code. This is useful if you have a stronger ordering principle than package names. (Iconic, honestly.)

Although this keyword is optional and in some cases provides redundant information, it should always be used. This keyword will ensure that the XSUBs appear in the desired package. 🫢

⭐ The PREFIX Keyword ⭐

The PREFIX keyword designates prefixes which should be removed from the Perl function names. So if the C function is rpcb_gettime() and the PREFIX value is rpcb_ then Perl will see this function as gettime(). Lowkey a glow-up. πŸ’…

This keyword should follow the PACKAGE keyword when used. If PACKAGE is not used then PREFIX should follow the MODULE keyword.

MODULE = RPC  PREFIX = rpc_

MODULE = RPC  PACKAGE = RPCB  PREFIX = rpcb_

πŸ”₯ The OUTPUT: Keyword πŸ”₯

The OUTPUT: keyword indicates that certain function parameters should be updated (new values made visible to Perl) when the XSUB terminates β€” or that certain values should be returned to the calling Perl function.

For simple functions which have no CODE: or PPCODE: section, such as the sin() function above, the RETVAL variable is automatically designated as an output value. For more complex functions the xsubpp compiler will need help to determine which variables are output variables. πŸ˜…

This keyword will normally be used to complement the CODE: keyword. The RETVAL variable is not recognized as an output variable when the CODE: keyword is present. The OUTPUT: keyword is used in this situation to tell the compiler that RETVAL really is an output variable.

The OUTPUT: keyword can also be used to indicate that function parameters are output variables. This may be necessary when a parameter has been modified within the function and the programmer would like the update to be seen by Perl.

bool_t
rpcb_gettime(host,timep)
     char *host
     time_t &timep
   OUTPUT:
     timep

The OUTPUT: keyword will also allow an output parameter to be mapped to a matching piece of code rather than to a typemap. πŸ‘‘

bool_t
rpcb_gettime(host,timep)
     char *host
     time_t &timep
   OUTPUT:
     timep sv_setnv(ST(1), (double)timep);

xsubpp emits an automatic SvSETMAGIC() for all parameters in the OUTPUT section of the XSUB, except RETVAL. This is the usually desired behavior, as it takes care of properly invoking 'set' magic on output parameters (needed for hash or array element parameters that must be created if they didn't exist). If for some reason this behavior is not desired, the OUTPUT section may contain a SETMAGIC: DISABLE line to disable it for the remainder of the parameters in the OUTPUT section. Likewise, SETMAGIC: ENABLE can be used to reenable it for the remainder of the OUTPUT section. See perlguts for more details about 'set' magic. ✨

πŸ’œ The NO_OUTPUT Keyword πŸ’œ

The NO_OUTPUT can be placed as the first token of the XSUB. This keyword indicates that while the C subroutine we provide an interface to has a non-void return type, the return value of this C subroutine should not be returned from the generated Perl subroutine.

With this keyword present, "The RETVAL Variable" is created, and in the generated call to the subroutine this variable is assigned to β€” but the value of this variable is not going to be used in the auto-generated code.

This keyword makes sense only if RETVAL is going to be accessed by the user-supplied code. It's especially useful to make a function interface more Perl-like, especially when the C return value is just an error condition indicator. For example:

NO_OUTPUT int
delete_file(char *name)
  POSTCALL:
    if (RETVAL != 0)
        croak("Error %d while deleting file '%s'", RETVAL, name);

Here the generated XS function returns nothing on success, and will die() with a meaningful error message on error. 🎯

🌈 The CODE: Keyword 🌈

This keyword is used in the more complicated XSUBs which require special handling for the C function. The RETVAL variable is still declared, but it will not be returned unless it is specified in the OUTPUT: section.

The following XSUB is for a C function which requires special handling of its parameters. The Perl usage is given first.

     $status = rpcb_gettime( "localhost", $timep );

The XSUB follows.

     bool_t
     rpcb_gettime(host,timep)
          char *host
          time_t timep
        CODE:
               RETVAL = rpcb_gettime( host, &timep );
        OUTPUT:
          timep
          RETVAL

πŸ“ The INIT: Keyword πŸ“

The INIT: keyword allows initialization to be inserted into the XSUB before the compiler generates the call to the C function. Unlike the CODE: keyword above, this keyword does not affect the way the compiler handles RETVAL. 🧚

bool_t
rpcb_gettime(host,timep)
      char *host
      time_t &timep
    INIT:
      printf("# Host is %s\n", host );
    OUTPUT:
      timep

And okay bestie, here's the other thing INIT: is literally iconic for: checking your preconditions before you even think about calling the C function. Like a vibe check but for arguments. 🫢

long long
lldiv(a,b)
    long long a
    long long b
  INIT:
    if (a == 0 && b == 0)
        XSRETURN_UNDEF;
    if (b == 0)
        croak("lldiv: cannot divide by 0");

No cap, that's the whole flex β€” bail out before the call, not after. πŸ’…


🌸 The NO_INIT Keyword 🌸

So NO_INIT is the flag you slap on a parameter when it's output-only. Like, it's not coming in, it's coming out. πŸ’€

Here's the deal: the xsubpp compiler will normally generate code to read the values of all function parameters from the argument stack and assign them to C variables upon entry to the function. NO_INIT tells the compiler that some parameters are gonna be used for output rather than for input, and that they get handled before the function terminates. ✨

This next example is a variation on rpcb_gettime(). The timep variable is used only as an output variable here, and it genuinely does not care about its initial contents. Zero thoughts, head empty, output only. πŸŽ€

 bool_t
 rpcb_gettime(host,timep)
      char *host
      time_t &timep = NO_INIT
    OUTPUT:
      timep

πŸ¦‹ The TYPEMAP: Keyword πŸ¦‹

Starting with Perl 5.16 (iykyk, that's the version gate), you can embed typemaps into your XS code instead of β€” or in addition to β€” typemaps in a separate file. 🌈

Multiple such embedded typemaps get processed in order of appearance in the XS code, and just like local typemap files they take precedence over the default typemap. Also: the embedded typemaps may overwrite previous definitions of TYPEMAP, INPUT, and OUTPUT stanzas. So like… order matters, bestie. πŸ‘‘

The syntax for embedded typemaps is:

TYPEMAP: <<HERE
... your typemap code here ...
HERE

where the "TYPEMAP" keyword must appear in the first column of a new line. No sneaking it in with indentation, it will not slay. πŸ™ˆ

Refer to perlxstypemap for details on writing typemaps. πŸ“š


πŸ’œ Initializing Function Parameters πŸ’œ

Okay so normally, C function parameters get initialized with their values from the argument stack β€” which in turn contains the parameters that were passed to the XSUB from Perl. The typemaps hold the code segments that translate the Perl values into the C parameters. 🧚

But you, main character, are allowed to override the typemaps and supply alternate (or additional) initialization code. Initialization code starts with the first "=", ";" or "+" on a line in the INPUT: section. The only exception happens if this ";" terminates the line β€” then that ";" is quietly ignored. Like it said nothing. πŸ’€

Here's how you supply initialization code for function parameters. Heads up though: the initialization code is eval'ed within double quotes by the compiler before it's added to the output, so anything that should be interpreted literally β€” [mainly "$", "@", or "\\"] β€” must be protected with backslashes. And the variables $var, $arg, and $type can be used as in typemaps. ✨

 bool_t
 rpcb_gettime(host,timep)
      char *host = (char *)SvPVbyte_nolen($arg);
      time_t &timep = 0;
    OUTPUT:
      timep
⚠️ Okay lowkey important: this should not be used to supply default values for parameters. You'd normally reach for this when a function parameter must be processed by another library function before it can be used. Default parameters are a whole separate section, coming right up. 🫢

Now, the rules, and they're actually cute once you get them:

  • If the initialization begins with "=", then it's output in the declaration</> for the input variable, replacing the initialization supplied by the typemap. πŸ’…
  • If the initialization begins with ";" or "+", then it's performed after all of the input variables have been declared.
    • In the ";" case, the initialization normally supplied by the typemap is not performed. πŸ™…
    • For the "+" case, the declaration for the variable will include the initialization from the typemap. βœ…

Also there's a global variable, %v, available for the truly rare case where information from one initialization is needed in another initialization. It's giving "I know a guy." 😭

Here's a truly obscure example β€” and yes, the manpage itself calls it obscure, I'm not being mean:

 bool_t
 rpcb_gettime(host,timep)
      time_t &timep; /* \$v{timep}=@{[$v{timep}=$arg]} */
      char *host + SvOK($v{timep}) ? SvPVbyte_nolen($arg) : NULL;
    OUTPUT:
      timep

The construct "\$v{timep}=@{[$v{timep}=$arg]}" used in the example above has a two-fold purpose: ✨✨

  1. First, when this line is processed by xsubpp, the Perl snippet "$v{timep}=$arg" is evaluated.
  2. Second, the text of the evaluated snippet is output into the generated C file β€” inside a C comment! πŸ’€

During the processing of the "char *host" line, $arg will evaluate to ST(0), and $v{timep} will evaluate to ST(1). Iconic. πŸŽ€


πŸ“ Default Parameter Values πŸ“

Default values for XSUB arguments can be specified by placing an assignment statement in the parameter list. The default value may be a number, a string, or the special string "NO_INIT". And fr fr: defaults should always be used on the right-most parameters only. πŸ™

So to let the XSUB for rpcb_gettime() have a default host value, the parameters to the XSUB could be rearranged. The XSUB will then call the real rpcb_gettime() function with the parameters in the correct order. Slay β€” Perl side stays comfy, C side gets what it needs. 🌸

This XSUB can be called from Perl with either of these:

$status = rpcb_gettime( $timep, $host );

$status = rpcb_gettime( $timep );

And the XSUB will look like the code that follows. A CODE: block is used to call the real rpcb_gettime() function with the parameters in the correct order for that function. πŸ’–

 bool_t
 rpcb_gettime(timep,host="localhost")
      char *host
      time_t timep = NO_INIT
    CODE:
           RETVAL = rpcb_gettime( host, &timep );
    OUTPUT:
      timep
      RETVAL

⭐ The PREINIT: Keyword ⭐

The PREINIT: keyword lets extra variables be declared immediately before or after the declarations of the parameters from the INPUT: section are emitted. It's basically priority boarding for your variable declarations. 🧳✨

Here's the tea though: if you declare a variable inside a CODE: section, it will follow any typemap code that's emitted for the input parameters. And that may result in the declaration ending up after C code β€” which is a C syntax error. 😭 Similar errors may happen when an explicit ";"-type or "+"-type initialization of parameters is used (peep "Initializing Function Parameters"). And declaring these variables in an INIT: section will not help. πŸ’€

So in those cases, to force an additional variable to be declared together with the declarations of the other variables, put the declaration into a PREINIT: section. The PREINIT: keyword may be used one or more times within an XSUB. πŸ‘‘

These following examples are equivalent, but if the code is using complex typemaps, then the first example is safer. πŸ”₯

 bool_t
 rpcb_gettime(timep)
      time_t timep = NO_INIT
    PREINIT:
      char *host = "localhost";
    CODE:
      RETVAL = rpcb_gettime( host, &timep );
    OUTPUT:
      timep
      RETVAL

For this particular case, an INIT: keyword would generate the same C code as the PREINIT: keyword. Here's another correct-but-error-prone example β€” yes the manpage said "error-prone," I'm just the messenger πŸ™ˆ:

 bool_t
 rpcb_gettime(timep)
      time_t timep = NO_INIT
    CODE:
      char *host = "localhost";
      RETVAL = rpcb_gettime( host, &timep );
    OUTPUT:
      timep
      RETVAL

Another way to declare "host" is to use a C block in the CODE: section:

 bool_t
 rpcb_gettime(timep)
      time_t timep = NO_INIT
    CODE:
      {
        char *host = "localhost";
        RETVAL = rpcb_gettime( host, &timep );
      }
    OUTPUT:
      timep
      RETVAL

And the ability to put additional declarations before the typemap entries are processed is very handy in the cases when typemap conversions manipulate some global state. Like here:

MyObject
mutate(o)
    PREINIT:
        MyState st = global_state;
    INPUT:
        MyObject o;
    CLEANUP:
        reset_to(global_state, st);

Here we suppose that conversion to "MyObject" in the INPUT: section and from MyObject when processing RETVAL will modify a global variable "global_state". After these conversions are performed, we restore the old value of "global_state" β€” to avoid memory leaks, for example. Responsible queen behaviour. πŸ‘‘βœ¨

There's another way to trade clarity for compactness: INPUT sections allow declaration of C variables which do not appear in the parameter list of a subroutine. So the code above for mutate() can be rewritten as:

MyObject
mutate(o)
      MyState st = global_state;
      MyObject o;
    CLEANUP:
      reset_to(global_state, st);

and the code for rpcb_gettime() can be rewritten as:

 bool_t
 rpcb_gettime(timep)
      time_t timep = NO_INIT
      char *host = "localhost";
    C_ARGS:
      host, &timep
    OUTPUT:
      timep
      RETVAL

πŸŽ€ The SCOPE: Keyword πŸŽ€

The SCOPE: keyword allows scoping to be enabled for a particular XSUB. If it's enabled, the XSUB will invoke ENTER and LEAVE automatically. Set it up and let it handle itself, we love a self-sufficient icon. πŸ’…

And to support potentially complex type mappings: if a typemap entry used by an XSUB contains a comment like "/*scope*/", then scoping will be automatically enabled for that XSUB too. Sneaky but valid. πŸ¦‹

To enable scoping:

SCOPE: ENABLE

To disable scoping:

SCOPE: DISABLE

🌈 The INPUT: Keyword 🌈

An XSUB's parameters are usually evaluated immediately after entering the XSUB. The INPUT: keyword can be used to force those parameters to be evaluated a little later β€” fashionably late, if you will. ⏰✨

The INPUT: keyword can be used multiple times within an XSUB and can be used to list one or more input variables. This keyword is used together with the PREINIT: keyword. They're a duo, fr. 🫢

This example shows how the input parameter "timep" can be evaluated late, after a PREINIT:

 bool_t
 rpcb_gettime(host,timep)
      char *host
    PREINIT:
      time_t tt;
    INPUT:
      time_t timep
    CODE:
           RETVAL = rpcb_gettime( host, &tt );
           timep = tt;
    OUTPUT:
      timep
      RETVAL

The next example shows each input parameter evaluated late. πŸ“

bool_t
rpcb_gettime(host,timep)
    PREINIT:
      time_t tt;
    INPUT:
      char *host
    PREINIT:
      char *h;
    INPUT:
      time_t timep
    CODE:
           h = host;
           RETVAL = rpcb_gettime( h, &tt );
           timep = tt;
    OUTPUT:
      timep
      RETVAL

okay so here's the thing β€” INPUT sections let you declare C variables that don't even show up in the subroutine's parameter list, so the whole thing above can be squished down to this:

bool_t
rpcb_gettime(host,timep)
      time_t tt;
      char *host;
      char *h = host;
      time_t timep;
    CODE:
      RETVAL = rpcb_gettime( h, &tt );
      timep = tt;
    OUTPUT:
      timep
      RETVAL

(and yeah, we're lowkey flexing here β€” we knew that input conversion for char * is a "simple" one, so host gets initialized right there on the declaration line, and our assignment h = host doesn't happen too early. If it weren't like that, you'd have to park the h = host assignment inside a CODE: or INIT: section instead. iykyk πŸ’…)


πŸŽ€ The IN/OUTLIST/IN_OUTLIST/OUT/IN_OUT Keywords πŸŽ€

So in an XSUB's parameter list, you can stick these keywords right in front of the parameter names: IN, OUTLIST, IN_OUTLIST, OUT, IN_OUT. IN is the default bestie β€” the others are basically telling Perl "hey, this one works differently than the C side does."

  • Parameters with OUTLIST/IN_OUTLIST/OUT/IN_OUT in front of them are treated as things the C subroutine uses via pointers.
  • OUTLIST/OUT mean the C subroutine doesn't even look at the memory that parameter points to β€” it's just gonna write through that pointer to hand back extra return values. not reading, just writing, periodt.
  • Parameters with OUTLIST in front do not appear in the usage signature of the generated Perl function.
  • Parameters with IN_OUTLIST/IN_OUT/OUT do show up as parameters to the Perl function. Except for OUT-parameters, those get converted to the matching C type, and then pointers to that data are passed as arguments to the C function. The expectation? The C function writes through those pointers. 🫢

The return list of the generated Perl function is made of the C return value from the function (unless the XSUB has a void return type or "The NO_OUTPUT Keyword" was used) followed by all the OUTLIST and IN_OUTLIST parameters, in the order they appeared. When the XSUB returns, the IN_OUT/OUT Perl parameter gets modified to hold whatever values the C function wrote. iconic behaviour honestly.

Example time β€” an XSUB like this:

void
day_month(OUTLIST day, IN unix_time, OUTLIST month)
  int day
  int unix_time
  int month

should be used from Perl like this:

my ($day, $month) = day_month(time);

and the C signature of the matching function should be:

void day_month(int *day, int unix_time, int *month);

You can mix these IN/OUTLIST/IN_OUTLIST/IN_OUT/OUT keywords with ANSI-style declarations too, like:

void
day_month(OUTLIST int day, int unix_time, OUTLIST int month)

(see how the optional IN keyword just dipped? gone. πŸ™ˆ)

IN_OUT parameters are literally identical to parameters introduced with "The & Unary Operator" and dropped into the OUTPUT: section (peep "The OUTPUT: Keyword"). IN_OUTLIST parameters are super similar β€” the only difference is that the value the C function writes through the pointer won't modify the Perl parameter, it just gets put in the output list instead.

And OUTLIST/OUT parameters differ from IN_OUTLIST/IN_OUT parameters in exactly one way: the initial value of the Perl parameter isn't read (and isn't given to the C function β€” which gets some garbage instead 😭). So like, that same C function from before can be interfaced with as:

void day_month(OUT int day, int unix_time, OUT int month);

or as:

void
day_month(day, unix_time, month)
    int &day = NO_INIT
    int  unix_time
    int &month = NO_INIT
  OUTPUT:
    day
    month

But heads up: the generated Perl function gets called in a very C-ish style:

my ($day, $month);
day_month($day, time, $month);

🌸 The length(NAME) Keyword 🌸

If one of the input arguments to the C function is the length of a string argument called NAME, you can swap out the name of that length-argument for length(NAME) in the XSUB declaration. That argument has to be left out when you call the generated Perl function. For example:

void
dump_chars(char *s, short l)
{
  short n = 0;
  while (n < l) {
      printf("s[%d] = \"\\%#03o\"\n", n, (int)s[n]);
      n++;
  }
}

MODULE = x            PACKAGE = x

void dump_chars(char *s, short length(s))

and that one should be called as dump_chars($string).

⚠️ This directive is supported with ANSI-type function declarations only. no ANSI, no length() magic, sorry bestie πŸ’€

πŸ¦‹ Variable-length Parameter Lists πŸ¦‹

XSUBs can have variable-length parameter lists β€” you just throw an ellipsis (...) in the parameter list. This use of the ellipsis is basically the same as the one in ANSI C. The programmer can figure out how many arguments got passed to the XSUB by checking the items variable, which the xsubpp compiler hands to all XSUBs. With this mechanism you can build an XSUB that accepts a list of parameters of unknown length. main character energy honestly πŸ‘‘

The host parameter for the rpcb_gettime() XSUB can be optional, so the ellipsis can signal that this XSUB takes a variable number of parameters. Perl should be able to call this XSUB with either of these:

$status = rpcb_gettime( $timep, $host );

$status = rpcb_gettime( $timep );

Here's the XS code, with the ellipsis:

bool_t
rpcb_gettime(timep, ...)
     time_t timep = NO_INIT
   PREINIT:
     char *host = "localhost";
   CODE:
     if( items > 1 )
          host = (char *)SvPVbyte_nolen(ST(1));
     RETVAL = rpcb_gettime( host, &timep );
   OUTPUT:
     timep
     RETVAL

⭐ The C_ARGS: Keyword ⭐

The C_ARGS: keyword lets you create XSUBs whose calling sequence from Perl is different from their calling sequence from C β€” and you don't have to write a CODE: or PPCODE: section to do it. Whatever's in the C_ARGS: paragraph gets dropped in as the argument to the called C function, with zero changes. slay ✨

So say a C function is declared as:

symbolic nth_derivative(int n, symbolic function, int flags);

and the default flags live in a global C variable called default_flags. Now pretend you want to build an interface that gets called as:

$second_deriv = $function->nth_derivative(2);

To pull that off, you declare the XSUB like:

symbolic
nth_derivative(function, n)
    symbolic        function
    int             n
  C_ARGS:
    n, function, default_flags

πŸ’œ The PPCODE: Keyword πŸ’œ

PPCODE: is an alternate form of CODE:. It tells the xsubpp compiler that you, the programmer, are supplying the code that controls the argument stack for the XSUB's return values. Sometimes you want an XSUB to return a whole list of values instead of just one, and in those cases you have to use PPCODE: and then explicitly push that list of values onto the stack. Oh and β€” PPCODE: and CODE: should not be used together inside the same XSUB. pick a lane bestie πŸ’…

The actual difference between PPCODE: and CODE: sections is in how the SP macro gets initialized (that's the current Perl stack pointer, iykyk) and in how data on the stack is handled when the XSUB returns.

  • In CODE: sections, SP keeps the value it had when entering the XSUB: SP is on the function pointer, which follows the last parameter.
  • In PPCODE: sections, SP gets moved backward to the beginning of the parameter list β€” which lets the PUSH*() macros drop output values exactly where Perl expects them to be when the XSUB goes back to Perl. 🎯

The generated trailer for a CODE: section makes sure the number of return values Perl sees is either 0 or 1 (depends on the void-ness of the C function's return value, plus the heuristics mentioned in "The RETVAL Variable"). The trailer generated for a PPCODE: section is based on the number of return values and on how many times SP got updated by the [X]PUSH*() macros.

Also worth knowing: the macros ST(i), XST_m*() and XSRETURN*() work equally well in CODE: sections and PPCODE: sections. equal opportunity macros fr fr.

The following XSUB calls the C rpcb_gettime() function and returns its two output values, timep and status, to Perl as a single list:

void
rpcb_gettime(host)
     char *host
   PREINIT:
     time_t  timep;
     bool_t  status;
   PPCODE:
     status = rpcb_gettime( host, &timep );
     EXTEND(SP, 2);
     PUSHs(sv_2mortal(newSViv(status)));
     PUSHs(sv_2mortal(newSViv(timep)));

Notice that the programmer has to supply the C code necessary to get the real rpcb_gettime() function called and to get those return values placed properly on the argument stack. it's giving DIY πŸ› οΈ

The void return type for this function is telling the xsubpp compiler that the RETVAL variable isn't needed or used, and that it shouldn't be created. In most scenarios, the void return type is what you want alongside the PPCODE: directive.

The EXTEND() macro makes room on the argument stack for 2 return values. The PPCODE: directive causes the xsubpp compiler to create a stack pointer available as SP, and that's the pointer being used inside the EXTEND() macro. Then the values get pushed onto the stack with the PUSHs() macro. chef's kiss πŸ‘‘

Now the rpcb_gettime() function can be used from Perl with this statement:

($status, $timep) = rpcb_gettime("localhost");
🚨 When you're handling output parameters with a PPCODE section, be sure to handle 'set' magic properly. See perlguts for the deets about 'set' magic. don't skip this one.

🌈 Returning Undef And Empty Lists 🌈

Sometimes the programmer just wants to return plain undef, or an empty list, when a function fails β€” instead of bothering with a separate status value. The rpcb_gettime() function is literally this exact situation. If it succeeds, we want the time back; if it fails, we want undef back. In the Perl code below, $timep is either gonna be undef or a valid time:

$timep = rpcb_gettime( "localhost" );

The XSUB below uses the SV * return type as a mnemonic only, and uses a CODE: block to tell the compiler that the programmer supplied all the necessary code. The sv_newmortal() call initializes the return value to undef, which makes undef the default return value. πŸ¦‹

     SV *
     rpcb_gettime(host)
          char *  host
        PREINIT:
          time_t  timep;
          bool_t x;
        CODE:
          ST(0) = sv_newmortal();
          if( rpcb_gettime( host, &timep ) )
               sv_setnv( ST(0), (double)timep);

okay so next up bestie, this one shows how you'd drop an explicit undef in the return value if the vibe calls for it 🫢

     SV *
     rpcb_gettime(host)
          char *  host
        PREINIT:
          time_t  timep;
          bool_t x;
        CODE:
          if( rpcb_gettime( host, &timep ) ){
               ST(0) = sv_newmortal();
               sv_setnv( ST(0), (double)timep);
          }
          else{
               ST(0) = &PL_sv_undef;
          }

and to return an empty list? you literally have to use a PPCODE: block and then not push any return values onto the stack, that's the whole trick 😌

     void
     rpcb_gettime(host)
          char *host
        PREINIT:
          time_t  timep;
        PPCODE:
          if( rpcb_gettime( host, &timep ) )
               PUSHs(sv_2mortal(newSViv(timep)));
          else{
              /* Nothing pushed on stack, so an empty
               * list is implicitly returned. */
          }

so some people get the urge to slap an explicit return in that XSUB instead of just letting control fall through to the end β€” and like, don't πŸ™ˆ in those situations use XSRETURN_EMPTY instead, because that's what makes sure the XSUB stack gets properly adjusted. for the other XSRETURN macros, go consult perlapi, it's got you ✨

and since XSRETURN_* macros work with CODE blocks too, you can rewrite the whole thing like this:

     int
     rpcb_gettime(host)
          char *host
        PREINIT:
          time_t  timep;
        CODE:
          RETVAL = rpcb_gettime( host, &timep );
          if (RETVAL == 0)
                XSRETURN_UNDEF;
        OUTPUT:
          RETVAL

fr fr, you can even yeet that check into a POSTCALL: section. combine that with the PREINIT: simplifications and you land on:

     int
     rpcb_gettime(host)
          char *host
          time_t  timep;
        POSTCALL:
          if (RETVAL == 0)
                XSRETURN_UNDEF;

πŸŽ€ The REQUIRE: Keyword πŸŽ€

the REQUIRE: keyword is how you tell everyone the minimum version of the xsubpp compiler needed to build the XS module. so an XS module with this line in it will only compile with xsubpp version 1.922 or greater:

        REQUIRE: 1.922

🧚 The CLEANUP: Keyword 🧚

use this one when an XSUB needs special cleanup procedures before it terminates ✨ when you use the CLEANUP: keyword it must come after any CODE: or OUTPUT: blocks that are present in the XSUB. the code you put in the cleanup block gets added as the literal last statements in the XSUB, periodt.

πŸ’… The POSTCALL: Keyword πŸ’…

this keyword is for when an XSUB needs special procedures executed after the C subroutine call is done. when you use POSTCALL: it must precede the OUTPUT: and CLEANUP: blocks present in the XSUB.

peep the examples in "The NO_OUTPUT Keyword" and "Returning Undef And Empty Lists" πŸ‘€

lowkey the POSTCALL: block doesn't make a lot of sense when the C subroutine call is supplied by you via either a CODE: or PPCODE: section. just so you know πŸ’€

πŸ¦‹ The BOOT: Keyword πŸ¦‹

BOOT: is the keyword you use to add code to the extension's bootstrap function. the bootstrap function is generated by the xsubpp compiler and normally holds all the statements necessary to register any XSUBs with Perl. with BOOT: you get to tell the compiler "hey, add these extra statements to the bootstrap function too" ✨

you can use this keyword any time after the first MODULE keyword, and it should appear on a line all by itself. the first blank line after the keyword terminates the code block.

     BOOT:
     # The following message will be printed when the
     # bootstrap function executes.
     printf("Hello from the bootstrap!\n");

🌸 The VERSIONCHECK: Keyword 🌸

VERSIONCHECK: corresponds to xsubpp's -versioncheck and -noversioncheck options. this keyword overrides the command line options. version checking is enabled by default. when it's enabled, the XS module will attempt to verify that its version matches the version of the PM module πŸ‘‘

to enable version checking:

    VERSIONCHECK: ENABLE

to disable version checking:

    VERSIONCHECK: DISABLE
⚠️ heads up bestie: if the version of the PM module is an NV (a floating point number), it'll get stringified with a possible loss of precision (currently chopping to nine decimal places), so it may not match the version of the XS module anymore 😭 quoting the $VERSION declaration to make it a string is the recommended move if you're using long version numbers.

πŸ“ The PROTOTYPES: Keyword πŸ“

PROTOTYPES: corresponds to xsubpp's -prototypes and -noprototypes options. this keyword overrides the command line options. prototypes are disabled by default. when prototypes are enabled, XSUBs get Perl prototypes πŸ’– this keyword can be used multiple times in an XS module to enable and disable prototypes for different parts of the module. also note that xsubpp will nag you if you don't explicitly enable or disable prototypes, with:

    Please specify prototyping behavior for Foo.xs (see perlxs manual)

to enable prototypes:

    PROTOTYPES: ENABLE

to disable prototypes:

    PROTOTYPES: DISABLE

⭐ The PROTOTYPE: Keyword ⭐

this one's similar to PROTOTYPES: above, but it forces xsubpp to use a specific prototype for the XSUB. it overrides all other prototype options and keywords, but it only affects the current XSUB. for all the lore about Perl prototypes, consult "Prototypes" in perlsub πŸ“š

    bool_t
    rpcb_gettime(timep, ...)
          time_t timep = NO_INIT
        PROTOTYPE: $;$
        PREINIT:
          char *host = "localhost";
        CODE:
                  if( items > 1 )
                       host = (char *)SvPVbyte_nolen(ST(1));
                  RETVAL = rpcb_gettime( host, &timep );
        OUTPUT:
          timep
          RETVAL

and if prototypes are enabled globally, you can disable it locally for a given XSUB like this:

    void
    rpcb_gettime_noproto()
        PROTOTYPE: DISABLE
    ...

πŸ‘‘ The ALIAS: Keyword πŸ‘‘

the ALIAS: keyword lets an XSUB have two or more unique Perl names and know which of those names was used when it got invoked 🀯 the Perl names may be fully-qualified with package names. each alias gets an index. the compiler sets up a variable called ix which holds the index of the alias that was used. when the XSUB is called with its declared name, ix will be 0.

this next example creates the aliases FOO::gettime() and BAR::getit() for this function:

    bool_t
    rpcb_gettime(host,timep)
          char *host
          time_t &timep
        ALIAS:
            FOO::gettime = 1
            BAR::getit = 2
        INIT:
          printf("# ix = %d\n", ix );
        OUTPUT:
          timep
⚠️ you'll get a warning when you create more than one alias pointing to the same value. you can work around it in a backwards compatible way by creating multiple defines that resolve to the same value, or β€” with a modern version of ExtUtils::ParseXS β€” you can use a symbolic alias, which is denoted with a => instead of a =. for instance you could change the above so the alias section looked like this:
        ALIAS:
            FOO::gettime = 1
            BAR::getit = 2
            BAZ::gettime => FOO::gettime

this would have the exact same effect as this:

        ALIAS:
            FOO::gettime = 1
            BAR::getit = 2
            BAZ::gettime = 1

except that the latter will produce warnings during the build process πŸ™ˆ a mechanism that would work in a backwards compatible way with older versions of our tool chain would be to do this:

    #define FOO_GETTIME 1
    #define BAR_GETIT 2
    #define BAZ_GETTIME 1

    bool_t
    rpcb_gettime(host,timep)
          char *host
          time_t &timep
        ALIAS:
            FOO::gettime = FOO_GETTIME
            BAR::getit = BAR_GETIT
            BAZ::gettime = BAZ_GETTIME
        INIT:
          printf("# ix = %d\n", ix );
        OUTPUT:
          timep

🌈 The OVERLOAD: Keyword 🌈

instead of writing an overloaded interface using pure Perl, you can also use the OVERLOAD keyword to define additional Perl names for your functions (just like the ALIAS: keyword above, iconic behaviour honestly). BUT β€” the overloaded functions have to be defined so they accept the number of parameters that perl's overload system supplies. for most overload methods that's three parameters; for the nomethod function it's four. however, the bitwise operators &, |, ^, and ~ may be called with three or five arguments (see overload) πŸ’…

if any function has the OVERLOAD: keyword, several additional lines get defined in the c file generated by xsubpp, all so it can register with the overload magic ✨

since blessed objects are actually stored as RV's, it's genuinely useful to use the typemap features to preprocess parameters and extract the actual SV stored inside the blessed RV. see the sample for T_PTROBJ_SPECIAL in perlxstypemap 🫢

to use the OVERLOAD: keyword, create an XS function that takes three input parameters (or use the C-style ... definition) like this:

    SV *
    cmp (lobj, robj, swap)
    My_Module_obj    lobj
    My_Module_obj    robj
    IV               swap
    OVERLOAD: cmp <=>
    { /* function defined here */}

in this case, the function overloads both of the three way comparison operators 😀 for all overload operations using non-alpha characters, you must type the parameter without quoting, separating multiple overloads with whitespace. note that "" (the stringify overload) should be entered as \"\" (i.e. escaped).

and since β€” as mentioned above β€” bitwise operators may take extra arguments, you might wanna use something like (lobj, robj, swap, ...) (with the literal ...) as your parameter list.

πŸ’œ The FALLBACK: Keyword πŸ’œ

on top of the OVERLOAD keyword, if you need to control how Perl autogenerates missing overloaded operators, you can set the FALLBACK keyword in the module header section, like this:

    MODULE = RPC  PACKAGE = RPC

    FALLBACK: TRUE
    ...

...where FALLBACK can be any of the three values TRUE, FALSE, or UNDEF. If you don't set any FALLBACK value while using OVERLOAD, bestie, it just defaults to UNDEF. And heads up β€” FALLBACK is literally not used at all unless one or more functions using OVERLOAD have been defined. Peep "fallback" in overload for the full tea. πŸ’…


✨ The INTERFACE: Keyword ✨

Okay so this one is a whole vibe. INTERFACE: declares that the current XSUB is the keeper of the given calling signature. And if some text follows the keyword? That text counts as a list of functions that have this signature and should get attached to the current XSUB. 🫢

So like, say you've got 4 C functions β€” multiply(), divide(), add(), subtract() β€” and all of them have the signature:

symbolic f(symbolic, symbolic);

You can make them all use the same XSUB with this:

symbolic
interface_s_ss(arg1, arg2)
    symbolic        arg1
    symbolic        arg2
INTERFACE:
    multiply divide
    add subtract

(Yes, that is the complete XSUB code for 4 Perl functions! Iconic.) Four generated Perl functions share names with the corresponding C functions.

The advantage of this approach compared to the ALIAS: keyword is that you don't have to code a switch statement at all β€” each Perl function (which shares the same XSUB) just knows which C function it should call. Chef's kiss. 😌 And on top of that, you can attach an extra function remainder() at runtime, like:

CV *mycv = newXSproto("Symbolic::remainder",
                      XS_Symbolic_interface_s_ss, __FILE__, "$$");
XSINTERFACE_FUNC_SET(mycv, remainder);

β€” say, from another XSUB. (This example supposes there was no INTERFACE_MACRO: section, because otherwise you'd need to use something else instead of XSINTERFACE_FUNC_SET β€” see the next section, bestie, don't skip it. πŸ™ˆ)


πŸ¦‹ The INTERFACE_MACRO: Keyword πŸ¦‹

This keyword lets you define an INTERFACE using a different way of extracting a function pointer from an XSUB. The text that follows it should give the name of the macros which would extract/set a function pointer. The extractor macro is given the return type, CV*, and XSANY.any_dptr for this CV*. The setter macro gets given cv, and the function pointer.

The default value is XSINTERFACE_FUNC and XSINTERFACE_FUNC_SET. Also, an INTERFACE keyword with an empty list of functions can totally be omitted if the INTERFACE_MACRO keyword is used. ✨

So suppose in the previous example the function pointers for multiply(), divide(), add(), subtract() are kept in a global C array fp[] with offsets being multiply_off, divide_off, add_off, subtract_off. Then you can use:

#define XSINTERFACE_FUNC_BYOFFSET(ret,cv,f) \
    ((XSINTERFACE_CVT_ANON(ret))fp[CvXSUBANY(cv).any_i32])
#define XSINTERFACE_FUNC_BYOFFSET_set(cv,f) \
    CvXSUBANY(cv).any_i32 = CAT2( f, _off )

in the C section, and then:

symbolic
interface_s_ss(arg1, arg2)
    symbolic        arg1
    symbolic        arg2
  INTERFACE_MACRO:
    XSINTERFACE_FUNC_BYOFFSET
    XSINTERFACE_FUNC_BYOFFSET_set
  INTERFACE:
    multiply divide
    add subtract

in the XSUB section. Slay. πŸ’–


πŸŽ€ The INCLUDE: Keyword πŸŽ€

This keyword pulls other files into the XS module. Those other files may have XS code in them. And INCLUDE: can also be used to run a command that generates XS code to be pulled into the module. Versatile queen. πŸ‘‘

So the file Rpcb1.xsh contains our rpcb_gettime() function:

bool_t
rpcb_gettime(host,timep)
      char *host
      time_t &timep
    OUTPUT:
      timep

Then the XS module can use INCLUDE: to pull that file into it:

INCLUDE: Rpcb1.xsh

If the parameters to the INCLUDE: keyword are followed by a pipe ("|") then the compiler will interpret the parameters as a command. Ngl, this feature is mildly deprecated in favour of the "INCLUDE_COMMAND:" directive, which is documented right below this.

INCLUDE: cat Rpcb1.xsh |
🚨 Do not use this to run perl: INCLUDE: perl | will run whatever perl happens to be first in your path, and not necessarily the same perl that's used to run "xsubpp". See "The INCLUDE_COMMAND: Keyword". Like, actually don't, bestie. 😭

🌸 The INCLUDE_COMMAND: Keyword 🌸

Runs the supplied command and includes its output into the current XS document. "INCLUDE_COMMAND" gives special meaning to the $^X token β€” it runs the same perl interpreter that's running "xsubpp". Obsessed, honestly.

INCLUDE_COMMAND: cat Rpcb1.xsh

INCLUDE_COMMAND: $^X -e ...

πŸ’€ The CASE: Keyword πŸ’€

The CASE: keyword lets an XSUB have multiple distinct parts, with each part acting as a virtual XSUB. But β€” and this is the important bit β€” CASE: is greedy. If you use it, then all other XS keywords must be contained within a CASE:. Which means nothing may come before the first CASE: in the XSUB, and anything following the last CASE: gets included in that case. No exceptions, no sneaky stragglers. πŸ™…β€β™€οΈ

A CASE: might switch via a parameter of the XSUB, via the "ix" ALIAS: variable (see "The ALIAS: Keyword"), or maybe via the "items" variable (see "Variable-length Parameter Lists"). The last CASE: becomes the default case if it's not associated with a conditional. The following example shows CASE switched via "ix", with a function rpcb_gettime() that has an alias x_gettime(). When the function is called as rpcb_gettime() its parameters are the usual (char *host, time_t *timep), but when it's called as x_gettime() its parameters are reversed β€” (time_t *timep, char *host):

long
rpcb_gettime(a,b)
  CASE: ix == 1
    ALIAS:
      x_gettime = 1
    INPUT:
      # 'a' is timep, 'b' is host
      char *b
      time_t a = NO_INIT
    CODE:
           RETVAL = rpcb_gettime( b, &a );
    OUTPUT:
      a
      RETVAL
  CASE:
      # 'a' is host, 'b' is timep
      char *a
      time_t &b = NO_INIT
    OUTPUT:
      b
      RETVAL

That function can be called with either of the following statements β€” note the different argument lists:

$status = rpcb_gettime( $host, $timep );

$status = x_gettime( $timep, $host );

⭐ The EXPORT_XSUB_SYMBOLS: Keyword ⭐

Real talk: the EXPORT_XSUB_SYMBOLS: keyword is probably something you will never need. In perl versions earlier than 5.16.0, this keyword does nothing at all. Starting with 5.16, XSUB symbols are no longer exported by default β€” that is, they're "static" functions. If you include

EXPORT_XSUB_SYMBOLS: ENABLE

in your XS code, then the XSUBs following this line will not be declared "static". You can later disable it again with

EXPORT_XSUB_SYMBOLS: DISABLE

which, again, is the default that you should probably never change. And you cannot use this keyword on versions of perl before 5.16 to make XSUBs "static". That's just, like, the rules. 🌈


🧚 The & Unary Operator 🧚

The "&" unary operator in the INPUT: section tells xsubpp that it should convert a Perl value to/from C using the C type to the left of "&", but provide a pointer to this value when the C function is called.

This is useful for avoiding a CODE: block for a C function that takes a parameter by reference. Typically the parameter should not be a pointer type (an "int" or "long" but not an "int*" or "long*").

So the following XSUB will generate incorrect C code. The xsubpp compiler will turn this into code which calls rpcb_gettime() with parameters (char *host, time_t timep), but the real rpcb_gettime() wants the "timep" parameter to be of type time_t* rather than time_t. Rude. 😀

bool_t
rpcb_gettime(host,timep)
      char *host
      time_t timep
    OUTPUT:
      timep

That problem gets corrected by using the "&" operator. Now the xsubpp compiler will turn this into code which calls rpcb_gettime() correctly with parameters (char *host, time_t *timep). It does this by carrying the "&" through, so the function call looks like "rpcb_gettime(host, &timep)":

bool_t
rpcb_gettime(host,timep)
      char *host
      time_t &timep
    OUTPUT:
      timep

πŸ’œ Inserting POD, Comments and C Preprocessor Directives πŸ’œ

C preprocessor directives are allowed inside BOOT:, PREINIT: INIT:, CODE:, PPCODE:, POSTCALL:, and CLEANUP: blocks, and also outside the functions. Comments are allowed anywhere after the MODULE keyword. The compiler passes the preprocessor directives through untouched and removes the commented lines. POD documentation is allowed at any point, both in the C and XS language sections. POD must be terminated with a "=cut" command β€” "xsubpp" will exit with an error if it isn't. πŸ™ˆ

It's very unlikely that human generated C code will be mistaken for POD, since most indenting styles result in whitespace in front of any line starting with "=". Machine generated XS files may fall into this trap unless care is taken to ensure that a space breaks the sequence "\n=".

Comments can be added to XSUBs by placing a "#" as the first non-whitespace of a line. Care should be taken to avoid making the comment look like a C preprocessor directive, lest it be interpreted as one. The simplest way to prevent this is to put whitespace in front of the "#".

If you use preprocessor directives to choose one of two versions of a function, use

#if ... version1
#else /* ... version2  */
#endif

and not

#if ... version1
#endif
#if ... version2
#endif

because otherwise xsubpp will believe you made a duplicate definition of the function. Also, put a blank line before the #else/#endif so it won't be seen as part of the function body. Small thing, big difference. πŸŽ€


πŸ‘‘ Using XS With C++ πŸ‘‘

If an XSUB name contains "::", it's considered to be a C++ method. The generated Perl function will assume its first argument is an object pointer. That object pointer gets stored in a variable called THIS. The object should have been created by C++ with the new() function and should be blessed by Perl with the sv_setref_pv() macro. The blessing of the object by Perl can be handled by a typemap β€” there's an example typemap shown at the end of this section. 🫢

If the return type of the XSUB includes "static", the method is considered to be a static method. It'll call the C++ function using the class::method() syntax. If the method isn't static, the function will be called using the THIS->method() syntax.

The next examples will use the following C++ class:

 class color {
      public:
      color();
      ~color();
      int blue();
      void set_blue( int );

      private:
      int c_blue;
 };

The XSUBs for the blue() and set_blue() methods are defined with the class name, but the parameter for the object (THIS, or "self") is implicit and is not listed:

 int
 color::blue()

Both of those Perl functions expect an object as the first parameter, bestie. In the generated C++ code the object is called THIS, and the method call gets performed on this object. So in C++ land the blue() and set_blue() methods are called like this:

RETVAL = THIS->blue();

THIS->set_blue( val );

You could also write a single get/set method using an optional argument:

int
color::blue( val = NO_INIT )
    int val
    PROTOTYPE $;$
    CODE:
        if (items > 1)
            THIS->set_blue( val );
        RETVAL = THIS->blue();
    OUTPUT:
        RETVAL

If the function's name is DESTROY then the C++ delete function will be called and THIS will be given as its parameter. The generated C++ code for

void
color::DESTROY()

will look like this:

color *THIS = ...;  // Initialized as in typemap

delete THIS;

If the function's name is new then the C++ new function will be called to create a dynamic C++ object. The XSUB will expect the class name, which will be kept in a variable called CLASS, to be given as the first argument.

color *
color::new()

The generated C++ code will call new.

RETVAL = new color();

The following is an example of a typemap that could be used for this C++ example.

TYPEMAP
color *  O_OBJECT

OUTPUT
# The Perl object is blessed into 'CLASS', which should be a
# char* having the name of the package for the blessing.
O_OBJECT
    sv_setref_pv( $arg, CLASS, (void*)$var );

INPUT
O_OBJECT
    if( sv_isobject($arg) && (SvTYPE(SvRV($arg)) == SVt_PVMG) )
        $var = ($type)SvIV((SV*)SvRV( $arg ));
    else{
        warn("${Package}::$func_name() -- "
            "$var is not a blessed SV reference");
        XSRETURN_UNDEF;
    }

πŸ’‘ Interface Strategy πŸ’‘

When designing an interface between Perl and a C library, a straight translation from C to XS (such as the kind created by h2xs -x) is often sufficient. But sometimes the interface will look very C-like and occasionally nonintuitive, especially when the C function modifies one of its parameters, or returns failure inband (as in β€œnegative return values mean failure”). In cases where the programmer wishes to create a more Perl-like interface, the following strategy may help to identify the more critical parts of the interface.

  • Identify the C functions with input/output or output parameters. The XSUBs for these functions may be able to return lists to Perl.
  • Identify the C functions which use some inband info as an indication of failure. They may be candidates to return undef or an empty list in case of failure. If the failure may be detected without a call to the C function, you may want to use an INIT: section to report the failure. For failures detectable after the C function returns one may want to use a POSTCALL: section to process the failure. In more complicated cases use CODE: or PPCODE: sections.
  • If many functions use the same failure indication based on the return value, you may want to create a special typedef to handle this situation. Put
typedef int negative_is_failure;

near the beginning of the XS file, and create an OUTPUT typemap entry for negative_is_failure which converts negative values to undef, or maybe croak()s. After this the return value of type negative_is_failure will create a more Perl-like interface.

  • Identify which values are used by only the C and XSUB functions themselves. Say, when a parameter to a function should be a contents of a global variable. If Perl does not need to access the contents of the value then it may not be necessary to provide a translation for that value from C to Perl.
  • Identify the pointers in the C function parameter lists and return values. Some pointers may be used to implement input/output or output parameters, and they can be handled in XS with the & unary operator, and, possibly, using the NO_INIT keyword. Some others will require handling of types like int *, and one needs to decide what a useful Perl translation will do in such a case. When the semantic is clear, it is advisable to put the translation into a typemap file.
  • Identify the structures used by the C functions. In many cases it may be helpful to use the T_PTROBJ typemap for these structures so they can be manipulated by Perl as blessed objects. (This is handled automatically by h2xs -x.)
  • If the same C type is used in several different contexts which require different translations, typedef several new types mapped to this C type, and create separate typemap entries for these new types. Use these types in declarations of return type and parameters to XSUBs.

🧸 Perl Objects And C Structures 🧸

When dealing with C structures, you should select either T_PTROBJ or T_PTRREF for the XS type. Both types are designed to handle pointers to complex objects. The T_PTRREF type will allow the Perl object to be unblessed while the T_PTROBJ type requires that the object be blessed. By using T_PTROBJ one can achieve a form of type-checking because the XSUB will attempt to verify that the Perl object is of the expected type.

The following XS code shows the getnetconfigent() function which is used with ONC+ TIRPC. The getnetconfigent() function will return a pointer to a C structure and has the C prototype shown below. The example will demonstrate how the C pointer will become a Perl reference. Perl will consider this reference to be a pointer to a blessed object and will attempt to call a destructor for the object. A destructor will be provided in the XS source to free the memory used by getnetconfigent(). Destructors in XS can be created by specifying an XSUB function whose name ends with the word DESTROY. XS destructors can be used to free memory which may have been malloc'd by another XSUB.

struct netconfig *getnetconfigent(const char *netid);

A typedef will be created for struct netconfig. The Perl object will be blessed in a class matching the name of the C type, with the tag Ptr appended, and the name should not have embedded spaces if it will be a Perl package name. The destructor will be placed in a class corresponding to the class of the object and the PREFIX keyword will be used to trim the name to the word DESTROY as Perl will expect.

typedef struct netconfig Netconfig;

MODULE = RPC  PACKAGE = RPC

Netconfig *
getnetconfigent(netid)
     char *netid

MODULE = RPC  PACKAGE = NetconfigPtr  PREFIX = rpcb_

void
rpcb_DESTROY(netconf)
     Netconfig *netconf
   CODE:
     printf("Now in NetconfigPtr::DESTROY\n");
     free( netconf );

This example requires the following typemap entry. Consult perlxstypemap for more information about adding new typemaps for an extension.

TYPEMAP
Netconfig *  T_PTROBJ

This example will be used with the following Perl statements.

use RPC;
$netconf = getnetconfigent("udp");

When Perl destroys the object referenced by $netconf it will send the object to the supplied XSUB DESTROY function. Perl cannot determine, and does not care, that this object is a C struct and not a Perl object. In this sense, there is no difference between the object created by the getnetconfigent() XSUB and an object created by a normal Perl subroutine.

🌈 Safely Storing Static Data in XS 🌈

Starting with Perl 5.8, a macro framework has been defined to allow static data to be safely stored in XS modules that will be accessed from a multi-threaded Perl.

Although primarily designed for use with multi-threaded Perl, the macros have been designed so that they will work with non-threaded Perl as well.

It is therefore strongly recommended that these macros be used by all XS modules that make use of static data.

The easiest way to get a template set of macros to use is by specifying the -g (--global) option with h2xs (see h2xs).

Below is an example module that makes use of the macros.

#define PERL_NO_GET_CONTEXT
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"

/* Global Data */

#define MY_CXT_KEY "BlindMice::_guts" XS_VERSION

typedef struct {
    int count;
    char name[3][100];
} my_cxt_t;

START_MY_CXT

MODULE = BlindMice           PACKAGE = BlindMice

BOOT:
{
    MY_CXT_INIT;
    MY_CXT.count = 0;
    strcpy(MY_CXT.name[0], "None");
    strcpy(MY_CXT.name[1], "None");
    strcpy(MY_CXT.name[2], "None");
}

int
newMouse(char * name)
    PREINIT:
      dMY_CXT;
    CODE:
      if (MY_CXT.count >= 3) {
          warn("Already have 3 blind mice");
          RETVAL = 0;
      }
      else {
          RETVAL = ++ MY_CXT.count;
          strcpy(MY_CXT.name[MY_CXT.count - 1], name);
      }
    OUTPUT:
      RETVAL

char *
get_mouse_name(index)
      int index
    PREINIT:
      dMY_CXT;
    CODE:
      if (index > MY_CXT.count)
        croak("There are only 3 blind mice.");
      else
        RETVAL = MY_CXT.name[index - 1];
    OUTPUT:
      RETVAL

void
CLONE(...)
    CODE:
      MY_CXT_CLONE;

πŸŽ€ MY_CXT REFERENCE πŸŽ€

  • MY_CXT_KEY β€” This macro is used to define a unique key to refer to the static data for an XS module. The suggested naming scheme, as used by h2xs, is to use a string that consists of the module name, the string ::_guts and the module version number.
#define MY_CXT_KEY "MyModule::_guts" XS_VERSION
  • typedef my_cxt_t β€” This struct typedef must always be called my_cxt_t. The other CXT* macros assume the existence of the my_cxt_t typedef name.

Declare a typedef named my_cxt_t that is a structure that contains all the data that needs to be interpreter-local.

typedef struct {
    int some_value;
} my_cxt_t;
  • START_MY_CXT β€” Always place the START_MY_CXT macro directly after the declaration of my_cxt_t.
  • MY_CXT_INIT β€” The MY_CXT_INIT macro initializes storage for the my_cxt_t struct.

okay so this one must be called exactly once, bestie β€” typically in a BOOT: section. If you’re maintaining multiple interpreters, it should be called once in each interpreter instance, except for interpreters cloned from existing ones. (But see MY_CXT_CLONE below.) 🫢

  • dMY_CXT β€” use the dMY_CXT macro (it’s a declaration, okay?) in all the functions that access MY_CXT.
  • MY_CXT β€” use MY_CXT to access members of the my_cxt_t struct. For example, if my_cxt_t is
typedef struct {
    int index;
} my_cxt_t;

then use this to access the index member:

dMY_CXT;
MY_CXT.index = 2;
  • aMY_CXT/pMY_CXT β€” dMY_CXT may be quite expensive to calculate, and to avoid the overhead of invoking it in each function it’s possible to pass the declaration onto other functions using the aMY_CXT/pMY_CXT macros, eg
void sub1() {
    dMY_CXT;
    MY_CXT.index = 1;
    sub2(aMY_CXT);
}

void sub2(pMY_CXT) {
    MY_CXT.index = 2;
}

Analogously to pTHX, there are equivalent forms for when the macro is the first or last in multiple arguments, where an underscore represents a comma, i.e. _aMY_CXT, aMY_CXT_, _pMY_CXT and pMY_CXT_.

  • MY_CXT_CLONE β€” by default, when a new interpreter is created as a copy of an existing one (eg via threads->create()), both interpreters share the same physical my_cxt_t structure. Calling MY_CXT_CLONE (typically via the package’s CLONE() function) causes a byte-for-byte copy of the structure to be taken, and any future dMY_CXT will cause the copy to be accessed instead.
  • MY_CXT_INIT_INTERP(my_perl), dMY_CXT_INTERP(my_perl) β€” these are versions of the macros which take an explicit interpreter as an argument.
🚨 note, bestie: these macros will only work together within the same source file; that is, a dMY_CTX in one source file will access a different structure than a dMY_CTX in another source file.

πŸ’– EXAMPLES πŸ’–

πŸ“ File RPC.xs: Interface to some ONC+ RPC bind library functions.

#define PERL_NO_GET_CONTEXT
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"

/* Note: On glibc 2.13 and earlier, this needs be <rpc/rpc.h> */
#include <tirpc/rpc.h>

typedef struct netconfig Netconfig;

MODULE = RPC  PACKAGE = RPC

SV *
rpcb_gettime(host="localhost")
     char *host
   PREINIT:
     time_t  timep;
   CODE:
     ST(0) = sv_newmortal();
     if( rpcb_gettime( host, &timep ) )
          sv_setnv( ST(0), (double)timep );

Netconfig *
getnetconfigent(netid="udp")
     char *netid

MODULE = RPC  PACKAGE = NetconfigPtr  PREFIX = rpcb_

void
rpcb_DESTROY(netconf)
     Netconfig *netconf
   CODE:
     printf("NetconfigPtr::DESTROY\n");
     free( netconf );

πŸ“ File typemap: custom typemap for RPC.xs. (cf. perlxstypemap)

TYPEMAP
Netconfig *  T_PTROBJ

πŸ“ File RPC.pm: Perl module for the RPC extension.

package RPC;

require Exporter;
require DynaLoader;
@ISA = qw(Exporter DynaLoader);
@EXPORT = qw(rpcb_gettime getnetconfigent);

bootstrap RPC;
1;

πŸ“ File rpctest.pl: Perl test program for the RPC extension.

use RPC;

$netconf = getnetconfigent();
$a = rpcb_gettime();
print "time = $a\n";
print "netconf = $netconf\n";

$netconf = getnetconfigent("tcp");
$a = rpcb_gettime("poplar");
print "time = $a\n";
print "netconf = $netconf\n";

In Makefile.PL add -ltirpc and -I/usr/include/tirpc.


⚠️ CAVEATS ⚠️

Use of standard C library functions

See perlclib.

Event loops and control flow

Some modules have an event loop, waiting for user-input. It is highly unlikely that two such modules would work adequately together in a single Perl application.

In general, the perl interpreter views itself as the center of the universe as far as the Perl program goes. XS code is viewed as a help-mate, to accomplish things that perl doesn’t do, or doesn’t do fast enough, but always subservient to perl. The closer XS code adheres to this model, the less likely conflicts will occur.


🧚 XS VERSION 🧚

This document covers features supported by ExtUtils::ParseXS (also known as xsubpp) 3.57.


πŸ‘‘ AUTHOR DIAGNOSTICS πŸ‘‘

As of version 3.49 certain warnings are disabled by default. While developing you can set $ENV{AUTHOR_WARNINGS} to true in your environment or in your Makefile.PL, or set $ExtUtils::ParseXS::AUTHOR_WARNINGS to true via code, or pass author_warnings=>1 into process_file() explicitly. Currently this will enable stricter alias checking but more warnings might be added in the future. The kind of warnings this will enable are only helpful to the author of the XS file, and the diagnostics produced will not include installation specific details so they are only useful to the maintainer of the XS code itself.


🌸 AUTHOR 🌸

Originally written by Dean Roehrich <roehrich@cray.com>.

Maintained since 1996 by The Perl Porters <perl5-porters@perl.org>.


πŸ’– that’s the whole page, bestie β€” the footer says perl v5.42.3, dated 2026-11-03, and this icon is PERLXS(1). periodt. πŸ’…

πŸ“¦ where this page comes from