This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Remove bad advice from perllocale.pod
[perl5.git] / pod / perlxs.pod
CommitLineData
a0d0e21e
LW
1=head1 NAME
2
8e07c86e 3perlxs - XS language reference manual
a0d0e21e
LW
4
5=head1 DESCRIPTION
6
7=head2 Introduction
8
9XS is a language used to create an extension interface
10between Perl and some C library which one wishes to use with
11Perl. The XS interface is combined with the library to
12create a new library which can be linked to Perl. An B<XSUB>
13is a function in the XS language and is the core component
14of the Perl application interface.
15
16The XS compiler is called B<xsubpp>. This compiler will embed
17the constructs necessary to let an XSUB, which is really a C
18function in disguise, manipulate Perl values and creates the
19glue necessary to let Perl access the XSUB. The compiler
20uses B<typemaps> to determine how to map C function parameters
21and variables to Perl values. The default typemap handles
22many common C types. A supplement typemap must be created
23to handle special structures and types for the library being
24linked.
25
cb1a09d0 26See L<perlxstut> for a tutorial on the whole extension creation process.
8e07c86e
AD
27
28=head2 On The Road
29
a5f75d66
AD
30Many of the examples which follow will concentrate on creating an interface
31between Perl and the ONC+ RPC bind library functions. The rpcb_gettime()
32function is used to demonstrate many features of the XS language. This
33function has two parameters; the first is an input parameter and the second
34is an output parameter. The function also returns a status value.
a0d0e21e
LW
35
36 bool_t rpcb_gettime(const char *host, time_t *timep);
37
38From C this function will be called with the following
39statements.
40
41 #include <rpc/rpc.h>
42 bool_t status;
43 time_t timep;
44 status = rpcb_gettime( "localhost", &timep );
45
46If an XSUB is created to offer a direct translation between this function
47and Perl, then this XSUB will be used from Perl with the following code.
48The $status and $timep variables will contain the output of the function.
49
50 use RPC;
51 $status = rpcb_gettime( "localhost", $timep );
52
53The following XS file shows an XS subroutine, or XSUB, which
54demonstrates one possible interface to the rpcb_gettime()
55function. This XSUB represents a direct translation between
56C and Perl and so preserves the interface even from Perl.
57This XSUB will be invoked from Perl with the usage shown
58above. Note that the first three #include statements, for
59C<EXTERN.h>, C<perl.h>, and C<XSUB.h>, will always be present at the
60beginning of an XS file. This approach and others will be
61expanded later in this document.
62
63 #include "EXTERN.h"
64 #include "perl.h"
65 #include "XSUB.h"
66 #include <rpc/rpc.h>
67
68 MODULE = RPC PACKAGE = RPC
69
70 bool_t
71 rpcb_gettime(host,timep)
8e07c86e
AD
72 char *host
73 time_t &timep
a0d0e21e
LW
74 OUTPUT:
75 timep
76
77Any extension to Perl, including those containing XSUBs,
78should have a Perl module to serve as the bootstrap which
79pulls the extension into Perl. This module will export the
80extension's functions and variables to the Perl program and
81will cause the extension's XSUBs to be linked into Perl.
82The following module will be used for most of the examples
83in this document and should be used from Perl with the C<use>
84command as shown earlier. Perl modules are explained in
85more detail later in this document.
86
87 package RPC;
88
89 require Exporter;
90 require DynaLoader;
91 @ISA = qw(Exporter DynaLoader);
92 @EXPORT = qw( rpcb_gettime );
93
94 bootstrap RPC;
95 1;
96
97Throughout this document a variety of interfaces to the rpcb_gettime()
98XSUB will be explored. The XSUBs will take their parameters in different
99orders or will take different numbers of parameters. In each case the
100XSUB is an abstraction between Perl and the real C rpcb_gettime()
101function, and the XSUB must always ensure that the real rpcb_gettime()
102function is called with the correct parameters. This abstraction will
103allow the programmer to create a more Perl-like interface to the C
104function.
105
106=head2 The Anatomy of an XSUB
107
8e07c86e
AD
108The following XSUB allows a Perl program to access a C library function
109called sin(). The XSUB will imitate the C function which takes a single
110argument and returns a single value.
a0d0e21e
LW
111
112 double
113 sin(x)
8e07c86e 114 double x
a0d0e21e 115
8e07c86e
AD
116When using C pointers the indirection operator C<*> should be considered
117part of the type and the address operator C<&> should be considered part of
118the variable, as is demonstrated in the rpcb_gettime() function above. See
119the section on typemaps for more about handling qualifiers and unary
120operators in C types.
a0d0e21e 121
a0d0e21e
LW
122The function name and the return type must be placed on
123separate lines.
124
125 INCORRECT CORRECT
126
127 double sin(x) double
8e07c86e
AD
128 double x sin(x)
129 double x
a0d0e21e 130
c07a80fd
PP
131The function body may be indented or left-adjusted. The following example
132shows a function with its body left-adjusted. Most examples in this
133document will indent the body.
134
135 CORRECT
136
137 double
138 sin(x)
139 double x
140
a0d0e21e
LW
141=head2 The Argument Stack
142
143The argument stack is used to store the values which are
144sent as parameters to the XSUB and to store the XSUB's
145return value. In reality all Perl functions keep their
146values on this stack at the same time, each limited to its
147own range of positions on the stack. In this document the
148first position on that stack which belongs to the active
149function will be referred to as position 0 for that function.
150
8e07c86e
AD
151XSUBs refer to their stack arguments with the macro B<ST(x)>, where I<x>
152refers to a position in this XSUB's part of the stack. Position 0 for that
a0d0e21e
LW
153function would be known to the XSUB as ST(0). The XSUB's incoming
154parameters and outgoing return values always begin at ST(0). For many
155simple cases the B<xsubpp> compiler will generate the code necessary to
156handle the argument stack by embedding code fragments found in the
157typemaps. In more complex cases the programmer must supply the code.
158
159=head2 The RETVAL Variable
160
161The RETVAL variable is a magic variable which always matches
162the return type of the C library function. The B<xsubpp> compiler will
163supply this variable in each XSUB and by default will use it to hold the
164return value of the C library function being called. In simple cases the
165value of RETVAL will be placed in ST(0) of the argument stack where it can
166be received by Perl as the return value of the XSUB.
167
168If the XSUB has a return type of C<void> then the compiler will
169not supply a RETVAL variable for that function. When using
170the PPCODE: directive the RETVAL variable may not be needed.
171
172=head2 The MODULE Keyword
173
174The MODULE keyword is used to start the XS code and to
175specify the package of the functions which are being
176defined. All text preceding the first MODULE keyword is
177considered C code and is passed through to the output
178untouched. Every XS module will have a bootstrap function
179which is used to hook the XSUBs into Perl. The package name
180of this bootstrap function will match the value of the last
181MODULE statement in the XS source files. The value of
182MODULE should always remain constant within the same XS
183file, though this is not required.
184
185The following example will start the XS code and will place
186all functions in a package named RPC.
187
188 MODULE = RPC
189
190=head2 The PACKAGE Keyword
191
192When functions within an XS source file must be separated into packages
193the PACKAGE keyword should be used. This keyword is used with the MODULE
194keyword and must follow immediately after it when used.
195
196 MODULE = RPC PACKAGE = RPC
197
198 [ XS code in package RPC ]
199
200 MODULE = RPC PACKAGE = RPCB
201
202 [ XS code in package RPCB ]
203
204 MODULE = RPC PACKAGE = RPC
205
206 [ XS code in package RPC ]
207
208Although this keyword is optional and in some cases provides redundant
209information it should always be used. This keyword will ensure that the
210XSUBs appear in the desired package.
211
212=head2 The PREFIX Keyword
213
214The PREFIX keyword designates prefixes which should be
215removed from the Perl function names. If the C function is
216C<rpcb_gettime()> and the PREFIX value is C<rpcb_> then Perl will
217see this function as C<gettime()>.
218
219This keyword should follow the PACKAGE keyword when used.
220If PACKAGE is not used then PREFIX should follow the MODULE
221keyword.
222
223 MODULE = RPC PREFIX = rpc_
224
225 MODULE = RPC PACKAGE = RPCB PREFIX = rpcb_
226
227=head2 The OUTPUT: Keyword
228
229The OUTPUT: keyword indicates that certain function parameters should be
230updated (new values made visible to Perl) when the XSUB terminates or that
231certain values should be returned to the calling Perl function. For
232simple functions, such as the sin() function above, the RETVAL variable is
233automatically designated as an output value. In more complex functions
234the B<xsubpp> compiler will need help to determine which variables are output
235variables.
236
237This keyword will normally be used to complement the CODE: keyword.
238The RETVAL variable is not recognized as an output variable when the
239CODE: keyword is present. The OUTPUT: keyword is used in this
240situation to tell the compiler that RETVAL really is an output
241variable.
242
243The OUTPUT: keyword can also be used to indicate that function parameters
244are output variables. This may be necessary when a parameter has been
245modified within the function and the programmer would like the update to
8e07c86e 246be seen by Perl.
a0d0e21e
LW
247
248 bool_t
249 rpcb_gettime(host,timep)
8e07c86e
AD
250 char *host
251 time_t &timep
a0d0e21e
LW
252 OUTPUT:
253 timep
254
255The OUTPUT: keyword will also allow an output parameter to
256be mapped to a matching piece of code rather than to a
257typemap.
258
259 bool_t
260 rpcb_gettime(host,timep)
8e07c86e
AD
261 char *host
262 time_t &timep
a0d0e21e 263 OUTPUT:
8e07c86e 264 timep sv_setnv(ST(1), (double)timep);
a0d0e21e
LW
265
266=head2 The CODE: Keyword
267
268This keyword is used in more complicated XSUBs which require
269special handling for the C function. The RETVAL variable is
270available but will not be returned unless it is specified
271under the OUTPUT: keyword.
272
273The following XSUB is for a C function which requires special handling of
274its parameters. The Perl usage is given first.
275
276 $status = rpcb_gettime( "localhost", $timep );
277
278The XSUB follows.
279
d1b91892
AD
280 bool_t
281 rpcb_gettime(host,timep)
8e07c86e
AD
282 char *host
283 time_t timep
a0d0e21e
LW
284 CODE:
285 RETVAL = rpcb_gettime( host, &timep );
286 OUTPUT:
287 timep
288 RETVAL
289
c07a80fd
PP
290=head2 The INIT: Keyword
291
292The INIT: keyword allows initialization to be inserted into the XSUB before
293the compiler generates the call to the C function. Unlike the CODE: keyword
294above, this keyword does not affect the way the compiler handles RETVAL.
295
296 bool_t
297 rpcb_gettime(host,timep)
298 char *host
299 time_t &timep
300 INIT:
301 printf("# Host is %s\n", host );
302 OUTPUT:
303 timep
a0d0e21e
LW
304
305=head2 The NO_INIT Keyword
306
307The NO_INIT keyword is used to indicate that a function
d1b91892 308parameter is being used as only an output value. The B<xsubpp>
a0d0e21e
LW
309compiler will normally generate code to read the values of
310all function parameters from the argument stack and assign
311them to C variables upon entry to the function. NO_INIT
312will tell the compiler that some parameters will be used for
313output rather than for input and that they will be handled
314before the function terminates.
315
316The following example shows a variation of the rpcb_gettime() function.
d1b91892 317This function uses the timep variable as only an output variable and does
a0d0e21e
LW
318not care about its initial contents.
319
320 bool_t
321 rpcb_gettime(host,timep)
8e07c86e
AD
322 char *host
323 time_t &timep = NO_INIT
a0d0e21e
LW
324 OUTPUT:
325 timep
326
327=head2 Initializing Function Parameters
328
329Function parameters are normally initialized with their
330values from the argument stack. The typemaps contain the
331code segments which are used to transfer the Perl values to
332the C parameters. The programmer, however, is allowed to
333override the typemaps and supply alternate initialization
334code.
335
336The following code demonstrates how to supply initialization code for
337function parameters. The initialization code is eval'd by the compiler
338before it is added to the output so anything which should be interpreted
339literally, such as double quotes, must be protected with backslashes.
340
341 bool_t
342 rpcb_gettime(host,timep)
8e07c86e
AD
343 char *host = (char *)SvPV(ST(0),na);
344 time_t &timep = 0;
a0d0e21e
LW
345 OUTPUT:
346 timep
347
348This should not be used to supply default values for parameters. One
349would normally use this when a function parameter must be processed by
350another library function before it can be used. Default parameters are
351covered in the next section.
352
353=head2 Default Parameter Values
354
355Default values can be specified for function parameters by
356placing an assignment statement in the parameter list. The
357default value may be a number or a string. Defaults should
358always be used on the right-most parameters only.
359
360To allow the XSUB for rpcb_gettime() to have a default host
361value the parameters to the XSUB could be rearranged. The
362XSUB will then call the real rpcb_gettime() function with
363the parameters in the correct order. Perl will call this
364XSUB with either of the following statements.
365
366 $status = rpcb_gettime( $timep, $host );
367
368 $status = rpcb_gettime( $timep );
369
370The XSUB will look like the code which follows. A CODE:
371block is used to call the real rpcb_gettime() function with
372the parameters in the correct order for that function.
373
374 bool_t
375 rpcb_gettime(timep,host="localhost")
8e07c86e
AD
376 char *host
377 time_t timep = NO_INIT
a0d0e21e
LW
378 CODE:
379 RETVAL = rpcb_gettime( host, &timep );
380 OUTPUT:
381 timep
382 RETVAL
383
c07a80fd
PP
384=head2 The PREINIT: Keyword
385
386The PREINIT: keyword allows extra variables to be declared before the
387typemaps are expanded. If a variable is declared in a CODE: block then that
388variable will follow any typemap code. This may result in a C syntax
389error. To force the variable to be declared before the typemap code, place
390it into a PREINIT: block. The PREINIT: keyword may be used one or more
391times within an XSUB.
392
393The following examples are equivalent, but if the code is using complex
394typemaps then the first example is safer.
395
396 bool_t
397 rpcb_gettime(timep)
398 time_t timep = NO_INIT
399 PREINIT:
400 char *host = "localhost";
401 CODE:
402 RETVAL = rpcb_gettime( host, &timep );
403 OUTPUT:
404 timep
405 RETVAL
406
407A correct, but error-prone example.
408
409 bool_t
410 rpcb_gettime(timep)
411 time_t timep = NO_INIT
412 CODE:
413 char *host = "localhost";
414 RETVAL = rpcb_gettime( host, &timep );
415 OUTPUT:
416 timep
417 RETVAL
418
84287afe
PP
419=head2 The SCOPE: Keyword
420
421The SCOPE: keyword allows scoping to be enabled for a particular XSUB. If
422enabled, the XSUB will invoke ENTER and LEAVE automatically.
423
424To support potentially complex type mappings, if a typemap entry used
425by this XSUB contains a comment like C</*scope*/> then scoping will
426automatically be enabled for that XSUB.
427
428To enable scoping:
429
430 SCOPE: ENABLE
431
432To disable scoping:
433
434 SCOPE: DISABLE
435
c07a80fd
PP
436=head2 The INPUT: Keyword
437
438The XSUB's parameters are usually evaluated immediately after entering the
439XSUB. The INPUT: keyword can be used to force those parameters to be
440evaluated a little later. The INPUT: keyword can be used multiple times
441within an XSUB and can be used to list one or more input variables. This
442keyword is used with the PREINIT: keyword.
443
444The following example shows how the input parameter C<timep> can be
445evaluated late, after a PREINIT.
446
447 bool_t
448 rpcb_gettime(host,timep)
449 char *host
450 PREINIT:
451 time_t tt;
452 INPUT:
453 time_t timep
454 CODE:
455 RETVAL = rpcb_gettime( host, &tt );
456 timep = tt;
457 OUTPUT:
458 timep
459 RETVAL
460
461The next example shows each input parameter evaluated late.
462
463 bool_t
464 rpcb_gettime(host,timep)
465 PREINIT:
466 time_t tt;
467 INPUT:
468 char *host
469 PREINIT:
470 char *h;
471 INPUT:
472 time_t timep
473 CODE:
474 h = host;
475 RETVAL = rpcb_gettime( h, &tt );
476 timep = tt;
477 OUTPUT:
478 timep
479 RETVAL
480
a0d0e21e
LW
481=head2 Variable-length Parameter Lists
482
483XSUBs can have variable-length parameter lists by specifying an ellipsis
484C<(...)> in the parameter list. This use of the ellipsis is similar to that
485found in ANSI C. The programmer is able to determine the number of
486arguments passed to the XSUB by examining the C<items> variable which the
487B<xsubpp> compiler supplies for all XSUBs. By using this mechanism one can
488create an XSUB which accepts a list of parameters of unknown length.
489
490The I<host> parameter for the rpcb_gettime() XSUB can be
491optional so the ellipsis can be used to indicate that the
492XSUB will take a variable number of parameters. Perl should
d1b91892 493be able to call this XSUB with either of the following statements.
a0d0e21e
LW
494
495 $status = rpcb_gettime( $timep, $host );
496
497 $status = rpcb_gettime( $timep );
498
499The XS code, with ellipsis, follows.
500
501 bool_t
502 rpcb_gettime(timep, ...)
8e07c86e 503 time_t timep = NO_INIT
c07a80fd 504 PREINIT:
a0d0e21e 505 char *host = "localhost";
c07a80fd
PP
506 CODE:
507 if( items > 1 )
508 host = (char *)SvPV(ST(1), na);
509 RETVAL = rpcb_gettime( host, &timep );
a0d0e21e
LW
510 OUTPUT:
511 timep
512 RETVAL
513
514=head2 The PPCODE: Keyword
515
516The PPCODE: keyword is an alternate form of the CODE: keyword and is used
517to tell the B<xsubpp> compiler that the programmer is supplying the code to
d1b91892 518control the argument stack for the XSUBs return values. Occasionally one
a0d0e21e
LW
519will want an XSUB to return a list of values rather than a single value.
520In these cases one must use PPCODE: and then explicitly push the list of
521values on the stack. The PPCODE: and CODE: keywords are not used
522together within the same XSUB.
523
524The following XSUB will call the C rpcb_gettime() function
525and will return its two output values, timep and status, to
526Perl as a single list.
527
d1b91892
AD
528 void
529 rpcb_gettime(host)
8e07c86e 530 char *host
c07a80fd 531 PREINIT:
a0d0e21e
LW
532 time_t timep;
533 bool_t status;
c07a80fd 534 PPCODE:
a0d0e21e
LW
535 status = rpcb_gettime( host, &timep );
536 EXTEND(sp, 2);
cb1a09d0
AD
537 PUSHs(sv_2mortal(newSViv(status)));
538 PUSHs(sv_2mortal(newSViv(timep)));
a0d0e21e
LW
539
540Notice that the programmer must supply the C code necessary
541to have the real rpcb_gettime() function called and to have
542the return values properly placed on the argument stack.
543
544The C<void> return type for this function tells the B<xsubpp> compiler that
545the RETVAL variable is not needed or used and that it should not be created.
546In most scenarios the void return type should be used with the PPCODE:
547directive.
548
549The EXTEND() macro is used to make room on the argument
550stack for 2 return values. The PPCODE: directive causes the
551B<xsubpp> compiler to create a stack pointer called C<sp>, and it
552is this pointer which is being used in the EXTEND() macro.
553The values are then pushed onto the stack with the PUSHs()
554macro.
555
556Now the rpcb_gettime() function can be used from Perl with
557the following statement.
558
559 ($status, $timep) = rpcb_gettime("localhost");
560
561=head2 Returning Undef And Empty Lists
562
5f05dabc 563Occasionally the programmer will want to return simply
a0d0e21e
LW
564C<undef> or an empty list if a function fails rather than a
565separate status value. The rpcb_gettime() function offers
566just this situation. If the function succeeds we would like
567to have it return the time and if it fails we would like to
568have undef returned. In the following Perl code the value
569of $timep will either be undef or it will be a valid time.
570
571 $timep = rpcb_gettime( "localhost" );
572
573The following XSUB uses the C<void> return type to disable the generation of
574the RETVAL variable and uses a CODE: block to indicate to the compiler
575that the programmer has supplied all the necessary code. The
576sv_newmortal() call will initialize the return value to undef, making that
577the default return value.
578
579 void
580 rpcb_gettime(host)
581 char * host
c07a80fd 582 PREINIT:
a0d0e21e
LW
583 time_t timep;
584 bool_t x;
c07a80fd 585 CODE:
a0d0e21e
LW
586 ST(0) = sv_newmortal();
587 if( rpcb_gettime( host, &timep ) )
588 sv_setnv( ST(0), (double)timep);
a0d0e21e
LW
589
590The next example demonstrates how one would place an explicit undef in the
591return value, should the need arise.
592
593 void
594 rpcb_gettime(host)
595 char * host
c07a80fd 596 PREINIT:
a0d0e21e
LW
597 time_t timep;
598 bool_t x;
c07a80fd 599 CODE:
a0d0e21e
LW
600 ST(0) = sv_newmortal();
601 if( rpcb_gettime( host, &timep ) ){
602 sv_setnv( ST(0), (double)timep);
603 }
604 else{
605 ST(0) = &sv_undef;
606 }
a0d0e21e
LW
607
608To return an empty list one must use a PPCODE: block and
609then not push return values on the stack.
610
611 void
612 rpcb_gettime(host)
8e07c86e 613 char *host
c07a80fd 614 PREINIT:
a0d0e21e 615 time_t timep;
c07a80fd 616 PPCODE:
a0d0e21e 617 if( rpcb_gettime( host, &timep ) )
cb1a09d0 618 PUSHs(sv_2mortal(newSViv(timep)));
a0d0e21e
LW
619 else{
620 /* Nothing pushed on stack, so an empty */
621 /* list is implicitly returned. */
622 }
a0d0e21e 623
f27cfbbe
PP
624Some people may be inclined to include an explicit C<return> in the above
625XSUB, rather than letting control fall through to the end. In those
626situations C<XSRETURN_EMPTY> should be used, instead. This will ensure that
627the XSUB stack is properly adjusted. Consult L<perlguts/"API LISTING"> for
628other C<XSRETURN> macros.
629
4633a7c4
LW
630=head2 The REQUIRE: Keyword
631
632The REQUIRE: keyword is used to indicate the minimum version of the
633B<xsubpp> compiler needed to compile the XS module. An XS module which
5f05dabc 634contains the following statement will compile with only B<xsubpp> version
4633a7c4
LW
6351.922 or greater:
636
637 REQUIRE: 1.922
638
a0d0e21e
LW
639=head2 The CLEANUP: Keyword
640
641This keyword can be used when an XSUB requires special cleanup procedures
642before it terminates. When the CLEANUP: keyword is used it must follow
643any CODE:, PPCODE:, or OUTPUT: blocks which are present in the XSUB. The
644code specified for the cleanup block will be added as the last statements
645in the XSUB.
646
647=head2 The BOOT: Keyword
648
649The BOOT: keyword is used to add code to the extension's bootstrap
650function. The bootstrap function is generated by the B<xsubpp> compiler and
651normally holds the statements necessary to register any XSUBs with Perl.
652With the BOOT: keyword the programmer can tell the compiler to add extra
653statements to the bootstrap function.
654
655This keyword may be used any time after the first MODULE keyword and should
656appear on a line by itself. The first blank line after the keyword will
657terminate the code block.
658
659 BOOT:
660 # The following message will be printed when the
661 # bootstrap function executes.
662 printf("Hello from the bootstrap!\n");
663
c07a80fd
PP
664=head2 The VERSIONCHECK: Keyword
665
666The VERSIONCHECK: keyword corresponds to B<xsubpp>'s C<-versioncheck> and
5f05dabc 667C<-noversioncheck> options. This keyword overrides the command line
c07a80fd
PP
668options. Version checking is enabled by default. When version checking is
669enabled the XS module will attempt to verify that its version matches the
670version of the PM module.
671
672To enable version checking:
673
674 VERSIONCHECK: ENABLE
675
676To disable version checking:
677
678 VERSIONCHECK: DISABLE
679
680=head2 The PROTOTYPES: Keyword
681
682The PROTOTYPES: keyword corresponds to B<xsubpp>'s C<-prototypes> and
5f05dabc 683C<-noprototypes> options. This keyword overrides the command-line options.
c07a80fd
PP
684Prototypes are enabled by default. When prototypes are enabled XSUBs will
685be given Perl prototypes. This keyword may be used multiple times in an XS
686module to enable and disable prototypes for different parts of the module.
687
688To enable prototypes:
689
690 PROTOTYPES: ENABLE
691
692To disable prototypes:
693
694 PROTOTYPES: DISABLE
695
696=head2 The PROTOTYPE: Keyword
697
698This keyword is similar to the PROTOTYPES: keyword above but can be used to
699force B<xsubpp> to use a specific prototype for the XSUB. This keyword
700overrides all other prototype options and keywords but affects only the
701current XSUB. Consult L<perlsub/Prototypes> for information about Perl
702prototypes.
703
704 bool_t
705 rpcb_gettime(timep, ...)
706 time_t timep = NO_INIT
707 PROTOTYPE: $;$
708 PREINIT:
709 char *host = "localhost";
710 CODE:
711 if( items > 1 )
712 host = (char *)SvPV(ST(1), na);
713 RETVAL = rpcb_gettime( host, &timep );
714 OUTPUT:
715 timep
716 RETVAL
717
718=head2 The ALIAS: Keyword
719
720The ALIAS: keyword allows an XSUB to have two more more unique Perl names
721and to know which of those names was used when it was invoked. The Perl
722names may be fully-qualified with package names. Each alias is given an
723index. The compiler will setup a variable called C<ix> which contain the
724index of the alias which was used. When the XSUB is called with its
725declared name C<ix> will be 0.
726
727The following example will create aliases C<FOO::gettime()> and
728C<BAR::getit()> for this function.
729
730 bool_t
731 rpcb_gettime(host,timep)
732 char *host
733 time_t &timep
734 ALIAS:
735 FOO::gettime = 1
736 BAR::getit = 2
737 INIT:
738 printf("# ix = %d\n", ix );
739 OUTPUT:
740 timep
741
742=head2 The INCLUDE: Keyword
743
744This keyword can be used to pull other files into the XS module. The other
745files may have XS code. INCLUDE: can also be used to run a command to
746generate the XS code to be pulled into the module.
747
748The file F<Rpcb1.xsh> contains our C<rpcb_gettime()> function:
749
750 bool_t
751 rpcb_gettime(host,timep)
752 char *host
753 time_t &timep
754 OUTPUT:
755 timep
756
757The XS module can use INCLUDE: to pull that file into it.
758
759 INCLUDE: Rpcb1.xsh
760
761If the parameters to the INCLUDE: keyword are followed by a pipe (C<|>) then
762the compiler will interpret the parameters as a command.
763
764 INCLUDE: cat Rpcb1.xsh |
765
766=head2 The CASE: Keyword
767
768The CASE: keyword allows an XSUB to have multiple distinct parts with each
769part acting as a virtual XSUB. CASE: is greedy and if it is used then all
770other XS keywords must be contained within a CASE:. This means nothing may
771precede the first CASE: in the XSUB and anything following the last CASE: is
772included in that case.
773
774A CASE: might switch via a parameter of the XSUB, via the C<ix> ALIAS:
775variable (see L<"The ALIAS: Keyword">), or maybe via the C<items> variable
776(see L<"Variable-length Parameter Lists">). The last CASE: becomes the
777B<default> case if it is not associated with a conditional. The following
778example shows CASE switched via C<ix> with a function C<rpcb_gettime()>
779having an alias C<x_gettime()>. When the function is called as
b772cb6e
PP
780C<rpcb_gettime()> its parameters are the usual C<(char *host, time_t *timep)>,
781but when the function is called as C<x_gettime()> its parameters are
c07a80fd
PP
782reversed, C<(time_t *timep, char *host)>.
783
784 long
785 rpcb_gettime(a,b)
786 CASE: ix == 1
787 ALIAS:
788 x_gettime = 1
789 INPUT:
790 # 'a' is timep, 'b' is host
791 char *b
792 time_t a = NO_INIT
793 CODE:
794 RETVAL = rpcb_gettime( b, &a );
795 OUTPUT:
796 a
797 RETVAL
798 CASE:
799 # 'a' is host, 'b' is timep
800 char *a
801 time_t &b = NO_INIT
802 OUTPUT:
803 b
804 RETVAL
805
806That function can be called with either of the following statements. Note
807the different argument lists.
808
809 $status = rpcb_gettime( $host, $timep );
810
811 $status = x_gettime( $timep, $host );
812
813=head2 The & Unary Operator
814
815The & unary operator is used to tell the compiler that it should dereference
816the object when it calls the C function. This is used when a CODE: block is
817not used and the object is a not a pointer type (the object is an C<int> or
818C<long> but not a C<int*> or C<long*>).
819
820The following XSUB will generate incorrect C code. The xsubpp compiler will
821turn this into code which calls C<rpcb_gettime()> with parameters C<(char
822*host, time_t timep)>, but the real C<rpcb_gettime()> wants the C<timep>
823parameter to be of type C<time_t*> rather than C<time_t>.
824
825 bool_t
826 rpcb_gettime(host,timep)
827 char *host
828 time_t timep
829 OUTPUT:
830 timep
831
832That problem is corrected by using the C<&> operator. The xsubpp compiler
833will now turn this into code which calls C<rpcb_gettime()> correctly with
834parameters C<(char *host, time_t *timep)>. It does this by carrying the
835C<&> through, so the function call looks like C<rpcb_gettime(host, &timep)>.
836
837 bool_t
838 rpcb_gettime(host,timep)
839 char *host
840 time_t &timep
841 OUTPUT:
842 timep
843
a0d0e21e
LW
844=head2 Inserting Comments and C Preprocessor Directives
845
f27cfbbe 846C preprocessor directives are allowed within BOOT:, PREINIT: INIT:,
5f05dabc 847CODE:, PPCODE:, and CLEANUP: blocks, as well as outside the functions.
f27cfbbe
PP
848Comments are allowed anywhere after the MODULE keyword. The compiler
849will pass the preprocessor directives through untouched and will remove
850the commented lines.
b772cb6e 851
f27cfbbe
PP
852Comments can be added to XSUBs by placing a C<#> as the first
853non-whitespace of a line. Care should be taken to avoid making the
854comment look like a C preprocessor directive, lest it be interpreted as
855such. The simplest way to prevent this is to put whitespace in front of
856the C<#>.
857
f27cfbbe
PP
858If you use preprocessor directives to choose one of two
859versions of a function, use
860
861 #if ... version1
862 #else /* ... version2 */
863 #endif
864
865and not
866
867 #if ... version1
868 #endif
869 #if ... version2
870 #endif
871
872because otherwise xsubpp will believe that you made a duplicate
873definition of the function. Also, put a blank line before the
874#else/#endif so it will not be seen as part of the function body.
a0d0e21e
LW
875
876=head2 Using XS With C++
877
878If a function is defined as a C++ method then it will assume
879its first argument is an object pointer. The object pointer
880will be stored in a variable called THIS. The object should
881have been created by C++ with the new() function and should
cb1a09d0
AD
882be blessed by Perl with the sv_setref_pv() macro. The
883blessing of the object by Perl can be handled by a typemap. An example
884typemap is shown at the end of this section.
a0d0e21e
LW
885
886If the method is defined as static it will call the C++
887function using the class::method() syntax. If the method is not static
f27cfbbe 888the function will be called using the THIS-E<gt>method() syntax.
a0d0e21e 889
cb1a09d0 890The next examples will use the following C++ class.
a0d0e21e 891
a5f75d66 892 class color {
cb1a09d0 893 public:
a5f75d66
AD
894 color();
895 ~color();
cb1a09d0
AD
896 int blue();
897 void set_blue( int );
898
899 private:
900 int c_blue;
901 };
902
903The XSUBs for the blue() and set_blue() methods are defined with the class
904name but the parameter for the object (THIS, or "self") is implicit and is
905not listed.
906
907 int
908 color::blue()
a0d0e21e
LW
909
910 void
cb1a09d0
AD
911 color::set_blue( val )
912 int val
a0d0e21e 913
cb1a09d0
AD
914Both functions will expect an object as the first parameter. The xsubpp
915compiler will call that object C<THIS> and will use it to call the specified
916method. So in the C++ code the blue() and set_blue() methods will be called
917in the following manner.
a0d0e21e 918
cb1a09d0 919 RETVAL = THIS->blue();
a0d0e21e 920
cb1a09d0 921 THIS->set_blue( val );
a0d0e21e 922
cb1a09d0
AD
923If the function's name is B<DESTROY> then the C++ C<delete> function will be
924called and C<THIS> will be given as its parameter.
a0d0e21e 925
d1b91892 926 void
cb1a09d0
AD
927 color::DESTROY()
928
929The C++ code will call C<delete>.
930
931 delete THIS;
a0d0e21e 932
cb1a09d0
AD
933If the function's name is B<new> then the C++ C<new> function will be called
934to create a dynamic C++ object. The XSUB will expect the class name, which
935will be kept in a variable called C<CLASS>, to be given as the first
936argument.
a0d0e21e 937
cb1a09d0
AD
938 color *
939 color::new()
a0d0e21e 940
cb1a09d0 941The C++ code will call C<new>.
a0d0e21e 942
cb1a09d0
AD
943 RETVAL = new color();
944
945The following is an example of a typemap that could be used for this C++
946example.
947
948 TYPEMAP
949 color * O_OBJECT
950
951 OUTPUT
952 # The Perl object is blessed into 'CLASS', which should be a
953 # char* having the name of the package for the blessing.
954 O_OBJECT
955 sv_setref_pv( $arg, CLASS, (void*)$var );
a6006777 956
cb1a09d0
AD
957 INPUT
958 O_OBJECT
959 if( sv_isobject($arg) && (SvTYPE(SvRV($arg)) == SVt_PVMG) )
960 $var = ($type)SvIV((SV*)SvRV( $arg ));
961 else{
962 warn( \"${Package}::$func_name() -- $var is not a blessed SV reference\" );
963 XSRETURN_UNDEF;
964 }
a0d0e21e 965
d1b91892 966=head2 Interface Strategy
a0d0e21e
LW
967
968When designing an interface between Perl and a C library a straight
969translation from C to XS is often sufficient. The interface will often be
970very C-like and occasionally nonintuitive, especially when the C function
971modifies one of its parameters. In cases where the programmer wishes to
972create a more Perl-like interface the following strategy may help to
973identify the more critical parts of the interface.
974
975Identify the C functions which modify their parameters. The XSUBs for
976these functions may be able to return lists to Perl, or may be
977candidates to return undef or an empty list in case of failure.
978
d1b91892 979Identify which values are used by only the C and XSUB functions
a0d0e21e
LW
980themselves. If Perl does not need to access the contents of the value
981then it may not be necessary to provide a translation for that value
982from C to Perl.
983
984Identify the pointers in the C function parameter lists and return
985values. Some pointers can be handled in XS with the & unary operator on
986the variable name while others will require the use of the * operator on
987the type name. In general it is easier to work with the & operator.
988
989Identify the structures used by the C functions. In many
990cases it may be helpful to use the T_PTROBJ typemap for
991these structures so they can be manipulated by Perl as
992blessed objects.
993
a0d0e21e
LW
994=head2 Perl Objects And C Structures
995
996When dealing with C structures one should select either
997B<T_PTROBJ> or B<T_PTRREF> for the XS type. Both types are
998designed to handle pointers to complex objects. The
999T_PTRREF type will allow the Perl object to be unblessed
1000while the T_PTROBJ type requires that the object be blessed.
1001By using T_PTROBJ one can achieve a form of type-checking
d1b91892 1002because the XSUB will attempt to verify that the Perl object
a0d0e21e
LW
1003is of the expected type.
1004
1005The following XS code shows the getnetconfigent() function which is used
8e07c86e 1006with ONC+ TIRPC. The getnetconfigent() function will return a pointer to a
a0d0e21e
LW
1007C structure and has the C prototype shown below. The example will
1008demonstrate how the C pointer will become a Perl reference. Perl will
1009consider this reference to be a pointer to a blessed object and will
1010attempt to call a destructor for the object. A destructor will be
1011provided in the XS source to free the memory used by getnetconfigent().
1012Destructors in XS can be created by specifying an XSUB function whose name
1013ends with the word B<DESTROY>. XS destructors can be used to free memory
1014which may have been malloc'd by another XSUB.
1015
1016 struct netconfig *getnetconfigent(const char *netid);
1017
1018A C<typedef> will be created for C<struct netconfig>. The Perl
1019object will be blessed in a class matching the name of the C
1020type, with the tag C<Ptr> appended, and the name should not
1021have embedded spaces if it will be a Perl package name. The
1022destructor will be placed in a class corresponding to the
1023class of the object and the PREFIX keyword will be used to
1024trim the name to the word DESTROY as Perl will expect.
1025
1026 typedef struct netconfig Netconfig;
1027
1028 MODULE = RPC PACKAGE = RPC
1029
1030 Netconfig *
1031 getnetconfigent(netid)
8e07c86e 1032 char *netid
a0d0e21e
LW
1033
1034 MODULE = RPC PACKAGE = NetconfigPtr PREFIX = rpcb_
1035
1036 void
1037 rpcb_DESTROY(netconf)
8e07c86e 1038 Netconfig *netconf
a0d0e21e
LW
1039 CODE:
1040 printf("Now in NetconfigPtr::DESTROY\n");
1041 free( netconf );
1042
1043This example requires the following typemap entry. Consult the typemap
1044section for more information about adding new typemaps for an extension.
1045
1046 TYPEMAP
1047 Netconfig * T_PTROBJ
1048
1049This example will be used with the following Perl statements.
1050
1051 use RPC;
1052 $netconf = getnetconfigent("udp");
1053
1054When Perl destroys the object referenced by $netconf it will send the
1055object to the supplied XSUB DESTROY function. Perl cannot determine, and
1056does not care, that this object is a C struct and not a Perl object. In
1057this sense, there is no difference between the object created by the
1058getnetconfigent() XSUB and an object created by a normal Perl subroutine.
1059
a0d0e21e
LW
1060=head2 The Typemap
1061
1062The typemap is a collection of code fragments which are used by the B<xsubpp>
1063compiler to map C function parameters and values to Perl values. The
1064typemap file may consist of three sections labeled C<TYPEMAP>, C<INPUT>, and
1065C<OUTPUT>. The INPUT section tells the compiler how to translate Perl values
1066into variables of certain C types. The OUTPUT section tells the compiler
1067how to translate the values from certain C types into values Perl can
1068understand. The TYPEMAP section tells the compiler which of the INPUT and
1069OUTPUT code fragments should be used to map a given C type to a Perl value.
1070Each of the sections of the typemap must be preceded by one of the TYPEMAP,
1071INPUT, or OUTPUT keywords.
1072
1073The default typemap in the C<ext> directory of the Perl source contains many
1074useful types which can be used by Perl extensions. Some extensions define
1075additional typemaps which they keep in their own directory. These
1076additional typemaps may reference INPUT and OUTPUT maps in the main
1077typemap. The B<xsubpp> compiler will allow the extension's own typemap to
1078override any mappings which are in the default typemap.
1079
1080Most extensions which require a custom typemap will need only the TYPEMAP
1081section of the typemap file. The custom typemap used in the
1082getnetconfigent() example shown earlier demonstrates what may be the typical
1083use of extension typemaps. That typemap is used to equate a C structure
1084with the T_PTROBJ typemap. The typemap used by getnetconfigent() is shown
1085here. Note that the C type is separated from the XS type with a tab and
1086that the C unary operator C<*> is considered to be a part of the C type name.
1087
1088 TYPEMAP
1089 Netconfig *<tab>T_PTROBJ
1090
1091=head1 EXAMPLES
1092
1093File C<RPC.xs>: Interface to some ONC+ RPC bind library functions.
1094
1095 #include "EXTERN.h"
1096 #include "perl.h"
1097 #include "XSUB.h"
1098
1099 #include <rpc/rpc.h>
1100
1101 typedef struct netconfig Netconfig;
1102
1103 MODULE = RPC PACKAGE = RPC
1104
1105 void
1106 rpcb_gettime(host="localhost")
8e07c86e 1107 char *host
c07a80fd 1108 PREINIT:
a0d0e21e 1109 time_t timep;
c07a80fd 1110 CODE:
a0d0e21e
LW
1111 ST(0) = sv_newmortal();
1112 if( rpcb_gettime( host, &timep ) )
1113 sv_setnv( ST(0), (double)timep );
a0d0e21e
LW
1114
1115 Netconfig *
1116 getnetconfigent(netid="udp")
8e07c86e 1117 char *netid
a0d0e21e
LW
1118
1119 MODULE = RPC PACKAGE = NetconfigPtr PREFIX = rpcb_
1120
1121 void
1122 rpcb_DESTROY(netconf)
8e07c86e 1123 Netconfig *netconf
a0d0e21e
LW
1124 CODE:
1125 printf("NetconfigPtr::DESTROY\n");
1126 free( netconf );
1127
1128File C<typemap>: Custom typemap for RPC.xs.
1129
1130 TYPEMAP
1131 Netconfig * T_PTROBJ
1132
1133File C<RPC.pm>: Perl module for the RPC extension.
1134
1135 package RPC;
1136
1137 require Exporter;
1138 require DynaLoader;
1139 @ISA = qw(Exporter DynaLoader);
1140 @EXPORT = qw(rpcb_gettime getnetconfigent);
1141
1142 bootstrap RPC;
1143 1;
1144
1145File C<rpctest.pl>: Perl test program for the RPC extension.
1146
1147 use RPC;
1148
1149 $netconf = getnetconfigent();
1150 $a = rpcb_gettime();
1151 print "time = $a\n";
1152 print "netconf = $netconf\n";
1153
1154 $netconf = getnetconfigent("tcp");
1155 $a = rpcb_gettime("poplar");
1156 print "time = $a\n";
1157 print "netconf = $netconf\n";
1158
1159
c07a80fd
PP
1160=head1 XS VERSION
1161
f27cfbbe 1162This document covers features supported by C<xsubpp> 1.935.
c07a80fd 1163
a0d0e21e
LW
1164=head1 AUTHOR
1165
d1b91892 1166Dean Roehrich F<E<lt>roehrich@cray.comE<gt>>
b772cb6e 1167Jul 8, 1996