3 perlxs - XS language reference manual
9 XS is an interface description file format used to create an extension
10 interface between Perl and C code (or a C library) which one wishes
11 to use with Perl. The XS interface is combined with the library to
12 create a new library which can then be either dynamically loaded
13 or statically linked into perl. The XS interface description is
14 written in the XS language and is the core component of the Perl
17 An B<XSUB> forms the basic unit of the XS interface. After compilation
18 by the B<xsubpp> compiler, each XSUB amounts to a C function definition
19 which will provide the glue between Perl calling conventions and C
22 The glue code pulls the arguments from the Perl stack, converts these
23 Perl values to the formats expected by a C function, call this C function,
24 transfers the return values of the C function back to Perl.
25 Return values here may be a conventional C return value or any C
26 function arguments that may serve as output parameters. These return
27 values may be passed back to Perl either by putting them on the
28 Perl stack, or by modifying the arguments supplied from the Perl side.
30 The above is a somewhat simplified view of what really happens. Since
31 Perl allows more flexible calling conventions than C, XSUBs may do much
32 more in practice, such as checking input parameters for validity,
33 throwing exceptions (or returning undef/empty list) if the return value
34 from the C function indicates failure, calling different C functions
35 based on numbers and types of the arguments, providing an object-oriented
38 Of course, one could write such glue code directly in C. However, this
39 would be a tedious task, especially if one needs to write glue for
40 multiple C functions, and/or one is not familiar enough with the Perl
41 stack discipline and other such arcana. XS comes to the rescue here:
42 instead of writing this glue C code in long-hand, one can write
43 a more concise short-hand I<description> of what should be done by
44 the glue, and let the XS compiler B<xsubpp> handle the rest.
46 The XS language allows one to describe the mapping between how the C
47 routine is used, and how the corresponding Perl routine is used. It
48 also allows creation of Perl routines which are directly translated to
49 C code and which are not related to a pre-existing C function. In cases
50 when the C interface coincides with the Perl interface, the XSUB
51 declaration is almost identical to a declaration of a C function (in K&R
52 style). In such circumstances, there is another tool called C<h2xs>
53 that is able to translate an entire C header file into a corresponding
54 XS file that will provide glue to the functions/macros described in
57 The XS compiler is called B<xsubpp>. This compiler creates
58 the constructs necessary to let an XSUB manipulate Perl values, and
59 creates the glue necessary to let Perl call the XSUB. The compiler
60 uses B<typemaps> to determine how to map C function parameters
61 and output values to Perl values and back. The default typemap
62 (which comes with Perl) handles many common C types. A supplementary
63 typemap may also be needed to handle any special structures and types
64 for the library being linked.
66 A file in XS format starts with a C language section which goes until the
67 first C<MODULE =Z<>> directive. Other XS directives and XSUB definitions
68 may follow this line. The "language" used in this part of the file
69 is usually referred to as the XS language.
71 See L<perlxstut> for a tutorial on the whole extension creation process.
73 Note: For some extensions, Dave Beazley's SWIG system may provide a
74 significantly more convenient mechanism for creating the extension glue
75 code. See L<http://www.cs.utah.edu/~beazley/SWIG> for more
80 Many of the examples which follow will concentrate on creating an interface
81 between Perl and the ONC+ RPC bind library functions. The rpcb_gettime()
82 function is used to demonstrate many features of the XS language. This
83 function has two parameters; the first is an input parameter and the second
84 is an output parameter. The function also returns a status value.
86 bool_t rpcb_gettime(const char *host, time_t *timep);
88 From C this function will be called with the following
94 status = rpcb_gettime( "localhost", &timep );
96 If an XSUB is created to offer a direct translation between this function
97 and Perl, then this XSUB will be used from Perl with the following code.
98 The $status and $timep variables will contain the output of the function.
101 $status = rpcb_gettime( "localhost", $timep );
103 The following XS file shows an XS subroutine, or XSUB, which
104 demonstrates one possible interface to the rpcb_gettime()
105 function. This XSUB represents a direct translation between
106 C and Perl and so preserves the interface even from Perl.
107 This XSUB will be invoked from Perl with the usage shown
108 above. Note that the first three #include statements, for
109 C<EXTERN.h>, C<perl.h>, and C<XSUB.h>, will always be present at the
110 beginning of an XS file. This approach and others will be
111 expanded later in this document.
118 MODULE = RPC PACKAGE = RPC
121 rpcb_gettime(host,timep)
127 Any extension to Perl, including those containing XSUBs,
128 should have a Perl module to serve as the bootstrap which
129 pulls the extension into Perl. This module will export the
130 extension's functions and variables to the Perl program and
131 will cause the extension's XSUBs to be linked into Perl.
132 The following module will be used for most of the examples
133 in this document and should be used from Perl with the C<use>
134 command as shown earlier. Perl modules are explained in
135 more detail later in this document.
141 @ISA = qw(Exporter DynaLoader);
142 @EXPORT = qw( rpcb_gettime );
147 Throughout this document a variety of interfaces to the rpcb_gettime()
148 XSUB will be explored. The XSUBs will take their parameters in different
149 orders or will take different numbers of parameters. In each case the
150 XSUB is an abstraction between Perl and the real C rpcb_gettime()
151 function, and the XSUB must always ensure that the real rpcb_gettime()
152 function is called with the correct parameters. This abstraction will
153 allow the programmer to create a more Perl-like interface to the C
156 =head2 The Anatomy of an XSUB
158 The simplest XSUBs consist of 3 parts: a description of the return
159 value, the name of the XSUB routine and the names of its arguments,
160 and a description of types or formats of the arguments.
162 The following XSUB allows a Perl program to access a C library function
163 called sin(). The XSUB will imitate the C function which takes a single
164 argument and returns a single value.
170 When using parameters with C pointer types, as in
172 double string_to_double(char *s);
174 there may be two ways to describe this argument to B<xsubpp>:
179 Both these XS declarations correspond to the C<char*> C type, but they have
180 different semantics. It is convenient to think that the indirection operator
181 C<*> should be considered as a part of the type and the address operator C<&>
182 should be considered part of the variable. See L<"The Typemap"> and
183 L<"The & Unary Operator"> for more info about handling qualifiers and unary
184 operators in C types.
186 The function name and the return type must be placed on
187 separate lines and should be flush left-adjusted.
195 The function body may be indented or left-adjusted. The following example
196 shows a function with its body left-adjusted. Most examples in this
197 document will indent the body for better readability.
205 More complicated XSUBs may contain many other sections. Each section of
206 an XSUB starts with the corresponding keyword, such as INIT: or CLEANUP:.
207 However, the first two lines of an XSUB always contain the same data:
208 descriptions of the return type and the names of the function and its
209 parameters. Whatever immediately follows these is considered to be
210 an INPUT: section unless explicitly marked with another keyword.
211 (See L<The INPUT: Keyword>.)
213 An XSUB section continues until another section-start keyword is found.
215 =head2 The Argument Stack
217 The Perl argument stack is used to store the values which are
218 sent as parameters to the XSUB and to store the XSUB's
219 return value(s). In reality all Perl functions (including non-XSUB
220 ones) keep their values on this stack all the same time, each limited
221 to its own range of positions on the stack. In this document the
222 first position on that stack which belongs to the active
223 function will be referred to as position 0 for that function.
225 XSUBs refer to their stack arguments with the macro B<ST(x)>, where I<x>
226 refers to a position in this XSUB's part of the stack. Position 0 for that
227 function would be known to the XSUB as ST(0). The XSUB's incoming
228 parameters and outgoing return values always begin at ST(0). For many
229 simple cases the B<xsubpp> compiler will generate the code necessary to
230 handle the argument stack by embedding code fragments found in the
231 typemaps. In more complex cases the programmer must supply the code.
233 =head2 The RETVAL Variable
235 The RETVAL variable is a special C variable that is declared automatically
236 for you. The C type of RETVAL matches the return type of the C library
237 function. The B<xsubpp> compiler will declare this variable in each XSUB
238 with non-C<void> return type. By default the generated C function
239 will use RETVAL to hold the return value of the C library function being
240 called. In simple cases the value of RETVAL will be placed in ST(0) of
241 the argument stack where it can be received by Perl as the return value
244 If the XSUB has a return type of C<void> then the compiler will
245 not declare a RETVAL variable for that function. When using
246 a PPCODE: section no manipulation of the RETVAL variable is required, the
247 section may use direct stack manipulation to place output values on the stack.
249 If PPCODE: directive is not used, C<void> return value should be used
250 only for subroutines which do not return a value, I<even if> CODE:
251 directive is used which sets ST(0) explicitly.
253 Older versions of this document recommended to use C<void> return
254 value in such cases. It was discovered that this could lead to
255 segfaults in cases when XSUB was I<truly> C<void>. This practice is
256 now deprecated, and may be not supported at some future version. Use
257 the return value C<SV *> in such cases. (Currently C<xsubpp> contains
258 some heuristic code which tries to disambiguate between "truly-void"
259 and "old-practice-declared-as-void" functions. Hence your code is at
260 mercy of this heuristics unless you use C<SV *> as return value.)
262 =head2 The MODULE Keyword
264 The MODULE keyword is used to start the XS code and to
265 specify the package of the functions which are being
266 defined. All text preceding the first MODULE keyword is
267 considered C code and is passed through to the output
268 untouched. Every XS module will have a bootstrap function
269 which is used to hook the XSUBs into Perl. The package name
270 of this bootstrap function will match the value of the last
271 MODULE statement in the XS source files. The value of
272 MODULE should always remain constant within the same XS
273 file, though this is not required.
275 The following example will start the XS code and will place
276 all functions in a package named RPC.
280 =head2 The PACKAGE Keyword
282 When functions within an XS source file must be separated into packages
283 the PACKAGE keyword should be used. This keyword is used with the MODULE
284 keyword and must follow immediately after it when used.
286 MODULE = RPC PACKAGE = RPC
288 [ XS code in package RPC ]
290 MODULE = RPC PACKAGE = RPCB
292 [ XS code in package RPCB ]
294 MODULE = RPC PACKAGE = RPC
296 [ XS code in package RPC ]
298 Although this keyword is optional and in some cases provides redundant
299 information it should always be used. This keyword will ensure that the
300 XSUBs appear in the desired package.
302 =head2 The PREFIX Keyword
304 The PREFIX keyword designates prefixes which should be
305 removed from the Perl function names. If the C function is
306 C<rpcb_gettime()> and the PREFIX value is C<rpcb_> then Perl will
307 see this function as C<gettime()>.
309 This keyword should follow the PACKAGE keyword when used.
310 If PACKAGE is not used then PREFIX should follow the MODULE
313 MODULE = RPC PREFIX = rpc_
315 MODULE = RPC PACKAGE = RPCB PREFIX = rpcb_
317 =head2 The OUTPUT: Keyword
319 The OUTPUT: keyword indicates that certain function parameters should be
320 updated (new values made visible to Perl) when the XSUB terminates or that
321 certain values should be returned to the calling Perl function. For
322 simple functions which have no CODE: or PPCODE: section,
323 such as the sin() function above, the RETVAL variable is
324 automatically designated as an output value. For more complex functions
325 the B<xsubpp> compiler will need help to determine which variables are output
328 This keyword will normally be used to complement the CODE: keyword.
329 The RETVAL variable is not recognized as an output variable when the
330 CODE: keyword is present. The OUTPUT: keyword is used in this
331 situation to tell the compiler that RETVAL really is an output
334 The OUTPUT: keyword can also be used to indicate that function parameters
335 are output variables. This may be necessary when a parameter has been
336 modified within the function and the programmer would like the update to
340 rpcb_gettime(host,timep)
346 The OUTPUT: keyword will also allow an output parameter to
347 be mapped to a matching piece of code rather than to a
351 rpcb_gettime(host,timep)
355 timep sv_setnv(ST(1), (double)timep);
357 B<xsubpp> emits an automatic C<SvSETMAGIC()> for all parameters in the
358 OUTPUT section of the XSUB, except RETVAL. This is the usually desired
359 behavior, as it takes care of properly invoking 'set' magic on output
360 parameters (needed for hash or array element parameters that must be
361 created if they didn't exist). If for some reason, this behavior is
362 not desired, the OUTPUT section may contain a C<SETMAGIC: DISABLE> line
363 to disable it for the remainder of the parameters in the OUTPUT section.
364 Likewise, C<SETMAGIC: ENABLE> can be used to reenable it for the
365 remainder of the OUTPUT section. See L<perlguts> for more details
368 =head2 The CODE: Keyword
370 This keyword is used in more complicated XSUBs which require
371 special handling for the C function. The RETVAL variable is
372 still declared, but it will not be returned unless it is specified
373 in the OUTPUT: section.
375 The following XSUB is for a C function which requires special handling of
376 its parameters. The Perl usage is given first.
378 $status = rpcb_gettime( "localhost", $timep );
383 rpcb_gettime(host,timep)
387 RETVAL = rpcb_gettime( host, &timep );
392 =head2 The INIT: Keyword
394 The INIT: keyword allows initialization to be inserted into the XSUB before
395 the compiler generates the call to the C function. Unlike the CODE: keyword
396 above, this keyword does not affect the way the compiler handles RETVAL.
399 rpcb_gettime(host,timep)
403 printf("# Host is %s\n", host );
407 Another use for the INIT: section is to check for preconditions before
408 making a call to the C function:
415 if (a == 0 && b == 0)
418 croak("lldiv: cannot divide by 0");
420 =head2 The NO_INIT Keyword
422 The NO_INIT keyword is used to indicate that a function
423 parameter is being used only as an output value. The B<xsubpp>
424 compiler will normally generate code to read the values of
425 all function parameters from the argument stack and assign
426 them to C variables upon entry to the function. NO_INIT
427 will tell the compiler that some parameters will be used for
428 output rather than for input and that they will be handled
429 before the function terminates.
431 The following example shows a variation of the rpcb_gettime() function.
432 This function uses the timep variable only as an output variable and does
433 not care about its initial contents.
436 rpcb_gettime(host,timep)
438 time_t &timep = NO_INIT
442 =head2 Initializing Function Parameters
444 C function parameters are normally initialized with their values from
445 the argument stack (which in turn contains the parameters that were
446 passed to the XSUB from Perl). The typemaps contain the
447 code segments which are used to translate the Perl values to
448 the C parameters. The programmer, however, is allowed to
449 override the typemaps and supply alternate (or additional)
450 initialization code. Initialization code starts with the first
451 C<=>, C<;> or C<+> on a line in the INPUT: section. The only
452 exception happens if this C<;> terminates the line, then this C<;>
455 The following code demonstrates how to supply initialization code for
456 function parameters. The initialization code is eval'd within double
457 quotes by the compiler before it is added to the output so anything
458 which should be interpreted literally [mainly C<$>, C<@>, or C<\\>]
459 must be protected with backslashes. The variables $var, $arg,
460 and $type can be used as in typemaps.
463 rpcb_gettime(host,timep)
464 char *host = (char *)SvPV($arg,PL_na);
469 This should not be used to supply default values for parameters. One
470 would normally use this when a function parameter must be processed by
471 another library function before it can be used. Default parameters are
472 covered in the next section.
474 If the initialization begins with C<=>, then it is output in
475 the declaration for the input variable, replacing the initialization
476 supplied by the typemap. If the initialization
477 begins with C<;> or C<+>, then it is performed after
478 all of the input variables have been declared. In the C<;>
479 case the initialization normally supplied by the typemap is not performed.
480 For the C<+> case, the declaration for the variable will include the
481 initialization from the typemap. A global
482 variable, C<%v>, is available for the truly rare case where
483 information from one initialization is needed in another
486 Here's a truly obscure example:
489 rpcb_gettime(host,timep)
490 time_t &timep ; /* \$v{timep}=@{[$v{timep}=$arg]} */
491 char *host + SvOK($v{timep}) ? SvPV($arg,PL_na) : NULL;
495 The construct C<\$v{timep}=@{[$v{timep}=$arg]}> used in the above
496 example has a two-fold purpose: first, when this line is processed by
497 B<xsubpp>, the Perl snippet C<$v{timep}=$arg> is evaluated. Second,
498 the text of the evaluated snippet is output into the generated C file
499 (inside a C comment)! During the processing of C<char *host> line,
500 $arg will evaluate to C<ST(0)>, and C<$v{timep}> will evaluate to
503 =head2 Default Parameter Values
505 Default values for XSUB arguments can be specified by
506 placing an assignment statement in the parameter list. The
507 default value may be a number or a string. Defaults should
508 always be used on the right-most parameters only.
510 To allow the XSUB for rpcb_gettime() to have a default host
511 value the parameters to the XSUB could be rearranged. The
512 XSUB will then call the real rpcb_gettime() function with
513 the parameters in the correct order. This XSUB can be called
514 from Perl with either of the following statements:
516 $status = rpcb_gettime( $timep, $host );
518 $status = rpcb_gettime( $timep );
520 The XSUB will look like the code which follows. A CODE:
521 block is used to call the real rpcb_gettime() function with
522 the parameters in the correct order for that function.
525 rpcb_gettime(timep,host="localhost")
527 time_t timep = NO_INIT
529 RETVAL = rpcb_gettime( host, &timep );
534 =head2 The PREINIT: Keyword
536 The PREINIT: keyword allows extra variables to be declared immediately
537 before or after the declartions of the parameters from the INPUT: section
540 If a variable is declared inside a CODE: section it will follow any typemap
541 code that is emitted for the input parameters. This may result in the
542 declaration ending up after C code, which is C syntax error. Similar
543 errors may happen with an explicit C<;>-type or C<+>-type initialization of
544 parameters is used (see L<"Initializing Function Parameters">). Declaring
545 these variables in an INIT: section will not help.
547 In such cases, to force an additional variable to be declared together
548 with declarations of other variables, place the declaration into a
549 PREINIT: section. The PREINIT: keyword may be used one or more times
552 The following examples are equivalent, but if the code is using complex
553 typemaps then the first example is safer.
557 time_t timep = NO_INIT
559 char *host = "localhost";
561 RETVAL = rpcb_gettime( host, &timep );
566 For this particular case an INIT: keyword would generate the
567 same C code as the PREINIT: keyword. Another correct, but error-prone example:
571 time_t timep = NO_INIT
573 char *host = "localhost";
574 RETVAL = rpcb_gettime( host, &timep );
579 Another way to declare C<host> is to use a C block in the CODE: section:
583 time_t timep = NO_INIT
586 char *host = "localhost";
587 RETVAL = rpcb_gettime( host, &timep );
593 The ability to put additional declarations before the typemap entries are
594 processed is very handy in the cases when typemap conversions manipulate
600 MyState st = global_state;
604 reset_to(global_state, st);
606 Here we suppose that conversion to C<MyObject> in the INPUT: section and from
607 MyObject when processing RETVAL will modify a global variable C<global_state>.
608 After these conversions are performed, we restore the old value of
609 C<global_state> (to avoid memory leaks, for example).
611 There is another way to trade clarity for compactness: INPUT sections allow
612 declaration of C variables which do not appear in the parameter list of
613 a subroutine. Thus the above code for mutate() can be rewritten as
617 MyState st = global_state;
620 reset_to(global_state, st);
622 and the code for rpcb_gettime() can be rewritten as
626 time_t timep = NO_INIT
627 char *host = "localhost";
634 =head2 The SCOPE: Keyword
636 The SCOPE: keyword allows scoping to be enabled for a particular XSUB. If
637 enabled, the XSUB will invoke ENTER and LEAVE automatically.
639 To support potentially complex type mappings, if a typemap entry used
640 by an XSUB contains a comment like C</*scope*/> then scoping will
641 be automatically enabled for that XSUB.
651 =head2 The INPUT: Keyword
653 The XSUB's parameters are usually evaluated immediately after entering the
654 XSUB. The INPUT: keyword can be used to force those parameters to be
655 evaluated a little later. The INPUT: keyword can be used multiple times
656 within an XSUB and can be used to list one or more input variables. This
657 keyword is used with the PREINIT: keyword.
659 The following example shows how the input parameter C<timep> can be
660 evaluated late, after a PREINIT.
663 rpcb_gettime(host,timep)
670 RETVAL = rpcb_gettime( host, &tt );
676 The next example shows each input parameter evaluated late.
679 rpcb_gettime(host,timep)
690 RETVAL = rpcb_gettime( h, &tt );
696 Since INPUT sections allow declaration of C variables which do not appear
697 in the parameter list of a subroutine, this may be shortened to:
700 rpcb_gettime(host,timep)
706 RETVAL = rpcb_gettime( h, &tt );
712 (We used our knowledge that input conversion for C<char *> is a "simple" one,
713 thus C<host> is initialized on the declaration line, and our assignment
714 C<h = host> is not performed too early. Otherwise one would need to have the
715 assignment C<h = host> in a CODE: or INIT: section.)
717 =head2 Variable-length Parameter Lists
719 XSUBs can have variable-length parameter lists by specifying an ellipsis
720 C<(...)> in the parameter list. This use of the ellipsis is similar to that
721 found in ANSI C. The programmer is able to determine the number of
722 arguments passed to the XSUB by examining the C<items> variable which the
723 B<xsubpp> compiler supplies for all XSUBs. By using this mechanism one can
724 create an XSUB which accepts a list of parameters of unknown length.
726 The I<host> parameter for the rpcb_gettime() XSUB can be
727 optional so the ellipsis can be used to indicate that the
728 XSUB will take a variable number of parameters. Perl should
729 be able to call this XSUB with either of the following statements.
731 $status = rpcb_gettime( $timep, $host );
733 $status = rpcb_gettime( $timep );
735 The XS code, with ellipsis, follows.
738 rpcb_gettime(timep, ...)
739 time_t timep = NO_INIT
741 char *host = "localhost";
745 host = (char *)SvPV(ST(1), n_a);
746 RETVAL = rpcb_gettime( host, &timep );
751 =head2 The C_ARGS: Keyword
753 The C_ARGS: keyword allows creating of XSUBS which have different
754 calling sequence from Perl than from C, without a need to write
755 CODE: or PPCODE: section. The contents of the C_ARGS: paragraph is
756 put as the argument to the called C function without any change.
758 For example, suppose that a C function is declared as
760 symbolic nth_derivative(int n, symbolic function, int flags);
762 and that the default flags are kept in a global C variable
763 C<default_flags>. Suppose that you want to create an interface which
766 $second_deriv = $function->nth_derivative(2);
768 To do this, declare the XSUB as
771 nth_derivative(function, n)
775 n, function, default_flags
777 =head2 The PPCODE: Keyword
779 The PPCODE: keyword is an alternate form of the CODE: keyword and is used
780 to tell the B<xsubpp> compiler that the programmer is supplying the code to
781 control the argument stack for the XSUBs return values. Occasionally one
782 will want an XSUB to return a list of values rather than a single value.
783 In these cases one must use PPCODE: and then explicitly push the list of
784 values on the stack. The PPCODE: and CODE: keywords should not be used
785 together within the same XSUB.
787 The actual difference between PPCODE: and CODE: sections is in the
788 initialization of C<SP> macro (which stands for the I<current> Perl
789 stack pointer), and in the handling of data on the stack when returning
790 from an XSUB. In CODE: sections SP preserves the value which was on
791 entry to the XSUB: SP is on the function pointer (which follows the
792 last parameter). In PPCODE: sections SP is moved backward to the
793 beginning of the parameter list, which allows C<PUSH*()> macros
794 to place output values in the place Perl expects them to be when
795 the XSUB returns back to Perl.
797 The generated trailer for a CODE: section ensures that the number of return
798 values Perl will see is either 0 or 1 (depending on the C<void>ness of the
799 return value of the C function, and heuristics mentioned in
800 L<"The RETVAL Variable">). The trailer generated for a PPCODE: section
801 is based on the number of return values and on the number of times
802 C<SP> was updated by C<[X]PUSH*()> macros.
804 Note that macros C<ST(i)>, C<XST_m*()> and C<XSRETURN*()> work equally
805 well in CODE: sections and PPCODE: sections.
807 The following XSUB will call the C rpcb_gettime() function
808 and will return its two output values, timep and status, to
809 Perl as a single list.
818 status = rpcb_gettime( host, &timep );
820 PUSHs(sv_2mortal(newSViv(status)));
821 PUSHs(sv_2mortal(newSViv(timep)));
823 Notice that the programmer must supply the C code necessary
824 to have the real rpcb_gettime() function called and to have
825 the return values properly placed on the argument stack.
827 The C<void> return type for this function tells the B<xsubpp> compiler that
828 the RETVAL variable is not needed or used and that it should not be created.
829 In most scenarios the void return type should be used with the PPCODE:
832 The EXTEND() macro is used to make room on the argument
833 stack for 2 return values. The PPCODE: directive causes the
834 B<xsubpp> compiler to create a stack pointer available as C<SP>, and it
835 is this pointer which is being used in the EXTEND() macro.
836 The values are then pushed onto the stack with the PUSHs()
839 Now the rpcb_gettime() function can be used from Perl with
840 the following statement.
842 ($status, $timep) = rpcb_gettime("localhost");
844 When handling output parameters with a PPCODE section, be sure to handle
845 'set' magic properly. See L<perlguts> for details about 'set' magic.
847 =head2 Returning Undef And Empty Lists
849 Occasionally the programmer will want to return simply
850 C<undef> or an empty list if a function fails rather than a
851 separate status value. The rpcb_gettime() function offers
852 just this situation. If the function succeeds we would like
853 to have it return the time and if it fails we would like to
854 have undef returned. In the following Perl code the value
855 of $timep will either be undef or it will be a valid time.
857 $timep = rpcb_gettime( "localhost" );
859 The following XSUB uses the C<SV *> return type as a mnemonic only,
860 and uses a CODE: block to indicate to the compiler
861 that the programmer has supplied all the necessary code. The
862 sv_newmortal() call will initialize the return value to undef, making that
863 the default return value.
872 ST(0) = sv_newmortal();
873 if( rpcb_gettime( host, &timep ) )
874 sv_setnv( ST(0), (double)timep);
876 The next example demonstrates how one would place an explicit undef in the
877 return value, should the need arise.
886 ST(0) = sv_newmortal();
887 if( rpcb_gettime( host, &timep ) ){
888 sv_setnv( ST(0), (double)timep);
891 ST(0) = &PL_sv_undef;
894 To return an empty list one must use a PPCODE: block and
895 then not push return values on the stack.
903 if( rpcb_gettime( host, &timep ) )
904 PUSHs(sv_2mortal(newSViv(timep)));
906 /* Nothing pushed on stack, so an empty
907 * list is implicitly returned. */
910 Some people may be inclined to include an explicit C<return> in the above
911 XSUB, rather than letting control fall through to the end. In those
912 situations C<XSRETURN_EMPTY> should be used, instead. This will ensure that
913 the XSUB stack is properly adjusted. Consult L<perlguts/"API LISTING"> for
914 other C<XSRETURN> macros.
916 Since C<XSRETURN_*> macros can be used with CODE blocks as well, one can
917 rewrite this example as:
925 RETVAL = rpcb_gettime( host, &timep );
931 In fact, one can put this check into a CLEANUP: section as well. Together
932 with PREINIT: simplifications, this leads to:
942 =head2 The REQUIRE: Keyword
944 The REQUIRE: keyword is used to indicate the minimum version of the
945 B<xsubpp> compiler needed to compile the XS module. An XS module which
946 contains the following statement will compile with only B<xsubpp> version
951 =head2 The CLEANUP: Keyword
953 This keyword can be used when an XSUB requires special cleanup procedures
954 before it terminates. When the CLEANUP: keyword is used it must follow
955 any CODE:, PPCODE:, or OUTPUT: blocks which are present in the XSUB. The
956 code specified for the cleanup block will be added as the last statements
959 =head2 The BOOT: Keyword
961 The BOOT: keyword is used to add code to the extension's bootstrap
962 function. The bootstrap function is generated by the B<xsubpp> compiler and
963 normally holds the statements necessary to register any XSUBs with Perl.
964 With the BOOT: keyword the programmer can tell the compiler to add extra
965 statements to the bootstrap function.
967 This keyword may be used any time after the first MODULE keyword and should
968 appear on a line by itself. The first blank line after the keyword will
969 terminate the code block.
972 # The following message will be printed when the
973 # bootstrap function executes.
974 printf("Hello from the bootstrap!\n");
976 =head2 The VERSIONCHECK: Keyword
978 The VERSIONCHECK: keyword corresponds to B<xsubpp>'s C<-versioncheck> and
979 C<-noversioncheck> options. This keyword overrides the command line
980 options. Version checking is enabled by default. When version checking is
981 enabled the XS module will attempt to verify that its version matches the
982 version of the PM module.
984 To enable version checking:
988 To disable version checking:
990 VERSIONCHECK: DISABLE
992 =head2 The PROTOTYPES: Keyword
994 The PROTOTYPES: keyword corresponds to B<xsubpp>'s C<-prototypes> and
995 C<-noprototypes> options. This keyword overrides the command line options.
996 Prototypes are enabled by default. When prototypes are enabled XSUBs will
997 be given Perl prototypes. This keyword may be used multiple times in an XS
998 module to enable and disable prototypes for different parts of the module.
1000 To enable prototypes:
1004 To disable prototypes:
1008 =head2 The PROTOTYPE: Keyword
1010 This keyword is similar to the PROTOTYPES: keyword above but can be used to
1011 force B<xsubpp> to use a specific prototype for the XSUB. This keyword
1012 overrides all other prototype options and keywords but affects only the
1013 current XSUB. Consult L<perlsub/Prototypes> for information about Perl
1017 rpcb_gettime(timep, ...)
1018 time_t timep = NO_INIT
1021 char *host = "localhost";
1025 host = (char *)SvPV(ST(1), n_a);
1026 RETVAL = rpcb_gettime( host, &timep );
1031 =head2 The ALIAS: Keyword
1033 The ALIAS: keyword allows an XSUB to have two or more unique Perl names
1034 and to know which of those names was used when it was invoked. The Perl
1035 names may be fully-qualified with package names. Each alias is given an
1036 index. The compiler will setup a variable called C<ix> which contain the
1037 index of the alias which was used. When the XSUB is called with its
1038 declared name C<ix> will be 0.
1040 The following example will create aliases C<FOO::gettime()> and
1041 C<BAR::getit()> for this function.
1044 rpcb_gettime(host,timep)
1051 printf("# ix = %d\n", ix );
1055 =head2 The INTERFACE: Keyword
1057 This keyword declares the current XSUB as a keeper of the given
1058 calling signature. If some text follows this keyword, it is
1059 considered as a list of functions which have this signature, and
1060 should be attached to the current XSUB.
1062 For example, if you have 4 C functions multiply(), divide(), add(),
1063 subtract() all having the signature:
1065 symbolic f(symbolic, symbolic);
1067 you can make them all to use the same XSUB using this:
1070 interface_s_ss(arg1, arg2)
1077 (This is the complete XSUB code for 4 Perl functions!) Four generated
1078 Perl function share names with corresponding C functions.
1080 The advantage of this approach comparing to ALIAS: keyword is that there
1081 is no need to code a switch statement, each Perl function (which shares
1082 the same XSUB) knows which C function it should call. Additionally, one
1083 can attach an extra function remainder() at runtime by using
1085 CV *mycv = newXSproto("Symbolic::remainder",
1086 XS_Symbolic_interface_s_ss, __FILE__, "$$");
1087 XSINTERFACE_FUNC_SET(mycv, remainder);
1089 say, from another XSUB. (This example supposes that there was no
1090 INTERFACE_MACRO: section, otherwise one needs to use something else instead of
1091 C<XSINTERFACE_FUNC_SET>, see the next section.)
1093 =head2 The INTERFACE_MACRO: Keyword
1095 This keyword allows one to define an INTERFACE using a different way
1096 to extract a function pointer from an XSUB. The text which follows
1097 this keyword should give the name of macros which would extract/set a
1098 function pointer. The extractor macro is given return type, C<CV*>,
1099 and C<XSANY.any_dptr> for this C<CV*>. The setter macro is given cv,
1100 and the function pointer.
1102 The default value is C<XSINTERFACE_FUNC> and C<XSINTERFACE_FUNC_SET>.
1103 An INTERFACE keyword with an empty list of functions can be omitted if
1104 INTERFACE_MACRO keyword is used.
1106 Suppose that in the previous example functions pointers for
1107 multiply(), divide(), add(), subtract() are kept in a global C array
1108 C<fp[]> with offsets being C<multiply_off>, C<divide_off>, C<add_off>,
1109 C<subtract_off>. Then one can use
1111 #define XSINTERFACE_FUNC_BYOFFSET(ret,cv,f) \
1112 ((XSINTERFACE_CVT(ret,))fp[CvXSUBANY(cv).any_i32])
1113 #define XSINTERFACE_FUNC_BYOFFSET_set(cv,f) \
1114 CvXSUBANY(cv).any_i32 = CAT2( f, _off )
1119 interface_s_ss(arg1, arg2)
1123 XSINTERFACE_FUNC_BYOFFSET
1124 XSINTERFACE_FUNC_BYOFFSET_set
1131 =head2 The INCLUDE: Keyword
1133 This keyword can be used to pull other files into the XS module. The other
1134 files may have XS code. INCLUDE: can also be used to run a command to
1135 generate the XS code to be pulled into the module.
1137 The file F<Rpcb1.xsh> contains our C<rpcb_gettime()> function:
1140 rpcb_gettime(host,timep)
1146 The XS module can use INCLUDE: to pull that file into it.
1150 If the parameters to the INCLUDE: keyword are followed by a pipe (C<|>) then
1151 the compiler will interpret the parameters as a command.
1153 INCLUDE: cat Rpcb1.xsh |
1155 =head2 The CASE: Keyword
1157 The CASE: keyword allows an XSUB to have multiple distinct parts with each
1158 part acting as a virtual XSUB. CASE: is greedy and if it is used then all
1159 other XS keywords must be contained within a CASE:. This means nothing may
1160 precede the first CASE: in the XSUB and anything following the last CASE: is
1161 included in that case.
1163 A CASE: might switch via a parameter of the XSUB, via the C<ix> ALIAS:
1164 variable (see L<"The ALIAS: Keyword">), or maybe via the C<items> variable
1165 (see L<"Variable-length Parameter Lists">). The last CASE: becomes the
1166 B<default> case if it is not associated with a conditional. The following
1167 example shows CASE switched via C<ix> with a function C<rpcb_gettime()>
1168 having an alias C<x_gettime()>. When the function is called as
1169 C<rpcb_gettime()> its parameters are the usual C<(char *host, time_t *timep)>,
1170 but when the function is called as C<x_gettime()> its parameters are
1171 reversed, C<(time_t *timep, char *host)>.
1179 # 'a' is timep, 'b' is host
1183 RETVAL = rpcb_gettime( b, &a );
1188 # 'a' is host, 'b' is timep
1195 That function can be called with either of the following statements. Note
1196 the different argument lists.
1198 $status = rpcb_gettime( $host, $timep );
1200 $status = x_gettime( $timep, $host );
1202 =head2 The & Unary Operator
1204 The C<&> unary operator in the INPUT: section is used to tell B<xsubpp>
1205 that it should convert a Perl value to/from C using the C type to the left
1206 of C<&>, but provide a pointer to this value when the C function is called.
1208 This is useful to avoid a CODE: block for a C function which takes a parameter
1209 by reference. Typically, the parameter should be not a pointer type (an
1210 C<int> or C<long> but not a C<int*> or C<long*>).
1212 The following XSUB will generate incorrect C code. The B<xsubpp> compiler will
1213 turn this into code which calls C<rpcb_gettime()> with parameters C<(char
1214 *host, time_t timep)>, but the real C<rpcb_gettime()> wants the C<timep>
1215 parameter to be of type C<time_t*> rather than C<time_t>.
1218 rpcb_gettime(host,timep)
1224 That problem is corrected by using the C<&> operator. The B<xsubpp> compiler
1225 will now turn this into code which calls C<rpcb_gettime()> correctly with
1226 parameters C<(char *host, time_t *timep)>. It does this by carrying the
1227 C<&> through, so the function call looks like C<rpcb_gettime(host, &timep)>.
1230 rpcb_gettime(host,timep)
1236 =head2 Inserting Comments and C Preprocessor Directives
1238 C preprocessor directives are allowed within BOOT:, PREINIT: INIT:,
1239 CODE:, PPCODE:, and CLEANUP: blocks, as well as outside the functions.
1240 Comments are allowed anywhere after the MODULE keyword. The compiler
1241 will pass the preprocessor directives through untouched and will remove
1242 the commented lines.
1244 Comments can be added to XSUBs by placing a C<#> as the first
1245 non-whitespace of a line. Care should be taken to avoid making the
1246 comment look like a C preprocessor directive, lest it be interpreted as
1247 such. The simplest way to prevent this is to put whitespace in front of
1250 If you use preprocessor directives to choose one of two
1251 versions of a function, use
1254 #else /* ... version2 */
1264 because otherwise B<xsubpp> will believe that you made a duplicate
1265 definition of the function. Also, put a blank line before the
1266 #else/#endif so it will not be seen as part of the function body.
1268 =head2 Using XS With C++
1270 If an XSUB name contains C<::>, it is considered to be a C++ method.
1271 The generated Perl function will assume that
1272 its first argument is an object pointer. The object pointer
1273 will be stored in a variable called THIS. The object should
1274 have been created by C++ with the new() function and should
1275 be blessed by Perl with the sv_setref_pv() macro. The
1276 blessing of the object by Perl can be handled by a typemap. An example
1277 typemap is shown at the end of this section.
1279 If the return type of the XSUB includes C<static>, the method is considered
1280 to be a static method. It will call the C++
1281 function using the class::method() syntax. If the method is not static
1282 the function will be called using the THIS-E<gt>method() syntax.
1284 The next examples will use the following C++ class.
1291 void set_blue( int );
1297 The XSUBs for the blue() and set_blue() methods are defined with the class
1298 name but the parameter for the object (THIS, or "self") is implicit and is
1305 color::set_blue( val )
1308 Both Perl functions will expect an object as the first parameter. In the
1309 generated C++ code the object is called C<THIS>, and the method call will
1310 be performed on this object. So in the C++ code the blue() and set_blue()
1311 methods will be called as this:
1313 RETVAL = THIS->blue();
1315 THIS->set_blue( val );
1317 If the function's name is B<DESTROY> then the C++ C<delete> function will be
1318 called and C<THIS> will be given as its parameter. The generated C++ code for
1323 will look like this:
1325 color *THIS = ...; // Initialized as in typemap
1329 If the function's name is B<new> then the C++ C<new> function will be called
1330 to create a dynamic C++ object. The XSUB will expect the class name, which
1331 will be kept in a variable called C<CLASS>, to be given as the first
1337 The generated C++ code will call C<new>.
1339 RETVAL = new color();
1341 The following is an example of a typemap that could be used for this C++
1348 # The Perl object is blessed into 'CLASS', which should be a
1349 # char* having the name of the package for the blessing.
1351 sv_setref_pv( $arg, CLASS, (void*)$var );
1355 if( sv_isobject($arg) && (SvTYPE(SvRV($arg)) == SVt_PVMG) )
1356 $var = ($type)SvIV((SV*)SvRV( $arg ));
1358 warn( \"${Package}::$func_name() -- $var is not a blessed SV reference\" );
1362 =head2 Interface Strategy
1364 When designing an interface between Perl and a C library a straight
1365 translation from C to XS (such as created by C<h2xs -x>) is often sufficient.
1366 However, sometimes the interface will look
1367 very C-like and occasionally nonintuitive, especially when the C function
1368 modifies one of its parameters, or returns failure inband (as in "negative
1369 return values mean failure"). In cases where the programmer wishes to
1370 create a more Perl-like interface the following strategy may help to
1371 identify the more critical parts of the interface.
1373 Identify the C functions with input/output or output parameters. The XSUBs for
1374 these functions may be able to return lists to Perl.
1376 Identify the C functions which use some inband info as an indication
1377 of failure. They may be
1378 candidates to return undef or an empty list in case of failure. If the
1379 failure may be detected without a call to the C function, you may want to use
1380 an INIT: section to report the failure. For failures detectable after the C
1381 function returns one may want to use a CLEANUP: section to process the
1382 failure. In more complicated cases use CODE: or PPCODE: sections.
1384 If many functions use the same failure indication based on the return value,
1385 you may want to create a special typedef to handle this situation. Put
1387 typedef int negative_is_failure;
1389 near the beginning of XS file, and create an OUTPUT typemap entry
1390 for C<negative_is_failure> which converts negative values to C<undef>, or
1391 maybe croak()s. After this the return value of type C<negative_is_failure>
1392 will create more Perl-like interface.
1394 Identify which values are used by only the C and XSUB functions
1395 themselves, say, when a parameter to a function should be a contents of a
1396 global variable. If Perl does not need to access the contents of the value
1397 then it may not be necessary to provide a translation for that value
1400 Identify the pointers in the C function parameter lists and return
1401 values. Some pointers may be used to implement input/output or
1402 output parameters, they can be handled in XS with the C<&> unary operator,
1403 and, possibly, using the NO_INIT keyword.
1404 Some others will require handling of types like C<int *>, and one needs
1405 to decide what a useful Perl translation will do in such a case. When
1406 the semantic is clear, it is advisable to put the translation into a typemap
1409 Identify the structures used by the C functions. In many
1410 cases it may be helpful to use the T_PTROBJ typemap for
1411 these structures so they can be manipulated by Perl as
1412 blessed objects. (This is handled automatically by C<h2xs -x>.)
1414 If the same C type is used in several different contexts which require
1415 different translations, C<typedef> several new types mapped to this C type,
1416 and create separate F<typemap> entries for these new types. Use these
1417 types in declarations of return type and parameters to XSUBs.
1419 =head2 Perl Objects And C Structures
1421 When dealing with C structures one should select either
1422 B<T_PTROBJ> or B<T_PTRREF> for the XS type. Both types are
1423 designed to handle pointers to complex objects. The
1424 T_PTRREF type will allow the Perl object to be unblessed
1425 while the T_PTROBJ type requires that the object be blessed.
1426 By using T_PTROBJ one can achieve a form of type-checking
1427 because the XSUB will attempt to verify that the Perl object
1428 is of the expected type.
1430 The following XS code shows the getnetconfigent() function which is used
1431 with ONC+ TIRPC. The getnetconfigent() function will return a pointer to a
1432 C structure and has the C prototype shown below. The example will
1433 demonstrate how the C pointer will become a Perl reference. Perl will
1434 consider this reference to be a pointer to a blessed object and will
1435 attempt to call a destructor for the object. A destructor will be
1436 provided in the XS source to free the memory used by getnetconfigent().
1437 Destructors in XS can be created by specifying an XSUB function whose name
1438 ends with the word B<DESTROY>. XS destructors can be used to free memory
1439 which may have been malloc'd by another XSUB.
1441 struct netconfig *getnetconfigent(const char *netid);
1443 A C<typedef> will be created for C<struct netconfig>. The Perl
1444 object will be blessed in a class matching the name of the C
1445 type, with the tag C<Ptr> appended, and the name should not
1446 have embedded spaces if it will be a Perl package name. The
1447 destructor will be placed in a class corresponding to the
1448 class of the object and the PREFIX keyword will be used to
1449 trim the name to the word DESTROY as Perl will expect.
1451 typedef struct netconfig Netconfig;
1453 MODULE = RPC PACKAGE = RPC
1456 getnetconfigent(netid)
1459 MODULE = RPC PACKAGE = NetconfigPtr PREFIX = rpcb_
1462 rpcb_DESTROY(netconf)
1465 printf("Now in NetconfigPtr::DESTROY\n");
1468 This example requires the following typemap entry. Consult the typemap
1469 section for more information about adding new typemaps for an extension.
1472 Netconfig * T_PTROBJ
1474 This example will be used with the following Perl statements.
1477 $netconf = getnetconfigent("udp");
1479 When Perl destroys the object referenced by $netconf it will send the
1480 object to the supplied XSUB DESTROY function. Perl cannot determine, and
1481 does not care, that this object is a C struct and not a Perl object. In
1482 this sense, there is no difference between the object created by the
1483 getnetconfigent() XSUB and an object created by a normal Perl subroutine.
1487 The typemap is a collection of code fragments which are used by the B<xsubpp>
1488 compiler to map C function parameters and values to Perl values. The
1489 typemap file may consist of three sections labeled C<TYPEMAP>, C<INPUT>, and
1490 C<OUTPUT>. An unlabelled initial section is assumed to be a C<TYPEMAP>
1491 section. The INPUT section tells
1492 the compiler how to translate Perl values
1493 into variables of certain C types. The OUTPUT section tells the compiler
1494 how to translate the values from certain C types into values Perl can
1495 understand. The TYPEMAP section tells the compiler which of the INPUT and
1496 OUTPUT code fragments should be used to map a given C type to a Perl value.
1497 The section labels C<TYPEMAP>, C<INPUT>, or C<OUTPUT> must begin
1498 in the first column on a line by themselves, and must be in uppercase.
1500 The default typemap in the C<ext> directory of the Perl source contains many
1501 useful types which can be used by Perl extensions. Some extensions define
1502 additional typemaps which they keep in their own directory. These
1503 additional typemaps may reference INPUT and OUTPUT maps in the main
1504 typemap. The B<xsubpp> compiler will allow the extension's own typemap to
1505 override any mappings which are in the default typemap.
1507 Most extensions which require a custom typemap will need only the TYPEMAP
1508 section of the typemap file. The custom typemap used in the
1509 getnetconfigent() example shown earlier demonstrates what may be the typical
1510 use of extension typemaps. That typemap is used to equate a C structure
1511 with the T_PTROBJ typemap. The typemap used by getnetconfigent() is shown
1512 here. Note that the C type is separated from the XS type with a tab and
1513 that the C unary operator C<*> is considered to be a part of the C type name.
1516 Netconfig *<tab>T_PTROBJ
1518 Here's a more complicated example: suppose that you wanted C<struct
1519 netconfig> to be blessed into the class C<Net::Config>. One way to do
1520 this is to use underscores (_) to separate package names, as follows:
1522 typedef struct netconfig * Net_Config;
1524 And then provide a typemap entry C<T_PTROBJ_SPECIAL> that maps underscores to
1525 double-colons (::), and declare C<Net_Config> to be of that type:
1529 Net_Config T_PTROBJ_SPECIAL
1533 if (sv_derived_from($arg, \"${(my $ntt=$ntype)=~s/_/::/g;\$ntt}\")) {
1534 IV tmp = SvIV((SV*)SvRV($arg));
1538 croak(\"$var is not of type ${(my $ntt=$ntype)=~s/_/::/g;\$ntt}\")
1542 sv_setref_pv($arg, \"${(my $ntt=$ntype)=~s/_/::/g;\$ntt}\",
1545 The INPUT and OUTPUT sections substitute underscores for double-colons
1546 on the fly, giving the desired effect. This example demonstrates some
1547 of the power and versatility of the typemap facility.
1551 File C<RPC.xs>: Interface to some ONC+ RPC bind library functions.
1557 #include <rpc/rpc.h>
1559 typedef struct netconfig Netconfig;
1561 MODULE = RPC PACKAGE = RPC
1564 rpcb_gettime(host="localhost")
1569 ST(0) = sv_newmortal();
1570 if( rpcb_gettime( host, &timep ) )
1571 sv_setnv( ST(0), (double)timep );
1574 getnetconfigent(netid="udp")
1577 MODULE = RPC PACKAGE = NetconfigPtr PREFIX = rpcb_
1580 rpcb_DESTROY(netconf)
1583 printf("NetconfigPtr::DESTROY\n");
1586 File C<typemap>: Custom typemap for RPC.xs.
1589 Netconfig * T_PTROBJ
1591 File C<RPC.pm>: Perl module for the RPC extension.
1597 @ISA = qw(Exporter DynaLoader);
1598 @EXPORT = qw(rpcb_gettime getnetconfigent);
1603 File C<rpctest.pl>: Perl test program for the RPC extension.
1607 $netconf = getnetconfigent();
1608 $a = rpcb_gettime();
1609 print "time = $a\n";
1610 print "netconf = $netconf\n";
1612 $netconf = getnetconfigent("tcp");
1613 $a = rpcb_gettime("poplar");
1614 print "time = $a\n";
1615 print "netconf = $netconf\n";
1620 This document covers features supported by C<xsubpp> 1.935.
1624 Originally written by Dean Roehrich <F<roehrich@cray.com>>.
1626 Maintained since 1996 by The Perl Porters <F<perlbug@perl.com>>.