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