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