perlxs.pod revision 7c478bd95313f5f23a4c958a745db2134aa03244
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm=head1 NAME
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmperlxs - XS language reference manual
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm=head1 DESCRIPTION
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm=head2 Introduction
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmXS is an interface description file format used to create an extension
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gminterface between Perl and C code (or a C library) which one wishes
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmto use with Perl. The XS interface is combined with the library to
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmcreate a new library which can then be either dynamically loaded
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmor statically linked into perl. The XS interface description is
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmwritten in the XS language and is the core component of the Perl
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmextension interface.
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmAn B<XSUB> forms the basic unit of the XS interface. After compilation
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmby the B<xsubpp> compiler, each XSUB amounts to a C function definition
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmwhich will provide the glue between Perl calling conventions and C
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmcalling conventions.
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmThe glue code pulls the arguments from the Perl stack, converts these
f317a3a3712d9b82387b437ac621db3733d8c804krishnaPerl values to the formats expected by a C function, call this C function,
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmtransfers the return values of the C function back to Perl.
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmReturn values here may be a conventional C return value or any C
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmfunction arguments that may serve as output parameters. These return
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmvalues may be passed back to Perl either by putting them on the
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmPerl stack, or by modifying the arguments supplied from the Perl side.
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmThe above is a somewhat simplified view of what really happens. Since
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmPerl allows more flexible calling conventions than C, XSUBs may do much
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmmore in practice, such as checking input parameters for validity,
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmthrowing exceptions (or returning undef/empty list) if the return value
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmfrom the C function indicates failure, calling different C functions
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmbased on numbers and types of the arguments, providing an object-oriented
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gminterface, etc.
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmOf course, one could write such glue code directly in C. However, this
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmwould be a tedious task, especially if one needs to write glue for
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmmultiple C functions, and/or one is not familiar enough with the Perl
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmstack discipline and other such arcana. XS comes to the rescue here:
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gminstead of writing this glue C code in long-hand, one can write
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gma more concise short-hand I<description> of what should be done by
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmthe glue, and let the XS compiler B<xsubpp> handle the rest.
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmThe XS language allows one to describe the mapping between how the C
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmroutine is used, and how the corresponding Perl routine is used. It
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmalso allows creation of Perl routines which are directly translated to
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmC code and which are not related to a pre-existing C function. In cases
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmwhen the C interface coincides with the Perl interface, the XSUB
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmdeclaration is almost identical to a declaration of a C function (in K&R
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmstyle). In such circumstances, there is another tool called C<h2xs>
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmthat is able to translate an entire C header file into a corresponding
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmXS file that will provide glue to the functions/macros described in
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmthe header file.
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmThe XS compiler is called B<xsubpp>. This compiler creates
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmthe constructs necessary to let an XSUB manipulate Perl values, and
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmcreates the glue necessary to let Perl call the XSUB. The compiler
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmuses B<typemaps> to determine how to map C function parameters
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmand output values to Perl values and back. The default typemap
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm(which comes with Perl) handles many common C types. A supplementary
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmtypemap may also be needed to handle any special structures and types
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmfor the library being linked.
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmA file in XS format starts with a C language section which goes until the
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmfirst C<MODULE =Z<>> directive. Other XS directives and XSUB definitions
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmmay follow this line. The "language" used in this part of the file
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmis usually referred to as the XS language. B<xsubpp> recognizes and
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmskips POD (see L<perlpod>) in both the C and XS language sections, which
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmallows the XS file to contain embedded documentation.
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmSee L<perlxstut> for a tutorial on the whole extension creation process.
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmNote: For some extensions, Dave Beazley's SWIG system may provide a
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmsignificantly more convenient mechanism for creating the extension
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmglue code. See http://www.swig.org/ for more information.
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm=head2 On The Road
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmMany of the examples which follow will concentrate on creating an interface
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmbetween Perl and the ONC+ RPC bind library functions. The rpcb_gettime()
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmfunction is used to demonstrate many features of the XS language. This
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmfunction has two parameters; the first is an input parameter and the second
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmis an output parameter. The function also returns a status value.
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm bool_t rpcb_gettime(const char *host, time_t *timep);
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmFrom C this function will be called with the following
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmstatements.
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm bool_t status;
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm time_t timep;
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm status = rpcb_gettime( "localhost", &timep );
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmIf an XSUB is created to offer a direct translation between this function
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmand Perl, then this XSUB will be used from Perl with the following code.
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmThe $status and $timep variables will contain the output of the function.
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm $status = rpcb_gettime( "localhost", $timep );
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmThe following XS file shows an XS subroutine, or XSUB, which
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmdemonstrates one possible interface to the rpcb_gettime()
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmfunction. This XSUB represents a direct translation between
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmC and Perl and so preserves the interface even from Perl.
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmThis XSUB will be invoked from Perl with the usage shown
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmabove. Note that the first three #include statements, for
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmC<EXTERN.h>, C<perl.h>, and C<XSUB.h>, will always be present at the
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmbeginning of an XS file. This approach and others will be
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmexpanded later in this document.
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm #include "perl.h"
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm #include "XSUB.h"
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm MODULE = RPC PACKAGE = RPC
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm rpcb_gettime(host,timep)
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm char *host
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm time_t &timep
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmAny extension to Perl, including those containing XSUBs,
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmshould have a Perl module to serve as the bootstrap which
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmpulls the extension into Perl. This module will export the
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmextension's functions and variables to the Perl program and
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmwill cause the extension's XSUBs to be linked into Perl.
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmThe following module will be used for most of the examples
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmin this document and should be used from Perl with the C<use>
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmcommand as shown earlier. Perl modules are explained in
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmmore detail later in this document.
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm package RPC;
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm require Exporter;
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm require DynaLoader;
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm @ISA = qw(Exporter DynaLoader);
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm @EXPORT = qw( rpcb_gettime );
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm bootstrap RPC;
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmThroughout this document a variety of interfaces to the rpcb_gettime()
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmXSUB will be explored. The XSUBs will take their parameters in different
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmorders or will take different numbers of parameters. In each case the
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmXSUB is an abstraction between Perl and the real C rpcb_gettime()
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmfunction, and the XSUB must always ensure that the real rpcb_gettime()
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmfunction is called with the correct parameters. This abstraction will
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmallow the programmer to create a more Perl-like interface to the C
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm=head2 The Anatomy of an XSUB
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmThe simplest XSUBs consist of 3 parts: a description of the return
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmvalue, the name of the XSUB routine and the names of its arguments,
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmand a description of types or formats of the arguments.
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmThe following XSUB allows a Perl program to access a C library function
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmcalled sin(). The XSUB will imitate the C function which takes a single
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmargument and returns a single value.
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmOptionally, one can merge the description of types and the list of
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmargument names, rewriting this as
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm sin(double x)
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmThis makes this XSUB look similar to an ANSI C declaration. An optional
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmsemicolon is allowed after the argument list, as in
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm sin(double x);
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmParameters with C pointer types can have different semantic: C functions
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmwith similar declarations
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm bool string_looks_as_a_number(char *s);
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm bool make_char_uppercase(char *c);
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmare used in absolutely incompatible manner. Parameters to these functions
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmcould be described B<xsubpp> like this:
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmBoth these XS declarations correspond to the C<char*> C type, but they have
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmdifferent semantics, see L<"The & Unary Operator">.
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmIt is convenient to think that the indirection operator
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmC<*> should be considered as a part of the type and the address operator C<&>
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmshould be considered part of the variable. See L<"The Typemap">
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmfor more info about handling qualifiers and unary operators in C types.
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmThe function name and the return type must be placed on
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmseparate lines and should be flush left-adjusted.
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm INCORRECT CORRECT
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm double sin(x) double
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm double x sin(x)
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmThe rest of the function description may be indented or left-adjusted. The
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmfollowing example shows a function with its body left-adjusted. Most
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmexamples in this document will indent the body for better readability.
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmMore complicated XSUBs may contain many other sections. Each section of
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gman XSUB starts with the corresponding keyword, such as INIT: or CLEANUP:.
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmHowever, the first two lines of an XSUB always contain the same data:
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmdescriptions of the return type and the names of the function and its
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmparameters. Whatever immediately follows these is considered to be
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gman INPUT: section unless explicitly marked with another keyword.
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm(See L<The INPUT: Keyword>.)
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmAn XSUB section continues until another section-start keyword is found.
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm=head2 The Argument Stack
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmThe Perl argument stack is used to store the values which are
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmsent as parameters to the XSUB and to store the XSUB's
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmreturn value(s). In reality all Perl functions (including non-XSUB
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmones) keep their values on this stack all the same time, each limited
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmto its own range of positions on the stack. In this document the
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmfirst position on that stack which belongs to the active
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmfunction will be referred to as position 0 for that function.
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmXSUBs refer to their stack arguments with the macro B<ST(x)>, where I<x>
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmrefers to a position in this XSUB's part of the stack. Position 0 for that
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmfunction would be known to the XSUB as ST(0). The XSUB's incoming
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmparameters and outgoing return values always begin at ST(0). For many
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmsimple cases the B<xsubpp> compiler will generate the code necessary to
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmhandle the argument stack by embedding code fragments found in the
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmtypemaps. In more complex cases the programmer must supply the code.
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm=head2 The RETVAL Variable
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmThe RETVAL variable is a special C variable that is declared automatically
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmfor you. The C type of RETVAL matches the return type of the C library
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmfunction. The B<xsubpp> compiler will declare this variable in each XSUB
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmwith non-C<void> return type. By default the generated C function
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmwill use RETVAL to hold the return value of the C library function being
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmcalled. In simple cases the value of RETVAL will be placed in ST(0) of
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmthe argument stack where it can be received by Perl as the return value
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmof the XSUB.
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmIf the XSUB has a return type of C<void> then the compiler will
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmnot declare a RETVAL variable for that function. When using
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gma PPCODE: section no manipulation of the RETVAL variable is required, the
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmsection may use direct stack manipulation to place output values on the stack.
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmIf PPCODE: directive is not used, C<void> return value should be used
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmonly for subroutines which do not return a value, I<even if> CODE:
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmdirective is used which sets ST(0) explicitly.
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmOlder versions of this document recommended to use C<void> return
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmvalue in such cases. It was discovered that this could lead to
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmsegfaults in cases when XSUB was I<truly> C<void>. This practice is
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmnow deprecated, and may be not supported at some future version. Use
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmthe return value C<SV *> in such cases. (Currently C<xsubpp> contains
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmsome heuristic code which tries to disambiguate between "truly-void"
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmand "old-practice-declared-as-void" functions. Hence your code is at
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmmercy of this heuristics unless you use C<SV *> as return value.)
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm=head2 Returning SVs, AVs and HVs through RETVAL
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmWhen you're using RETVAL to return an C<SV *>, there's some magic
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmgoing on behind the scenes that should be mentioned. When you're
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmmanipulating the argument stack using the ST(x) macro, for example,
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmyou usually have to pay special attention to reference counts. (For
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmmore about reference counts, see L<perlguts>.) To make your life
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmeasier, the typemap file automatically makes C<RETVAL> mortal when
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmyou're returning an C<SV *>. Thus, the following two XSUBs are more
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmor less equivalent:
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm ST(0) = newSVpv("Hello World",0);
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm sv_2mortal(ST(0));
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm XSRETURN(1);
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm RETVAL = newSVpv("Hello World",0);
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmThis is quite useful as it usually improves readability. While
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmthis works fine for an C<SV *>, it's unfortunately not as easy
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmto have C<AV *> or C<HV *> as a return value. You I<should> be
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmable to write:
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm RETVAL = newAV();
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm /* do something with RETVAL */
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmBut due to an unfixable bug (fixing it would break lots of existing
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmCPAN modules) in the typemap file, the reference count of the C<AV *>
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmis not properly decremented. Thus, the above XSUB would leak memory
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmwhenever it is being called. The same problem exists for C<HV *>.
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmWhen you're returning an C<AV *> or a C<HV *>, you have make sure
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmtheir reference count is decremented by making the AV or HV mortal:
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm RETVAL = newAV();
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm sv_2mortal((SV*)RETVAL);
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm /* do something with RETVAL */
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmAnd also remember that you don't have to do this for an C<SV *>.
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm=head2 The MODULE Keyword
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmThe MODULE keyword is used to start the XS code and to specify the package
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmof the functions which are being defined. All text preceding the first
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmMODULE keyword is considered C code and is passed through to the output with
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmPOD stripped, but otherwise untouched. Every XS module will have a
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmbootstrap function which is used to hook the XSUBs into Perl. The package
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmname of this bootstrap function will match the value of the last MODULE
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmstatement in the XS source files. The value of MODULE should always remain
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmconstant within the same XS file, though this is not required.
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmThe following example will start the XS code and will place
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmall functions in a package named RPC.
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm MODULE = RPC
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm=head2 The PACKAGE Keyword
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmWhen functions within an XS source file must be separated into packages
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmthe PACKAGE keyword should be used. This keyword is used with the MODULE
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmkeyword and must follow immediately after it when used.
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm MODULE = RPC PACKAGE = RPC
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm [ XS code in package RPC ]
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm MODULE = RPC PACKAGE = RPCB
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm [ XS code in package RPCB ]
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm MODULE = RPC PACKAGE = RPC
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm [ XS code in package RPC ]
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmThe same package name can be used more than once, allowing for
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmnon-contiguous code. This is useful if you have a stronger ordering
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmprinciple than package names.
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmAlthough this keyword is optional and in some cases provides redundant
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gminformation it should always be used. This keyword will ensure that the
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmXSUBs appear in the desired package.
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm=head2 The PREFIX Keyword
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmThe PREFIX keyword designates prefixes which should be
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmremoved from the Perl function names. If the C function is
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmC<rpcb_gettime()> and the PREFIX value is C<rpcb_> then Perl will
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmsee this function as C<gettime()>.
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmThis keyword should follow the PACKAGE keyword when used.
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmIf PACKAGE is not used then PREFIX should follow the MODULE
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm MODULE = RPC PREFIX = rpc_
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm MODULE = RPC PACKAGE = RPCB PREFIX = rpcb_
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm=head2 The OUTPUT: Keyword
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmThe OUTPUT: keyword indicates that certain function parameters should be
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmupdated (new values made visible to Perl) when the XSUB terminates or that
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmcertain values should be returned to the calling Perl function. For
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmsimple functions which have no CODE: or PPCODE: section,
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmsuch as the sin() function above, the RETVAL variable is
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmautomatically designated as an output value. For more complex functions
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmthe B<xsubpp> compiler will need help to determine which variables are output
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmThis keyword will normally be used to complement the CODE: keyword.
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmThe RETVAL variable is not recognized as an output variable when the
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmCODE: keyword is present. The OUTPUT: keyword is used in this
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmsituation to tell the compiler that RETVAL really is an output
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmThe OUTPUT: keyword can also be used to indicate that function parameters
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmare output variables. This may be necessary when a parameter has been
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmmodified within the function and the programmer would like the update to
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmbe seen by Perl.
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm rpcb_gettime(host,timep)
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm char *host
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm time_t &timep
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmThe OUTPUT: keyword will also allow an output parameter to
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmbe mapped to a matching piece of code rather than to a
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm rpcb_gettime(host,timep)
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm char *host
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm time_t &timep
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm timep sv_setnv(ST(1), (double)timep);
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmB<xsubpp> emits an automatic C<SvSETMAGIC()> for all parameters in the
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmOUTPUT section of the XSUB, except RETVAL. This is the usually desired
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmbehavior, as it takes care of properly invoking 'set' magic on output
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmparameters (needed for hash or array element parameters that must be
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmcreated if they didn't exist). If for some reason, this behavior is
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmnot desired, the OUTPUT section may contain a C<SETMAGIC: DISABLE> line
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmto disable it for the remainder of the parameters in the OUTPUT section.
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmLikewise, C<SETMAGIC: ENABLE> can be used to reenable it for the
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmremainder of the OUTPUT section. See L<perlguts> for more details
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmabout 'set' magic.
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm=head2 The NO_OUTPUT Keyword
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmThe NO_OUTPUT can be placed as the first token of the XSUB. This keyword
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmindicates that while the C subroutine we provide an interface to has
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gma non-C<void> return type, the return value of this C subroutine should not
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmbe returned from the generated Perl subroutine.
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmWith this keyword present L<The RETVAL Variable> is created, and in the
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmgenerated call to the subroutine this variable is assigned to, but the value
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmof this variable is not going to be used in the auto-generated code.
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmThis keyword makes sense only if C<RETVAL> is going to be accessed by the
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmuser-supplied code. It is especially useful to make a function interface
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmmore Perl-like, especially when the C return value is just an error condition
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmindicator. For example,
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm NO_OUTPUT int
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm delete_file(char *name)
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm if (RETVAL != 0)
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm croak("Error %d while deleting file '%s'", RETVAL, name);
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmHere the generated XS function returns nothing on success, and will die()
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmwith a meaningful error message on error.
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm=head2 The CODE: Keyword
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmThis keyword is used in more complicated XSUBs which require
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmspecial handling for the C function. The RETVAL variable is
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmstill declared, but it will not be returned unless it is specified
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmin the OUTPUT: section.
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmThe following XSUB is for a C function which requires special handling of
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmits parameters. The Perl usage is given first.
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm $status = rpcb_gettime( "localhost", $timep );
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmThe XSUB follows.
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm rpcb_gettime(host,timep)
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm char *host
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm time_t timep
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm RETVAL = rpcb_gettime( host, &timep );
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm=head2 The INIT: Keyword
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmThe INIT: keyword allows initialization to be inserted into the XSUB before
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmthe compiler generates the call to the C function. Unlike the CODE: keyword
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmabove, this keyword does not affect the way the compiler handles RETVAL.
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm rpcb_gettime(host,timep)
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm char *host
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm time_t &timep
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm printf("# Host is %s\n", host );
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmAnother use for the INIT: section is to check for preconditions before
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmmaking a call to the C function:
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm lldiv(a,b)
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm long long a
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm long long b
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm if (a == 0 && b == 0)
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm XSRETURN_UNDEF;
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm if (b == 0)
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm croak("lldiv: cannot divide by 0");
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm=head2 The NO_INIT Keyword
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmThe NO_INIT keyword is used to indicate that a function
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmparameter is being used only as an output value. The B<xsubpp>
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmcompiler will normally generate code to read the values of
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmall function parameters from the argument stack and assign
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmthem to C variables upon entry to the function. NO_INIT
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmwill tell the compiler that some parameters will be used for
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmoutput rather than for input and that they will be handled
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmbefore the function terminates.
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmThe following example shows a variation of the rpcb_gettime() function.
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmThis function uses the timep variable only as an output variable and does
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmnot care about its initial contents.
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm rpcb_gettime(host,timep)
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm char *host
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm time_t &timep = NO_INIT
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm=head2 Initializing Function Parameters
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmC function parameters are normally initialized with their values from
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmthe argument stack (which in turn contains the parameters that were
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmpassed to the XSUB from Perl). The typemaps contain the
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmcode segments which are used to translate the Perl values to
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmthe C parameters. The programmer, however, is allowed to
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmoverride the typemaps and supply alternate (or additional)
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gminitialization code. Initialization code starts with the first
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmC<=>, C<;> or C<+> on a line in the INPUT: section. The only
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmexception happens if this C<;> terminates the line, then this C<;>
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmis quietly ignored.
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmThe following code demonstrates how to supply initialization code for
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmfunction parameters. The initialization code is eval'd within double
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmquotes by the compiler before it is added to the output so anything
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmwhich should be interpreted literally [mainly C<$>, C<@>, or C<\\>]
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmmust be protected with backslashes. The variables $var, $arg,
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmand $type can be used as in typemaps.
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm rpcb_gettime(host,timep)
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm char *host = (char *)SvPV($arg,PL_na);
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gm time_t &timep = 0;
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmThis should not be used to supply default values for parameters. One
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmwould normally use this when a function parameter must be processed by
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmanother library function before it can be used. Default parameters are
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmcovered in the next section.
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmIf the initialization begins with C<=>, then it is output in
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmthe declaration for the input variable, replacing the initialization
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmsupplied by the typemap. If the initialization
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmbegins with C<;> or C<+>, then it is performed after
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmall of the input variables have been declared. In the C<;>
88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98gmcase the initialization normally supplied by the typemap is not performed.
=head2 The IN/OUTLIST/IN_OUTLIST/OUT/IN_OUT Keywords
the generated Perl function is called. E.g.,
current XSUB. Consult L<perlsub/Prototypes> for information about Perl
overload) should be entered as \"\" (i.e. escaped).
L<overload/Fallback> for more details.
this keyword should give the name of macros which would extract/set a
and C<XSANY.any_dptr> for this C<CV*>. The setter macro is given cv,
The file F<Rpcb1.xsh> contains our C<rpcb_gettime()> function:
INCLUDE: Rpcb1.xsh
INCLUDE: cat Rpcb1.xsh |
that it should convert a Perl value to/from C using the C type to the left
You could also write a single get/set method using an optional argument:
Identify the C functions with input/output or output parameters. The XSUBs for
values. Some pointers may be used to implement input/output or
The default typemap in the C<lib/ExtUtils> directory of the Perl source
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
MY_CXT.count = 0;
strcpy(MY_CXT.name[0], "None");
strcpy(MY_CXT.name[1], "None");
strcpy(MY_CXT.name[2], "None");
if (MY_CXT.count >= 3) {
RETVAL = ++ MY_CXT.count;
RETVAL = MY_CXT.lives ++;
if (index > MY_CXT.count)
RETVAL = newSVpv(MY_CXT.name[index - 1]);
MY_CXT.index = 2;
File C<RPC.xs>: Interface to some ONC+ RPC bind library functions.
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include <rpc/rpc.h>
File C<typemap>: Custom typemap for RPC.xs.
File C<RPC.pm>: Perl module for the RPC extension.
File C<rpctest.pl>: Perl test program for the RPC extension.