The XS compiler is called xsubpp . This compiler will embed the constructs necessary to let an XSUB, which is really a C function in disguise, manipulate Perl values and creates the glue necessary to let Perl access the XSUB. The compiler uses typemaps to determine how to map C function parameters and variables to Perl values. The default typemap handles many common C types. A supplement typemap must be created to handle special structures and types for the library being linked.
Many of the examples which follow will concentrate on creating an interface between Perl and the ONC+RPC bind library functions. Specifically, the rpcb_gettime() function will be 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.
From C this function will be called with the following statements.
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.
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.
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.
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.
The compiler expects a tab between the parameter name and its type, and any or no whitespace before the type. When using C pointers the indirection operator * should be considered part of the type and the address operator & should be considered part of the variable, as is demonstrated in the rpcb_gettime() function above. See the section on typemaps for more about handling qualifiers and unary operators in C types.
The parameter list of a function must not have whitespace after the open-parenthesis or before the close-parenthesis.
The function name and the return type must be placed on separate lines.
XSUBs refer to their stack arguments with the macro ST(x) , where x refers to a position in this XSUB's part of the stack. Position 0 for that function would be known to the XSUB as ST(0). The XSUB's incoming parameters and outgoing return values always begin at ST(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.
If the XSUB has a return type of void then the compiler will not supply a RETVAL variable for that function. When using the PPCODE: directive the RETVAL variable may not be needed.
The following example will start the XS code and will place all functions in a package named RPC.
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.
This keyword should follow the PACKAGE keyword when used. If PACKAGE is not used then PREFIX should follow the MODULE keyword.
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. If function parameters are listed under OUTPUT: along with the RETVAL variable then the RETVAL variable must be the last one listed.
The OUTPUT: keyword will also allow an output parameter to be mapped to a matching piece of code rather than to a typemap.
The following XSUB is for a C function which requires special handling of its parameters. The Perl usage is given first.
The XSUB follows.
In many of the examples shown here the CODE: block (and other blocks) will often be contained within braces ( { and } ). This protects the CODE: block from complex INPUT typemaps and ensures the resulting C code is legal.
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.
The following code demonstrates how to supply initialization code for function parameters. The initialization code is eval'd by the compiler before it is added to the output so anything which should be interpreted literally, such as double quotes, must be protected with backslashes.
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.
To allow the XSUB for rpcb_gettime() to 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. Perl will call this XSUB with either of the following statements.
The XSUB will look like the code which follows. A CODE: block is used to call the real rpcb_gettime() function with the parameters in the correct order for that function.
The host parameter for the rpcb_gettime() XSUB can be optional so the ellipsis can be used to indicate that the XSUB will take a variable number of parameters. Perl should be able to call this XSUB with either of the following statments.
The XS code, with ellipsis, follows.
The following XSUB will call the C rpcb_gettime() function and will return its two output values, timep and status, to Perl as a single list.
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 void return type for this function tells the xsubpp compiler that the RETVAL 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 PPCODE: directive.
The EXTEND() macro is used to make room on the argument stack for 2 return values. The PPCODE: directive causes the xsubpp compiler to create a stack pointer called sp , and it is this pointer which is being used in the EXTEND() 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.
The following XSUB uses the void return type to disable the generation of the RETVAL variable and uses a CODE: 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.
The next example demonstrates how one would place an explicit undef in the return value, should the need arise.
To return an empty list one must use a PPCODE: block and then not push return values on the stack.
This keyword may be used any time after the first MODULE keyword and should appear on a line by itself. The first blank line after the keyword will terminate the code block.
If the method is defined as static it will call the C++ function using the class:: method() syntax. If the method is not static the function will be called using the THIS-> method() syntax.
This Perl code can be used to call that XSUB.
In the above example the SV contained a C char* but a Perl scalar variable may also contain numbers and references. If the SV is expected to have a C int then the macro SvIVX() should be used to dereference the SV. When the SV contains a C double then SvNVX() should be used.
The macro SvRV() can be used to dereference an SV when it is a Perl reference. The result will be another SV which points to the actual Perl variable. This can then be dereferenced with SvPVX() , SvNVX() , or SvIVX() . The following XSUB will use SvRV() .
This Perl code will create a variable $RPC::host which is a reference to $MY::host . The variable $MY::host contains the hostname which will be used.
The second argument to perl_get_sv() will normally be FALSE as shown in the above examples. An argument of TRUE will cause variables to be created if they do not already exist. One should not use TRUE unless steps are taken to deal with a possibly empty SV.
XSUBs may use perl_get_av() , perl_get_hv() , and perl_get_cv() to access Perl arrays, hashes, and code values.
Identify the C functions which modify their parameters. The XSUBs for these functions may be able to return lists to Perl, or may be candidates to return undef or an empty list in case of failure.
Identify which values are used only by the C and XSUB functions themselves. 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 can be handled in XS with the & unary operator on the variable name while others will require the use of the * operator on the type name. In general it is easier to work with the & operator.
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.
The following is a Perl module for an extension containing some ONC+ RPC bind library functions.
The RPC extension contains the functions found in the @EXPORT list. By using the Exporter module the RPC module can make these function names visible to the rest of the Perl program. The DynaLoader module will allow the RPC module to bootstrap the extension library. To load this extension and make the functions available, the following Perl statement should be used.
For more information about the DynaLoader consult its documentation in the ext/DynaLoader directory in the Perl source.
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.
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.
This example requires the following typemap entry. Consult the typemap section for more information about adding new typemaps for an extension.
This example will be used with the following Perl statements.
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.
The following command will create an extension called Rusers from the<rpcsvc/rusers.h> header.
When the Rusers extension has been compiled and installed Perl can use it to retrieve any #define statements which were in the C header.
The extension will not use autoloaded functions and will not define constants, so the -A option will be given to h2xs . When run from the Perl source directory, the h2xs compiler will create the directory ext/RPC and will populate it with files called RPC.xs, RPC.pm, Makefile.PL, and MANIFEST. The XS code for the RPC functions should be added to the RPC.xs file. The @EXPORT list in RPC.pm should be updated to include the functions from RPC.xs.
To compile the extension for dynamic loading the following command should be executed from the ext/RPC directory.
If the extension will be statically linked into the Perl binary then the makefile (use makefile , not Makefile ) in the Perl source directory should be edited to add ext/RPC/RPC.a to the static_ext variable. Before making this change Perl should have already been built. After the makefile has been updated the following command should be executed from the Perl source directory.
Perl's Configure script can also be used to add extensions. The extension should be placed in the ext directory under the Perl source before Perl has been built and prior to running Configure. When Configure is run it will find the extension along with the other extensions in the ext directory and will add it to the list of extensions to be built. When make is run the extension will be built along with the other extensions.
Configure recognizes extensions if they have an XS source file which matches the name of the extension directory. If the extension directory includes a MANIFEST file Configure will search that file for any .SH files and extract them after it extracts all the other .SH files listed in the main MANIFEST. The main Perl Makefile will then run make in the extension's directory if it finds an XS file matching the name of the extension's directory.
The default typemap in the ext directory of the Perl source contains many useful types which can be used by Perl extensions. Some extensions define additional typemaps which they keep in their own directory. These additional typemaps may reference INPUT and OUTPUT maps in the main typemap. The xsubpp compiler will allow the extension's own typemap to override any mappings which are in the default typemap.
Most extensions which require a custom typemap will need only the TYPEMAP 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 XS type with a tab and that the C unary operator * is considered to be a part of the C type name.
File typemap : Custom typemap for RPC.xs.
File RPC.pm : Perl module for the RPC extension.
File rpctest.pl : Perl test program for the RPC extension.