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