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