|
Many of the examples which follow will concentrate on creating an interface between Perl
and the ONC+ RPC bind library functions. The rpcb_gettime() function is used to demonstrate
many features of the XS language. This function has two parameters; the first is an input
parameter and the second is an output parameter. The function also returns a status value.
bool_t rpcb_gettime(const char *host, time_t *timep);
|
|
From C this function will be called with the following statements.
#include <rpc/rpc.h>
bool_t status;
time_t timep;
status = rpcb_gettime( "localhost", &timep );
|
|
If an XSUB is created to offer a direct translation between this function and Perl, then
this XSUB will be used from Perl with the following code. The $status and $timep variables
will contain the output of the function.
use RPC;
$status = rpcb_gettime( "localhost", $timep );
|
|
The following XS file shows an XS subroutine, or XSUB, which demonstrates one possible
interface to the rpcb_gettime() function. This XSUB represents a direct translation between C
and Perl and so preserves the interface even from Perl. This XSUB will be invoked from Perl
with the usage shown above. Note that the first three #include statements, for EXTERN.h,
perl.h, and XSUB.h, will always be present at the beginning of an XS
file. This approach and others will be expanded later in this document.
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include <rpc/rpc.h>
MODULE = RPC PACKAGE = RPC
bool_t
rpcb_gettime(host,timep)
char *host
time_t &timep
OUTPUT:
timep
|
|
Any extension to Perl, including those containing XSUBs, should have a Perl module to serve
as the bootstrap which pulls the extension into Perl. This module will export the extension's
functions and variables to the Perl program and will cause the extension's XSUBs to be linked
into Perl. The following module will be used for most of the examples in this document and
should be used from Perl with the use command as shown earlier. Perl modules are
explained in more detail later in this document.
package RPC;
require Exporter;
require DynaLoader;
@ISA = qw(Exporter DynaLoader);
@EXPORT = qw( rpcb_gettime );
bootstrap RPC;
1;
|
|
Throughout this document a variety of interfaces to the rpcb_gettime() XSUB will be
explored. The XSUBs will take their parameters in different orders or will take different
numbers of parameters. In each case the XSUB is an abstraction between Perl and the real C
rpcb_gettime() function, and the XSUB must always ensure that the real rpcb_gettime() function
is called with the correct parameters. This abstraction will allow the programmer to create a
more Perl-like interface to the C function.
|
|