perlxs

NAME

perlxs - XS language reference manual

DESCRIPTION

Introduction

\s-1XS\s0 is an interface description file format used to create an extension interface between Perl and C code (or a C library) which one wishes to use with Perl. The \s-1XS\s0 interface is combined with the library to create a new library which can then be either dynamically loaded or statically linked into perl. The \s-1XS\s0 interface description is written in the \s-1XS\s0 language and is the core component of the Perl extension interface.
An \s-1XSUB\s0 forms the basic unit of the \s-1XS\s0 interface. After compilation by the xsubpp compiler, each \s-1XSUB\s0 amounts to a C function definition which will provide the glue between Perl calling conventions and C calling conventions.
The glue code pulls the arguments from the Perl stack, converts these Perl values to the formats expected by a C function, call this C function, 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, such as 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.
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. \s-1XS\s0 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 \s-1XS\s0 compiler xsubpp handle the rest.
The \s-1XS\s0 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 \s-1XSUB\s0 declaration is almost identical to a declaration of a C function (in K&R style). In such circumstances, there is another tool called CWh2xs that is able to translate an entire C header file into a corresponding \s-1XS\s0 file that will provide glue to the functions/macros described in the header file.
The \s-1XS\s0 compiler is called xsubpp. This compiler creates the constructs necessary to let an \s-1XSUB\s0 manipulate Perl values, and creates the glue necessary to let Perl call the \s-1XSUB\s0. 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.
A file in \s-1XS\s0 format starts with a C language section which goes until the first CWMODULE = directive. Other \s-1XS\s0 directives and \s-1XSUB\s0 definitions may follow this line. The “language” used in this part of the file is usually referred to as the \s-1XS\s0 language. xsubpp recognizes and skips \s-1POD\s0 (see perlpod) in both the C and \s-1XS\s0 language sections, which allows the \s-1XS\s0 file to contain embedded documentation.
See perlxstut for a tutorial on the whole extension creation process.
Note: For some extensions, Dave Beazley's \s-1SWIG\s0 system may provide a significantly more convenient mechanism for creating the extension glue code. See http://www.swig.org/ for more information.

On The Road

Many of the examples which follow will concentrate on creating an interface between Perl and the \s-1ONC+\s0 \s-1RPC\s0 bind library functions. The rpcb_gettime() function is used to demonstrate many features of the \s-1XS\s0 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 \s-1XSUB\s0 is created to offer a direct translation between this function and Perl, then this \s-1XSUB\s0 will be used from Perl with the following code. The CW$status and CW$timep variables will contain the output of the function.
use RPC; $status = rpcb_gettime( "localhost", $timep );
The following \s-1XS\s0 file shows an \s-1XS\s0 subroutine, or \s-1XSUB\s0, which demonstrates one possible interface to the rpcb_gettime() function. This \s-1XSUB\s0 represents a direct translation between C and Perl and so preserves the interface even from Perl. This \s-1XSUB\s0 will be invoked from Perl with the usage shown above. Note that the first three #include statements, for CWEXTERN.h, CWperl.h, and CWXSUB.h, will always be present at the beginning of an \s-1XS\s0 file. This approach and others will be expanded later in this document.
#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 CWuse 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() \s-1XSUB\s0 will be explored. The XSUBs will take their parameters in different orders or will take different numbers of parameters. In each case the \s-1XSUB\s0 is an abstraction between Perl and the real C rpcb_gettime() function, and the \s-1XSUB\s0 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.

The Anatomy of an \s-1XSUB\s0

The simplest XSUBs consist of 3 parts: a description of the return value, the name of the \s-1XSUB\s0 routine and the names of its arguments, and a description of types or formats of the arguments.
The following \s-1XSUB\s0 allows a Perl program to access a C library function called sin(). The \s-1XSUB\s0 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 \s-1XSUB\s0 look similar to an \s-1ANSI\s0 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 xsubpp like this:
char * s char &c
Both these \s-1XS\s0 declarations correspond to the CWchar* C type, but they have different semantics, see “The & Unary Operator”.
It is convenient to think that the indirection operator CW* should be considered as a part of the type and the address operator CW& should be considered part of the variable. See “The Typemap” 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 \s-1XSUB\s0 starts with the corresponding keyword, such as \s-1INIT:\s0 or \s-1CLEANUP:\s0. However, the first two lines of an \s-1XSUB\s0 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 \s-1INPUT:\s0 section unless explicitly marked with another keyword. (See “The \s-1INPUT:\s0 Keyword”.)
An \s-1XSUB\s0 section continues until another section-start keyword is found.

The Argument Stack

The Perl argument stack is used to store the values which are sent as parameters to the \s-1XSUB\s0 and to store the \s-1XSUB\s0's return value(s). In reality all Perl functions (including non-XSUB ones) keep their values on this stack all the same time, each limited to its own range of positions on the stack. In this document the first position on that stack which belongs to the active function will be referred to as position 0 for that function.
XSUBs refer to their stack arguments with the macro \s-1ST\s0(x), where x refers to a position in this \s-1XSUB\s0's part of the stack. Position 0 for that function would be known to the \s-1XSUB\s0 as \s-1ST\s0(0). The \s-1XSUB\s0's incoming parameters and outgoing return values always begin at \s-1ST\s0(0). For many simple cases the xsubpp compiler will generate the code necessary to handle the argument stack by embedding code fragments found in the typemaps. In more complex cases the programmer must supply the code.

The \s-1RETVAL\s0 Variable

The \s-1RETVAL\s0 variable is a special C variable that is declared automatically for you. The C type of \s-1RETVAL\s0 matches the return type of the C library function. The xsubpp compiler will declare this variable in each \s-1XSUB\s0 with non-CWvoid return type. By default the generated C function will use \s-1RETVAL\s0 to hold the return value of the C library function being called. In simple cases the value of \s-1RETVAL\s0 will be placed in \s-1ST\s0(0) of the argument stack where it can be received by Perl as the return value of the \s-1XSUB\s0.
If the \s-1XSUB\s0 has a return type of CWvoid then the compiler will not declare a \s-1RETVAL\s0 variable for that function. When using a \s-1PPCODE:\s0 section no manipulation of the \s-1RETVAL\s0 variable is required, the section may use direct stack manipulation to place output values on the stack.
If \s-1PPCODE:\s0 directive is not used, CWvoid return value should be used only for subroutines which do not return a value, even if \s-1CODE:\s0 directive is used which sets \s-1ST\s0(0) explicitly.
Older versions of this document recommended to use CWvoid return value in such cases. It was discovered that this could lead to segfaults in cases when \s-1XSUB\s0 was truly CWvoid. This practice is now deprecated, and may be not supported at some future version. Use the return value CWSV * in such cases. (Currently CWxsubpp contains some heuristic code which tries to disambiguate between “truly-void” and “old-practice-declared-as-void” functions. Hence your code is at mercy of this heuristics unless you use CWSV * as return value.)

Returning SVs, AVs and HVs through \s-1RETVAL\s0

When you're using \s-1RETVAL\s0 to return an CWSV *, there's some magic going on behind the scenes that should be mentioned. When you're manipulating the argument stack using the \s-1ST\s0(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 CWRETVAL mortal when you're returning an CWSV *. Thus, the following 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. While this works fine for an CWSV *, it's unfortunately not as easy to have CWAV * or CWHV * as a return value. 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 \s-1CPAN\s0 modules) in the typemap file, the reference count of the CWAV * is not properly decremented. Thus, the above \s-1XSUB\s0 would leak memory whenever it is being called. The same problem exists for CWHV *.
When you're returning an CWAV * or a CWHV *, you have make sure their reference count is decremented by making the \s-1AV\s0 or \s-1HV\s0 mortal:
AV * array() CODE: RETVAL = newAV(); sv_2mortal((SV*)RETVAL); /* do something with RETVAL */ OUTPUT: RETVAL
And also remember that you don't have to do this for an CWSV *.

The \s-1MODULE\s0 Keyword

The \s-1MODULE\s0 keyword is used to start the \s-1XS\s0 code and to specify the package of the functions which are being defined. All text preceding the first \s-1MODULE\s0 keyword is considered C code and is passed through to the output with \s-1POD\s0 stripped, but otherwise untouched. Every \s-1XS\s0 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 \s-1MODULE\s0 statement in the \s-1XS\s0 source files. The value of \s-1MODULE\s0 should always remain constant within the same \s-1XS\s0 file, though this is not required.
The following example will start the \s-1XS\s0 code and will place all functions in a package named \s-1RPC\s0.
MODULE = RPC

The \s-1PACKAGE\s0 Keyword

When functions within an \s-1XS\s0 source file must be separated into packages the \s-1PACKAGE\s0 keyword should be used. This keyword is used with the \s-1MODULE\s0 keyword and must follow immediately after it when used.
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.
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 \s-1PREFIX\s0 Keyword

The \s-1PREFIX\s0 keyword designates prefixes which should be removed from the Perl function names. If the C function is CWrpcb_gettime() and the \s-1PREFIX\s0 value is CWrpcb_ then Perl will see this function as CWgettime().
This keyword should follow the \s-1PACKAGE\s0 keyword when used. If \s-1PACKAGE\s0 is not used then \s-1PREFIX\s0 should follow the \s-1MODULE\s0 keyword.
MODULE = RPC PREFIX = rpc_
MODULE = RPC PACKAGE = RPCB PREFIX = rpcb_

The \s-1OUTPUT:\s0 Keyword

The \s-1OUTPUT:\s0 keyword indicates that certain function parameters should be updated (new values made visible to Perl) when the \s-1XSUB\s0 terminates or that certain values should be returned to the calling Perl function. For simple functions which have no \s-1CODE:\s0 or \s-1PPCODE:\s0 section, such as the sin() function above, the \s-1RETVAL\s0 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 \s-1CODE:\s0 keyword. The \s-1RETVAL\s0 variable is not recognized as an output variable when the \s-1CODE:\s0 keyword is present. The \s-1OUTPUT:\s0 keyword is used in this situation to tell the compiler that \s-1RETVAL\s0 really is an output variable.
The \s-1OUTPUT:\s0 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 \s-1OUTPUT:\s0 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 CWSvSETMAGIC() for all parameters in the \s-1OUTPUT\s0 section of the \s-1XSUB\s0, except \s-1RETVAL\s0. 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 \s-1OUTPUT\s0 section may contain a CWSETMAGIC: DISABLE line to disable it for the remainder of the parameters in the \s-1OUTPUT\s0 section. Likewise, CWSETMAGIC: ENABLE can be used to reenable it for the remainder of the \s-1OUTPUT\s0 section. See perlguts for more details about 'set' magic.

The \s-1NO_OUTPUT\s0 Keyword

The \s-1NO_OUTPUT\s0 can be placed as the first token of the \s-1XSUB\s0. This keyword indicates that while the C subroutine we provide an interface to has a non-CWvoid return type, the return value of this C subroutine should not be returned from the generated Perl subroutine.
With this keyword present “The \s-1RETVAL\s0 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 CWRETVAL is going to be accessed by the user-supplied code. It is 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 \s-1XS\s0 function returns nothing on success, and will die() with a meaningful error message on error.

The \s-1CODE:\s0 Keyword

This keyword is used in more complicated XSUBs which require special handling for the C function. The \s-1RETVAL\s0 variable is still declared, but it will not be returned unless it is specified in the \s-1OUTPUT:\s0 section.
The following \s-1XSUB\s0 is for a C function which requires special handling of its parameters. The Perl usage is given first.
$status = rpcb_gettime( "localhost", $timep );
The \s-1XSUB\s0 follows.
bool_t rpcb_gettime(host,timep) char *host time_t timep CODE: RETVAL = rpcb_gettime( host, &timep ); OUTPUT: timep RETVAL

The \s-1INIT:\s0 Keyword

The \s-1INIT:\s0 keyword allows initialization to be inserted into the \s-1XSUB\s0 before the compiler generates the call to the C function. Unlike the \s-1CODE:\s0 keyword above, this keyword does not affect the way the compiler handles \s-1RETVAL\s0.
bool_t rpcb_gettime(host,timep) char *host time_t &timep INIT: printf("# Host is %s\n", host ); OUTPUT: timep
Another use for the \s-1INIT:\s0 section is to check for preconditions before making a call to the C function:
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");

The \s-1NO_INIT\s0 Keyword

The \s-1NO_INIT\s0 keyword is used to indicate that a function parameter is being used only as an output value. 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. \s-1NO_INIT\s0 will tell the compiler that some parameters will be used for output rather than for input and that they will be handled before the function terminates.
The following example shows a variation of the rpcb_gettime() function. This function uses the timep variable only as an output variable and does not care about its initial contents.
bool_t rpcb_gettime(host,timep) char *host time_t &timep = NO_INIT OUTPUT: timep

Initializing Function Parameters

C function parameters are normally initialized with their values from the argument stack (which in turn contains the parameters that were passed to the \s-1XSUB\s0 from Perl). The typemaps contain the code segments which are used to translate the Perl values to the C parameters. The programmer, however, is allowed to override the typemaps and supply alternate (or additional) initialization code. Initialization code starts with the first CW=, CW; or CW+ on a line in the \s-1INPUT:\s0 section. The only exception happens if this CW; terminates the line, then this CW; is quietly ignored.
The following code demonstrates how to supply initialization code for function parameters. The initialization code is eval'd within double quotes by the compiler before it is added to the output so anything which should be interpreted literally [mainly CW$, CW@, or CW\] must be protected with backslashes. The variables CW$var, CW$arg, and CW$type can be used as in typemaps.
bool_t rpcb_gettime(host,timep) char *host = (char *)SvPV($arg,PL_na); time_t &timep = 0; OUTPUT: timep
This should not be used to supply default values for parameters. One would normally use this when a function parameter must be processed by another library function before it can be used. Default parameters are covered in the next section.
If the initialization begins with CW=, then it is output in the declaration for the input variable, replacing the initialization supplied by the typemap. If the initialization begins with CW; or CW+, then it is performed after all of the input variables have been declared. In the CW; case the initialization normally supplied by the typemap is not performed. For the CW+ case, the declaration for the variable will include the initialization from the typemap. A global variable, CW%v, is available for the truly rare case where information from one initialization is needed in another initialization.
Here's a truly obscure example:
bool_t rpcb_gettime(host,timep) time_t &timep; /* \$v{timep}=@{[$v{timep}=$arg]} */ char *host + SvOK($v{timep}) ? SvPV($arg,PL_na) : NULL; OUTPUT: timep
The construct CW\$v{timep}=@{[$v{timep}=$arg]} used in the above example has a two-fold purpose: first, when this line is processed by xsubpp, the Perl snippet CW$v{timep}=$arg is evaluated. Second, the text of the evaluated snippet is output into the generated C file (inside a C comment)! During the processing of CWchar *host line, CW$arg will evaluate to CWST(0), and CW$v{timep} will evaluate to CWST(1).

Default Parameter Values

Default values for \s-1XSUB\s0 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 CWNO_INIT. Defaults should always be used on the right-most parameters only.
To allow the \s-1XSUB\s0 for rpcb_gettime() to have a default host value the parameters to the \s-1XSUB\s0 could be rearranged. The \s-1XSUB\s0 will then call the real rpcb_gettime() function with the parameters in the correct order. This \s-1XSUB\s0 can be called from Perl with either of the following statements:
$status = rpcb_gettime( $timep, $host );
$status = rpcb_gettime( $timep );
The \s-1XSUB\s0 will look like the code which follows. A \s-1CODE:\s0 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 \s-1PREINIT:\s0 Keyword

The \s-1PREINIT:\s0 keyword allows extra variables to be declared immediately before or after the declarations of the parameters from the \s-1INPUT:\s0 section are emitted.
If a variable is declared inside a \s-1CODE:\s0 section it will follow any typemap code that is emitted for the input parameters. This may result in the declaration ending up after C code, which is C syntax error. Similar errors may happen with an explicit CW;-type or CW+-type initialization of parameters is used (see “Initializing Function Parameters”). Declaring these variables in an \s-1INIT:\s0 section will not help.
In such cases, to force an additional variable to be declared together with declarations of other variables, place the declaration into a \s-1PREINIT:\s0 section. The \s-1PREINIT:\s0 keyword may be used one or more times within an \s-1XSUB\s0.
The 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 \s-1INIT:\s0 keyword would generate the same C code as the \s-1PREINIT:\s0 keyword. Another correct, but error-prone example:
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 CWhost is to use a C block in the \s-1CODE:\s0 section:
bool_t rpcb_gettime(timep) time_t timep = NO_INIT CODE: { char *host = "localhost"; RETVAL = rpcb_gettime( host, &timep ); } OUTPUT: timep RETVAL
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:
MyObject mutate(o) PREINIT: MyState st = global_state; INPUT: MyObject o; CLEANUP: reset_to(global_state, st);
Here we suppose that conversion to CWMyObject in the \s-1INPUT:\s0 section and from MyObject when processing \s-1RETVAL\s0 will modify a global variable CWglobal_state. After these conversions are performed, we restore the old value of CWglobal_state (to avoid memory leaks, for example).
There is another way to trade clarity for compactness: \s-1INPUT\s0 sections allow declaration of C variables which do not appear in the parameter list of a subroutine. Thus the above code 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 \s-1SCOPE:\s0 Keyword

The \s-1SCOPE:\s0 keyword allows scoping to be enabled for a particular \s-1XSUB\s0. If enabled, the \s-1XSUB\s0 will invoke \s-1ENTER\s0 and \s-1LEAVE\s0 automatically.
To support potentially complex type mappings, if a typemap entry used by an \s-1XSUB\s0 contains a comment like CW/*scope*/ then scoping will be automatically enabled for that \s-1XSUB\s0.
To enable scoping:
SCOPE: ENABLE
To disable scoping:
SCOPE: DISABLE

The \s-1INPUT:\s0 Keyword

The \s-1XSUB\s0's parameters are usually evaluated immediately after entering the \s-1XSUB\s0. The \s-1INPUT:\s0 keyword can be used to force those parameters to be evaluated a little later. The \s-1INPUT:\s0 keyword can be used multiple times within an \s-1XSUB\s0 and can be used to list one or more input variables. This keyword is used with the \s-1PREINIT:\s0 keyword.
The following example shows how the input parameter CWtimep can be evaluated late, after a \s-1PREINIT\s0.
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
Since \s-1INPUT\s0 sections allow declaration of C variables which do not appear in the parameter list of a subroutine, this may be shortened to:
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
(We used our knowledge that input conversion for CWchar * is a “simple” one, thus CWhost is initialized on the declaration line, and our assignment CWh = host is not performed too early. Otherwise one would need to have the assignment CWh = host in a \s-1CODE:\s0 or \s-1INIT:\s0 section.)

The \s-1IN/OUTLIST/IN_OUTLIST/OUT/IN_OUT\s0 Keywords

In the list of parameters for an \s-1XSUB\s0, one can precede parameter names by the CWIN/CWOUTLIST/CWIN_OUTLIST/CWOUT/CWIN_OUT keywords. CWIN keyword is the default, the other keywords indicate how the Perl interface should differ from the C interface.
Parameters preceded by CWOUTLIST/CWIN_OUTLIST/CWOUT/CWIN_OUT keywords are considered to be used by the C subroutine via pointers. CWOUTLIST/CWOUT keywords indicate that the C subroutine does not inspect the memory pointed by this parameter, but will write through this pointer to provide additional return values.
Parameters preceded by CWOUTLIST keyword do not appear in the usage signature of the generated Perl function.
Parameters preceded by CWIN_OUTLIST/CWIN_OUT/CWOUT do appear as parameters to the Perl function. With the exception of CWOUT-parameters, these parameters are converted to the corresponding C type, then pointers to these data are given as arguments to the C function. It is expected that the C function will write through these pointers.
The return list of the generated Perl function consists of the C return value from the function (unless the \s-1XSUB\s0 is of CWvoid return type or CWThe NO_OUTPUT Keyword was used) followed by all the CWOUTLIST and CWIN_OUTLIST parameters (in the order of appearance). On the return from the \s-1XSUB\s0 the CWIN_OUT/CWOUT Perl parameter will be modified to have the values written by the C function.
For example, an \s-1XSUB\s0
void day_month(OUTLIST day, IN unix_time, OUTLIST month) int day int unix_time int month
should be used from Perl as
my ($day, $month) = day_month(time);
The C signature of the corresponding function should be
void day_month(int *day, int unix_time, int *month);
The CWIN/CWOUTLIST/CWIN_OUTLIST/CWIN_OUT/CWOUT keywords can be mixed with ANSI-style declarations, as in
void day_month(OUTLIST int day, int unix_time, OUTLIST int month)
(here the optional CWIN keyword is omitted).
The CWIN_OUT parameters are identical with parameters introduced with “The & Unary Operator” and put into the CWOUTPUT: section (see “The \s-1OUTPUT:\s0 Keyword”). The CWIN_OUTLIST parameters are very similar, the only difference being that the value C function writes through the pointer would not modify the Perl parameter, but is put in the output list.
The CWOUTLIST/CWOUT parameter differ from CWIN_OUTLIST/CWIN_OUT parameters only by the initial value of the Perl parameter not being read (and not being given to the C function - which gets some garbage instead). For example, the same C function as above can be interfaced with as
void day_month(OUT int day, int unix_time, OUT int month);
or
void day_month(day, unix_time, month) int &day = NO_INIT int unix_time int &month = NO_INIT OUTPUT: day month
However, the generated Perl function is called in very C-ish style:
my ($day, $month); day_month($day, time, $month); If one of the input arguments to the C function is the length of a string argument CWNAME, one can substitute the name of the length-argument by CWlength(NAME) in the \s-1XSUB\s0 declaration. This argument must be omitted when the generated Perl function is called. E.g.,
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))
should be called as CWdump_chars($string).
This directive is supported with ANSI-type function declarations only.

Variable-length Parameter Lists

XSUBs can have variable-length parameter lists by specifying an ellipsis CW(...) in the parameter list. This use of the ellipsis is similar to that found in \s-1ANSI\s0 C. The programmer is able to determine the number of arguments passed to the \s-1XSUB\s0 by examining the CWitems variable which the xsubpp compiler supplies for all XSUBs. By using this mechanism one can create an \s-1XSUB\s0 which accepts a list of parameters of unknown length.
The host parameter for the rpcb_gettime() \s-1XSUB\s0 can be optional so the ellipsis can be used to indicate that the \s-1XSUB\s0 will take a variable number of parameters. Perl should be able to call this \s-1XSUB\s0 with either of the following statements.
$status = rpcb_gettime( $timep, $host );
$status = rpcb_gettime( $timep );
The \s-1XS\s0 code, with ellipsis, follows.
bool_t rpcb_gettime(timep, ...) time_t timep = NO_INIT PREINIT: char *host = "localhost"; STRLEN n_a; CODE: if( items > 1 ) host = (char *)SvPV(ST(1), n_a); RETVAL = rpcb_gettime( host, &timep ); OUTPUT: timep RETVAL

The C_ARGS: Keyword

The C_ARGS: keyword allows creating of \s-1XSUBS\s0 which have different calling sequence from Perl than from C, without a need to write \s-1CODE:\s0 or \s-1PPCODE:\s0 section. The contents of the C_ARGS: paragraph is put as the argument to the called C function without any change.
For example, suppose that a C function is declared as
symbolic nth_derivative(int n, symbolic function, int flags);
and that the default flags are kept in a global C variable CWdefault_flags. Suppose that you want to create an interface which is called as
$second_deriv = $function->nth_derivative(2);
To do this, declare the \s-1XSUB\s0 as
symbolic nth_derivative(function, n) symbolic function int n C_ARGS: n, function, default_flags

The \s-1PPCODE:\s0 Keyword

The \s-1PPCODE:\s0 keyword is an alternate form of the \s-1CODE:\s0 keyword and is used to tell the xsubpp compiler that the programmer is supplying the code to control the argument stack for the XSUBs return values. Occasionally one will want an \s-1XSUB\s0 to return a list of values rather than a single value. In these cases one must use \s-1PPCODE:\s0 and then explicitly push the list of values on the stack. The \s-1PPCODE:\s0 and \s-1CODE:\s0 keywords should not be used together within the same \s-1XSUB\s0.
The actual difference between \s-1PPCODE:\s0 and \s-1CODE:\s0 sections is in the initialization of CWSP macro (which stands for the current Perl stack pointer), and in the handling of data on the stack when returning from an \s-1XSUB\s0. In \s-1CODE:\s0 sections \s-1SP\s0 preserves the value which was on entry to the \s-1XSUB:\s0 \s-1SP\s0 is on the function pointer (which follows the last parameter). In \s-1PPCODE:\s0 sections \s-1SP\s0 is moved backward to the beginning of the parameter list, which allows CWPUSH*() macros to place output values in the place Perl expects them to be when the \s-1XSUB\s0 returns back to Perl.
The generated trailer for a \s-1CODE:\s0 section ensures that the number of return values Perl will see is either 0 or 1 (depending on the CWvoidness of the return value of the C function, and heuristics mentioned in “The \s-1RETVAL\s0 Variable”). The trailer generated for a \s-1PPCODE:\s0 section is based on the number of return values and on the number of times CWSP was updated by CW[X]PUSH*() macros.
Note that macros CWST(i), CWXST_m*() and CWXSRETURN*() work equally well in \s-1CODE:\s0 sections and \s-1PPCODE:\s0 sections.
The following \s-1XSUB\s0 will call the C rpcb_gettime() function and will return 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 must supply the C code necessary to have the real rpcb_gettime() function called and to have the return values properly placed on the argument stack.
The CWvoid return type for this function tells the xsubpp compiler that the \s-1RETVAL\s0 variable is not needed or used and that it should not be created. In most scenarios the void return type should be used with the \s-1PPCODE:\s0 directive.
The \s-1EXTEND\s0() macro is used to make room on the argument stack for 2 return values. The \s-1PPCODE:\s0 directive causes the xsubpp compiler to create a stack pointer available as CWSP, and it is this pointer which is being used in the \s-1EXTEND\s0() macro. The values are then pushed onto the stack with the PUSHs() macro.
Now the rpcb_gettime() function can be used from Perl with the following statement.
($status, $timep) = rpcb_gettime("localhost");
When handling output parameters with a \s-1PPCODE\s0 section, be sure to handle 'set' magic properly. See perlguts for details about 'set' magic.

Returning Undef And Empty Lists

Occasionally the programmer will want to return simply CWundef or an empty list if a function fails rather than a separate status value. The rpcb_gettime() function offers just this situation. If the function succeeds we would like to have it return the time and if it fails we would like to have undef returned. In the following Perl code the value of CW$timep will either be undef or it will be a valid time.
$timep = rpcb_gettime( "localhost" );
The following \s-1XSUB\s0 uses the CWSV * return type as a mnemonic only, and uses a \s-1CODE:\s0 block to indicate to the compiler that the programmer has supplied all the necessary code. The sv_newmortal() call will initialize the return value to undef, making that 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);
The next example demonstrates how one would place an explicit undef in the return value, should the need arise.
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); } else{ ST(0) = &PL_sv_undef; }
To return an empty list one must use a \s-1PPCODE:\s0 block and then not push return values on the stack.
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. */ }
Some people may be inclined to include an explicit CWreturn in the above \s-1XSUB\s0, rather than letting control fall through to the end. In those situations CWXSRETURN_EMPTY should be used, instead. This will ensure that the \s-1XSUB\s0 stack is properly adjusted. Consult perlapi for other CWXSRETURN macros.
Since CWXSRETURN_* macros can be used with \s-1CODE\s0 blocks as well, one can rewrite this example as:
int rpcb_gettime(host) char *host PREINIT: time_t timep; CODE: RETVAL = rpcb_gettime( host, &timep ); if (RETVAL == 0) XSRETURN_UNDEF; OUTPUT: RETVAL
In fact, one can put this check into a \s-1POSTCALL:\s0 section as well. Together with \s-1PREINIT:\s0 simplifications, this leads to:
int rpcb_gettime(host) char *host time_t timep; POSTCALL: if (RETVAL == 0) XSRETURN_UNDEF;

The \s-1REQUIRE:\s0 Keyword

The \s-1REQUIRE:\s0 keyword is used to indicate the minimum version of the xsubpp compiler needed to compile the \s-1XS\s0 module. An \s-1XS\s0 module which contains the following statement will compile with only xsubpp version 1.922 or greater:
REQUIRE: 1.922

The \s-1CLEANUP:\s0 Keyword

This keyword can be used when an \s-1XSUB\s0 requires special cleanup procedures before it terminates. When the \s-1CLEANUP:\s0 keyword is used it must follow any \s-1CODE:\s0, \s-1PPCODE:\s0, or \s-1OUTPUT:\s0 blocks which are present in the \s-1XSUB\s0. The code specified for the cleanup block will be added as the last statements in the \s-1XSUB\s0.

The \s-1POSTCALL:\s0 Keyword

This keyword can be used when an \s-1XSUB\s0 requires special procedures executed after the C subroutine call is performed. When the \s-1POSTCALL:\s0 keyword is used it must precede \s-1OUTPUT:\s0 and \s-1CLEANUP:\s0 blocks which are present in the \s-1XSUB\s0.
See examples in “The \s-1NO_OUTPUT\s0 Keyword” and “Returning Undef And Empty Lists”.
The \s-1POSTCALL:\s0 block does not make a lot of sense when the C subroutine call is supplied by user by providing either \s-1CODE:\s0 or \s-1PPCODE:\s0 section.

The \s-1BOOT:\s0 Keyword

The \s-1BOOT:\s0 keyword is used to add code to the extension's bootstrap function. The bootstrap function is generated by the xsubpp compiler and normally holds the statements necessary to register any XSUBs with Perl. With the \s-1BOOT:\s0 keyword the programmer can tell the compiler to add extra statements to the bootstrap function.
This keyword may be used any time after the first \s-1MODULE\s0 keyword and should appear on a line by itself. The first blank line after the keyword will terminate the code block.
BOOT: # The following message will be printed when the # bootstrap function executes. printf("Hello from the bootstrap!\n");

The \s-1VERSIONCHECK:\s0 Keyword

The \s-1VERSIONCHECK:\s0 keyword corresponds to xsubpp's CW-versioncheck and CW-noversioncheck options. This keyword overrides the command line options. Version checking is enabled by default. When version checking is enabled the \s-1XS\s0 module will attempt to verify that its version matches the version of the \s-1PM\s0 module.
To enable version checking:
VERSIONCHECK: ENABLE
To disable version checking:
VERSIONCHECK: DISABLE

The \s-1PROTOTYPES:\s0 Keyword

The \s-1PROTOTYPES:\s0 keyword corresponds to xsubpp's CW-prototypes and CW-noprototypes options. This keyword overrides the command line options. Prototypes are enabled by default. When prototypes are enabled XSUBs will be given Perl prototypes. This keyword may be used multiple times in an \s-1XS\s0 module to enable and disable prototypes for different parts of the module.
To enable prototypes:
PROTOTYPES: ENABLE
To disable prototypes:
PROTOTYPES: DISABLE

The \s-1PROTOTYPE:\s0 Keyword

This keyword is similar to the \s-1PROTOTYPES:\s0 keyword above but can be used to force xsubpp to use a specific prototype for the \s-1XSUB\s0. This keyword overrides all other prototype options and keywords but affects only the current \s-1XSUB\s0. Consult “Prototypes” in perlsub for information about Perl prototypes.
bool_t rpcb_gettime(timep, ...) time_t timep = NO_INIT PROTOTYPE: $;$ PREINIT: char *host = "localhost"; STRLEN n_a; CODE: if( items > 1 ) host = (char *)SvPV(ST(1), n_a); RETVAL = rpcb_gettime( host, &timep ); OUTPUT: timep RETVAL
If the prototypes are enabled, you can disable it locally for a given \s-1XSUB\s0 as in the following example:
void rpcb_gettime_noproto() PROTOTYPE: DISABLE ...

The \s-1ALIAS:\s0 Keyword

The \s-1ALIAS:\s0 keyword allows an \s-1XSUB\s0 to have two or more unique Perl names and to know which of those names was used when it was invoked. The Perl names may be fully-qualified with package names. Each alias is given an index. The compiler will setup a variable called CWix which contain the index of the alias which was used. When the \s-1XSUB\s0 is called with its declared name CWix will be 0.
The following example will create aliases CWFOO::gettime() and CWBAR::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

The \s-1OVERLOAD:\s0 Keyword

Instead of writing an overloaded interface using pure Perl, you can also use the \s-1OVERLOAD\s0 keyword to define additional Perl names for your functions (like the \s-1ALIAS:\s0 keyword above). However, the overloaded functions must be defined with three parameters (except for the nomethod() function which needs four parameters). If any function has the \s-1OVERLOAD:\s0 keyword, several additional lines will be defined in the c file generated by xsubpp in order to register with the overload magic.
Since blessed objects are actually stored as \s-1RV\s0's, it is useful to use the typemap features to preprocess parameters and extract the actual \s-1SV\s0 stored within the blessed \s-1RV\s0. See the sample for T_PTROBJ_SPECIAL below.
To use the \s-1OVERLOAD:\s0 keyword, create an \s-1XS\s0 function which 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 will overload both of the three way comparison operators. For all overload operations using non-alpha characters, you must type the parameter without quoting, seperating multiple overloads with whitespace. Note that "“ (the stringify overload) should be entered as \”\" (i.e. escaped).

The \s-1FALLBACK:\s0 Keyword

In addition to the \s-1OVERLOAD\s0 keyword, if you need to control how Perl autogenerates missing overloaded operators, you can set the \s-1FALLBACK\s0 keyword in the module header section, like this:
MODULE = RPC PACKAGE = RPC
FALLBACK: TRUE ...
where \s-1FALLBACK\s0 can take any of the three values \s-1TRUE\s0, \s-1FALSE\s0, or \s-1UNDEF\s0. If you do not set any \s-1FALLBACK\s0 value when using \s-1OVERLOAD\s0, it defaults to \s-1UNDEF\s0. \s-1FALLBACK\s0 is not used except when one or more functions using \s-1OVERLOAD\s0 have been defined. Please see “Fallback” in overload for more details.

The \s-1INTERFACE:\s0 Keyword

This keyword declares the current \s-1XSUB\s0 as a keeper of the given calling signature. If some text follows this keyword, it is considered as a list of functions which have this signature, and should be attached to the current \s-1XSUB\s0.
For example, if you have 4 C functions multiply(), divide(), add(), subtract() all having the signature:
symbolic f(symbolic, symbolic);
you can make them all to use the same \s-1XSUB\s0 using this:
symbolic interface_s_ss(arg1, arg2) symbolic arg1 symbolic arg2 INTERFACE: multiply divide add subtract
(This is the complete \s-1XSUB\s0 code for 4 Perl functions!) Four generated Perl function share names with corresponding C functions.
The advantage of this approach comparing to \s-1ALIAS:\s0 keyword is that there is no need to code a switch statement, each Perl function (which shares the same \s-1XSUB\s0) knows which C function it should call. Additionally, one can attach an extra function remainder() at runtime by using
CV *mycv = newXSproto("Symbolic::remainder", XS_Symbolic_interface_s_ss, __FILE__, "$$"); XSINTERFACE_FUNC_SET(mycv, remainder);
say, from another \s-1XSUB\s0. (This example supposes that there was no \s-1INTERFACE_MACRO:\s0 section, otherwise one needs to use something else instead of CWXSINTERFACE_FUNC_SET, see the next section.)

The \s-1INTERFACE_MACRO:\s0 Keyword

This keyword allows one to define an \s-1INTERFACE\s0 using a different way to extract a function pointer from an \s-1XSUB\s0. The text which follows this keyword should give the name of macros which would extract/set a function pointer. The extractor macro is given return type, CWCV*, and CWXSANY.any_dptr for this CWCV*. The setter macro is given cv, and the function pointer.
The default value is CWXSINTERFACE_FUNC and CWXSINTERFACE_FUNC_SET. An \s-1INTERFACE\s0 keyword with an empty list of functions can be omitted if \s-1INTERFACE_MACRO\s0 keyword is used.
Suppose that in the previous example functions pointers for multiply(), divide(), add(), subtract() are kept in a global C array CWfp[] with offsets being CWmultiply_off, CWdivide_off, CWadd_off, CWsubtract_off. Then one can use
#define XSINTERFACE_FUNC_BYOFFSET(ret,cv,f)   ((XSINTERFACE_CVT(ret,))fp[CvXSUBANY(cv).any_i32]) #define XSINTERFACE_FUNC_BYOFFSET_set(cv,f)   CvXSUBANY(cv).any_i32 = CAT2( f, _off )
in C section,
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 \s-1XSUB\s0 section.

The \s-1INCLUDE:\s0 Keyword

This keyword can be used to pull other files into the \s-1XS\s0 module. The other files may have \s-1XS\s0 code. \s-1INCLUDE:\s0 can also be used to run a command to generate the \s-1XS\s0 code to be pulled into the module.
The file Rpcb1.xsh contains our CWrpcb_gettime() function:
bool_t rpcb_gettime(host,timep) char *host time_t &timep OUTPUT: timep
The \s-1XS\s0 module can use \s-1INCLUDE:\s0 to pull that file into it.
INCLUDE: Rpcb1.xsh
If the parameters to the \s-1INCLUDE:\s0 keyword are followed by a pipe (CW|) then the compiler will interpret the parameters as a command.
INCLUDE: cat Rpcb1.xsh |

The \s-1CASE:\s0 Keyword

The \s-1CASE:\s0 keyword allows an \s-1XSUB\s0 to have multiple distinct parts with each part acting as a virtual \s-1XSUB\s0. \s-1CASE:\s0 is greedy and if it is used then all other \s-1XS\s0 keywords must be contained within a \s-1CASE:\s0. This means nothing may precede the first \s-1CASE:\s0 in the \s-1XSUB\s0 and anything following the last \s-1CASE:\s0 is included in that case.
A \s-1CASE:\s0 might switch via a parameter of the \s-1XSUB\s0, via the CWix \s-1ALIAS:\s0 variable (see “The \s-1ALIAS:\s0 Keyword”), or maybe via the CWitems variable (see “Variable-length Parameter Lists”). The last \s-1CASE:\s0 becomes the default case if it is not associated with a conditional. The following example shows \s-1CASE\s0 switched via CWix with a function CWrpcb_gettime() having an alias CWx_gettime(). When the function is called as CWrpcb_gettime() its parameters are the usual CW(char *host, time_t *timep), but when the function is called as CWx_gettime() its parameters are reversed, CW(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 & Unary Operator

The CW& unary operator in the \s-1INPUT:\s0 section is used to tell xsubpp that it should convert a Perl value to/from C using the C type to the left of CW&, but provide a pointer to this value when the C function is called.
This is useful to avoid a \s-1CODE:\s0 block for a C function which takes a parameter by reference. Typically, the parameter should be not a pointer type (an CWint or CWlong but not an CWint* or CWlong*).
The following \s-1XSUB\s0 will generate incorrect C code. The xsubpp compiler will turn this into code which calls CWrpcb_gettime() with parameters CW(char *host, time_t timep), but the real CWrpcb_gettime() wants the CWtimep parameter to be of type CWtime_t* rather than CWtime_t.
bool_t rpcb_gettime(host,timep) char *host time_t timep OUTPUT: timep
That problem is corrected by using the CW& operator. The xsubpp compiler will now turn this into code which calls CWrpcb_gettime() correctly with parameters CW(char *host, time_t *timep). It does this by carrying the CW& through, so the function call looks like CWrpcb_gettime(host, &timep).
bool_t rpcb_gettime(host,timep) char *host time_t &timep OUTPUT: timep

Inserting \s-1POD\s0, Comments and C Preprocessor Directives

C preprocessor directives are allowed within \s-1BOOT:\s0, \s-1PREINIT:\s0 \s-1INIT:\s0, \s-1CODE:\s0, \s-1PPCODE:\s0, \s-1POSTCALL:\s0, and \s-1CLEANUP:\s0 blocks, as well as outside the functions. Comments are allowed anywhere after the \s-1MODULE\s0 keyword. The compiler will pass the preprocessor directives through untouched and will remove the commented lines. \s-1POD\s0 documentation is allowed at any point, both in the C and \s-1XS\s0 language sections. \s-1POD\s0 must be terminated with a CW=cut command; CWxsubpp will exit with an error if it does not. It is very unlikely that human generated C code will be mistaken for \s-1POD\s0, as most indenting styles result in whitespace in front of any line starting with CW=. Machine generated \s-1XS\s0 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 CW# 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 such. The simplest way to prevent this is to put whitespace in front of the CW#.
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 that you made a duplicate definition of the function. Also, put a blank line before the #else/#endif so it will not be seen as part of the function body.

Using \s-1XS\s0 With

If an \s-1XSUB\s0 name contains CW::, it is considered to be a method. The generated Perl function will assume that its first argument is an object pointer. The object pointer will be stored in a variable called \s-1THIS\s0. The object should have been created by 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. An example typemap is shown at the end of this section.
If the return type of the \s-1XSUB\s0 includes CWstatic, the method is considered to be a static method. It will call the function using the class::method() syntax. If the method is not static the function will be called using the \s-1THIS-\s0>method() syntax.
The next examples will use the following 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 (\s-1THIS\s0, or “self”) is implicit and is not listed.
int color::blue()
void color::set_blue( val ) int val
Both Perl functions will expect an object as the first parameter. In the generated code the object is called CWTHIS, and the method call will be performed on this object. So in the code the blue() and set_blue() methods will be called as 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 \s-1DESTROY\s0 then the CWdelete function will be called and CWTHIS will be given as its parameter. The generated 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 CWnew function will be called to create a dynamic object. The \s-1XSUB\s0 will expect the class name, which will be kept in a variable called CWCLASS, to be given as the first argument.
color * color::new()
The generated code will call CWnew.
RETVAL = new color();
The following is an example of a typemap that could be used for this 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 \s-1XS\s0 (such as created by CWh2xs -x) is often sufficient. However, 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 \s-1INIT:\s0 section to report the failure. For failures detectable after the C function returns one may want to use a \s-1POSTCALL:\s0 section to process the failure. In more complicated cases use \s-1CODE:\s0 or \s-1PPCODE:\s0 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 \s-1XS\s0 file, and create an \s-1OUTPUT\s0 typemap entry for CWnegative_is_failure which converts negative values to CWundef, or maybe croak()s. After this the return value of type CWnegative_is_failure will create more Perl-like interface.
Identify which values are used by only the C and \s-1XSUB\s0 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, they can be handled in \s-1XS\s0 with the CW& unary operator, and, possibly, using the \s-1NO_INIT\s0 keyword. Some others will require handling of types like CWint *, 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 CWh2xs -x.)
If the same C type is used in several different contexts which require different translations, CWtypedef 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 one should select either T_PTROBJ or T_PTRREF for the \s-1XS\s0 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 \s-1XSUB\s0 will attempt to verify that the Perl object is of the expected type.
The following \s-1XS\s0 code shows the getnetconfigent() function which is used with \s-1ONC+\s0 \s-1TIRPC\s0. 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 \s-1XS\s0 source to free the memory used by getnetconfigent(). Destructors in \s-1XS\s0 can be created by specifying an \s-1XSUB\s0 function whose name ends with the word \s-1DESTROY\s0. \s-1XS\s0 destructors can be used to free memory which may have been malloc'd by another \s-1XSUB\s0.
struct netconfig *getnetconfigent(const char *netid);
A CWtypedef will be created for CWstruct netconfig. The Perl object will be blessed in a class matching the name of the C type, with the tag CWPtr 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 \s-1PREFIX\s0 keyword will be used to trim the name to the word \s-1DESTROY\s0 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 the typemap section 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 CW$netconf it will send the object to the supplied \s-1XSUB\s0 \s-1DESTROY\s0 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() \s-1XSUB\s0 and an object created by a normal Perl subroutine.

The Typemap

The typemap is a collection of code fragments which are used by the xsubpp compiler to map C function parameters and values to Perl values. The typemap file may consist of three sections labelled CWTYPEMAP, CWINPUT, and CWOUTPUT. An unlabelled initial section is assumed to be a CWTYPEMAP section. The \s-1INPUT\s0 section tells the compiler how to translate Perl values into variables of certain C types. The \s-1OUTPUT\s0 section tells the compiler how to translate the values from certain C types into values Perl can understand. The \s-1TYPEMAP\s0 section tells the compiler which of the \s-1INPUT\s0 and \s-1OUTPUT\s0 code fragments should be used to map a given C type to a Perl value. The section labels CWTYPEMAP, CWINPUT, or CWOUTPUT must begin in the first column on a line by themselves, and must be in uppercase.
The default typemap in the CWlib/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 \s-1INPUT\s0 and \s-1OUTPUT\s0 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.
Most extensions which require a custom typemap will need only the \s-1TYPEMAP\s0 section of the typemap file. The custom typemap used in the getnetconfigent() example shown earlier demonstrates what may be the typical use of extension typemaps. That typemap is used to equate a C structure with the T_PTROBJ typemap. The typemap used by getnetconfigent() is shown here. Note that the C type is separated from the \s-1XS\s0 type with a tab and that the C unary operator CW* is considered to be a part of the C type name.
TYPEMAP Netconfig *<tab>T_PTROBJ
Here's a more complicated example: suppose that you wanted CWstruct netconfig to be blessed into the class CWNet::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 CWT_PTROBJ_SPECIAL that maps underscores to double-colons (::), and declare CWNet_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 \s-1INPUT\s0 and \s-1OUTPUT\s0 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.
The \s-1INT2PTR\s0 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 \s-1PTR2IV\s0, \s-1PTR2UV\s0, \s-1PTR2NV\s0 macros, to map the other way, which may be useful in \s-1OUTPUT\s0 sections.

Safely Storing Static Data in \s-1XS\s0

Starting with Perl 5.8, a macro framework has been defined to allow static data to be safely stored in \s-1XS\s0 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 \s-1XS\s0 modules that make use of static data.
The easiest way to get a template set of macros to use is by specifying the CW-g (CW--global) option with h2xs (see h2xs).
Below is an example module that makes use of the macros.
#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) 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); }
char * get_mouse_name(index) int index CODE: dMY_CXT; RETVAL = MY_CXT.lives ++; if (index > MY_CXT.count) croak("There are only 3 blind mice."); else RETVAL = newSVpv(MY_CXT.name[index - 1]);
\s-1REFERENCE\s0
"\s-1MY_CXT_KEY\s0" This macro is used to define a unique key to refer to the static data for an \s-1XS\s0 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 This struct typedef must always be called CWmy_cxt_t the other CWCXT* macros assume the existence of the CWmy_cxt_t typedef name. Declare a typedef named CWmy_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;
"\s-1START_MY_CXT\s0" Always place the \s-1START_MY_CXT\s0 macro directly after the declaration of CWmy_cxt_t.
"\s-1MY_CXT_INIT\s0" The \s-1MY_CXT_INIT\s0 macro initialises storage for the CWmy_cxt_t struct. It must be called exactly once typically in a \s-1BOOT:\s0 section.
"dMY_CXT" Use the dMY_CXT macro (a declaration) in all the functions that access \s-1MY_CXT\s0.
"\s-1MY_CXT\s0" Use the \s-1MY_CXT\s0 macro to access members of the CWmy_cxt_t struct. For example, if CWmy_cxt_t is typedef struct { int index; } my_cxt_t; then use this to access the CWindex member dMY_CXT; MY_CXT.index = 2;

EXAMPLES

File CWRPC.xs: Interface to some \s-1ONC+\s0 \s-1RPC\s0 bind library functions.
#include "EXTERN.h" #include "perl.h" #include "XSUB.h"
#include <rpc/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 CWtypemap: Custom typemap for \s-1RPC\s0.xs.
TYPEMAP Netconfig * T_PTROBJ
File CWRPC.pm: Perl module for the \s-1RPC\s0 extension.
package RPC;
require Exporter; require DynaLoader; @ISA = qw(Exporter DynaLoader); @EXPORT = qw(rpcb_gettime getnetconfigent);
bootstrap RPC; 1;
File CWrpctest.pl: Perl test program for the \s-1RPC\s0 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";

XS VERSION

This document covers features supported by CWxsubpp 1.935.

AUTHOR

Originally written by Dean Roehrich <roehrich@cray.com>.
Maintained since 1996 by The Perl Porters <perlbug@perl.org>.