1*0Sstevel@tonic-gate=head1 NAME 2*0Sstevel@tonic-gate 3*0Sstevel@tonic-gateperlxs - XS language reference manual 4*0Sstevel@tonic-gate 5*0Sstevel@tonic-gate=head1 DESCRIPTION 6*0Sstevel@tonic-gate 7*0Sstevel@tonic-gate=head2 Introduction 8*0Sstevel@tonic-gate 9*0Sstevel@tonic-gateXS is an interface description file format used to create an extension 10*0Sstevel@tonic-gateinterface between Perl and C code (or a C library) which one wishes 11*0Sstevel@tonic-gateto use with Perl. The XS interface is combined with the library to 12*0Sstevel@tonic-gatecreate a new library which can then be either dynamically loaded 13*0Sstevel@tonic-gateor statically linked into perl. The XS interface description is 14*0Sstevel@tonic-gatewritten in the XS language and is the core component of the Perl 15*0Sstevel@tonic-gateextension interface. 16*0Sstevel@tonic-gate 17*0Sstevel@tonic-gateAn B<XSUB> forms the basic unit of the XS interface. After compilation 18*0Sstevel@tonic-gateby the B<xsubpp> compiler, each XSUB amounts to a C function definition 19*0Sstevel@tonic-gatewhich will provide the glue between Perl calling conventions and C 20*0Sstevel@tonic-gatecalling conventions. 21*0Sstevel@tonic-gate 22*0Sstevel@tonic-gateThe glue code pulls the arguments from the Perl stack, converts these 23*0Sstevel@tonic-gatePerl values to the formats expected by a C function, call this C function, 24*0Sstevel@tonic-gatetransfers the return values of the C function back to Perl. 25*0Sstevel@tonic-gateReturn values here may be a conventional C return value or any C 26*0Sstevel@tonic-gatefunction arguments that may serve as output parameters. These return 27*0Sstevel@tonic-gatevalues may be passed back to Perl either by putting them on the 28*0Sstevel@tonic-gatePerl stack, or by modifying the arguments supplied from the Perl side. 29*0Sstevel@tonic-gate 30*0Sstevel@tonic-gateThe above is a somewhat simplified view of what really happens. Since 31*0Sstevel@tonic-gatePerl allows more flexible calling conventions than C, XSUBs may do much 32*0Sstevel@tonic-gatemore in practice, such as checking input parameters for validity, 33*0Sstevel@tonic-gatethrowing exceptions (or returning undef/empty list) if the return value 34*0Sstevel@tonic-gatefrom the C function indicates failure, calling different C functions 35*0Sstevel@tonic-gatebased on numbers and types of the arguments, providing an object-oriented 36*0Sstevel@tonic-gateinterface, etc. 37*0Sstevel@tonic-gate 38*0Sstevel@tonic-gateOf course, one could write such glue code directly in C. However, this 39*0Sstevel@tonic-gatewould be a tedious task, especially if one needs to write glue for 40*0Sstevel@tonic-gatemultiple C functions, and/or one is not familiar enough with the Perl 41*0Sstevel@tonic-gatestack discipline and other such arcana. XS comes to the rescue here: 42*0Sstevel@tonic-gateinstead of writing this glue C code in long-hand, one can write 43*0Sstevel@tonic-gatea more concise short-hand I<description> of what should be done by 44*0Sstevel@tonic-gatethe glue, and let the XS compiler B<xsubpp> handle the rest. 45*0Sstevel@tonic-gate 46*0Sstevel@tonic-gateThe XS language allows one to describe the mapping between how the C 47*0Sstevel@tonic-gateroutine is used, and how the corresponding Perl routine is used. It 48*0Sstevel@tonic-gatealso allows creation of Perl routines which are directly translated to 49*0Sstevel@tonic-gateC code and which are not related to a pre-existing C function. In cases 50*0Sstevel@tonic-gatewhen the C interface coincides with the Perl interface, the XSUB 51*0Sstevel@tonic-gatedeclaration is almost identical to a declaration of a C function (in K&R 52*0Sstevel@tonic-gatestyle). In such circumstances, there is another tool called C<h2xs> 53*0Sstevel@tonic-gatethat is able to translate an entire C header file into a corresponding 54*0Sstevel@tonic-gateXS file that will provide glue to the functions/macros described in 55*0Sstevel@tonic-gatethe header file. 56*0Sstevel@tonic-gate 57*0Sstevel@tonic-gateThe XS compiler is called B<xsubpp>. This compiler creates 58*0Sstevel@tonic-gatethe constructs necessary to let an XSUB manipulate Perl values, and 59*0Sstevel@tonic-gatecreates the glue necessary to let Perl call the XSUB. The compiler 60*0Sstevel@tonic-gateuses B<typemaps> to determine how to map C function parameters 61*0Sstevel@tonic-gateand output values to Perl values and back. The default typemap 62*0Sstevel@tonic-gate(which comes with Perl) handles many common C types. A supplementary 63*0Sstevel@tonic-gatetypemap may also be needed to handle any special structures and types 64*0Sstevel@tonic-gatefor the library being linked. 65*0Sstevel@tonic-gate 66*0Sstevel@tonic-gateA file in XS format starts with a C language section which goes until the 67*0Sstevel@tonic-gatefirst C<MODULE =Z<>> directive. Other XS directives and XSUB definitions 68*0Sstevel@tonic-gatemay follow this line. The "language" used in this part of the file 69*0Sstevel@tonic-gateis usually referred to as the XS language. B<xsubpp> recognizes and 70*0Sstevel@tonic-gateskips POD (see L<perlpod>) in both the C and XS language sections, which 71*0Sstevel@tonic-gateallows the XS file to contain embedded documentation. 72*0Sstevel@tonic-gate 73*0Sstevel@tonic-gateSee L<perlxstut> for a tutorial on the whole extension creation process. 74*0Sstevel@tonic-gate 75*0Sstevel@tonic-gateNote: For some extensions, Dave Beazley's SWIG system may provide a 76*0Sstevel@tonic-gatesignificantly more convenient mechanism for creating the extension 77*0Sstevel@tonic-gateglue code. See http://www.swig.org/ for more information. 78*0Sstevel@tonic-gate 79*0Sstevel@tonic-gate=head2 On The Road 80*0Sstevel@tonic-gate 81*0Sstevel@tonic-gateMany of the examples which follow will concentrate on creating an interface 82*0Sstevel@tonic-gatebetween Perl and the ONC+ RPC bind library functions. The rpcb_gettime() 83*0Sstevel@tonic-gatefunction is used to demonstrate many features of the XS language. This 84*0Sstevel@tonic-gatefunction has two parameters; the first is an input parameter and the second 85*0Sstevel@tonic-gateis an output parameter. The function also returns a status value. 86*0Sstevel@tonic-gate 87*0Sstevel@tonic-gate bool_t rpcb_gettime(const char *host, time_t *timep); 88*0Sstevel@tonic-gate 89*0Sstevel@tonic-gateFrom C this function will be called with the following 90*0Sstevel@tonic-gatestatements. 91*0Sstevel@tonic-gate 92*0Sstevel@tonic-gate #include <rpc/rpc.h> 93*0Sstevel@tonic-gate bool_t status; 94*0Sstevel@tonic-gate time_t timep; 95*0Sstevel@tonic-gate status = rpcb_gettime( "localhost", &timep ); 96*0Sstevel@tonic-gate 97*0Sstevel@tonic-gateIf an XSUB is created to offer a direct translation between this function 98*0Sstevel@tonic-gateand Perl, then this XSUB will be used from Perl with the following code. 99*0Sstevel@tonic-gateThe $status and $timep variables will contain the output of the function. 100*0Sstevel@tonic-gate 101*0Sstevel@tonic-gate use RPC; 102*0Sstevel@tonic-gate $status = rpcb_gettime( "localhost", $timep ); 103*0Sstevel@tonic-gate 104*0Sstevel@tonic-gateThe following XS file shows an XS subroutine, or XSUB, which 105*0Sstevel@tonic-gatedemonstrates one possible interface to the rpcb_gettime() 106*0Sstevel@tonic-gatefunction. This XSUB represents a direct translation between 107*0Sstevel@tonic-gateC and Perl and so preserves the interface even from Perl. 108*0Sstevel@tonic-gateThis XSUB will be invoked from Perl with the usage shown 109*0Sstevel@tonic-gateabove. Note that the first three #include statements, for 110*0Sstevel@tonic-gateC<EXTERN.h>, C<perl.h>, and C<XSUB.h>, will always be present at the 111*0Sstevel@tonic-gatebeginning of an XS file. This approach and others will be 112*0Sstevel@tonic-gateexpanded later in this document. 113*0Sstevel@tonic-gate 114*0Sstevel@tonic-gate #include "EXTERN.h" 115*0Sstevel@tonic-gate #include "perl.h" 116*0Sstevel@tonic-gate #include "XSUB.h" 117*0Sstevel@tonic-gate #include <rpc/rpc.h> 118*0Sstevel@tonic-gate 119*0Sstevel@tonic-gate MODULE = RPC PACKAGE = RPC 120*0Sstevel@tonic-gate 121*0Sstevel@tonic-gate bool_t 122*0Sstevel@tonic-gate rpcb_gettime(host,timep) 123*0Sstevel@tonic-gate char *host 124*0Sstevel@tonic-gate time_t &timep 125*0Sstevel@tonic-gate OUTPUT: 126*0Sstevel@tonic-gate timep 127*0Sstevel@tonic-gate 128*0Sstevel@tonic-gateAny extension to Perl, including those containing XSUBs, 129*0Sstevel@tonic-gateshould have a Perl module to serve as the bootstrap which 130*0Sstevel@tonic-gatepulls the extension into Perl. This module will export the 131*0Sstevel@tonic-gateextension's functions and variables to the Perl program and 132*0Sstevel@tonic-gatewill cause the extension's XSUBs to be linked into Perl. 133*0Sstevel@tonic-gateThe following module will be used for most of the examples 134*0Sstevel@tonic-gatein this document and should be used from Perl with the C<use> 135*0Sstevel@tonic-gatecommand as shown earlier. Perl modules are explained in 136*0Sstevel@tonic-gatemore detail later in this document. 137*0Sstevel@tonic-gate 138*0Sstevel@tonic-gate package RPC; 139*0Sstevel@tonic-gate 140*0Sstevel@tonic-gate require Exporter; 141*0Sstevel@tonic-gate require DynaLoader; 142*0Sstevel@tonic-gate @ISA = qw(Exporter DynaLoader); 143*0Sstevel@tonic-gate @EXPORT = qw( rpcb_gettime ); 144*0Sstevel@tonic-gate 145*0Sstevel@tonic-gate bootstrap RPC; 146*0Sstevel@tonic-gate 1; 147*0Sstevel@tonic-gate 148*0Sstevel@tonic-gateThroughout this document a variety of interfaces to the rpcb_gettime() 149*0Sstevel@tonic-gateXSUB will be explored. The XSUBs will take their parameters in different 150*0Sstevel@tonic-gateorders or will take different numbers of parameters. In each case the 151*0Sstevel@tonic-gateXSUB is an abstraction between Perl and the real C rpcb_gettime() 152*0Sstevel@tonic-gatefunction, and the XSUB must always ensure that the real rpcb_gettime() 153*0Sstevel@tonic-gatefunction is called with the correct parameters. This abstraction will 154*0Sstevel@tonic-gateallow the programmer to create a more Perl-like interface to the C 155*0Sstevel@tonic-gatefunction. 156*0Sstevel@tonic-gate 157*0Sstevel@tonic-gate=head2 The Anatomy of an XSUB 158*0Sstevel@tonic-gate 159*0Sstevel@tonic-gateThe simplest XSUBs consist of 3 parts: a description of the return 160*0Sstevel@tonic-gatevalue, the name of the XSUB routine and the names of its arguments, 161*0Sstevel@tonic-gateand a description of types or formats of the arguments. 162*0Sstevel@tonic-gate 163*0Sstevel@tonic-gateThe following XSUB allows a Perl program to access a C library function 164*0Sstevel@tonic-gatecalled sin(). The XSUB will imitate the C function which takes a single 165*0Sstevel@tonic-gateargument and returns a single value. 166*0Sstevel@tonic-gate 167*0Sstevel@tonic-gate double 168*0Sstevel@tonic-gate sin(x) 169*0Sstevel@tonic-gate double x 170*0Sstevel@tonic-gate 171*0Sstevel@tonic-gateOptionally, one can merge the description of types and the list of 172*0Sstevel@tonic-gateargument names, rewriting this as 173*0Sstevel@tonic-gate 174*0Sstevel@tonic-gate double 175*0Sstevel@tonic-gate sin(double x) 176*0Sstevel@tonic-gate 177*0Sstevel@tonic-gateThis makes this XSUB look similar to an ANSI C declaration. An optional 178*0Sstevel@tonic-gatesemicolon is allowed after the argument list, as in 179*0Sstevel@tonic-gate 180*0Sstevel@tonic-gate double 181*0Sstevel@tonic-gate sin(double x); 182*0Sstevel@tonic-gate 183*0Sstevel@tonic-gateParameters with C pointer types can have different semantic: C functions 184*0Sstevel@tonic-gatewith similar declarations 185*0Sstevel@tonic-gate 186*0Sstevel@tonic-gate bool string_looks_as_a_number(char *s); 187*0Sstevel@tonic-gate bool make_char_uppercase(char *c); 188*0Sstevel@tonic-gate 189*0Sstevel@tonic-gateare used in absolutely incompatible manner. Parameters to these functions 190*0Sstevel@tonic-gatecould be described B<xsubpp> like this: 191*0Sstevel@tonic-gate 192*0Sstevel@tonic-gate char * s 193*0Sstevel@tonic-gate char &c 194*0Sstevel@tonic-gate 195*0Sstevel@tonic-gateBoth these XS declarations correspond to the C<char*> C type, but they have 196*0Sstevel@tonic-gatedifferent semantics, see L<"The & Unary Operator">. 197*0Sstevel@tonic-gate 198*0Sstevel@tonic-gateIt is convenient to think that the indirection operator 199*0Sstevel@tonic-gateC<*> should be considered as a part of the type and the address operator C<&> 200*0Sstevel@tonic-gateshould be considered part of the variable. See L<"The Typemap"> 201*0Sstevel@tonic-gatefor more info about handling qualifiers and unary operators in C types. 202*0Sstevel@tonic-gate 203*0Sstevel@tonic-gateThe function name and the return type must be placed on 204*0Sstevel@tonic-gateseparate lines and should be flush left-adjusted. 205*0Sstevel@tonic-gate 206*0Sstevel@tonic-gate INCORRECT CORRECT 207*0Sstevel@tonic-gate 208*0Sstevel@tonic-gate double sin(x) double 209*0Sstevel@tonic-gate double x sin(x) 210*0Sstevel@tonic-gate double x 211*0Sstevel@tonic-gate 212*0Sstevel@tonic-gateThe rest of the function description may be indented or left-adjusted. The 213*0Sstevel@tonic-gatefollowing example shows a function with its body left-adjusted. Most 214*0Sstevel@tonic-gateexamples in this document will indent the body for better readability. 215*0Sstevel@tonic-gate 216*0Sstevel@tonic-gate CORRECT 217*0Sstevel@tonic-gate 218*0Sstevel@tonic-gate double 219*0Sstevel@tonic-gate sin(x) 220*0Sstevel@tonic-gate double x 221*0Sstevel@tonic-gate 222*0Sstevel@tonic-gateMore complicated XSUBs may contain many other sections. Each section of 223*0Sstevel@tonic-gatean XSUB starts with the corresponding keyword, such as INIT: or CLEANUP:. 224*0Sstevel@tonic-gateHowever, the first two lines of an XSUB always contain the same data: 225*0Sstevel@tonic-gatedescriptions of the return type and the names of the function and its 226*0Sstevel@tonic-gateparameters. Whatever immediately follows these is considered to be 227*0Sstevel@tonic-gatean INPUT: section unless explicitly marked with another keyword. 228*0Sstevel@tonic-gate(See L<The INPUT: Keyword>.) 229*0Sstevel@tonic-gate 230*0Sstevel@tonic-gateAn XSUB section continues until another section-start keyword is found. 231*0Sstevel@tonic-gate 232*0Sstevel@tonic-gate=head2 The Argument Stack 233*0Sstevel@tonic-gate 234*0Sstevel@tonic-gateThe Perl argument stack is used to store the values which are 235*0Sstevel@tonic-gatesent as parameters to the XSUB and to store the XSUB's 236*0Sstevel@tonic-gatereturn value(s). In reality all Perl functions (including non-XSUB 237*0Sstevel@tonic-gateones) keep their values on this stack all the same time, each limited 238*0Sstevel@tonic-gateto its own range of positions on the stack. In this document the 239*0Sstevel@tonic-gatefirst position on that stack which belongs to the active 240*0Sstevel@tonic-gatefunction will be referred to as position 0 for that function. 241*0Sstevel@tonic-gate 242*0Sstevel@tonic-gateXSUBs refer to their stack arguments with the macro B<ST(x)>, where I<x> 243*0Sstevel@tonic-gaterefers to a position in this XSUB's part of the stack. Position 0 for that 244*0Sstevel@tonic-gatefunction would be known to the XSUB as ST(0). The XSUB's incoming 245*0Sstevel@tonic-gateparameters and outgoing return values always begin at ST(0). For many 246*0Sstevel@tonic-gatesimple cases the B<xsubpp> compiler will generate the code necessary to 247*0Sstevel@tonic-gatehandle the argument stack by embedding code fragments found in the 248*0Sstevel@tonic-gatetypemaps. In more complex cases the programmer must supply the code. 249*0Sstevel@tonic-gate 250*0Sstevel@tonic-gate=head2 The RETVAL Variable 251*0Sstevel@tonic-gate 252*0Sstevel@tonic-gateThe RETVAL variable is a special C variable that is declared automatically 253*0Sstevel@tonic-gatefor you. The C type of RETVAL matches the return type of the C library 254*0Sstevel@tonic-gatefunction. The B<xsubpp> compiler will declare this variable in each XSUB 255*0Sstevel@tonic-gatewith non-C<void> return type. By default the generated C function 256*0Sstevel@tonic-gatewill use RETVAL to hold the return value of the C library function being 257*0Sstevel@tonic-gatecalled. In simple cases the value of RETVAL will be placed in ST(0) of 258*0Sstevel@tonic-gatethe argument stack where it can be received by Perl as the return value 259*0Sstevel@tonic-gateof the XSUB. 260*0Sstevel@tonic-gate 261*0Sstevel@tonic-gateIf the XSUB has a return type of C<void> then the compiler will 262*0Sstevel@tonic-gatenot declare a RETVAL variable for that function. When using 263*0Sstevel@tonic-gatea PPCODE: section no manipulation of the RETVAL variable is required, the 264*0Sstevel@tonic-gatesection may use direct stack manipulation to place output values on the stack. 265*0Sstevel@tonic-gate 266*0Sstevel@tonic-gateIf PPCODE: directive is not used, C<void> return value should be used 267*0Sstevel@tonic-gateonly for subroutines which do not return a value, I<even if> CODE: 268*0Sstevel@tonic-gatedirective is used which sets ST(0) explicitly. 269*0Sstevel@tonic-gate 270*0Sstevel@tonic-gateOlder versions of this document recommended to use C<void> return 271*0Sstevel@tonic-gatevalue in such cases. It was discovered that this could lead to 272*0Sstevel@tonic-gatesegfaults in cases when XSUB was I<truly> C<void>. This practice is 273*0Sstevel@tonic-gatenow deprecated, and may be not supported at some future version. Use 274*0Sstevel@tonic-gatethe return value C<SV *> in such cases. (Currently C<xsubpp> contains 275*0Sstevel@tonic-gatesome heuristic code which tries to disambiguate between "truly-void" 276*0Sstevel@tonic-gateand "old-practice-declared-as-void" functions. Hence your code is at 277*0Sstevel@tonic-gatemercy of this heuristics unless you use C<SV *> as return value.) 278*0Sstevel@tonic-gate 279*0Sstevel@tonic-gate=head2 Returning SVs, AVs and HVs through RETVAL 280*0Sstevel@tonic-gate 281*0Sstevel@tonic-gateWhen you're using RETVAL to return an C<SV *>, there's some magic 282*0Sstevel@tonic-gategoing on behind the scenes that should be mentioned. When you're 283*0Sstevel@tonic-gatemanipulating the argument stack using the ST(x) macro, for example, 284*0Sstevel@tonic-gateyou usually have to pay special attention to reference counts. (For 285*0Sstevel@tonic-gatemore about reference counts, see L<perlguts>.) To make your life 286*0Sstevel@tonic-gateeasier, the typemap file automatically makes C<RETVAL> mortal when 287*0Sstevel@tonic-gateyou're returning an C<SV *>. Thus, the following two XSUBs are more 288*0Sstevel@tonic-gateor less equivalent: 289*0Sstevel@tonic-gate 290*0Sstevel@tonic-gate void 291*0Sstevel@tonic-gate alpha() 292*0Sstevel@tonic-gate PPCODE: 293*0Sstevel@tonic-gate ST(0) = newSVpv("Hello World",0); 294*0Sstevel@tonic-gate sv_2mortal(ST(0)); 295*0Sstevel@tonic-gate XSRETURN(1); 296*0Sstevel@tonic-gate 297*0Sstevel@tonic-gate SV * 298*0Sstevel@tonic-gate beta() 299*0Sstevel@tonic-gate CODE: 300*0Sstevel@tonic-gate RETVAL = newSVpv("Hello World",0); 301*0Sstevel@tonic-gate OUTPUT: 302*0Sstevel@tonic-gate RETVAL 303*0Sstevel@tonic-gate 304*0Sstevel@tonic-gateThis is quite useful as it usually improves readability. While 305*0Sstevel@tonic-gatethis works fine for an C<SV *>, it's unfortunately not as easy 306*0Sstevel@tonic-gateto have C<AV *> or C<HV *> as a return value. You I<should> be 307*0Sstevel@tonic-gateable to write: 308*0Sstevel@tonic-gate 309*0Sstevel@tonic-gate AV * 310*0Sstevel@tonic-gate array() 311*0Sstevel@tonic-gate CODE: 312*0Sstevel@tonic-gate RETVAL = newAV(); 313*0Sstevel@tonic-gate /* do something with RETVAL */ 314*0Sstevel@tonic-gate OUTPUT: 315*0Sstevel@tonic-gate RETVAL 316*0Sstevel@tonic-gate 317*0Sstevel@tonic-gateBut due to an unfixable bug (fixing it would break lots of existing 318*0Sstevel@tonic-gateCPAN modules) in the typemap file, the reference count of the C<AV *> 319*0Sstevel@tonic-gateis not properly decremented. Thus, the above XSUB would leak memory 320*0Sstevel@tonic-gatewhenever it is being called. The same problem exists for C<HV *>. 321*0Sstevel@tonic-gate 322*0Sstevel@tonic-gateWhen you're returning an C<AV *> or a C<HV *>, you have make sure 323*0Sstevel@tonic-gatetheir reference count is decremented by making the AV or HV mortal: 324*0Sstevel@tonic-gate 325*0Sstevel@tonic-gate AV * 326*0Sstevel@tonic-gate array() 327*0Sstevel@tonic-gate CODE: 328*0Sstevel@tonic-gate RETVAL = newAV(); 329*0Sstevel@tonic-gate sv_2mortal((SV*)RETVAL); 330*0Sstevel@tonic-gate /* do something with RETVAL */ 331*0Sstevel@tonic-gate OUTPUT: 332*0Sstevel@tonic-gate RETVAL 333*0Sstevel@tonic-gate 334*0Sstevel@tonic-gateAnd also remember that you don't have to do this for an C<SV *>. 335*0Sstevel@tonic-gate 336*0Sstevel@tonic-gate=head2 The MODULE Keyword 337*0Sstevel@tonic-gate 338*0Sstevel@tonic-gateThe MODULE keyword is used to start the XS code and to specify the package 339*0Sstevel@tonic-gateof the functions which are being defined. All text preceding the first 340*0Sstevel@tonic-gateMODULE keyword is considered C code and is passed through to the output with 341*0Sstevel@tonic-gatePOD stripped, but otherwise untouched. Every XS module will have a 342*0Sstevel@tonic-gatebootstrap function which is used to hook the XSUBs into Perl. The package 343*0Sstevel@tonic-gatename of this bootstrap function will match the value of the last MODULE 344*0Sstevel@tonic-gatestatement in the XS source files. The value of MODULE should always remain 345*0Sstevel@tonic-gateconstant within the same XS file, though this is not required. 346*0Sstevel@tonic-gate 347*0Sstevel@tonic-gateThe following example will start the XS code and will place 348*0Sstevel@tonic-gateall functions in a package named RPC. 349*0Sstevel@tonic-gate 350*0Sstevel@tonic-gate MODULE = RPC 351*0Sstevel@tonic-gate 352*0Sstevel@tonic-gate=head2 The PACKAGE Keyword 353*0Sstevel@tonic-gate 354*0Sstevel@tonic-gateWhen functions within an XS source file must be separated into packages 355*0Sstevel@tonic-gatethe PACKAGE keyword should be used. This keyword is used with the MODULE 356*0Sstevel@tonic-gatekeyword and must follow immediately after it when used. 357*0Sstevel@tonic-gate 358*0Sstevel@tonic-gate MODULE = RPC PACKAGE = RPC 359*0Sstevel@tonic-gate 360*0Sstevel@tonic-gate [ XS code in package RPC ] 361*0Sstevel@tonic-gate 362*0Sstevel@tonic-gate MODULE = RPC PACKAGE = RPCB 363*0Sstevel@tonic-gate 364*0Sstevel@tonic-gate [ XS code in package RPCB ] 365*0Sstevel@tonic-gate 366*0Sstevel@tonic-gate MODULE = RPC PACKAGE = RPC 367*0Sstevel@tonic-gate 368*0Sstevel@tonic-gate [ XS code in package RPC ] 369*0Sstevel@tonic-gate 370*0Sstevel@tonic-gateThe same package name can be used more than once, allowing for 371*0Sstevel@tonic-gatenon-contiguous code. This is useful if you have a stronger ordering 372*0Sstevel@tonic-gateprinciple than package names. 373*0Sstevel@tonic-gate 374*0Sstevel@tonic-gateAlthough this keyword is optional and in some cases provides redundant 375*0Sstevel@tonic-gateinformation it should always be used. This keyword will ensure that the 376*0Sstevel@tonic-gateXSUBs appear in the desired package. 377*0Sstevel@tonic-gate 378*0Sstevel@tonic-gate=head2 The PREFIX Keyword 379*0Sstevel@tonic-gate 380*0Sstevel@tonic-gateThe PREFIX keyword designates prefixes which should be 381*0Sstevel@tonic-gateremoved from the Perl function names. If the C function is 382*0Sstevel@tonic-gateC<rpcb_gettime()> and the PREFIX value is C<rpcb_> then Perl will 383*0Sstevel@tonic-gatesee this function as C<gettime()>. 384*0Sstevel@tonic-gate 385*0Sstevel@tonic-gateThis keyword should follow the PACKAGE keyword when used. 386*0Sstevel@tonic-gateIf PACKAGE is not used then PREFIX should follow the MODULE 387*0Sstevel@tonic-gatekeyword. 388*0Sstevel@tonic-gate 389*0Sstevel@tonic-gate MODULE = RPC PREFIX = rpc_ 390*0Sstevel@tonic-gate 391*0Sstevel@tonic-gate MODULE = RPC PACKAGE = RPCB PREFIX = rpcb_ 392*0Sstevel@tonic-gate 393*0Sstevel@tonic-gate=head2 The OUTPUT: Keyword 394*0Sstevel@tonic-gate 395*0Sstevel@tonic-gateThe OUTPUT: keyword indicates that certain function parameters should be 396*0Sstevel@tonic-gateupdated (new values made visible to Perl) when the XSUB terminates or that 397*0Sstevel@tonic-gatecertain values should be returned to the calling Perl function. For 398*0Sstevel@tonic-gatesimple functions which have no CODE: or PPCODE: section, 399*0Sstevel@tonic-gatesuch as the sin() function above, the RETVAL variable is 400*0Sstevel@tonic-gateautomatically designated as an output value. For more complex functions 401*0Sstevel@tonic-gatethe B<xsubpp> compiler will need help to determine which variables are output 402*0Sstevel@tonic-gatevariables. 403*0Sstevel@tonic-gate 404*0Sstevel@tonic-gateThis keyword will normally be used to complement the CODE: keyword. 405*0Sstevel@tonic-gateThe RETVAL variable is not recognized as an output variable when the 406*0Sstevel@tonic-gateCODE: keyword is present. The OUTPUT: keyword is used in this 407*0Sstevel@tonic-gatesituation to tell the compiler that RETVAL really is an output 408*0Sstevel@tonic-gatevariable. 409*0Sstevel@tonic-gate 410*0Sstevel@tonic-gateThe OUTPUT: keyword can also be used to indicate that function parameters 411*0Sstevel@tonic-gateare output variables. This may be necessary when a parameter has been 412*0Sstevel@tonic-gatemodified within the function and the programmer would like the update to 413*0Sstevel@tonic-gatebe seen by Perl. 414*0Sstevel@tonic-gate 415*0Sstevel@tonic-gate bool_t 416*0Sstevel@tonic-gate rpcb_gettime(host,timep) 417*0Sstevel@tonic-gate char *host 418*0Sstevel@tonic-gate time_t &timep 419*0Sstevel@tonic-gate OUTPUT: 420*0Sstevel@tonic-gate timep 421*0Sstevel@tonic-gate 422*0Sstevel@tonic-gateThe OUTPUT: keyword will also allow an output parameter to 423*0Sstevel@tonic-gatebe mapped to a matching piece of code rather than to a 424*0Sstevel@tonic-gatetypemap. 425*0Sstevel@tonic-gate 426*0Sstevel@tonic-gate bool_t 427*0Sstevel@tonic-gate rpcb_gettime(host,timep) 428*0Sstevel@tonic-gate char *host 429*0Sstevel@tonic-gate time_t &timep 430*0Sstevel@tonic-gate OUTPUT: 431*0Sstevel@tonic-gate timep sv_setnv(ST(1), (double)timep); 432*0Sstevel@tonic-gate 433*0Sstevel@tonic-gateB<xsubpp> emits an automatic C<SvSETMAGIC()> for all parameters in the 434*0Sstevel@tonic-gateOUTPUT section of the XSUB, except RETVAL. This is the usually desired 435*0Sstevel@tonic-gatebehavior, as it takes care of properly invoking 'set' magic on output 436*0Sstevel@tonic-gateparameters (needed for hash or array element parameters that must be 437*0Sstevel@tonic-gatecreated if they didn't exist). If for some reason, this behavior is 438*0Sstevel@tonic-gatenot desired, the OUTPUT section may contain a C<SETMAGIC: DISABLE> line 439*0Sstevel@tonic-gateto disable it for the remainder of the parameters in the OUTPUT section. 440*0Sstevel@tonic-gateLikewise, C<SETMAGIC: ENABLE> can be used to reenable it for the 441*0Sstevel@tonic-gateremainder of the OUTPUT section. See L<perlguts> for more details 442*0Sstevel@tonic-gateabout 'set' magic. 443*0Sstevel@tonic-gate 444*0Sstevel@tonic-gate=head2 The NO_OUTPUT Keyword 445*0Sstevel@tonic-gate 446*0Sstevel@tonic-gateThe NO_OUTPUT can be placed as the first token of the XSUB. This keyword 447*0Sstevel@tonic-gateindicates that while the C subroutine we provide an interface to has 448*0Sstevel@tonic-gatea non-C<void> return type, the return value of this C subroutine should not 449*0Sstevel@tonic-gatebe returned from the generated Perl subroutine. 450*0Sstevel@tonic-gate 451*0Sstevel@tonic-gateWith this keyword present L<The RETVAL Variable> is created, and in the 452*0Sstevel@tonic-gategenerated call to the subroutine this variable is assigned to, but the value 453*0Sstevel@tonic-gateof this variable is not going to be used in the auto-generated code. 454*0Sstevel@tonic-gate 455*0Sstevel@tonic-gateThis keyword makes sense only if C<RETVAL> is going to be accessed by the 456*0Sstevel@tonic-gateuser-supplied code. It is especially useful to make a function interface 457*0Sstevel@tonic-gatemore Perl-like, especially when the C return value is just an error condition 458*0Sstevel@tonic-gateindicator. For example, 459*0Sstevel@tonic-gate 460*0Sstevel@tonic-gate NO_OUTPUT int 461*0Sstevel@tonic-gate delete_file(char *name) 462*0Sstevel@tonic-gate POSTCALL: 463*0Sstevel@tonic-gate if (RETVAL != 0) 464*0Sstevel@tonic-gate croak("Error %d while deleting file '%s'", RETVAL, name); 465*0Sstevel@tonic-gate 466*0Sstevel@tonic-gateHere the generated XS function returns nothing on success, and will die() 467*0Sstevel@tonic-gatewith a meaningful error message on error. 468*0Sstevel@tonic-gate 469*0Sstevel@tonic-gate=head2 The CODE: Keyword 470*0Sstevel@tonic-gate 471*0Sstevel@tonic-gateThis keyword is used in more complicated XSUBs which require 472*0Sstevel@tonic-gatespecial handling for the C function. The RETVAL variable is 473*0Sstevel@tonic-gatestill declared, but it will not be returned unless it is specified 474*0Sstevel@tonic-gatein the OUTPUT: section. 475*0Sstevel@tonic-gate 476*0Sstevel@tonic-gateThe following XSUB is for a C function which requires special handling of 477*0Sstevel@tonic-gateits parameters. The Perl usage is given first. 478*0Sstevel@tonic-gate 479*0Sstevel@tonic-gate $status = rpcb_gettime( "localhost", $timep ); 480*0Sstevel@tonic-gate 481*0Sstevel@tonic-gateThe XSUB follows. 482*0Sstevel@tonic-gate 483*0Sstevel@tonic-gate bool_t 484*0Sstevel@tonic-gate rpcb_gettime(host,timep) 485*0Sstevel@tonic-gate char *host 486*0Sstevel@tonic-gate time_t timep 487*0Sstevel@tonic-gate CODE: 488*0Sstevel@tonic-gate RETVAL = rpcb_gettime( host, &timep ); 489*0Sstevel@tonic-gate OUTPUT: 490*0Sstevel@tonic-gate timep 491*0Sstevel@tonic-gate RETVAL 492*0Sstevel@tonic-gate 493*0Sstevel@tonic-gate=head2 The INIT: Keyword 494*0Sstevel@tonic-gate 495*0Sstevel@tonic-gateThe INIT: keyword allows initialization to be inserted into the XSUB before 496*0Sstevel@tonic-gatethe compiler generates the call to the C function. Unlike the CODE: keyword 497*0Sstevel@tonic-gateabove, this keyword does not affect the way the compiler handles RETVAL. 498*0Sstevel@tonic-gate 499*0Sstevel@tonic-gate bool_t 500*0Sstevel@tonic-gate rpcb_gettime(host,timep) 501*0Sstevel@tonic-gate char *host 502*0Sstevel@tonic-gate time_t &timep 503*0Sstevel@tonic-gate INIT: 504*0Sstevel@tonic-gate printf("# Host is %s\n", host ); 505*0Sstevel@tonic-gate OUTPUT: 506*0Sstevel@tonic-gate timep 507*0Sstevel@tonic-gate 508*0Sstevel@tonic-gateAnother use for the INIT: section is to check for preconditions before 509*0Sstevel@tonic-gatemaking a call to the C function: 510*0Sstevel@tonic-gate 511*0Sstevel@tonic-gate long long 512*0Sstevel@tonic-gate lldiv(a,b) 513*0Sstevel@tonic-gate long long a 514*0Sstevel@tonic-gate long long b 515*0Sstevel@tonic-gate INIT: 516*0Sstevel@tonic-gate if (a == 0 && b == 0) 517*0Sstevel@tonic-gate XSRETURN_UNDEF; 518*0Sstevel@tonic-gate if (b == 0) 519*0Sstevel@tonic-gate croak("lldiv: cannot divide by 0"); 520*0Sstevel@tonic-gate 521*0Sstevel@tonic-gate=head2 The NO_INIT Keyword 522*0Sstevel@tonic-gate 523*0Sstevel@tonic-gateThe NO_INIT keyword is used to indicate that a function 524*0Sstevel@tonic-gateparameter is being used only as an output value. The B<xsubpp> 525*0Sstevel@tonic-gatecompiler will normally generate code to read the values of 526*0Sstevel@tonic-gateall function parameters from the argument stack and assign 527*0Sstevel@tonic-gatethem to C variables upon entry to the function. NO_INIT 528*0Sstevel@tonic-gatewill tell the compiler that some parameters will be used for 529*0Sstevel@tonic-gateoutput rather than for input and that they will be handled 530*0Sstevel@tonic-gatebefore the function terminates. 531*0Sstevel@tonic-gate 532*0Sstevel@tonic-gateThe following example shows a variation of the rpcb_gettime() function. 533*0Sstevel@tonic-gateThis function uses the timep variable only as an output variable and does 534*0Sstevel@tonic-gatenot care about its initial contents. 535*0Sstevel@tonic-gate 536*0Sstevel@tonic-gate bool_t 537*0Sstevel@tonic-gate rpcb_gettime(host,timep) 538*0Sstevel@tonic-gate char *host 539*0Sstevel@tonic-gate time_t &timep = NO_INIT 540*0Sstevel@tonic-gate OUTPUT: 541*0Sstevel@tonic-gate timep 542*0Sstevel@tonic-gate 543*0Sstevel@tonic-gate=head2 Initializing Function Parameters 544*0Sstevel@tonic-gate 545*0Sstevel@tonic-gateC function parameters are normally initialized with their values from 546*0Sstevel@tonic-gatethe argument stack (which in turn contains the parameters that were 547*0Sstevel@tonic-gatepassed to the XSUB from Perl). The typemaps contain the 548*0Sstevel@tonic-gatecode segments which are used to translate the Perl values to 549*0Sstevel@tonic-gatethe C parameters. The programmer, however, is allowed to 550*0Sstevel@tonic-gateoverride the typemaps and supply alternate (or additional) 551*0Sstevel@tonic-gateinitialization code. Initialization code starts with the first 552*0Sstevel@tonic-gateC<=>, C<;> or C<+> on a line in the INPUT: section. The only 553*0Sstevel@tonic-gateexception happens if this C<;> terminates the line, then this C<;> 554*0Sstevel@tonic-gateis quietly ignored. 555*0Sstevel@tonic-gate 556*0Sstevel@tonic-gateThe following code demonstrates how to supply initialization code for 557*0Sstevel@tonic-gatefunction parameters. The initialization code is eval'd within double 558*0Sstevel@tonic-gatequotes by the compiler before it is added to the output so anything 559*0Sstevel@tonic-gatewhich should be interpreted literally [mainly C<$>, C<@>, or C<\\>] 560*0Sstevel@tonic-gatemust be protected with backslashes. The variables $var, $arg, 561*0Sstevel@tonic-gateand $type can be used as in typemaps. 562*0Sstevel@tonic-gate 563*0Sstevel@tonic-gate bool_t 564*0Sstevel@tonic-gate rpcb_gettime(host,timep) 565*0Sstevel@tonic-gate char *host = (char *)SvPV($arg,PL_na); 566*0Sstevel@tonic-gate time_t &timep = 0; 567*0Sstevel@tonic-gate OUTPUT: 568*0Sstevel@tonic-gate timep 569*0Sstevel@tonic-gate 570*0Sstevel@tonic-gateThis should not be used to supply default values for parameters. One 571*0Sstevel@tonic-gatewould normally use this when a function parameter must be processed by 572*0Sstevel@tonic-gateanother library function before it can be used. Default parameters are 573*0Sstevel@tonic-gatecovered in the next section. 574*0Sstevel@tonic-gate 575*0Sstevel@tonic-gateIf the initialization begins with C<=>, then it is output in 576*0Sstevel@tonic-gatethe declaration for the input variable, replacing the initialization 577*0Sstevel@tonic-gatesupplied by the typemap. If the initialization 578*0Sstevel@tonic-gatebegins with C<;> or C<+>, then it is performed after 579*0Sstevel@tonic-gateall of the input variables have been declared. In the C<;> 580*0Sstevel@tonic-gatecase the initialization normally supplied by the typemap is not performed. 581*0Sstevel@tonic-gateFor the C<+> case, the declaration for the variable will include the 582*0Sstevel@tonic-gateinitialization from the typemap. A global 583*0Sstevel@tonic-gatevariable, C<%v>, is available for the truly rare case where 584*0Sstevel@tonic-gateinformation from one initialization is needed in another 585*0Sstevel@tonic-gateinitialization. 586*0Sstevel@tonic-gate 587*0Sstevel@tonic-gateHere's a truly obscure example: 588*0Sstevel@tonic-gate 589*0Sstevel@tonic-gate bool_t 590*0Sstevel@tonic-gate rpcb_gettime(host,timep) 591*0Sstevel@tonic-gate time_t &timep ; /* \$v{timep}=@{[$v{timep}=$arg]} */ 592*0Sstevel@tonic-gate char *host + SvOK($v{timep}) ? SvPV($arg,PL_na) : NULL; 593*0Sstevel@tonic-gate OUTPUT: 594*0Sstevel@tonic-gate timep 595*0Sstevel@tonic-gate 596*0Sstevel@tonic-gateThe construct C<\$v{timep}=@{[$v{timep}=$arg]}> used in the above 597*0Sstevel@tonic-gateexample has a two-fold purpose: first, when this line is processed by 598*0Sstevel@tonic-gateB<xsubpp>, the Perl snippet C<$v{timep}=$arg> is evaluated. Second, 599*0Sstevel@tonic-gatethe text of the evaluated snippet is output into the generated C file 600*0Sstevel@tonic-gate(inside a C comment)! During the processing of C<char *host> line, 601*0Sstevel@tonic-gate$arg will evaluate to C<ST(0)>, and C<$v{timep}> will evaluate to 602*0Sstevel@tonic-gateC<ST(1)>. 603*0Sstevel@tonic-gate 604*0Sstevel@tonic-gate=head2 Default Parameter Values 605*0Sstevel@tonic-gate 606*0Sstevel@tonic-gateDefault values for XSUB arguments can be specified by placing an 607*0Sstevel@tonic-gateassignment statement in the parameter list. The default value may 608*0Sstevel@tonic-gatebe a number, a string or the special string C<NO_INIT>. Defaults should 609*0Sstevel@tonic-gatealways be used on the right-most parameters only. 610*0Sstevel@tonic-gate 611*0Sstevel@tonic-gateTo allow the XSUB for rpcb_gettime() to have a default host 612*0Sstevel@tonic-gatevalue the parameters to the XSUB could be rearranged. The 613*0Sstevel@tonic-gateXSUB will then call the real rpcb_gettime() function with 614*0Sstevel@tonic-gatethe parameters in the correct order. This XSUB can be called 615*0Sstevel@tonic-gatefrom Perl with either of the following statements: 616*0Sstevel@tonic-gate 617*0Sstevel@tonic-gate $status = rpcb_gettime( $timep, $host ); 618*0Sstevel@tonic-gate 619*0Sstevel@tonic-gate $status = rpcb_gettime( $timep ); 620*0Sstevel@tonic-gate 621*0Sstevel@tonic-gateThe XSUB will look like the code which follows. A CODE: 622*0Sstevel@tonic-gateblock is used to call the real rpcb_gettime() function with 623*0Sstevel@tonic-gatethe parameters in the correct order for that function. 624*0Sstevel@tonic-gate 625*0Sstevel@tonic-gate bool_t 626*0Sstevel@tonic-gate rpcb_gettime(timep,host="localhost") 627*0Sstevel@tonic-gate char *host 628*0Sstevel@tonic-gate time_t timep = NO_INIT 629*0Sstevel@tonic-gate CODE: 630*0Sstevel@tonic-gate RETVAL = rpcb_gettime( host, &timep ); 631*0Sstevel@tonic-gate OUTPUT: 632*0Sstevel@tonic-gate timep 633*0Sstevel@tonic-gate RETVAL 634*0Sstevel@tonic-gate 635*0Sstevel@tonic-gate=head2 The PREINIT: Keyword 636*0Sstevel@tonic-gate 637*0Sstevel@tonic-gateThe PREINIT: keyword allows extra variables to be declared immediately 638*0Sstevel@tonic-gatebefore or after the declarations of the parameters from the INPUT: section 639*0Sstevel@tonic-gateare emitted. 640*0Sstevel@tonic-gate 641*0Sstevel@tonic-gateIf a variable is declared inside a CODE: section it will follow any typemap 642*0Sstevel@tonic-gatecode that is emitted for the input parameters. This may result in the 643*0Sstevel@tonic-gatedeclaration ending up after C code, which is C syntax error. Similar 644*0Sstevel@tonic-gateerrors may happen with an explicit C<;>-type or C<+>-type initialization of 645*0Sstevel@tonic-gateparameters is used (see L<"Initializing Function Parameters">). Declaring 646*0Sstevel@tonic-gatethese variables in an INIT: section will not help. 647*0Sstevel@tonic-gate 648*0Sstevel@tonic-gateIn such cases, to force an additional variable to be declared together 649*0Sstevel@tonic-gatewith declarations of other variables, place the declaration into a 650*0Sstevel@tonic-gatePREINIT: section. The PREINIT: keyword may be used one or more times 651*0Sstevel@tonic-gatewithin an XSUB. 652*0Sstevel@tonic-gate 653*0Sstevel@tonic-gateThe following examples are equivalent, but if the code is using complex 654*0Sstevel@tonic-gatetypemaps then the first example is safer. 655*0Sstevel@tonic-gate 656*0Sstevel@tonic-gate bool_t 657*0Sstevel@tonic-gate rpcb_gettime(timep) 658*0Sstevel@tonic-gate time_t timep = NO_INIT 659*0Sstevel@tonic-gate PREINIT: 660*0Sstevel@tonic-gate char *host = "localhost"; 661*0Sstevel@tonic-gate CODE: 662*0Sstevel@tonic-gate RETVAL = rpcb_gettime( host, &timep ); 663*0Sstevel@tonic-gate OUTPUT: 664*0Sstevel@tonic-gate timep 665*0Sstevel@tonic-gate RETVAL 666*0Sstevel@tonic-gate 667*0Sstevel@tonic-gateFor this particular case an INIT: keyword would generate the 668*0Sstevel@tonic-gatesame C code as the PREINIT: keyword. Another correct, but error-prone example: 669*0Sstevel@tonic-gate 670*0Sstevel@tonic-gate bool_t 671*0Sstevel@tonic-gate rpcb_gettime(timep) 672*0Sstevel@tonic-gate time_t timep = NO_INIT 673*0Sstevel@tonic-gate CODE: 674*0Sstevel@tonic-gate char *host = "localhost"; 675*0Sstevel@tonic-gate RETVAL = rpcb_gettime( host, &timep ); 676*0Sstevel@tonic-gate OUTPUT: 677*0Sstevel@tonic-gate timep 678*0Sstevel@tonic-gate RETVAL 679*0Sstevel@tonic-gate 680*0Sstevel@tonic-gateAnother way to declare C<host> is to use a C block in the CODE: section: 681*0Sstevel@tonic-gate 682*0Sstevel@tonic-gate bool_t 683*0Sstevel@tonic-gate rpcb_gettime(timep) 684*0Sstevel@tonic-gate time_t timep = NO_INIT 685*0Sstevel@tonic-gate CODE: 686*0Sstevel@tonic-gate { 687*0Sstevel@tonic-gate char *host = "localhost"; 688*0Sstevel@tonic-gate RETVAL = rpcb_gettime( host, &timep ); 689*0Sstevel@tonic-gate } 690*0Sstevel@tonic-gate OUTPUT: 691*0Sstevel@tonic-gate timep 692*0Sstevel@tonic-gate RETVAL 693*0Sstevel@tonic-gate 694*0Sstevel@tonic-gateThe ability to put additional declarations before the typemap entries are 695*0Sstevel@tonic-gateprocessed is very handy in the cases when typemap conversions manipulate 696*0Sstevel@tonic-gatesome global state: 697*0Sstevel@tonic-gate 698*0Sstevel@tonic-gate MyObject 699*0Sstevel@tonic-gate mutate(o) 700*0Sstevel@tonic-gate PREINIT: 701*0Sstevel@tonic-gate MyState st = global_state; 702*0Sstevel@tonic-gate INPUT: 703*0Sstevel@tonic-gate MyObject o; 704*0Sstevel@tonic-gate CLEANUP: 705*0Sstevel@tonic-gate reset_to(global_state, st); 706*0Sstevel@tonic-gate 707*0Sstevel@tonic-gateHere we suppose that conversion to C<MyObject> in the INPUT: section and from 708*0Sstevel@tonic-gateMyObject when processing RETVAL will modify a global variable C<global_state>. 709*0Sstevel@tonic-gateAfter these conversions are performed, we restore the old value of 710*0Sstevel@tonic-gateC<global_state> (to avoid memory leaks, for example). 711*0Sstevel@tonic-gate 712*0Sstevel@tonic-gateThere is another way to trade clarity for compactness: INPUT sections allow 713*0Sstevel@tonic-gatedeclaration of C variables which do not appear in the parameter list of 714*0Sstevel@tonic-gatea subroutine. Thus the above code for mutate() can be rewritten as 715*0Sstevel@tonic-gate 716*0Sstevel@tonic-gate MyObject 717*0Sstevel@tonic-gate mutate(o) 718*0Sstevel@tonic-gate MyState st = global_state; 719*0Sstevel@tonic-gate MyObject o; 720*0Sstevel@tonic-gate CLEANUP: 721*0Sstevel@tonic-gate reset_to(global_state, st); 722*0Sstevel@tonic-gate 723*0Sstevel@tonic-gateand the code for rpcb_gettime() can be rewritten as 724*0Sstevel@tonic-gate 725*0Sstevel@tonic-gate bool_t 726*0Sstevel@tonic-gate rpcb_gettime(timep) 727*0Sstevel@tonic-gate time_t timep = NO_INIT 728*0Sstevel@tonic-gate char *host = "localhost"; 729*0Sstevel@tonic-gate C_ARGS: 730*0Sstevel@tonic-gate host, &timep 731*0Sstevel@tonic-gate OUTPUT: 732*0Sstevel@tonic-gate timep 733*0Sstevel@tonic-gate RETVAL 734*0Sstevel@tonic-gate 735*0Sstevel@tonic-gate=head2 The SCOPE: Keyword 736*0Sstevel@tonic-gate 737*0Sstevel@tonic-gateThe SCOPE: keyword allows scoping to be enabled for a particular XSUB. If 738*0Sstevel@tonic-gateenabled, the XSUB will invoke ENTER and LEAVE automatically. 739*0Sstevel@tonic-gate 740*0Sstevel@tonic-gateTo support potentially complex type mappings, if a typemap entry used 741*0Sstevel@tonic-gateby an XSUB contains a comment like C</*scope*/> then scoping will 742*0Sstevel@tonic-gatebe automatically enabled for that XSUB. 743*0Sstevel@tonic-gate 744*0Sstevel@tonic-gateTo enable scoping: 745*0Sstevel@tonic-gate 746*0Sstevel@tonic-gate SCOPE: ENABLE 747*0Sstevel@tonic-gate 748*0Sstevel@tonic-gateTo disable scoping: 749*0Sstevel@tonic-gate 750*0Sstevel@tonic-gate SCOPE: DISABLE 751*0Sstevel@tonic-gate 752*0Sstevel@tonic-gate=head2 The INPUT: Keyword 753*0Sstevel@tonic-gate 754*0Sstevel@tonic-gateThe XSUB's parameters are usually evaluated immediately after entering the 755*0Sstevel@tonic-gateXSUB. The INPUT: keyword can be used to force those parameters to be 756*0Sstevel@tonic-gateevaluated a little later. The INPUT: keyword can be used multiple times 757*0Sstevel@tonic-gatewithin an XSUB and can be used to list one or more input variables. This 758*0Sstevel@tonic-gatekeyword is used with the PREINIT: keyword. 759*0Sstevel@tonic-gate 760*0Sstevel@tonic-gateThe following example shows how the input parameter C<timep> can be 761*0Sstevel@tonic-gateevaluated late, after a PREINIT. 762*0Sstevel@tonic-gate 763*0Sstevel@tonic-gate bool_t 764*0Sstevel@tonic-gate rpcb_gettime(host,timep) 765*0Sstevel@tonic-gate char *host 766*0Sstevel@tonic-gate PREINIT: 767*0Sstevel@tonic-gate time_t tt; 768*0Sstevel@tonic-gate INPUT: 769*0Sstevel@tonic-gate time_t timep 770*0Sstevel@tonic-gate CODE: 771*0Sstevel@tonic-gate RETVAL = rpcb_gettime( host, &tt ); 772*0Sstevel@tonic-gate timep = tt; 773*0Sstevel@tonic-gate OUTPUT: 774*0Sstevel@tonic-gate timep 775*0Sstevel@tonic-gate RETVAL 776*0Sstevel@tonic-gate 777*0Sstevel@tonic-gateThe next example shows each input parameter evaluated late. 778*0Sstevel@tonic-gate 779*0Sstevel@tonic-gate bool_t 780*0Sstevel@tonic-gate rpcb_gettime(host,timep) 781*0Sstevel@tonic-gate PREINIT: 782*0Sstevel@tonic-gate time_t tt; 783*0Sstevel@tonic-gate INPUT: 784*0Sstevel@tonic-gate char *host 785*0Sstevel@tonic-gate PREINIT: 786*0Sstevel@tonic-gate char *h; 787*0Sstevel@tonic-gate INPUT: 788*0Sstevel@tonic-gate time_t timep 789*0Sstevel@tonic-gate CODE: 790*0Sstevel@tonic-gate h = host; 791*0Sstevel@tonic-gate RETVAL = rpcb_gettime( h, &tt ); 792*0Sstevel@tonic-gate timep = tt; 793*0Sstevel@tonic-gate OUTPUT: 794*0Sstevel@tonic-gate timep 795*0Sstevel@tonic-gate RETVAL 796*0Sstevel@tonic-gate 797*0Sstevel@tonic-gateSince INPUT sections allow declaration of C variables which do not appear 798*0Sstevel@tonic-gatein the parameter list of a subroutine, this may be shortened to: 799*0Sstevel@tonic-gate 800*0Sstevel@tonic-gate bool_t 801*0Sstevel@tonic-gate rpcb_gettime(host,timep) 802*0Sstevel@tonic-gate time_t tt; 803*0Sstevel@tonic-gate char *host; 804*0Sstevel@tonic-gate char *h = host; 805*0Sstevel@tonic-gate time_t timep; 806*0Sstevel@tonic-gate CODE: 807*0Sstevel@tonic-gate RETVAL = rpcb_gettime( h, &tt ); 808*0Sstevel@tonic-gate timep = tt; 809*0Sstevel@tonic-gate OUTPUT: 810*0Sstevel@tonic-gate timep 811*0Sstevel@tonic-gate RETVAL 812*0Sstevel@tonic-gate 813*0Sstevel@tonic-gate(We used our knowledge that input conversion for C<char *> is a "simple" one, 814*0Sstevel@tonic-gatethus C<host> is initialized on the declaration line, and our assignment 815*0Sstevel@tonic-gateC<h = host> is not performed too early. Otherwise one would need to have the 816*0Sstevel@tonic-gateassignment C<h = host> in a CODE: or INIT: section.) 817*0Sstevel@tonic-gate 818*0Sstevel@tonic-gate=head2 The IN/OUTLIST/IN_OUTLIST/OUT/IN_OUT Keywords 819*0Sstevel@tonic-gate 820*0Sstevel@tonic-gateIn the list of parameters for an XSUB, one can precede parameter names 821*0Sstevel@tonic-gateby the C<IN>/C<OUTLIST>/C<IN_OUTLIST>/C<OUT>/C<IN_OUT> keywords. 822*0Sstevel@tonic-gateC<IN> keyword is the default, the other keywords indicate how the Perl 823*0Sstevel@tonic-gateinterface should differ from the C interface. 824*0Sstevel@tonic-gate 825*0Sstevel@tonic-gateParameters preceded by C<OUTLIST>/C<IN_OUTLIST>/C<OUT>/C<IN_OUT> 826*0Sstevel@tonic-gatekeywords are considered to be used by the C subroutine I<via 827*0Sstevel@tonic-gatepointers>. C<OUTLIST>/C<OUT> keywords indicate that the C subroutine 828*0Sstevel@tonic-gatedoes not inspect the memory pointed by this parameter, but will write 829*0Sstevel@tonic-gatethrough this pointer to provide additional return values. 830*0Sstevel@tonic-gate 831*0Sstevel@tonic-gateParameters preceded by C<OUTLIST> keyword do not appear in the usage 832*0Sstevel@tonic-gatesignature of the generated Perl function. 833*0Sstevel@tonic-gate 834*0Sstevel@tonic-gateParameters preceded by C<IN_OUTLIST>/C<IN_OUT>/C<OUT> I<do> appear as 835*0Sstevel@tonic-gateparameters to the Perl function. With the exception of 836*0Sstevel@tonic-gateC<OUT>-parameters, these parameters are converted to the corresponding 837*0Sstevel@tonic-gateC type, then pointers to these data are given as arguments to the C 838*0Sstevel@tonic-gatefunction. It is expected that the C function will write through these 839*0Sstevel@tonic-gatepointers. 840*0Sstevel@tonic-gate 841*0Sstevel@tonic-gateThe return list of the generated Perl function consists of the C return value 842*0Sstevel@tonic-gatefrom the function (unless the XSUB is of C<void> return type or 843*0Sstevel@tonic-gateC<The NO_OUTPUT Keyword> was used) followed by all the C<OUTLIST> 844*0Sstevel@tonic-gateand C<IN_OUTLIST> parameters (in the order of appearance). On the 845*0Sstevel@tonic-gatereturn from the XSUB the C<IN_OUT>/C<OUT> Perl parameter will be 846*0Sstevel@tonic-gatemodified to have the values written by the C function. 847*0Sstevel@tonic-gate 848*0Sstevel@tonic-gateFor example, an XSUB 849*0Sstevel@tonic-gate 850*0Sstevel@tonic-gate void 851*0Sstevel@tonic-gate day_month(OUTLIST day, IN unix_time, OUTLIST month) 852*0Sstevel@tonic-gate int day 853*0Sstevel@tonic-gate int unix_time 854*0Sstevel@tonic-gate int month 855*0Sstevel@tonic-gate 856*0Sstevel@tonic-gateshould be used from Perl as 857*0Sstevel@tonic-gate 858*0Sstevel@tonic-gate my ($day, $month) = day_month(time); 859*0Sstevel@tonic-gate 860*0Sstevel@tonic-gateThe C signature of the corresponding function should be 861*0Sstevel@tonic-gate 862*0Sstevel@tonic-gate void day_month(int *day, int unix_time, int *month); 863*0Sstevel@tonic-gate 864*0Sstevel@tonic-gateThe C<IN>/C<OUTLIST>/C<IN_OUTLIST>/C<IN_OUT>/C<OUT> keywords can be 865*0Sstevel@tonic-gatemixed with ANSI-style declarations, as in 866*0Sstevel@tonic-gate 867*0Sstevel@tonic-gate void 868*0Sstevel@tonic-gate day_month(OUTLIST int day, int unix_time, OUTLIST int month) 869*0Sstevel@tonic-gate 870*0Sstevel@tonic-gate(here the optional C<IN> keyword is omitted). 871*0Sstevel@tonic-gate 872*0Sstevel@tonic-gateThe C<IN_OUT> parameters are identical with parameters introduced with 873*0Sstevel@tonic-gateL<The & Unary Operator> and put into the C<OUTPUT:> section (see 874*0Sstevel@tonic-gateL<The OUTPUT: Keyword>). The C<IN_OUTLIST> parameters are very similar, 875*0Sstevel@tonic-gatethe only difference being that the value C function writes through the 876*0Sstevel@tonic-gatepointer would not modify the Perl parameter, but is put in the output 877*0Sstevel@tonic-gatelist. 878*0Sstevel@tonic-gate 879*0Sstevel@tonic-gateThe C<OUTLIST>/C<OUT> parameter differ from C<IN_OUTLIST>/C<IN_OUT> 880*0Sstevel@tonic-gateparameters only by the initial value of the Perl parameter not 881*0Sstevel@tonic-gatebeing read (and not being given to the C function - which gets some 882*0Sstevel@tonic-gategarbage instead). For example, the same C function as above can be 883*0Sstevel@tonic-gateinterfaced with as 884*0Sstevel@tonic-gate 885*0Sstevel@tonic-gate void day_month(OUT int day, int unix_time, OUT int month); 886*0Sstevel@tonic-gate 887*0Sstevel@tonic-gateor 888*0Sstevel@tonic-gate 889*0Sstevel@tonic-gate void 890*0Sstevel@tonic-gate day_month(day, unix_time, month) 891*0Sstevel@tonic-gate int &day = NO_INIT 892*0Sstevel@tonic-gate int unix_time 893*0Sstevel@tonic-gate int &month = NO_INIT 894*0Sstevel@tonic-gate OUTPUT: 895*0Sstevel@tonic-gate day 896*0Sstevel@tonic-gate month 897*0Sstevel@tonic-gate 898*0Sstevel@tonic-gateHowever, the generated Perl function is called in very C-ish style: 899*0Sstevel@tonic-gate 900*0Sstevel@tonic-gate my ($day, $month); 901*0Sstevel@tonic-gate day_month($day, time, $month); 902*0Sstevel@tonic-gate 903*0Sstevel@tonic-gate=head2 The C<length(NAME)> Keyword 904*0Sstevel@tonic-gate 905*0Sstevel@tonic-gateIf one of the input arguments to the C function is the length of a string 906*0Sstevel@tonic-gateargument C<NAME>, one can substitute the name of the length-argument by 907*0Sstevel@tonic-gateC<length(NAME)> in the XSUB declaration. This argument must be omited when 908*0Sstevel@tonic-gatethe generated Perl function is called. E.g., 909*0Sstevel@tonic-gate 910*0Sstevel@tonic-gate void 911*0Sstevel@tonic-gate dump_chars(char *s, short l) 912*0Sstevel@tonic-gate { 913*0Sstevel@tonic-gate short n = 0; 914*0Sstevel@tonic-gate while (n < l) { 915*0Sstevel@tonic-gate printf("s[%d] = \"\\%#03o\"\n", n, (int)s[n]); 916*0Sstevel@tonic-gate n++; 917*0Sstevel@tonic-gate } 918*0Sstevel@tonic-gate } 919*0Sstevel@tonic-gate 920*0Sstevel@tonic-gate MODULE = x PACKAGE = x 921*0Sstevel@tonic-gate 922*0Sstevel@tonic-gate void dump_chars(char *s, short length(s)) 923*0Sstevel@tonic-gate 924*0Sstevel@tonic-gateshould be called as C<dump_chars($string)>. 925*0Sstevel@tonic-gate 926*0Sstevel@tonic-gateThis directive is supported with ANSI-type function declarations only. 927*0Sstevel@tonic-gate 928*0Sstevel@tonic-gate=head2 Variable-length Parameter Lists 929*0Sstevel@tonic-gate 930*0Sstevel@tonic-gateXSUBs can have variable-length parameter lists by specifying an ellipsis 931*0Sstevel@tonic-gateC<(...)> in the parameter list. This use of the ellipsis is similar to that 932*0Sstevel@tonic-gatefound in ANSI C. The programmer is able to determine the number of 933*0Sstevel@tonic-gatearguments passed to the XSUB by examining the C<items> variable which the 934*0Sstevel@tonic-gateB<xsubpp> compiler supplies for all XSUBs. By using this mechanism one can 935*0Sstevel@tonic-gatecreate an XSUB which accepts a list of parameters of unknown length. 936*0Sstevel@tonic-gate 937*0Sstevel@tonic-gateThe I<host> parameter for the rpcb_gettime() XSUB can be 938*0Sstevel@tonic-gateoptional so the ellipsis can be used to indicate that the 939*0Sstevel@tonic-gateXSUB will take a variable number of parameters. Perl should 940*0Sstevel@tonic-gatebe able to call this XSUB with either of the following statements. 941*0Sstevel@tonic-gate 942*0Sstevel@tonic-gate $status = rpcb_gettime( $timep, $host ); 943*0Sstevel@tonic-gate 944*0Sstevel@tonic-gate $status = rpcb_gettime( $timep ); 945*0Sstevel@tonic-gate 946*0Sstevel@tonic-gateThe XS code, with ellipsis, follows. 947*0Sstevel@tonic-gate 948*0Sstevel@tonic-gate bool_t 949*0Sstevel@tonic-gate rpcb_gettime(timep, ...) 950*0Sstevel@tonic-gate time_t timep = NO_INIT 951*0Sstevel@tonic-gate PREINIT: 952*0Sstevel@tonic-gate char *host = "localhost"; 953*0Sstevel@tonic-gate STRLEN n_a; 954*0Sstevel@tonic-gate CODE: 955*0Sstevel@tonic-gate if( items > 1 ) 956*0Sstevel@tonic-gate host = (char *)SvPV(ST(1), n_a); 957*0Sstevel@tonic-gate RETVAL = rpcb_gettime( host, &timep ); 958*0Sstevel@tonic-gate OUTPUT: 959*0Sstevel@tonic-gate timep 960*0Sstevel@tonic-gate RETVAL 961*0Sstevel@tonic-gate 962*0Sstevel@tonic-gate=head2 The C_ARGS: Keyword 963*0Sstevel@tonic-gate 964*0Sstevel@tonic-gateThe C_ARGS: keyword allows creating of XSUBS which have different 965*0Sstevel@tonic-gatecalling sequence from Perl than from C, without a need to write 966*0Sstevel@tonic-gateCODE: or PPCODE: section. The contents of the C_ARGS: paragraph is 967*0Sstevel@tonic-gateput as the argument to the called C function without any change. 968*0Sstevel@tonic-gate 969*0Sstevel@tonic-gateFor example, suppose that a C function is declared as 970*0Sstevel@tonic-gate 971*0Sstevel@tonic-gate symbolic nth_derivative(int n, symbolic function, int flags); 972*0Sstevel@tonic-gate 973*0Sstevel@tonic-gateand that the default flags are kept in a global C variable 974*0Sstevel@tonic-gateC<default_flags>. Suppose that you want to create an interface which 975*0Sstevel@tonic-gateis called as 976*0Sstevel@tonic-gate 977*0Sstevel@tonic-gate $second_deriv = $function->nth_derivative(2); 978*0Sstevel@tonic-gate 979*0Sstevel@tonic-gateTo do this, declare the XSUB as 980*0Sstevel@tonic-gate 981*0Sstevel@tonic-gate symbolic 982*0Sstevel@tonic-gate nth_derivative(function, n) 983*0Sstevel@tonic-gate symbolic function 984*0Sstevel@tonic-gate int n 985*0Sstevel@tonic-gate C_ARGS: 986*0Sstevel@tonic-gate n, function, default_flags 987*0Sstevel@tonic-gate 988*0Sstevel@tonic-gate=head2 The PPCODE: Keyword 989*0Sstevel@tonic-gate 990*0Sstevel@tonic-gateThe PPCODE: keyword is an alternate form of the CODE: keyword and is used 991*0Sstevel@tonic-gateto tell the B<xsubpp> compiler that the programmer is supplying the code to 992*0Sstevel@tonic-gatecontrol the argument stack for the XSUBs return values. Occasionally one 993*0Sstevel@tonic-gatewill want an XSUB to return a list of values rather than a single value. 994*0Sstevel@tonic-gateIn these cases one must use PPCODE: and then explicitly push the list of 995*0Sstevel@tonic-gatevalues on the stack. The PPCODE: and CODE: keywords should not be used 996*0Sstevel@tonic-gatetogether within the same XSUB. 997*0Sstevel@tonic-gate 998*0Sstevel@tonic-gateThe actual difference between PPCODE: and CODE: sections is in the 999*0Sstevel@tonic-gateinitialization of C<SP> macro (which stands for the I<current> Perl 1000*0Sstevel@tonic-gatestack pointer), and in the handling of data on the stack when returning 1001*0Sstevel@tonic-gatefrom an XSUB. In CODE: sections SP preserves the value which was on 1002*0Sstevel@tonic-gateentry to the XSUB: SP is on the function pointer (which follows the 1003*0Sstevel@tonic-gatelast parameter). In PPCODE: sections SP is moved backward to the 1004*0Sstevel@tonic-gatebeginning of the parameter list, which allows C<PUSH*()> macros 1005*0Sstevel@tonic-gateto place output values in the place Perl expects them to be when 1006*0Sstevel@tonic-gatethe XSUB returns back to Perl. 1007*0Sstevel@tonic-gate 1008*0Sstevel@tonic-gateThe generated trailer for a CODE: section ensures that the number of return 1009*0Sstevel@tonic-gatevalues Perl will see is either 0 or 1 (depending on the C<void>ness of the 1010*0Sstevel@tonic-gatereturn value of the C function, and heuristics mentioned in 1011*0Sstevel@tonic-gateL<"The RETVAL Variable">). The trailer generated for a PPCODE: section 1012*0Sstevel@tonic-gateis based on the number of return values and on the number of times 1013*0Sstevel@tonic-gateC<SP> was updated by C<[X]PUSH*()> macros. 1014*0Sstevel@tonic-gate 1015*0Sstevel@tonic-gateNote that macros C<ST(i)>, C<XST_m*()> and C<XSRETURN*()> work equally 1016*0Sstevel@tonic-gatewell in CODE: sections and PPCODE: sections. 1017*0Sstevel@tonic-gate 1018*0Sstevel@tonic-gateThe following XSUB will call the C rpcb_gettime() function 1019*0Sstevel@tonic-gateand will return its two output values, timep and status, to 1020*0Sstevel@tonic-gatePerl as a single list. 1021*0Sstevel@tonic-gate 1022*0Sstevel@tonic-gate void 1023*0Sstevel@tonic-gate rpcb_gettime(host) 1024*0Sstevel@tonic-gate char *host 1025*0Sstevel@tonic-gate PREINIT: 1026*0Sstevel@tonic-gate time_t timep; 1027*0Sstevel@tonic-gate bool_t status; 1028*0Sstevel@tonic-gate PPCODE: 1029*0Sstevel@tonic-gate status = rpcb_gettime( host, &timep ); 1030*0Sstevel@tonic-gate EXTEND(SP, 2); 1031*0Sstevel@tonic-gate PUSHs(sv_2mortal(newSViv(status))); 1032*0Sstevel@tonic-gate PUSHs(sv_2mortal(newSViv(timep))); 1033*0Sstevel@tonic-gate 1034*0Sstevel@tonic-gateNotice that the programmer must supply the C code necessary 1035*0Sstevel@tonic-gateto have the real rpcb_gettime() function called and to have 1036*0Sstevel@tonic-gatethe return values properly placed on the argument stack. 1037*0Sstevel@tonic-gate 1038*0Sstevel@tonic-gateThe C<void> return type for this function tells the B<xsubpp> compiler that 1039*0Sstevel@tonic-gatethe RETVAL variable is not needed or used and that it should not be created. 1040*0Sstevel@tonic-gateIn most scenarios the void return type should be used with the PPCODE: 1041*0Sstevel@tonic-gatedirective. 1042*0Sstevel@tonic-gate 1043*0Sstevel@tonic-gateThe EXTEND() macro is used to make room on the argument 1044*0Sstevel@tonic-gatestack for 2 return values. The PPCODE: directive causes the 1045*0Sstevel@tonic-gateB<xsubpp> compiler to create a stack pointer available as C<SP>, and it 1046*0Sstevel@tonic-gateis this pointer which is being used in the EXTEND() macro. 1047*0Sstevel@tonic-gateThe values are then pushed onto the stack with the PUSHs() 1048*0Sstevel@tonic-gatemacro. 1049*0Sstevel@tonic-gate 1050*0Sstevel@tonic-gateNow the rpcb_gettime() function can be used from Perl with 1051*0Sstevel@tonic-gatethe following statement. 1052*0Sstevel@tonic-gate 1053*0Sstevel@tonic-gate ($status, $timep) = rpcb_gettime("localhost"); 1054*0Sstevel@tonic-gate 1055*0Sstevel@tonic-gateWhen handling output parameters with a PPCODE section, be sure to handle 1056*0Sstevel@tonic-gate'set' magic properly. See L<perlguts> for details about 'set' magic. 1057*0Sstevel@tonic-gate 1058*0Sstevel@tonic-gate=head2 Returning Undef And Empty Lists 1059*0Sstevel@tonic-gate 1060*0Sstevel@tonic-gateOccasionally the programmer will want to return simply 1061*0Sstevel@tonic-gateC<undef> or an empty list if a function fails rather than a 1062*0Sstevel@tonic-gateseparate status value. The rpcb_gettime() function offers 1063*0Sstevel@tonic-gatejust this situation. If the function succeeds we would like 1064*0Sstevel@tonic-gateto have it return the time and if it fails we would like to 1065*0Sstevel@tonic-gatehave undef returned. In the following Perl code the value 1066*0Sstevel@tonic-gateof $timep will either be undef or it will be a valid time. 1067*0Sstevel@tonic-gate 1068*0Sstevel@tonic-gate $timep = rpcb_gettime( "localhost" ); 1069*0Sstevel@tonic-gate 1070*0Sstevel@tonic-gateThe following XSUB uses the C<SV *> return type as a mnemonic only, 1071*0Sstevel@tonic-gateand uses a CODE: block to indicate to the compiler 1072*0Sstevel@tonic-gatethat the programmer has supplied all the necessary code. The 1073*0Sstevel@tonic-gatesv_newmortal() call will initialize the return value to undef, making that 1074*0Sstevel@tonic-gatethe default return value. 1075*0Sstevel@tonic-gate 1076*0Sstevel@tonic-gate SV * 1077*0Sstevel@tonic-gate rpcb_gettime(host) 1078*0Sstevel@tonic-gate char * host 1079*0Sstevel@tonic-gate PREINIT: 1080*0Sstevel@tonic-gate time_t timep; 1081*0Sstevel@tonic-gate bool_t x; 1082*0Sstevel@tonic-gate CODE: 1083*0Sstevel@tonic-gate ST(0) = sv_newmortal(); 1084*0Sstevel@tonic-gate if( rpcb_gettime( host, &timep ) ) 1085*0Sstevel@tonic-gate sv_setnv( ST(0), (double)timep); 1086*0Sstevel@tonic-gate 1087*0Sstevel@tonic-gateThe next example demonstrates how one would place an explicit undef in the 1088*0Sstevel@tonic-gatereturn value, should the need arise. 1089*0Sstevel@tonic-gate 1090*0Sstevel@tonic-gate SV * 1091*0Sstevel@tonic-gate rpcb_gettime(host) 1092*0Sstevel@tonic-gate char * host 1093*0Sstevel@tonic-gate PREINIT: 1094*0Sstevel@tonic-gate time_t timep; 1095*0Sstevel@tonic-gate bool_t x; 1096*0Sstevel@tonic-gate CODE: 1097*0Sstevel@tonic-gate ST(0) = sv_newmortal(); 1098*0Sstevel@tonic-gate if( rpcb_gettime( host, &timep ) ){ 1099*0Sstevel@tonic-gate sv_setnv( ST(0), (double)timep); 1100*0Sstevel@tonic-gate } 1101*0Sstevel@tonic-gate else{ 1102*0Sstevel@tonic-gate ST(0) = &PL_sv_undef; 1103*0Sstevel@tonic-gate } 1104*0Sstevel@tonic-gate 1105*0Sstevel@tonic-gateTo return an empty list one must use a PPCODE: block and 1106*0Sstevel@tonic-gatethen not push return values on the stack. 1107*0Sstevel@tonic-gate 1108*0Sstevel@tonic-gate void 1109*0Sstevel@tonic-gate rpcb_gettime(host) 1110*0Sstevel@tonic-gate char *host 1111*0Sstevel@tonic-gate PREINIT: 1112*0Sstevel@tonic-gate time_t timep; 1113*0Sstevel@tonic-gate PPCODE: 1114*0Sstevel@tonic-gate if( rpcb_gettime( host, &timep ) ) 1115*0Sstevel@tonic-gate PUSHs(sv_2mortal(newSViv(timep))); 1116*0Sstevel@tonic-gate else{ 1117*0Sstevel@tonic-gate /* Nothing pushed on stack, so an empty 1118*0Sstevel@tonic-gate * list is implicitly returned. */ 1119*0Sstevel@tonic-gate } 1120*0Sstevel@tonic-gate 1121*0Sstevel@tonic-gateSome people may be inclined to include an explicit C<return> in the above 1122*0Sstevel@tonic-gateXSUB, rather than letting control fall through to the end. In those 1123*0Sstevel@tonic-gatesituations C<XSRETURN_EMPTY> should be used, instead. This will ensure that 1124*0Sstevel@tonic-gatethe XSUB stack is properly adjusted. Consult L<perlapi> for other 1125*0Sstevel@tonic-gateC<XSRETURN> macros. 1126*0Sstevel@tonic-gate 1127*0Sstevel@tonic-gateSince C<XSRETURN_*> macros can be used with CODE blocks as well, one can 1128*0Sstevel@tonic-gaterewrite this example as: 1129*0Sstevel@tonic-gate 1130*0Sstevel@tonic-gate int 1131*0Sstevel@tonic-gate rpcb_gettime(host) 1132*0Sstevel@tonic-gate char *host 1133*0Sstevel@tonic-gate PREINIT: 1134*0Sstevel@tonic-gate time_t timep; 1135*0Sstevel@tonic-gate CODE: 1136*0Sstevel@tonic-gate RETVAL = rpcb_gettime( host, &timep ); 1137*0Sstevel@tonic-gate if (RETVAL == 0) 1138*0Sstevel@tonic-gate XSRETURN_UNDEF; 1139*0Sstevel@tonic-gate OUTPUT: 1140*0Sstevel@tonic-gate RETVAL 1141*0Sstevel@tonic-gate 1142*0Sstevel@tonic-gateIn fact, one can put this check into a POSTCALL: section as well. Together 1143*0Sstevel@tonic-gatewith PREINIT: simplifications, this leads to: 1144*0Sstevel@tonic-gate 1145*0Sstevel@tonic-gate int 1146*0Sstevel@tonic-gate rpcb_gettime(host) 1147*0Sstevel@tonic-gate char *host 1148*0Sstevel@tonic-gate time_t timep; 1149*0Sstevel@tonic-gate POSTCALL: 1150*0Sstevel@tonic-gate if (RETVAL == 0) 1151*0Sstevel@tonic-gate XSRETURN_UNDEF; 1152*0Sstevel@tonic-gate 1153*0Sstevel@tonic-gate=head2 The REQUIRE: Keyword 1154*0Sstevel@tonic-gate 1155*0Sstevel@tonic-gateThe REQUIRE: keyword is used to indicate the minimum version of the 1156*0Sstevel@tonic-gateB<xsubpp> compiler needed to compile the XS module. An XS module which 1157*0Sstevel@tonic-gatecontains the following statement will compile with only B<xsubpp> version 1158*0Sstevel@tonic-gate1.922 or greater: 1159*0Sstevel@tonic-gate 1160*0Sstevel@tonic-gate REQUIRE: 1.922 1161*0Sstevel@tonic-gate 1162*0Sstevel@tonic-gate=head2 The CLEANUP: Keyword 1163*0Sstevel@tonic-gate 1164*0Sstevel@tonic-gateThis keyword can be used when an XSUB requires special cleanup procedures 1165*0Sstevel@tonic-gatebefore it terminates. When the CLEANUP: keyword is used it must follow 1166*0Sstevel@tonic-gateany CODE:, PPCODE:, or OUTPUT: blocks which are present in the XSUB. The 1167*0Sstevel@tonic-gatecode specified for the cleanup block will be added as the last statements 1168*0Sstevel@tonic-gatein the XSUB. 1169*0Sstevel@tonic-gate 1170*0Sstevel@tonic-gate=head2 The POSTCALL: Keyword 1171*0Sstevel@tonic-gate 1172*0Sstevel@tonic-gateThis keyword can be used when an XSUB requires special procedures 1173*0Sstevel@tonic-gateexecuted after the C subroutine call is performed. When the POSTCALL: 1174*0Sstevel@tonic-gatekeyword is used it must precede OUTPUT: and CLEANUP: blocks which are 1175*0Sstevel@tonic-gatepresent in the XSUB. 1176*0Sstevel@tonic-gate 1177*0Sstevel@tonic-gateSee examples in L<"The NO_OUTPUT Keyword"> and L<"Returning Undef And Empty Lists">. 1178*0Sstevel@tonic-gate 1179*0Sstevel@tonic-gateThe POSTCALL: block does not make a lot of sense when the C subroutine 1180*0Sstevel@tonic-gatecall is supplied by user by providing either CODE: or PPCODE: section. 1181*0Sstevel@tonic-gate 1182*0Sstevel@tonic-gate=head2 The BOOT: Keyword 1183*0Sstevel@tonic-gate 1184*0Sstevel@tonic-gateThe BOOT: keyword is used to add code to the extension's bootstrap 1185*0Sstevel@tonic-gatefunction. The bootstrap function is generated by the B<xsubpp> compiler and 1186*0Sstevel@tonic-gatenormally holds the statements necessary to register any XSUBs with Perl. 1187*0Sstevel@tonic-gateWith the BOOT: keyword the programmer can tell the compiler to add extra 1188*0Sstevel@tonic-gatestatements to the bootstrap function. 1189*0Sstevel@tonic-gate 1190*0Sstevel@tonic-gateThis keyword may be used any time after the first MODULE keyword and should 1191*0Sstevel@tonic-gateappear on a line by itself. The first blank line after the keyword will 1192*0Sstevel@tonic-gateterminate the code block. 1193*0Sstevel@tonic-gate 1194*0Sstevel@tonic-gate BOOT: 1195*0Sstevel@tonic-gate # The following message will be printed when the 1196*0Sstevel@tonic-gate # bootstrap function executes. 1197*0Sstevel@tonic-gate printf("Hello from the bootstrap!\n"); 1198*0Sstevel@tonic-gate 1199*0Sstevel@tonic-gate=head2 The VERSIONCHECK: Keyword 1200*0Sstevel@tonic-gate 1201*0Sstevel@tonic-gateThe VERSIONCHECK: keyword corresponds to B<xsubpp>'s C<-versioncheck> and 1202*0Sstevel@tonic-gateC<-noversioncheck> options. This keyword overrides the command line 1203*0Sstevel@tonic-gateoptions. Version checking is enabled by default. When version checking is 1204*0Sstevel@tonic-gateenabled the XS module will attempt to verify that its version matches the 1205*0Sstevel@tonic-gateversion of the PM module. 1206*0Sstevel@tonic-gate 1207*0Sstevel@tonic-gateTo enable version checking: 1208*0Sstevel@tonic-gate 1209*0Sstevel@tonic-gate VERSIONCHECK: ENABLE 1210*0Sstevel@tonic-gate 1211*0Sstevel@tonic-gateTo disable version checking: 1212*0Sstevel@tonic-gate 1213*0Sstevel@tonic-gate VERSIONCHECK: DISABLE 1214*0Sstevel@tonic-gate 1215*0Sstevel@tonic-gate=head2 The PROTOTYPES: Keyword 1216*0Sstevel@tonic-gate 1217*0Sstevel@tonic-gateThe PROTOTYPES: keyword corresponds to B<xsubpp>'s C<-prototypes> and 1218*0Sstevel@tonic-gateC<-noprototypes> options. This keyword overrides the command line options. 1219*0Sstevel@tonic-gatePrototypes are enabled by default. When prototypes are enabled XSUBs will 1220*0Sstevel@tonic-gatebe given Perl prototypes. This keyword may be used multiple times in an XS 1221*0Sstevel@tonic-gatemodule to enable and disable prototypes for different parts of the module. 1222*0Sstevel@tonic-gate 1223*0Sstevel@tonic-gateTo enable prototypes: 1224*0Sstevel@tonic-gate 1225*0Sstevel@tonic-gate PROTOTYPES: ENABLE 1226*0Sstevel@tonic-gate 1227*0Sstevel@tonic-gateTo disable prototypes: 1228*0Sstevel@tonic-gate 1229*0Sstevel@tonic-gate PROTOTYPES: DISABLE 1230*0Sstevel@tonic-gate 1231*0Sstevel@tonic-gate=head2 The PROTOTYPE: Keyword 1232*0Sstevel@tonic-gate 1233*0Sstevel@tonic-gateThis keyword is similar to the PROTOTYPES: keyword above but can be used to 1234*0Sstevel@tonic-gateforce B<xsubpp> to use a specific prototype for the XSUB. This keyword 1235*0Sstevel@tonic-gateoverrides all other prototype options and keywords but affects only the 1236*0Sstevel@tonic-gatecurrent XSUB. Consult L<perlsub/Prototypes> for information about Perl 1237*0Sstevel@tonic-gateprototypes. 1238*0Sstevel@tonic-gate 1239*0Sstevel@tonic-gate bool_t 1240*0Sstevel@tonic-gate rpcb_gettime(timep, ...) 1241*0Sstevel@tonic-gate time_t timep = NO_INIT 1242*0Sstevel@tonic-gate PROTOTYPE: $;$ 1243*0Sstevel@tonic-gate PREINIT: 1244*0Sstevel@tonic-gate char *host = "localhost"; 1245*0Sstevel@tonic-gate STRLEN n_a; 1246*0Sstevel@tonic-gate CODE: 1247*0Sstevel@tonic-gate if( items > 1 ) 1248*0Sstevel@tonic-gate host = (char *)SvPV(ST(1), n_a); 1249*0Sstevel@tonic-gate RETVAL = rpcb_gettime( host, &timep ); 1250*0Sstevel@tonic-gate OUTPUT: 1251*0Sstevel@tonic-gate timep 1252*0Sstevel@tonic-gate RETVAL 1253*0Sstevel@tonic-gate 1254*0Sstevel@tonic-gateIf the prototypes are enabled, you can disable it locally for a given 1255*0Sstevel@tonic-gateXSUB as in the following example: 1256*0Sstevel@tonic-gate 1257*0Sstevel@tonic-gate void 1258*0Sstevel@tonic-gate rpcb_gettime_noproto() 1259*0Sstevel@tonic-gate PROTOTYPE: DISABLE 1260*0Sstevel@tonic-gate ... 1261*0Sstevel@tonic-gate 1262*0Sstevel@tonic-gate=head2 The ALIAS: Keyword 1263*0Sstevel@tonic-gate 1264*0Sstevel@tonic-gateThe ALIAS: keyword allows an XSUB to have two or more unique Perl names 1265*0Sstevel@tonic-gateand to know which of those names was used when it was invoked. The Perl 1266*0Sstevel@tonic-gatenames may be fully-qualified with package names. Each alias is given an 1267*0Sstevel@tonic-gateindex. The compiler will setup a variable called C<ix> which contain the 1268*0Sstevel@tonic-gateindex of the alias which was used. When the XSUB is called with its 1269*0Sstevel@tonic-gatedeclared name C<ix> will be 0. 1270*0Sstevel@tonic-gate 1271*0Sstevel@tonic-gateThe following example will create aliases C<FOO::gettime()> and 1272*0Sstevel@tonic-gateC<BAR::getit()> for this function. 1273*0Sstevel@tonic-gate 1274*0Sstevel@tonic-gate bool_t 1275*0Sstevel@tonic-gate rpcb_gettime(host,timep) 1276*0Sstevel@tonic-gate char *host 1277*0Sstevel@tonic-gate time_t &timep 1278*0Sstevel@tonic-gate ALIAS: 1279*0Sstevel@tonic-gate FOO::gettime = 1 1280*0Sstevel@tonic-gate BAR::getit = 2 1281*0Sstevel@tonic-gate INIT: 1282*0Sstevel@tonic-gate printf("# ix = %d\n", ix ); 1283*0Sstevel@tonic-gate OUTPUT: 1284*0Sstevel@tonic-gate timep 1285*0Sstevel@tonic-gate 1286*0Sstevel@tonic-gate=head2 The OVERLOAD: Keyword 1287*0Sstevel@tonic-gate 1288*0Sstevel@tonic-gateInstead of writing an overloaded interface using pure Perl, you 1289*0Sstevel@tonic-gatecan also use the OVERLOAD keyword to define additional Perl names 1290*0Sstevel@tonic-gatefor your functions (like the ALIAS: keyword above). However, the 1291*0Sstevel@tonic-gateoverloaded functions must be defined with three parameters (except 1292*0Sstevel@tonic-gatefor the nomethod() function which needs four parameters). If any 1293*0Sstevel@tonic-gatefunction has the OVERLOAD: keyword, several additional lines 1294*0Sstevel@tonic-gatewill be defined in the c file generated by xsubpp in order to 1295*0Sstevel@tonic-gateregister with the overload magic. 1296*0Sstevel@tonic-gate 1297*0Sstevel@tonic-gateSince blessed objects are actually stored as RV's, it is useful 1298*0Sstevel@tonic-gateto use the typemap features to preprocess parameters and extract 1299*0Sstevel@tonic-gatethe actual SV stored within the blessed RV. See the sample for 1300*0Sstevel@tonic-gateT_PTROBJ_SPECIAL below. 1301*0Sstevel@tonic-gate 1302*0Sstevel@tonic-gateTo use the OVERLOAD: keyword, create an XS function which takes 1303*0Sstevel@tonic-gatethree input parameters ( or use the c style '...' definition) like 1304*0Sstevel@tonic-gatethis: 1305*0Sstevel@tonic-gate 1306*0Sstevel@tonic-gate SV * 1307*0Sstevel@tonic-gate cmp (lobj, robj, swap) 1308*0Sstevel@tonic-gate My_Module_obj lobj 1309*0Sstevel@tonic-gate My_Module_obj robj 1310*0Sstevel@tonic-gate IV swap 1311*0Sstevel@tonic-gate OVERLOAD: cmp <=> 1312*0Sstevel@tonic-gate { /* function defined here */} 1313*0Sstevel@tonic-gate 1314*0Sstevel@tonic-gateIn this case, the function will overload both of the three way 1315*0Sstevel@tonic-gatecomparison operators. For all overload operations using non-alpha 1316*0Sstevel@tonic-gatecharacters, you must type the parameter without quoting, seperating 1317*0Sstevel@tonic-gatemultiple overloads with whitespace. Note that "" (the stringify 1318*0Sstevel@tonic-gateoverload) should be entered as \"\" (i.e. escaped). 1319*0Sstevel@tonic-gate 1320*0Sstevel@tonic-gate=head2 The FALLBACK: Keyword 1321*0Sstevel@tonic-gate 1322*0Sstevel@tonic-gateIn addition to the OVERLOAD keyword, if you need to control how 1323*0Sstevel@tonic-gatePerl autogenerates missing overloaded operators, you can set the 1324*0Sstevel@tonic-gateFALLBACK keyword in the module header section, like this: 1325*0Sstevel@tonic-gate 1326*0Sstevel@tonic-gate MODULE = RPC PACKAGE = RPC 1327*0Sstevel@tonic-gate 1328*0Sstevel@tonic-gate FALLBACK: TRUE 1329*0Sstevel@tonic-gate ... 1330*0Sstevel@tonic-gate 1331*0Sstevel@tonic-gatewhere FALLBACK can take any of the three values TRUE, FALSE, or 1332*0Sstevel@tonic-gateUNDEF. If you do not set any FALLBACK value when using OVERLOAD, 1333*0Sstevel@tonic-gateit defaults to UNDEF. FALLBACK is not used except when one or 1334*0Sstevel@tonic-gatemore functions using OVERLOAD have been defined. Please see 1335*0Sstevel@tonic-gateL<overload/Fallback> for more details. 1336*0Sstevel@tonic-gate 1337*0Sstevel@tonic-gate=head2 The INTERFACE: Keyword 1338*0Sstevel@tonic-gate 1339*0Sstevel@tonic-gateThis keyword declares the current XSUB as a keeper of the given 1340*0Sstevel@tonic-gatecalling signature. If some text follows this keyword, it is 1341*0Sstevel@tonic-gateconsidered as a list of functions which have this signature, and 1342*0Sstevel@tonic-gateshould be attached to the current XSUB. 1343*0Sstevel@tonic-gate 1344*0Sstevel@tonic-gateFor example, if you have 4 C functions multiply(), divide(), add(), 1345*0Sstevel@tonic-gatesubtract() all having the signature: 1346*0Sstevel@tonic-gate 1347*0Sstevel@tonic-gate symbolic f(symbolic, symbolic); 1348*0Sstevel@tonic-gate 1349*0Sstevel@tonic-gateyou can make them all to use the same XSUB using this: 1350*0Sstevel@tonic-gate 1351*0Sstevel@tonic-gate symbolic 1352*0Sstevel@tonic-gate interface_s_ss(arg1, arg2) 1353*0Sstevel@tonic-gate symbolic arg1 1354*0Sstevel@tonic-gate symbolic arg2 1355*0Sstevel@tonic-gate INTERFACE: 1356*0Sstevel@tonic-gate multiply divide 1357*0Sstevel@tonic-gate add subtract 1358*0Sstevel@tonic-gate 1359*0Sstevel@tonic-gate(This is the complete XSUB code for 4 Perl functions!) Four generated 1360*0Sstevel@tonic-gatePerl function share names with corresponding C functions. 1361*0Sstevel@tonic-gate 1362*0Sstevel@tonic-gateThe advantage of this approach comparing to ALIAS: keyword is that there 1363*0Sstevel@tonic-gateis no need to code a switch statement, each Perl function (which shares 1364*0Sstevel@tonic-gatethe same XSUB) knows which C function it should call. Additionally, one 1365*0Sstevel@tonic-gatecan attach an extra function remainder() at runtime by using 1366*0Sstevel@tonic-gate 1367*0Sstevel@tonic-gate CV *mycv = newXSproto("Symbolic::remainder", 1368*0Sstevel@tonic-gate XS_Symbolic_interface_s_ss, __FILE__, "$$"); 1369*0Sstevel@tonic-gate XSINTERFACE_FUNC_SET(mycv, remainder); 1370*0Sstevel@tonic-gate 1371*0Sstevel@tonic-gatesay, from another XSUB. (This example supposes that there was no 1372*0Sstevel@tonic-gateINTERFACE_MACRO: section, otherwise one needs to use something else instead of 1373*0Sstevel@tonic-gateC<XSINTERFACE_FUNC_SET>, see the next section.) 1374*0Sstevel@tonic-gate 1375*0Sstevel@tonic-gate=head2 The INTERFACE_MACRO: Keyword 1376*0Sstevel@tonic-gate 1377*0Sstevel@tonic-gateThis keyword allows one to define an INTERFACE using a different way 1378*0Sstevel@tonic-gateto extract a function pointer from an XSUB. The text which follows 1379*0Sstevel@tonic-gatethis keyword should give the name of macros which would extract/set a 1380*0Sstevel@tonic-gatefunction pointer. The extractor macro is given return type, C<CV*>, 1381*0Sstevel@tonic-gateand C<XSANY.any_dptr> for this C<CV*>. The setter macro is given cv, 1382*0Sstevel@tonic-gateand the function pointer. 1383*0Sstevel@tonic-gate 1384*0Sstevel@tonic-gateThe default value is C<XSINTERFACE_FUNC> and C<XSINTERFACE_FUNC_SET>. 1385*0Sstevel@tonic-gateAn INTERFACE keyword with an empty list of functions can be omitted if 1386*0Sstevel@tonic-gateINTERFACE_MACRO keyword is used. 1387*0Sstevel@tonic-gate 1388*0Sstevel@tonic-gateSuppose that in the previous example functions pointers for 1389*0Sstevel@tonic-gatemultiply(), divide(), add(), subtract() are kept in a global C array 1390*0Sstevel@tonic-gateC<fp[]> with offsets being C<multiply_off>, C<divide_off>, C<add_off>, 1391*0Sstevel@tonic-gateC<subtract_off>. Then one can use 1392*0Sstevel@tonic-gate 1393*0Sstevel@tonic-gate #define XSINTERFACE_FUNC_BYOFFSET(ret,cv,f) \ 1394*0Sstevel@tonic-gate ((XSINTERFACE_CVT(ret,))fp[CvXSUBANY(cv).any_i32]) 1395*0Sstevel@tonic-gate #define XSINTERFACE_FUNC_BYOFFSET_set(cv,f) \ 1396*0Sstevel@tonic-gate CvXSUBANY(cv).any_i32 = CAT2( f, _off ) 1397*0Sstevel@tonic-gate 1398*0Sstevel@tonic-gatein C section, 1399*0Sstevel@tonic-gate 1400*0Sstevel@tonic-gate symbolic 1401*0Sstevel@tonic-gate interface_s_ss(arg1, arg2) 1402*0Sstevel@tonic-gate symbolic arg1 1403*0Sstevel@tonic-gate symbolic arg2 1404*0Sstevel@tonic-gate INTERFACE_MACRO: 1405*0Sstevel@tonic-gate XSINTERFACE_FUNC_BYOFFSET 1406*0Sstevel@tonic-gate XSINTERFACE_FUNC_BYOFFSET_set 1407*0Sstevel@tonic-gate INTERFACE: 1408*0Sstevel@tonic-gate multiply divide 1409*0Sstevel@tonic-gate add subtract 1410*0Sstevel@tonic-gate 1411*0Sstevel@tonic-gatein XSUB section. 1412*0Sstevel@tonic-gate 1413*0Sstevel@tonic-gate=head2 The INCLUDE: Keyword 1414*0Sstevel@tonic-gate 1415*0Sstevel@tonic-gateThis keyword can be used to pull other files into the XS module. The other 1416*0Sstevel@tonic-gatefiles may have XS code. INCLUDE: can also be used to run a command to 1417*0Sstevel@tonic-gategenerate the XS code to be pulled into the module. 1418*0Sstevel@tonic-gate 1419*0Sstevel@tonic-gateThe file F<Rpcb1.xsh> contains our C<rpcb_gettime()> function: 1420*0Sstevel@tonic-gate 1421*0Sstevel@tonic-gate bool_t 1422*0Sstevel@tonic-gate rpcb_gettime(host,timep) 1423*0Sstevel@tonic-gate char *host 1424*0Sstevel@tonic-gate time_t &timep 1425*0Sstevel@tonic-gate OUTPUT: 1426*0Sstevel@tonic-gate timep 1427*0Sstevel@tonic-gate 1428*0Sstevel@tonic-gateThe XS module can use INCLUDE: to pull that file into it. 1429*0Sstevel@tonic-gate 1430*0Sstevel@tonic-gate INCLUDE: Rpcb1.xsh 1431*0Sstevel@tonic-gate 1432*0Sstevel@tonic-gateIf the parameters to the INCLUDE: keyword are followed by a pipe (C<|>) then 1433*0Sstevel@tonic-gatethe compiler will interpret the parameters as a command. 1434*0Sstevel@tonic-gate 1435*0Sstevel@tonic-gate INCLUDE: cat Rpcb1.xsh | 1436*0Sstevel@tonic-gate 1437*0Sstevel@tonic-gate=head2 The CASE: Keyword 1438*0Sstevel@tonic-gate 1439*0Sstevel@tonic-gateThe CASE: keyword allows an XSUB to have multiple distinct parts with each 1440*0Sstevel@tonic-gatepart acting as a virtual XSUB. CASE: is greedy and if it is used then all 1441*0Sstevel@tonic-gateother XS keywords must be contained within a CASE:. This means nothing may 1442*0Sstevel@tonic-gateprecede the first CASE: in the XSUB and anything following the last CASE: is 1443*0Sstevel@tonic-gateincluded in that case. 1444*0Sstevel@tonic-gate 1445*0Sstevel@tonic-gateA CASE: might switch via a parameter of the XSUB, via the C<ix> ALIAS: 1446*0Sstevel@tonic-gatevariable (see L<"The ALIAS: Keyword">), or maybe via the C<items> variable 1447*0Sstevel@tonic-gate(see L<"Variable-length Parameter Lists">). The last CASE: becomes the 1448*0Sstevel@tonic-gateB<default> case if it is not associated with a conditional. The following 1449*0Sstevel@tonic-gateexample shows CASE switched via C<ix> with a function C<rpcb_gettime()> 1450*0Sstevel@tonic-gatehaving an alias C<x_gettime()>. When the function is called as 1451*0Sstevel@tonic-gateC<rpcb_gettime()> its parameters are the usual C<(char *host, time_t *timep)>, 1452*0Sstevel@tonic-gatebut when the function is called as C<x_gettime()> its parameters are 1453*0Sstevel@tonic-gatereversed, C<(time_t *timep, char *host)>. 1454*0Sstevel@tonic-gate 1455*0Sstevel@tonic-gate long 1456*0Sstevel@tonic-gate rpcb_gettime(a,b) 1457*0Sstevel@tonic-gate CASE: ix == 1 1458*0Sstevel@tonic-gate ALIAS: 1459*0Sstevel@tonic-gate x_gettime = 1 1460*0Sstevel@tonic-gate INPUT: 1461*0Sstevel@tonic-gate # 'a' is timep, 'b' is host 1462*0Sstevel@tonic-gate char *b 1463*0Sstevel@tonic-gate time_t a = NO_INIT 1464*0Sstevel@tonic-gate CODE: 1465*0Sstevel@tonic-gate RETVAL = rpcb_gettime( b, &a ); 1466*0Sstevel@tonic-gate OUTPUT: 1467*0Sstevel@tonic-gate a 1468*0Sstevel@tonic-gate RETVAL 1469*0Sstevel@tonic-gate CASE: 1470*0Sstevel@tonic-gate # 'a' is host, 'b' is timep 1471*0Sstevel@tonic-gate char *a 1472*0Sstevel@tonic-gate time_t &b = NO_INIT 1473*0Sstevel@tonic-gate OUTPUT: 1474*0Sstevel@tonic-gate b 1475*0Sstevel@tonic-gate RETVAL 1476*0Sstevel@tonic-gate 1477*0Sstevel@tonic-gateThat function can be called with either of the following statements. Note 1478*0Sstevel@tonic-gatethe different argument lists. 1479*0Sstevel@tonic-gate 1480*0Sstevel@tonic-gate $status = rpcb_gettime( $host, $timep ); 1481*0Sstevel@tonic-gate 1482*0Sstevel@tonic-gate $status = x_gettime( $timep, $host ); 1483*0Sstevel@tonic-gate 1484*0Sstevel@tonic-gate=head2 The & Unary Operator 1485*0Sstevel@tonic-gate 1486*0Sstevel@tonic-gateThe C<&> unary operator in the INPUT: section is used to tell B<xsubpp> 1487*0Sstevel@tonic-gatethat it should convert a Perl value to/from C using the C type to the left 1488*0Sstevel@tonic-gateof C<&>, but provide a pointer to this value when the C function is called. 1489*0Sstevel@tonic-gate 1490*0Sstevel@tonic-gateThis is useful to avoid a CODE: block for a C function which takes a parameter 1491*0Sstevel@tonic-gateby reference. Typically, the parameter should be not a pointer type (an 1492*0Sstevel@tonic-gateC<int> or C<long> but not an C<int*> or C<long*>). 1493*0Sstevel@tonic-gate 1494*0Sstevel@tonic-gateThe following XSUB will generate incorrect C code. The B<xsubpp> compiler will 1495*0Sstevel@tonic-gateturn this into code which calls C<rpcb_gettime()> with parameters C<(char 1496*0Sstevel@tonic-gate*host, time_t timep)>, but the real C<rpcb_gettime()> wants the C<timep> 1497*0Sstevel@tonic-gateparameter to be of type C<time_t*> rather than C<time_t>. 1498*0Sstevel@tonic-gate 1499*0Sstevel@tonic-gate bool_t 1500*0Sstevel@tonic-gate rpcb_gettime(host,timep) 1501*0Sstevel@tonic-gate char *host 1502*0Sstevel@tonic-gate time_t timep 1503*0Sstevel@tonic-gate OUTPUT: 1504*0Sstevel@tonic-gate timep 1505*0Sstevel@tonic-gate 1506*0Sstevel@tonic-gateThat problem is corrected by using the C<&> operator. The B<xsubpp> compiler 1507*0Sstevel@tonic-gatewill now turn this into code which calls C<rpcb_gettime()> correctly with 1508*0Sstevel@tonic-gateparameters C<(char *host, time_t *timep)>. It does this by carrying the 1509*0Sstevel@tonic-gateC<&> through, so the function call looks like C<rpcb_gettime(host, &timep)>. 1510*0Sstevel@tonic-gate 1511*0Sstevel@tonic-gate bool_t 1512*0Sstevel@tonic-gate rpcb_gettime(host,timep) 1513*0Sstevel@tonic-gate char *host 1514*0Sstevel@tonic-gate time_t &timep 1515*0Sstevel@tonic-gate OUTPUT: 1516*0Sstevel@tonic-gate timep 1517*0Sstevel@tonic-gate 1518*0Sstevel@tonic-gate=head2 Inserting POD, Comments and C Preprocessor Directives 1519*0Sstevel@tonic-gate 1520*0Sstevel@tonic-gateC preprocessor directives are allowed within BOOT:, PREINIT: INIT:, CODE:, 1521*0Sstevel@tonic-gatePPCODE:, POSTCALL:, and CLEANUP: blocks, as well as outside the functions. 1522*0Sstevel@tonic-gateComments are allowed anywhere after the MODULE keyword. The compiler will 1523*0Sstevel@tonic-gatepass the preprocessor directives through untouched and will remove the 1524*0Sstevel@tonic-gatecommented lines. POD documentation is allowed at any point, both in the 1525*0Sstevel@tonic-gateC and XS language sections. POD must be terminated with a C<=cut> command; 1526*0Sstevel@tonic-gateC<xsubpp> will exit with an error if it does not. It is very unlikely that 1527*0Sstevel@tonic-gatehuman generated C code will be mistaken for POD, as most indenting styles 1528*0Sstevel@tonic-gateresult in whitespace in front of any line starting with C<=>. Machine 1529*0Sstevel@tonic-gategenerated XS files may fall into this trap unless care is taken to 1530*0Sstevel@tonic-gateensure that a space breaks the sequence "\n=". 1531*0Sstevel@tonic-gate 1532*0Sstevel@tonic-gateComments can be added to XSUBs by placing a C<#> as the first 1533*0Sstevel@tonic-gatenon-whitespace of a line. Care should be taken to avoid making the 1534*0Sstevel@tonic-gatecomment look like a C preprocessor directive, lest it be interpreted as 1535*0Sstevel@tonic-gatesuch. The simplest way to prevent this is to put whitespace in front of 1536*0Sstevel@tonic-gatethe C<#>. 1537*0Sstevel@tonic-gate 1538*0Sstevel@tonic-gateIf you use preprocessor directives to choose one of two 1539*0Sstevel@tonic-gateversions of a function, use 1540*0Sstevel@tonic-gate 1541*0Sstevel@tonic-gate #if ... version1 1542*0Sstevel@tonic-gate #else /* ... version2 */ 1543*0Sstevel@tonic-gate #endif 1544*0Sstevel@tonic-gate 1545*0Sstevel@tonic-gateand not 1546*0Sstevel@tonic-gate 1547*0Sstevel@tonic-gate #if ... version1 1548*0Sstevel@tonic-gate #endif 1549*0Sstevel@tonic-gate #if ... version2 1550*0Sstevel@tonic-gate #endif 1551*0Sstevel@tonic-gate 1552*0Sstevel@tonic-gatebecause otherwise B<xsubpp> will believe that you made a duplicate 1553*0Sstevel@tonic-gatedefinition of the function. Also, put a blank line before the 1554*0Sstevel@tonic-gate#else/#endif so it will not be seen as part of the function body. 1555*0Sstevel@tonic-gate 1556*0Sstevel@tonic-gate=head2 Using XS With C++ 1557*0Sstevel@tonic-gate 1558*0Sstevel@tonic-gateIf an XSUB name contains C<::>, it is considered to be a C++ method. 1559*0Sstevel@tonic-gateThe generated Perl function will assume that 1560*0Sstevel@tonic-gateits first argument is an object pointer. The object pointer 1561*0Sstevel@tonic-gatewill be stored in a variable called THIS. The object should 1562*0Sstevel@tonic-gatehave been created by C++ with the new() function and should 1563*0Sstevel@tonic-gatebe blessed by Perl with the sv_setref_pv() macro. The 1564*0Sstevel@tonic-gateblessing of the object by Perl can be handled by a typemap. An example 1565*0Sstevel@tonic-gatetypemap is shown at the end of this section. 1566*0Sstevel@tonic-gate 1567*0Sstevel@tonic-gateIf the return type of the XSUB includes C<static>, the method is considered 1568*0Sstevel@tonic-gateto be a static method. It will call the C++ 1569*0Sstevel@tonic-gatefunction using the class::method() syntax. If the method is not static 1570*0Sstevel@tonic-gatethe function will be called using the THIS-E<gt>method() syntax. 1571*0Sstevel@tonic-gate 1572*0Sstevel@tonic-gateThe next examples will use the following C++ class. 1573*0Sstevel@tonic-gate 1574*0Sstevel@tonic-gate class color { 1575*0Sstevel@tonic-gate public: 1576*0Sstevel@tonic-gate color(); 1577*0Sstevel@tonic-gate ~color(); 1578*0Sstevel@tonic-gate int blue(); 1579*0Sstevel@tonic-gate void set_blue( int ); 1580*0Sstevel@tonic-gate 1581*0Sstevel@tonic-gate private: 1582*0Sstevel@tonic-gate int c_blue; 1583*0Sstevel@tonic-gate }; 1584*0Sstevel@tonic-gate 1585*0Sstevel@tonic-gateThe XSUBs for the blue() and set_blue() methods are defined with the class 1586*0Sstevel@tonic-gatename but the parameter for the object (THIS, or "self") is implicit and is 1587*0Sstevel@tonic-gatenot listed. 1588*0Sstevel@tonic-gate 1589*0Sstevel@tonic-gate int 1590*0Sstevel@tonic-gate color::blue() 1591*0Sstevel@tonic-gate 1592*0Sstevel@tonic-gate void 1593*0Sstevel@tonic-gate color::set_blue( val ) 1594*0Sstevel@tonic-gate int val 1595*0Sstevel@tonic-gate 1596*0Sstevel@tonic-gateBoth Perl functions will expect an object as the first parameter. In the 1597*0Sstevel@tonic-gategenerated C++ code the object is called C<THIS>, and the method call will 1598*0Sstevel@tonic-gatebe performed on this object. So in the C++ code the blue() and set_blue() 1599*0Sstevel@tonic-gatemethods will be called as this: 1600*0Sstevel@tonic-gate 1601*0Sstevel@tonic-gate RETVAL = THIS->blue(); 1602*0Sstevel@tonic-gate 1603*0Sstevel@tonic-gate THIS->set_blue( val ); 1604*0Sstevel@tonic-gate 1605*0Sstevel@tonic-gateYou could also write a single get/set method using an optional argument: 1606*0Sstevel@tonic-gate 1607*0Sstevel@tonic-gate int 1608*0Sstevel@tonic-gate color::blue( val = NO_INIT ) 1609*0Sstevel@tonic-gate int val 1610*0Sstevel@tonic-gate PROTOTYPE $;$ 1611*0Sstevel@tonic-gate CODE: 1612*0Sstevel@tonic-gate if (items > 1) 1613*0Sstevel@tonic-gate THIS->set_blue( val ); 1614*0Sstevel@tonic-gate RETVAL = THIS->blue(); 1615*0Sstevel@tonic-gate OUTPUT: 1616*0Sstevel@tonic-gate RETVAL 1617*0Sstevel@tonic-gate 1618*0Sstevel@tonic-gateIf the function's name is B<DESTROY> then the C++ C<delete> function will be 1619*0Sstevel@tonic-gatecalled and C<THIS> will be given as its parameter. The generated C++ code for 1620*0Sstevel@tonic-gate 1621*0Sstevel@tonic-gate void 1622*0Sstevel@tonic-gate color::DESTROY() 1623*0Sstevel@tonic-gate 1624*0Sstevel@tonic-gatewill look like this: 1625*0Sstevel@tonic-gate 1626*0Sstevel@tonic-gate color *THIS = ...; // Initialized as in typemap 1627*0Sstevel@tonic-gate 1628*0Sstevel@tonic-gate delete THIS; 1629*0Sstevel@tonic-gate 1630*0Sstevel@tonic-gateIf the function's name is B<new> then the C++ C<new> function will be called 1631*0Sstevel@tonic-gateto create a dynamic C++ object. The XSUB will expect the class name, which 1632*0Sstevel@tonic-gatewill be kept in a variable called C<CLASS>, to be given as the first 1633*0Sstevel@tonic-gateargument. 1634*0Sstevel@tonic-gate 1635*0Sstevel@tonic-gate color * 1636*0Sstevel@tonic-gate color::new() 1637*0Sstevel@tonic-gate 1638*0Sstevel@tonic-gateThe generated C++ code will call C<new>. 1639*0Sstevel@tonic-gate 1640*0Sstevel@tonic-gate RETVAL = new color(); 1641*0Sstevel@tonic-gate 1642*0Sstevel@tonic-gateThe following is an example of a typemap that could be used for this C++ 1643*0Sstevel@tonic-gateexample. 1644*0Sstevel@tonic-gate 1645*0Sstevel@tonic-gate TYPEMAP 1646*0Sstevel@tonic-gate color * O_OBJECT 1647*0Sstevel@tonic-gate 1648*0Sstevel@tonic-gate OUTPUT 1649*0Sstevel@tonic-gate # The Perl object is blessed into 'CLASS', which should be a 1650*0Sstevel@tonic-gate # char* having the name of the package for the blessing. 1651*0Sstevel@tonic-gate O_OBJECT 1652*0Sstevel@tonic-gate sv_setref_pv( $arg, CLASS, (void*)$var ); 1653*0Sstevel@tonic-gate 1654*0Sstevel@tonic-gate INPUT 1655*0Sstevel@tonic-gate O_OBJECT 1656*0Sstevel@tonic-gate if( sv_isobject($arg) && (SvTYPE(SvRV($arg)) == SVt_PVMG) ) 1657*0Sstevel@tonic-gate $var = ($type)SvIV((SV*)SvRV( $arg )); 1658*0Sstevel@tonic-gate else{ 1659*0Sstevel@tonic-gate warn( \"${Package}::$func_name() -- $var is not a blessed SV reference\" ); 1660*0Sstevel@tonic-gate XSRETURN_UNDEF; 1661*0Sstevel@tonic-gate } 1662*0Sstevel@tonic-gate 1663*0Sstevel@tonic-gate=head2 Interface Strategy 1664*0Sstevel@tonic-gate 1665*0Sstevel@tonic-gateWhen designing an interface between Perl and a C library a straight 1666*0Sstevel@tonic-gatetranslation from C to XS (such as created by C<h2xs -x>) is often sufficient. 1667*0Sstevel@tonic-gateHowever, sometimes the interface will look 1668*0Sstevel@tonic-gatevery C-like and occasionally nonintuitive, especially when the C function 1669*0Sstevel@tonic-gatemodifies one of its parameters, or returns failure inband (as in "negative 1670*0Sstevel@tonic-gatereturn values mean failure"). In cases where the programmer wishes to 1671*0Sstevel@tonic-gatecreate a more Perl-like interface the following strategy may help to 1672*0Sstevel@tonic-gateidentify the more critical parts of the interface. 1673*0Sstevel@tonic-gate 1674*0Sstevel@tonic-gateIdentify the C functions with input/output or output parameters. The XSUBs for 1675*0Sstevel@tonic-gatethese functions may be able to return lists to Perl. 1676*0Sstevel@tonic-gate 1677*0Sstevel@tonic-gateIdentify the C functions which use some inband info as an indication 1678*0Sstevel@tonic-gateof failure. They may be 1679*0Sstevel@tonic-gatecandidates to return undef or an empty list in case of failure. If the 1680*0Sstevel@tonic-gatefailure may be detected without a call to the C function, you may want to use 1681*0Sstevel@tonic-gatean INIT: section to report the failure. For failures detectable after the C 1682*0Sstevel@tonic-gatefunction returns one may want to use a POSTCALL: section to process the 1683*0Sstevel@tonic-gatefailure. In more complicated cases use CODE: or PPCODE: sections. 1684*0Sstevel@tonic-gate 1685*0Sstevel@tonic-gateIf many functions use the same failure indication based on the return value, 1686*0Sstevel@tonic-gateyou may want to create a special typedef to handle this situation. Put 1687*0Sstevel@tonic-gate 1688*0Sstevel@tonic-gate typedef int negative_is_failure; 1689*0Sstevel@tonic-gate 1690*0Sstevel@tonic-gatenear the beginning of XS file, and create an OUTPUT typemap entry 1691*0Sstevel@tonic-gatefor C<negative_is_failure> which converts negative values to C<undef>, or 1692*0Sstevel@tonic-gatemaybe croak()s. After this the return value of type C<negative_is_failure> 1693*0Sstevel@tonic-gatewill create more Perl-like interface. 1694*0Sstevel@tonic-gate 1695*0Sstevel@tonic-gateIdentify which values are used by only the C and XSUB functions 1696*0Sstevel@tonic-gatethemselves, say, when a parameter to a function should be a contents of a 1697*0Sstevel@tonic-gateglobal variable. If Perl does not need to access the contents of the value 1698*0Sstevel@tonic-gatethen it may not be necessary to provide a translation for that value 1699*0Sstevel@tonic-gatefrom C to Perl. 1700*0Sstevel@tonic-gate 1701*0Sstevel@tonic-gateIdentify the pointers in the C function parameter lists and return 1702*0Sstevel@tonic-gatevalues. Some pointers may be used to implement input/output or 1703*0Sstevel@tonic-gateoutput parameters, they can be handled in XS with the C<&> unary operator, 1704*0Sstevel@tonic-gateand, possibly, using the NO_INIT keyword. 1705*0Sstevel@tonic-gateSome others will require handling of types like C<int *>, and one needs 1706*0Sstevel@tonic-gateto decide what a useful Perl translation will do in such a case. When 1707*0Sstevel@tonic-gatethe semantic is clear, it is advisable to put the translation into a typemap 1708*0Sstevel@tonic-gatefile. 1709*0Sstevel@tonic-gate 1710*0Sstevel@tonic-gateIdentify the structures used by the C functions. In many 1711*0Sstevel@tonic-gatecases it may be helpful to use the T_PTROBJ typemap for 1712*0Sstevel@tonic-gatethese structures so they can be manipulated by Perl as 1713*0Sstevel@tonic-gateblessed objects. (This is handled automatically by C<h2xs -x>.) 1714*0Sstevel@tonic-gate 1715*0Sstevel@tonic-gateIf the same C type is used in several different contexts which require 1716*0Sstevel@tonic-gatedifferent translations, C<typedef> several new types mapped to this C type, 1717*0Sstevel@tonic-gateand create separate F<typemap> entries for these new types. Use these 1718*0Sstevel@tonic-gatetypes in declarations of return type and parameters to XSUBs. 1719*0Sstevel@tonic-gate 1720*0Sstevel@tonic-gate=head2 Perl Objects And C Structures 1721*0Sstevel@tonic-gate 1722*0Sstevel@tonic-gateWhen dealing with C structures one should select either 1723*0Sstevel@tonic-gateB<T_PTROBJ> or B<T_PTRREF> for the XS type. Both types are 1724*0Sstevel@tonic-gatedesigned to handle pointers to complex objects. The 1725*0Sstevel@tonic-gateT_PTRREF type will allow the Perl object to be unblessed 1726*0Sstevel@tonic-gatewhile the T_PTROBJ type requires that the object be blessed. 1727*0Sstevel@tonic-gateBy using T_PTROBJ one can achieve a form of type-checking 1728*0Sstevel@tonic-gatebecause the XSUB will attempt to verify that the Perl object 1729*0Sstevel@tonic-gateis of the expected type. 1730*0Sstevel@tonic-gate 1731*0Sstevel@tonic-gateThe following XS code shows the getnetconfigent() function which is used 1732*0Sstevel@tonic-gatewith ONC+ TIRPC. The getnetconfigent() function will return a pointer to a 1733*0Sstevel@tonic-gateC structure and has the C prototype shown below. The example will 1734*0Sstevel@tonic-gatedemonstrate how the C pointer will become a Perl reference. Perl will 1735*0Sstevel@tonic-gateconsider this reference to be a pointer to a blessed object and will 1736*0Sstevel@tonic-gateattempt to call a destructor for the object. A destructor will be 1737*0Sstevel@tonic-gateprovided in the XS source to free the memory used by getnetconfigent(). 1738*0Sstevel@tonic-gateDestructors in XS can be created by specifying an XSUB function whose name 1739*0Sstevel@tonic-gateends with the word B<DESTROY>. XS destructors can be used to free memory 1740*0Sstevel@tonic-gatewhich may have been malloc'd by another XSUB. 1741*0Sstevel@tonic-gate 1742*0Sstevel@tonic-gate struct netconfig *getnetconfigent(const char *netid); 1743*0Sstevel@tonic-gate 1744*0Sstevel@tonic-gateA C<typedef> will be created for C<struct netconfig>. The Perl 1745*0Sstevel@tonic-gateobject will be blessed in a class matching the name of the C 1746*0Sstevel@tonic-gatetype, with the tag C<Ptr> appended, and the name should not 1747*0Sstevel@tonic-gatehave embedded spaces if it will be a Perl package name. The 1748*0Sstevel@tonic-gatedestructor will be placed in a class corresponding to the 1749*0Sstevel@tonic-gateclass of the object and the PREFIX keyword will be used to 1750*0Sstevel@tonic-gatetrim the name to the word DESTROY as Perl will expect. 1751*0Sstevel@tonic-gate 1752*0Sstevel@tonic-gate typedef struct netconfig Netconfig; 1753*0Sstevel@tonic-gate 1754*0Sstevel@tonic-gate MODULE = RPC PACKAGE = RPC 1755*0Sstevel@tonic-gate 1756*0Sstevel@tonic-gate Netconfig * 1757*0Sstevel@tonic-gate getnetconfigent(netid) 1758*0Sstevel@tonic-gate char *netid 1759*0Sstevel@tonic-gate 1760*0Sstevel@tonic-gate MODULE = RPC PACKAGE = NetconfigPtr PREFIX = rpcb_ 1761*0Sstevel@tonic-gate 1762*0Sstevel@tonic-gate void 1763*0Sstevel@tonic-gate rpcb_DESTROY(netconf) 1764*0Sstevel@tonic-gate Netconfig *netconf 1765*0Sstevel@tonic-gate CODE: 1766*0Sstevel@tonic-gate printf("Now in NetconfigPtr::DESTROY\n"); 1767*0Sstevel@tonic-gate free( netconf ); 1768*0Sstevel@tonic-gate 1769*0Sstevel@tonic-gateThis example requires the following typemap entry. Consult the typemap 1770*0Sstevel@tonic-gatesection for more information about adding new typemaps for an extension. 1771*0Sstevel@tonic-gate 1772*0Sstevel@tonic-gate TYPEMAP 1773*0Sstevel@tonic-gate Netconfig * T_PTROBJ 1774*0Sstevel@tonic-gate 1775*0Sstevel@tonic-gateThis example will be used with the following Perl statements. 1776*0Sstevel@tonic-gate 1777*0Sstevel@tonic-gate use RPC; 1778*0Sstevel@tonic-gate $netconf = getnetconfigent("udp"); 1779*0Sstevel@tonic-gate 1780*0Sstevel@tonic-gateWhen Perl destroys the object referenced by $netconf it will send the 1781*0Sstevel@tonic-gateobject to the supplied XSUB DESTROY function. Perl cannot determine, and 1782*0Sstevel@tonic-gatedoes not care, that this object is a C struct and not a Perl object. In 1783*0Sstevel@tonic-gatethis sense, there is no difference between the object created by the 1784*0Sstevel@tonic-gategetnetconfigent() XSUB and an object created by a normal Perl subroutine. 1785*0Sstevel@tonic-gate 1786*0Sstevel@tonic-gate=head2 The Typemap 1787*0Sstevel@tonic-gate 1788*0Sstevel@tonic-gateThe typemap is a collection of code fragments which are used by the B<xsubpp> 1789*0Sstevel@tonic-gatecompiler to map C function parameters and values to Perl values. The 1790*0Sstevel@tonic-gatetypemap file may consist of three sections labelled C<TYPEMAP>, C<INPUT>, and 1791*0Sstevel@tonic-gateC<OUTPUT>. An unlabelled initial section is assumed to be a C<TYPEMAP> 1792*0Sstevel@tonic-gatesection. The INPUT section tells 1793*0Sstevel@tonic-gatethe compiler how to translate Perl values 1794*0Sstevel@tonic-gateinto variables of certain C types. The OUTPUT section tells the compiler 1795*0Sstevel@tonic-gatehow to translate the values from certain C types into values Perl can 1796*0Sstevel@tonic-gateunderstand. The TYPEMAP section tells the compiler which of the INPUT and 1797*0Sstevel@tonic-gateOUTPUT code fragments should be used to map a given C type to a Perl value. 1798*0Sstevel@tonic-gateThe section labels C<TYPEMAP>, C<INPUT>, or C<OUTPUT> must begin 1799*0Sstevel@tonic-gatein the first column on a line by themselves, and must be in uppercase. 1800*0Sstevel@tonic-gate 1801*0Sstevel@tonic-gateThe default typemap in the C<lib/ExtUtils> directory of the Perl source 1802*0Sstevel@tonic-gatecontains many useful types which can be used by Perl extensions. Some 1803*0Sstevel@tonic-gateextensions define additional typemaps which they keep in their own directory. 1804*0Sstevel@tonic-gateThese additional typemaps may reference INPUT and OUTPUT maps in the main 1805*0Sstevel@tonic-gatetypemap. The B<xsubpp> compiler will allow the extension's own typemap to 1806*0Sstevel@tonic-gateoverride any mappings which are in the default typemap. 1807*0Sstevel@tonic-gate 1808*0Sstevel@tonic-gateMost extensions which require a custom typemap will need only the TYPEMAP 1809*0Sstevel@tonic-gatesection of the typemap file. The custom typemap used in the 1810*0Sstevel@tonic-gategetnetconfigent() example shown earlier demonstrates what may be the typical 1811*0Sstevel@tonic-gateuse of extension typemaps. That typemap is used to equate a C structure 1812*0Sstevel@tonic-gatewith the T_PTROBJ typemap. The typemap used by getnetconfigent() is shown 1813*0Sstevel@tonic-gatehere. Note that the C type is separated from the XS type with a tab and 1814*0Sstevel@tonic-gatethat the C unary operator C<*> is considered to be a part of the C type name. 1815*0Sstevel@tonic-gate 1816*0Sstevel@tonic-gate TYPEMAP 1817*0Sstevel@tonic-gate Netconfig *<tab>T_PTROBJ 1818*0Sstevel@tonic-gate 1819*0Sstevel@tonic-gateHere's a more complicated example: suppose that you wanted C<struct 1820*0Sstevel@tonic-gatenetconfig> to be blessed into the class C<Net::Config>. One way to do 1821*0Sstevel@tonic-gatethis is to use underscores (_) to separate package names, as follows: 1822*0Sstevel@tonic-gate 1823*0Sstevel@tonic-gate typedef struct netconfig * Net_Config; 1824*0Sstevel@tonic-gate 1825*0Sstevel@tonic-gateAnd then provide a typemap entry C<T_PTROBJ_SPECIAL> that maps underscores to 1826*0Sstevel@tonic-gatedouble-colons (::), and declare C<Net_Config> to be of that type: 1827*0Sstevel@tonic-gate 1828*0Sstevel@tonic-gate 1829*0Sstevel@tonic-gate TYPEMAP 1830*0Sstevel@tonic-gate Net_Config T_PTROBJ_SPECIAL 1831*0Sstevel@tonic-gate 1832*0Sstevel@tonic-gate INPUT 1833*0Sstevel@tonic-gate T_PTROBJ_SPECIAL 1834*0Sstevel@tonic-gate if (sv_derived_from($arg, \"${(my $ntt=$ntype)=~s/_/::/g;\$ntt}\")) { 1835*0Sstevel@tonic-gate IV tmp = SvIV((SV*)SvRV($arg)); 1836*0Sstevel@tonic-gate $var = ($type) tmp; 1837*0Sstevel@tonic-gate } 1838*0Sstevel@tonic-gate else 1839*0Sstevel@tonic-gate croak(\"$var is not of type ${(my $ntt=$ntype)=~s/_/::/g;\$ntt}\") 1840*0Sstevel@tonic-gate 1841*0Sstevel@tonic-gate OUTPUT 1842*0Sstevel@tonic-gate T_PTROBJ_SPECIAL 1843*0Sstevel@tonic-gate sv_setref_pv($arg, \"${(my $ntt=$ntype)=~s/_/::/g;\$ntt}\", 1844*0Sstevel@tonic-gate (void*)$var); 1845*0Sstevel@tonic-gate 1846*0Sstevel@tonic-gateThe INPUT and OUTPUT sections substitute underscores for double-colons 1847*0Sstevel@tonic-gateon the fly, giving the desired effect. This example demonstrates some 1848*0Sstevel@tonic-gateof the power and versatility of the typemap facility. 1849*0Sstevel@tonic-gate 1850*0Sstevel@tonic-gate=head2 Safely Storing Static Data in XS 1851*0Sstevel@tonic-gate 1852*0Sstevel@tonic-gateStarting with Perl 5.8, a macro framework has been defined to allow 1853*0Sstevel@tonic-gatestatic data to be safely stored in XS modules that will be accessed from 1854*0Sstevel@tonic-gatea multi-threaded Perl. 1855*0Sstevel@tonic-gate 1856*0Sstevel@tonic-gateAlthough primarily designed for use with multi-threaded Perl, the macros 1857*0Sstevel@tonic-gatehave been designed so that they will work with non-threaded Perl as well. 1858*0Sstevel@tonic-gate 1859*0Sstevel@tonic-gateIt is therefore strongly recommended that these macros be used by all 1860*0Sstevel@tonic-gateXS modules that make use of static data. 1861*0Sstevel@tonic-gate 1862*0Sstevel@tonic-gateThe easiest way to get a template set of macros to use is by specifying 1863*0Sstevel@tonic-gatethe C<-g> (C<--global>) option with h2xs (see L<h2xs>). 1864*0Sstevel@tonic-gate 1865*0Sstevel@tonic-gateBelow is an example module that makes use of the macros. 1866*0Sstevel@tonic-gate 1867*0Sstevel@tonic-gate #include "EXTERN.h" 1868*0Sstevel@tonic-gate #include "perl.h" 1869*0Sstevel@tonic-gate #include "XSUB.h" 1870*0Sstevel@tonic-gate 1871*0Sstevel@tonic-gate /* Global Data */ 1872*0Sstevel@tonic-gate 1873*0Sstevel@tonic-gate #define MY_CXT_KEY "BlindMice::_guts" XS_VERSION 1874*0Sstevel@tonic-gate 1875*0Sstevel@tonic-gate typedef struct { 1876*0Sstevel@tonic-gate int count; 1877*0Sstevel@tonic-gate char name[3][100]; 1878*0Sstevel@tonic-gate } my_cxt_t; 1879*0Sstevel@tonic-gate 1880*0Sstevel@tonic-gate START_MY_CXT 1881*0Sstevel@tonic-gate 1882*0Sstevel@tonic-gate MODULE = BlindMice PACKAGE = BlindMice 1883*0Sstevel@tonic-gate 1884*0Sstevel@tonic-gate BOOT: 1885*0Sstevel@tonic-gate { 1886*0Sstevel@tonic-gate MY_CXT_INIT; 1887*0Sstevel@tonic-gate MY_CXT.count = 0; 1888*0Sstevel@tonic-gate strcpy(MY_CXT.name[0], "None"); 1889*0Sstevel@tonic-gate strcpy(MY_CXT.name[1], "None"); 1890*0Sstevel@tonic-gate strcpy(MY_CXT.name[2], "None"); 1891*0Sstevel@tonic-gate } 1892*0Sstevel@tonic-gate 1893*0Sstevel@tonic-gate int 1894*0Sstevel@tonic-gate newMouse(char * name) 1895*0Sstevel@tonic-gate char * name; 1896*0Sstevel@tonic-gate PREINIT: 1897*0Sstevel@tonic-gate dMY_CXT; 1898*0Sstevel@tonic-gate CODE: 1899*0Sstevel@tonic-gate if (MY_CXT.count >= 3) { 1900*0Sstevel@tonic-gate warn("Already have 3 blind mice") ; 1901*0Sstevel@tonic-gate RETVAL = 0; 1902*0Sstevel@tonic-gate } 1903*0Sstevel@tonic-gate else { 1904*0Sstevel@tonic-gate RETVAL = ++ MY_CXT.count; 1905*0Sstevel@tonic-gate strcpy(MY_CXT.name[MY_CXT.count - 1], name); 1906*0Sstevel@tonic-gate } 1907*0Sstevel@tonic-gate 1908*0Sstevel@tonic-gate char * 1909*0Sstevel@tonic-gate get_mouse_name(index) 1910*0Sstevel@tonic-gate int index 1911*0Sstevel@tonic-gate CODE: 1912*0Sstevel@tonic-gate dMY_CXT; 1913*0Sstevel@tonic-gate RETVAL = MY_CXT.lives ++; 1914*0Sstevel@tonic-gate if (index > MY_CXT.count) 1915*0Sstevel@tonic-gate croak("There are only 3 blind mice."); 1916*0Sstevel@tonic-gate else 1917*0Sstevel@tonic-gate RETVAL = newSVpv(MY_CXT.name[index - 1]); 1918*0Sstevel@tonic-gate 1919*0Sstevel@tonic-gate 1920*0Sstevel@tonic-gateB<REFERENCE> 1921*0Sstevel@tonic-gate 1922*0Sstevel@tonic-gate=over 5 1923*0Sstevel@tonic-gate 1924*0Sstevel@tonic-gate=item MY_CXT_KEY 1925*0Sstevel@tonic-gate 1926*0Sstevel@tonic-gateThis macro is used to define a unique key to refer to the static data 1927*0Sstevel@tonic-gatefor an XS module. The suggested naming scheme, as used by h2xs, is to 1928*0Sstevel@tonic-gateuse a string that consists of the module name, the string "::_guts" 1929*0Sstevel@tonic-gateand the module version number. 1930*0Sstevel@tonic-gate 1931*0Sstevel@tonic-gate #define MY_CXT_KEY "MyModule::_guts" XS_VERSION 1932*0Sstevel@tonic-gate 1933*0Sstevel@tonic-gate=item typedef my_cxt_t 1934*0Sstevel@tonic-gate 1935*0Sstevel@tonic-gateThis struct typedef I<must> always be called C<my_cxt_t> -- the other 1936*0Sstevel@tonic-gateC<CXT*> macros assume the existence of the C<my_cxt_t> typedef name. 1937*0Sstevel@tonic-gate 1938*0Sstevel@tonic-gateDeclare a typedef named C<my_cxt_t> that is a structure that contains 1939*0Sstevel@tonic-gateall the data that needs to be interpreter-local. 1940*0Sstevel@tonic-gate 1941*0Sstevel@tonic-gate typedef struct { 1942*0Sstevel@tonic-gate int some_value; 1943*0Sstevel@tonic-gate } my_cxt_t; 1944*0Sstevel@tonic-gate 1945*0Sstevel@tonic-gate=item START_MY_CXT 1946*0Sstevel@tonic-gate 1947*0Sstevel@tonic-gateAlways place the START_MY_CXT macro directly after the declaration 1948*0Sstevel@tonic-gateof C<my_cxt_t>. 1949*0Sstevel@tonic-gate 1950*0Sstevel@tonic-gate=item MY_CXT_INIT 1951*0Sstevel@tonic-gate 1952*0Sstevel@tonic-gateThe MY_CXT_INIT macro initialises storage for the C<my_cxt_t> struct. 1953*0Sstevel@tonic-gate 1954*0Sstevel@tonic-gateIt I<must> be called exactly once -- typically in a BOOT: section. 1955*0Sstevel@tonic-gate 1956*0Sstevel@tonic-gate=item dMY_CXT 1957*0Sstevel@tonic-gate 1958*0Sstevel@tonic-gateUse the dMY_CXT macro (a declaration) in all the functions that access 1959*0Sstevel@tonic-gateMY_CXT. 1960*0Sstevel@tonic-gate 1961*0Sstevel@tonic-gate=item MY_CXT 1962*0Sstevel@tonic-gate 1963*0Sstevel@tonic-gateUse the MY_CXT macro to access members of the C<my_cxt_t> struct. For 1964*0Sstevel@tonic-gateexample, if C<my_cxt_t> is 1965*0Sstevel@tonic-gate 1966*0Sstevel@tonic-gate typedef struct { 1967*0Sstevel@tonic-gate int index; 1968*0Sstevel@tonic-gate } my_cxt_t; 1969*0Sstevel@tonic-gate 1970*0Sstevel@tonic-gatethen use this to access the C<index> member 1971*0Sstevel@tonic-gate 1972*0Sstevel@tonic-gate dMY_CXT; 1973*0Sstevel@tonic-gate MY_CXT.index = 2; 1974*0Sstevel@tonic-gate 1975*0Sstevel@tonic-gate=back 1976*0Sstevel@tonic-gate 1977*0Sstevel@tonic-gate=head1 EXAMPLES 1978*0Sstevel@tonic-gate 1979*0Sstevel@tonic-gateFile C<RPC.xs>: Interface to some ONC+ RPC bind library functions. 1980*0Sstevel@tonic-gate 1981*0Sstevel@tonic-gate #include "EXTERN.h" 1982*0Sstevel@tonic-gate #include "perl.h" 1983*0Sstevel@tonic-gate #include "XSUB.h" 1984*0Sstevel@tonic-gate 1985*0Sstevel@tonic-gate #include <rpc/rpc.h> 1986*0Sstevel@tonic-gate 1987*0Sstevel@tonic-gate typedef struct netconfig Netconfig; 1988*0Sstevel@tonic-gate 1989*0Sstevel@tonic-gate MODULE = RPC PACKAGE = RPC 1990*0Sstevel@tonic-gate 1991*0Sstevel@tonic-gate SV * 1992*0Sstevel@tonic-gate rpcb_gettime(host="localhost") 1993*0Sstevel@tonic-gate char *host 1994*0Sstevel@tonic-gate PREINIT: 1995*0Sstevel@tonic-gate time_t timep; 1996*0Sstevel@tonic-gate CODE: 1997*0Sstevel@tonic-gate ST(0) = sv_newmortal(); 1998*0Sstevel@tonic-gate if( rpcb_gettime( host, &timep ) ) 1999*0Sstevel@tonic-gate sv_setnv( ST(0), (double)timep ); 2000*0Sstevel@tonic-gate 2001*0Sstevel@tonic-gate Netconfig * 2002*0Sstevel@tonic-gate getnetconfigent(netid="udp") 2003*0Sstevel@tonic-gate char *netid 2004*0Sstevel@tonic-gate 2005*0Sstevel@tonic-gate MODULE = RPC PACKAGE = NetconfigPtr PREFIX = rpcb_ 2006*0Sstevel@tonic-gate 2007*0Sstevel@tonic-gate void 2008*0Sstevel@tonic-gate rpcb_DESTROY(netconf) 2009*0Sstevel@tonic-gate Netconfig *netconf 2010*0Sstevel@tonic-gate CODE: 2011*0Sstevel@tonic-gate printf("NetconfigPtr::DESTROY\n"); 2012*0Sstevel@tonic-gate free( netconf ); 2013*0Sstevel@tonic-gate 2014*0Sstevel@tonic-gateFile C<typemap>: Custom typemap for RPC.xs. 2015*0Sstevel@tonic-gate 2016*0Sstevel@tonic-gate TYPEMAP 2017*0Sstevel@tonic-gate Netconfig * T_PTROBJ 2018*0Sstevel@tonic-gate 2019*0Sstevel@tonic-gateFile C<RPC.pm>: Perl module for the RPC extension. 2020*0Sstevel@tonic-gate 2021*0Sstevel@tonic-gate package RPC; 2022*0Sstevel@tonic-gate 2023*0Sstevel@tonic-gate require Exporter; 2024*0Sstevel@tonic-gate require DynaLoader; 2025*0Sstevel@tonic-gate @ISA = qw(Exporter DynaLoader); 2026*0Sstevel@tonic-gate @EXPORT = qw(rpcb_gettime getnetconfigent); 2027*0Sstevel@tonic-gate 2028*0Sstevel@tonic-gate bootstrap RPC; 2029*0Sstevel@tonic-gate 1; 2030*0Sstevel@tonic-gate 2031*0Sstevel@tonic-gateFile C<rpctest.pl>: Perl test program for the RPC extension. 2032*0Sstevel@tonic-gate 2033*0Sstevel@tonic-gate use RPC; 2034*0Sstevel@tonic-gate 2035*0Sstevel@tonic-gate $netconf = getnetconfigent(); 2036*0Sstevel@tonic-gate $a = rpcb_gettime(); 2037*0Sstevel@tonic-gate print "time = $a\n"; 2038*0Sstevel@tonic-gate print "netconf = $netconf\n"; 2039*0Sstevel@tonic-gate 2040*0Sstevel@tonic-gate $netconf = getnetconfigent("tcp"); 2041*0Sstevel@tonic-gate $a = rpcb_gettime("poplar"); 2042*0Sstevel@tonic-gate print "time = $a\n"; 2043*0Sstevel@tonic-gate print "netconf = $netconf\n"; 2044*0Sstevel@tonic-gate 2045*0Sstevel@tonic-gate 2046*0Sstevel@tonic-gate=head1 XS VERSION 2047*0Sstevel@tonic-gate 2048*0Sstevel@tonic-gateThis document covers features supported by C<xsubpp> 1.935. 2049*0Sstevel@tonic-gate 2050*0Sstevel@tonic-gate=head1 AUTHOR 2051*0Sstevel@tonic-gate 2052*0Sstevel@tonic-gateOriginally written by Dean Roehrich <F<roehrich@cray.com>>. 2053*0Sstevel@tonic-gate 2054*0Sstevel@tonic-gateMaintained since 1996 by The Perl Porters <F<perlbug@perl.org>>. 2055