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