1PERLXS(1)              Perl Programmers Reference Guide              PERLXS(1)
2
3
4

NAME

6       perlxs - XS language reference manual
7

DESCRIPTION

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

EXAMPLES

2000       File "RPC.xs": Interface to some ONC+ RPC bind library functions.
2001
2002            #include "EXTERN.h"
2003            #include "perl.h"
2004            #include "XSUB.h"
2005
2006            #include <rpc/rpc.h>
2007
2008            typedef struct netconfig Netconfig;
2009
2010            MODULE = RPC  PACKAGE = RPC
2011
2012            SV *
2013            rpcb_gettime(host="localhost")
2014                 char *host
2015               PREINIT:
2016                 time_t  timep;
2017               CODE:
2018                 ST(0) = sv_newmortal();
2019                 if( rpcb_gettime( host, &timep ) )
2020                      sv_setnv( ST(0), (double)timep );
2021
2022            Netconfig *
2023            getnetconfigent(netid="udp")
2024                 char *netid
2025
2026            MODULE = RPC  PACKAGE = NetconfigPtr  PREFIX = rpcb_
2027
2028            void
2029            rpcb_DESTROY(netconf)
2030                 Netconfig *netconf
2031               CODE:
2032                 printf("NetconfigPtr::DESTROY\n");
2033                 free( netconf );
2034
2035       File "typemap": Custom typemap for RPC.xs.
2036
2037            TYPEMAP
2038            Netconfig *  T_PTROBJ
2039
2040       File "RPC.pm": Perl module for the RPC extension.
2041
2042            package RPC;
2043
2044            require Exporter;
2045            require DynaLoader;
2046            @ISA = qw(Exporter DynaLoader);
2047            @EXPORT = qw(rpcb_gettime getnetconfigent);
2048
2049            bootstrap RPC;
2050            1;
2051
2052       File "rpctest.pl": Perl test program for the RPC extension.
2053
2054            use RPC;
2055
2056            $netconf = getnetconfigent();
2057            $a = rpcb_gettime();
2058            print "time = $a\n";
2059            print "netconf = $netconf\n";
2060
2061            $netconf = getnetconfigent("tcp");
2062            $a = rpcb_gettime("poplar");
2063            print "time = $a\n";
2064            print "netconf = $netconf\n";
2065

XS VERSION

2067       This document covers features supported by "xsubpp" 1.935.
2068

AUTHOR

2070       Originally written by Dean Roehrich <roehrich@cray.com>.
2071
2072       Maintained since 1996 by The Perl Porters <perlbug@perl.org>.
2073
2074
2075
2076perl v5.12.4                      2011-06-07                         PERLXS(1)
Impressum