perl 5.002beta2 patch: MANIFEST
[p5sagit/p5-mst-13.2.git] / pod / perlxs.pod
CommitLineData
a0d0e21e 1=head1 NAME
2
8e07c86e 3perlxs - XS language reference manual
a0d0e21e 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 27
28=head2 On The Road
29
a0d0e21e 30Many of the examples which follow will concentrate on creating an
8e07c86e 31interface between Perl and the ONC+ RPC bind library functions.
a0d0e21e 32Specifically, the rpcb_gettime() function will be used to demonstrate many
33features of the XS language. This function has two parameters; the first
34is an input parameter and the second is an output parameter. The function
35also returns a status value.
36
37 bool_t rpcb_gettime(const char *host, time_t *timep);
38
39From C this function will be called with the following
40statements.
41
42 #include <rpc/rpc.h>
43 bool_t status;
44 time_t timep;
45 status = rpcb_gettime( "localhost", &timep );
46
47If an XSUB is created to offer a direct translation between this function
48and Perl, then this XSUB will be used from Perl with the following code.
49The $status and $timep variables will contain the output of the function.
50
51 use RPC;
52 $status = rpcb_gettime( "localhost", $timep );
53
54The following XS file shows an XS subroutine, or XSUB, which
55demonstrates one possible interface to the rpcb_gettime()
56function. This XSUB represents a direct translation between
57C and Perl and so preserves the interface even from Perl.
58This XSUB will be invoked from Perl with the usage shown
59above. Note that the first three #include statements, for
60C<EXTERN.h>, C<perl.h>, and C<XSUB.h>, will always be present at the
61beginning of an XS file. This approach and others will be
62expanded later in this document.
63
64 #include "EXTERN.h"
65 #include "perl.h"
66 #include "XSUB.h"
67 #include <rpc/rpc.h>
68
69 MODULE = RPC PACKAGE = RPC
70
71 bool_t
72 rpcb_gettime(host,timep)
8e07c86e 73 char *host
74 time_t &timep
a0d0e21e 75 OUTPUT:
76 timep
77
78Any extension to Perl, including those containing XSUBs,
79should have a Perl module to serve as the bootstrap which
80pulls the extension into Perl. This module will export the
81extension's functions and variables to the Perl program and
82will cause the extension's XSUBs to be linked into Perl.
83The following module will be used for most of the examples
84in this document and should be used from Perl with the C<use>
85command as shown earlier. Perl modules are explained in
86more detail later in this document.
87
88 package RPC;
89
90 require Exporter;
91 require DynaLoader;
92 @ISA = qw(Exporter DynaLoader);
93 @EXPORT = qw( rpcb_gettime );
94
95 bootstrap RPC;
96 1;
97
98Throughout this document a variety of interfaces to the rpcb_gettime()
99XSUB will be explored. The XSUBs will take their parameters in different
100orders or will take different numbers of parameters. In each case the
101XSUB is an abstraction between Perl and the real C rpcb_gettime()
102function, and the XSUB must always ensure that the real rpcb_gettime()
103function is called with the correct parameters. This abstraction will
104allow the programmer to create a more Perl-like interface to the C
105function.
106
107=head2 The Anatomy of an XSUB
108
8e07c86e 109The following XSUB allows a Perl program to access a C library function
110called sin(). The XSUB will imitate the C function which takes a single
111argument and returns a single value.
a0d0e21e 112
113 double
114 sin(x)
8e07c86e 115 double x
a0d0e21e 116
8e07c86e 117When using C pointers the indirection operator C<*> should be considered
118part of the type and the address operator C<&> should be considered part of
119the variable, as is demonstrated in the rpcb_gettime() function above. See
120the section on typemaps for more about handling qualifiers and unary
121operators in C types.
a0d0e21e 122
a0d0e21e 123The function name and the return type must be placed on
124separate lines.
125
126 INCORRECT CORRECT
127
128 double sin(x) double
8e07c86e 129 double x sin(x)
130 double x
a0d0e21e 131
132=head2 The Argument Stack
133
134The argument stack is used to store the values which are
135sent as parameters to the XSUB and to store the XSUB's
136return value. In reality all Perl functions keep their
137values on this stack at the same time, each limited to its
138own range of positions on the stack. In this document the
139first position on that stack which belongs to the active
140function will be referred to as position 0 for that function.
141
8e07c86e 142XSUBs refer to their stack arguments with the macro B<ST(x)>, where I<x>
143refers to a position in this XSUB's part of the stack. Position 0 for that
a0d0e21e 144function would be known to the XSUB as ST(0). The XSUB's incoming
145parameters and outgoing return values always begin at ST(0). For many
146simple cases the B<xsubpp> compiler will generate the code necessary to
147handle the argument stack by embedding code fragments found in the
148typemaps. In more complex cases the programmer must supply the code.
149
150=head2 The RETVAL Variable
151
152The RETVAL variable is a magic variable which always matches
153the return type of the C library function. The B<xsubpp> compiler will
154supply this variable in each XSUB and by default will use it to hold the
155return value of the C library function being called. In simple cases the
156value of RETVAL will be placed in ST(0) of the argument stack where it can
157be received by Perl as the return value of the XSUB.
158
159If the XSUB has a return type of C<void> then the compiler will
160not supply a RETVAL variable for that function. When using
161the PPCODE: directive the RETVAL variable may not be needed.
162
163=head2 The MODULE Keyword
164
165The MODULE keyword is used to start the XS code and to
166specify the package of the functions which are being
167defined. All text preceding the first MODULE keyword is
168considered C code and is passed through to the output
169untouched. Every XS module will have a bootstrap function
170which is used to hook the XSUBs into Perl. The package name
171of this bootstrap function will match the value of the last
172MODULE statement in the XS source files. The value of
173MODULE should always remain constant within the same XS
174file, though this is not required.
175
176The following example will start the XS code and will place
177all functions in a package named RPC.
178
179 MODULE = RPC
180
181=head2 The PACKAGE Keyword
182
183When functions within an XS source file must be separated into packages
184the PACKAGE keyword should be used. This keyword is used with the MODULE
185keyword and must follow immediately after it when used.
186
187 MODULE = RPC PACKAGE = RPC
188
189 [ XS code in package RPC ]
190
191 MODULE = RPC PACKAGE = RPCB
192
193 [ XS code in package RPCB ]
194
195 MODULE = RPC PACKAGE = RPC
196
197 [ XS code in package RPC ]
198
199Although this keyword is optional and in some cases provides redundant
200information it should always be used. This keyword will ensure that the
201XSUBs appear in the desired package.
202
203=head2 The PREFIX Keyword
204
205The PREFIX keyword designates prefixes which should be
206removed from the Perl function names. If the C function is
207C<rpcb_gettime()> and the PREFIX value is C<rpcb_> then Perl will
208see this function as C<gettime()>.
209
210This keyword should follow the PACKAGE keyword when used.
211If PACKAGE is not used then PREFIX should follow the MODULE
212keyword.
213
214 MODULE = RPC PREFIX = rpc_
215
216 MODULE = RPC PACKAGE = RPCB PREFIX = rpcb_
217
218=head2 The OUTPUT: Keyword
219
220The OUTPUT: keyword indicates that certain function parameters should be
221updated (new values made visible to Perl) when the XSUB terminates or that
222certain values should be returned to the calling Perl function. For
223simple functions, such as the sin() function above, the RETVAL variable is
224automatically designated as an output value. In more complex functions
225the B<xsubpp> compiler will need help to determine which variables are output
226variables.
227
228This keyword will normally be used to complement the CODE: keyword.
229The RETVAL variable is not recognized as an output variable when the
230CODE: keyword is present. The OUTPUT: keyword is used in this
231situation to tell the compiler that RETVAL really is an output
232variable.
233
234The OUTPUT: keyword can also be used to indicate that function parameters
235are output variables. This may be necessary when a parameter has been
236modified within the function and the programmer would like the update to
8e07c86e 237be seen by Perl.
a0d0e21e 238
239 bool_t
240 rpcb_gettime(host,timep)
8e07c86e 241 char *host
242 time_t &timep
a0d0e21e 243 OUTPUT:
244 timep
245
246The OUTPUT: keyword will also allow an output parameter to
247be mapped to a matching piece of code rather than to a
248typemap.
249
250 bool_t
251 rpcb_gettime(host,timep)
8e07c86e 252 char *host
253 time_t &timep
a0d0e21e 254 OUTPUT:
8e07c86e 255 timep sv_setnv(ST(1), (double)timep);
a0d0e21e 256
257=head2 The CODE: Keyword
258
259This keyword is used in more complicated XSUBs which require
260special handling for the C function. The RETVAL variable is
261available but will not be returned unless it is specified
262under the OUTPUT: keyword.
263
264The following XSUB is for a C function which requires special handling of
265its parameters. The Perl usage is given first.
266
267 $status = rpcb_gettime( "localhost", $timep );
268
269The XSUB follows.
270
d1b91892 271 bool_t
272 rpcb_gettime(host,timep)
8e07c86e 273 char *host
274 time_t timep
a0d0e21e 275 CODE:
276 RETVAL = rpcb_gettime( host, &timep );
277 OUTPUT:
278 timep
279 RETVAL
280
281In many of the examples shown here the CODE: block (and
282other blocks) will often be contained within braces ( C<{> and
283C<}> ). This protects the CODE: block from complex INPUT
284typemaps and ensures the resulting C code is legal.
285
286=head2 The NO_INIT Keyword
287
288The NO_INIT keyword is used to indicate that a function
d1b91892 289parameter is being used as only an output value. The B<xsubpp>
a0d0e21e 290compiler will normally generate code to read the values of
291all function parameters from the argument stack and assign
292them to C variables upon entry to the function. NO_INIT
293will tell the compiler that some parameters will be used for
294output rather than for input and that they will be handled
295before the function terminates.
296
297The following example shows a variation of the rpcb_gettime() function.
d1b91892 298This function uses the timep variable as only an output variable and does
a0d0e21e 299not care about its initial contents.
300
301 bool_t
302 rpcb_gettime(host,timep)
8e07c86e 303 char *host
304 time_t &timep = NO_INIT
a0d0e21e 305 OUTPUT:
306 timep
307
308=head2 Initializing Function Parameters
309
310Function parameters are normally initialized with their
311values from the argument stack. The typemaps contain the
312code segments which are used to transfer the Perl values to
313the C parameters. The programmer, however, is allowed to
314override the typemaps and supply alternate initialization
315code.
316
317The following code demonstrates how to supply initialization code for
318function parameters. The initialization code is eval'd by the compiler
319before it is added to the output so anything which should be interpreted
320literally, such as double quotes, must be protected with backslashes.
321
322 bool_t
323 rpcb_gettime(host,timep)
8e07c86e 324 char *host = (char *)SvPV(ST(0),na);
325 time_t &timep = 0;
a0d0e21e 326 OUTPUT:
327 timep
328
329This should not be used to supply default values for parameters. One
330would normally use this when a function parameter must be processed by
331another library function before it can be used. Default parameters are
332covered in the next section.
333
334=head2 Default Parameter Values
335
336Default values can be specified for function parameters by
337placing an assignment statement in the parameter list. The
338default value may be a number or a string. Defaults should
339always be used on the right-most parameters only.
340
341To allow the XSUB for rpcb_gettime() to have a default host
342value the parameters to the XSUB could be rearranged. The
343XSUB will then call the real rpcb_gettime() function with
344the parameters in the correct order. Perl will call this
345XSUB with either of the following statements.
346
347 $status = rpcb_gettime( $timep, $host );
348
349 $status = rpcb_gettime( $timep );
350
351The XSUB will look like the code which follows. A CODE:
352block is used to call the real rpcb_gettime() function with
353the parameters in the correct order for that function.
354
355 bool_t
356 rpcb_gettime(timep,host="localhost")
8e07c86e 357 char *host
358 time_t timep = NO_INIT
a0d0e21e 359 CODE:
360 RETVAL = rpcb_gettime( host, &timep );
361 OUTPUT:
362 timep
363 RETVAL
364
365=head2 Variable-length Parameter Lists
366
367XSUBs can have variable-length parameter lists by specifying an ellipsis
368C<(...)> in the parameter list. This use of the ellipsis is similar to that
369found in ANSI C. The programmer is able to determine the number of
370arguments passed to the XSUB by examining the C<items> variable which the
371B<xsubpp> compiler supplies for all XSUBs. By using this mechanism one can
372create an XSUB which accepts a list of parameters of unknown length.
373
374The I<host> parameter for the rpcb_gettime() XSUB can be
375optional so the ellipsis can be used to indicate that the
376XSUB will take a variable number of parameters. Perl should
d1b91892 377be able to call this XSUB with either of the following statements.
a0d0e21e 378
379 $status = rpcb_gettime( $timep, $host );
380
381 $status = rpcb_gettime( $timep );
382
383The XS code, with ellipsis, follows.
384
385 bool_t
386 rpcb_gettime(timep, ...)
8e07c86e 387 time_t timep = NO_INIT
a0d0e21e 388 CODE:
389 {
390 char *host = "localhost";
391
392 if( items > 1 )
393 host = (char *)SvPV(ST(1), na);
394 RETVAL = rpcb_gettime( host, &timep );
395 }
396 OUTPUT:
397 timep
398 RETVAL
399
400=head2 The PPCODE: Keyword
401
402The PPCODE: keyword is an alternate form of the CODE: keyword and is used
403to tell the B<xsubpp> compiler that the programmer is supplying the code to
d1b91892 404control the argument stack for the XSUBs return values. Occasionally one
a0d0e21e 405will want an XSUB to return a list of values rather than a single value.
406In these cases one must use PPCODE: and then explicitly push the list of
407values on the stack. The PPCODE: and CODE: keywords are not used
408together within the same XSUB.
409
410The following XSUB will call the C rpcb_gettime() function
411and will return its two output values, timep and status, to
412Perl as a single list.
413
d1b91892 414 void
415 rpcb_gettime(host)
8e07c86e 416 char *host
a0d0e21e 417 PPCODE:
418 {
419 time_t timep;
420 bool_t status;
421 status = rpcb_gettime( host, &timep );
422 EXTEND(sp, 2);
cb1a09d0 423 PUSHs(sv_2mortal(newSViv(status)));
424 PUSHs(sv_2mortal(newSViv(timep)));
a0d0e21e 425 }
426
427Notice that the programmer must supply the C code necessary
428to have the real rpcb_gettime() function called and to have
429the return values properly placed on the argument stack.
430
431The C<void> return type for this function tells the B<xsubpp> compiler that
432the RETVAL variable is not needed or used and that it should not be created.
433In most scenarios the void return type should be used with the PPCODE:
434directive.
435
436The EXTEND() macro is used to make room on the argument
437stack for 2 return values. The PPCODE: directive causes the
438B<xsubpp> compiler to create a stack pointer called C<sp>, and it
439is this pointer which is being used in the EXTEND() macro.
440The values are then pushed onto the stack with the PUSHs()
441macro.
442
443Now the rpcb_gettime() function can be used from Perl with
444the following statement.
445
446 ($status, $timep) = rpcb_gettime("localhost");
447
448=head2 Returning Undef And Empty Lists
449
d1b91892 450Occasionally the programmer will want to simply return
a0d0e21e 451C<undef> or an empty list if a function fails rather than a
452separate status value. The rpcb_gettime() function offers
453just this situation. If the function succeeds we would like
454to have it return the time and if it fails we would like to
455have undef returned. In the following Perl code the value
456of $timep will either be undef or it will be a valid time.
457
458 $timep = rpcb_gettime( "localhost" );
459
460The following XSUB uses the C<void> return type to disable the generation of
461the RETVAL variable and uses a CODE: block to indicate to the compiler
462that the programmer has supplied all the necessary code. The
463sv_newmortal() call will initialize the return value to undef, making that
464the default return value.
465
466 void
467 rpcb_gettime(host)
468 char * host
469 CODE:
470 {
471 time_t timep;
472 bool_t x;
473 ST(0) = sv_newmortal();
474 if( rpcb_gettime( host, &timep ) )
475 sv_setnv( ST(0), (double)timep);
476 }
477
478The next example demonstrates how one would place an explicit undef in the
479return value, should the need arise.
480
481 void
482 rpcb_gettime(host)
483 char * host
484 CODE:
485 {
486 time_t timep;
487 bool_t x;
488 ST(0) = sv_newmortal();
489 if( rpcb_gettime( host, &timep ) ){
490 sv_setnv( ST(0), (double)timep);
491 }
492 else{
493 ST(0) = &sv_undef;
494 }
495 }
496
497To return an empty list one must use a PPCODE: block and
498then not push return values on the stack.
499
500 void
501 rpcb_gettime(host)
8e07c86e 502 char *host
a0d0e21e 503 PPCODE:
504 {
505 time_t timep;
506 if( rpcb_gettime( host, &timep ) )
cb1a09d0 507 PUSHs(sv_2mortal(newSViv(timep)));
a0d0e21e 508 else{
509 /* Nothing pushed on stack, so an empty */
510 /* list is implicitly returned. */
511 }
512 }
513
4633a7c4 514=head2 The REQUIRE: Keyword
515
516The REQUIRE: keyword is used to indicate the minimum version of the
517B<xsubpp> compiler needed to compile the XS module. An XS module which
518contains the following statement will only compile with B<xsubpp> version
5191.922 or greater:
520
521 REQUIRE: 1.922
522
a0d0e21e 523=head2 The CLEANUP: Keyword
524
525This keyword can be used when an XSUB requires special cleanup procedures
526before it terminates. When the CLEANUP: keyword is used it must follow
527any CODE:, PPCODE:, or OUTPUT: blocks which are present in the XSUB. The
528code specified for the cleanup block will be added as the last statements
529in the XSUB.
530
531=head2 The BOOT: Keyword
532
533The BOOT: keyword is used to add code to the extension's bootstrap
534function. The bootstrap function is generated by the B<xsubpp> compiler and
535normally holds the statements necessary to register any XSUBs with Perl.
536With the BOOT: keyword the programmer can tell the compiler to add extra
537statements to the bootstrap function.
538
539This keyword may be used any time after the first MODULE keyword and should
540appear on a line by itself. The first blank line after the keyword will
541terminate the code block.
542
543 BOOT:
544 # The following message will be printed when the
545 # bootstrap function executes.
546 printf("Hello from the bootstrap!\n");
547
548=head2 Inserting Comments and C Preprocessor Directives
549
550Comments and C preprocessor directives are allowed within
551CODE:, PPCODE:, BOOT:, and CLEANUP: blocks. The compiler
552will pass the preprocessor directives through untouched and
553will remove the commented lines. Comments can be added to
554XSUBs by placing a C<#> at the beginning of the line. Care
555should be taken to avoid making the comment look like a C
556preprocessor directive, lest it be interpreted as such.
557
558=head2 Using XS With C++
559
560If a function is defined as a C++ method then it will assume
561its first argument is an object pointer. The object pointer
562will be stored in a variable called THIS. The object should
563have been created by C++ with the new() function and should
cb1a09d0 564be blessed by Perl with the sv_setref_pv() macro. The
565blessing of the object by Perl can be handled by a typemap. An example
566typemap is shown at the end of this section.
a0d0e21e 567
568If the method is defined as static it will call the C++
569function using the class::method() syntax. If the method is not static
570the function will be called using the THIS->method() syntax.
571
cb1a09d0 572The next examples will use the following C++ class.
a0d0e21e 573
cb1a09d0 574 class colors {
575 public:
576 colors();
577 ~colors();
578 int blue();
579 void set_blue( int );
580
581 private:
582 int c_blue;
583 };
584
585The XSUBs for the blue() and set_blue() methods are defined with the class
586name but the parameter for the object (THIS, or "self") is implicit and is
587not listed.
588
589 int
590 color::blue()
a0d0e21e 591
592 void
cb1a09d0 593 color::set_blue( val )
594 int val
a0d0e21e 595
cb1a09d0 596Both functions will expect an object as the first parameter. The xsubpp
597compiler will call that object C<THIS> and will use it to call the specified
598method. So in the C++ code the blue() and set_blue() methods will be called
599in the following manner.
a0d0e21e 600
cb1a09d0 601 RETVAL = THIS->blue();
a0d0e21e 602
cb1a09d0 603 THIS->set_blue( val );
a0d0e21e 604
cb1a09d0 605If the function's name is B<DESTROY> then the C++ C<delete> function will be
606called and C<THIS> will be given as its parameter.
a0d0e21e 607
d1b91892 608 void
cb1a09d0 609 color::DESTROY()
610
611The C++ code will call C<delete>.
612
613 delete THIS;
a0d0e21e 614
cb1a09d0 615If the function's name is B<new> then the C++ C<new> function will be called
616to create a dynamic C++ object. The XSUB will expect the class name, which
617will be kept in a variable called C<CLASS>, to be given as the first
618argument.
a0d0e21e 619
cb1a09d0 620 color *
621 color::new()
a0d0e21e 622
cb1a09d0 623The C++ code will call C<new>.
a0d0e21e 624
cb1a09d0 625 RETVAL = new color();
626
627The following is an example of a typemap that could be used for this C++
628example.
629
630 TYPEMAP
631 color * O_OBJECT
632
633 OUTPUT
634 # The Perl object is blessed into 'CLASS', which should be a
635 # char* having the name of the package for the blessing.
636 O_OBJECT
637 sv_setref_pv( $arg, CLASS, (void*)$var );
638
639 INPUT
640 O_OBJECT
641 if( sv_isobject($arg) && (SvTYPE(SvRV($arg)) == SVt_PVMG) )
642 $var = ($type)SvIV((SV*)SvRV( $arg ));
643 else{
644 warn( \"${Package}::$func_name() -- $var is not a blessed SV reference\" );
645 XSRETURN_UNDEF;
646 }
a0d0e21e 647
d1b91892 648=head2 Interface Strategy
a0d0e21e 649
650When designing an interface between Perl and a C library a straight
651translation from C to XS is often sufficient. The interface will often be
652very C-like and occasionally nonintuitive, especially when the C function
653modifies one of its parameters. In cases where the programmer wishes to
654create a more Perl-like interface the following strategy may help to
655identify the more critical parts of the interface.
656
657Identify the C functions which modify their parameters. The XSUBs for
658these functions may be able to return lists to Perl, or may be
659candidates to return undef or an empty list in case of failure.
660
d1b91892 661Identify which values are used by only the C and XSUB functions
a0d0e21e 662themselves. If Perl does not need to access the contents of the value
663then it may not be necessary to provide a translation for that value
664from C to Perl.
665
666Identify the pointers in the C function parameter lists and return
667values. Some pointers can be handled in XS with the & unary operator on
668the variable name while others will require the use of the * operator on
669the type name. In general it is easier to work with the & operator.
670
671Identify the structures used by the C functions. In many
672cases it may be helpful to use the T_PTROBJ typemap for
673these structures so they can be manipulated by Perl as
674blessed objects.
675
a0d0e21e 676=head2 Perl Objects And C Structures
677
678When dealing with C structures one should select either
679B<T_PTROBJ> or B<T_PTRREF> for the XS type. Both types are
680designed to handle pointers to complex objects. The
681T_PTRREF type will allow the Perl object to be unblessed
682while the T_PTROBJ type requires that the object be blessed.
683By using T_PTROBJ one can achieve a form of type-checking
d1b91892 684because the XSUB will attempt to verify that the Perl object
a0d0e21e 685is of the expected type.
686
687The following XS code shows the getnetconfigent() function which is used
8e07c86e 688with ONC+ TIRPC. The getnetconfigent() function will return a pointer to a
a0d0e21e 689C structure and has the C prototype shown below. The example will
690demonstrate how the C pointer will become a Perl reference. Perl will
691consider this reference to be a pointer to a blessed object and will
692attempt to call a destructor for the object. A destructor will be
693provided in the XS source to free the memory used by getnetconfigent().
694Destructors in XS can be created by specifying an XSUB function whose name
695ends with the word B<DESTROY>. XS destructors can be used to free memory
696which may have been malloc'd by another XSUB.
697
698 struct netconfig *getnetconfigent(const char *netid);
699
700A C<typedef> will be created for C<struct netconfig>. The Perl
701object will be blessed in a class matching the name of the C
702type, with the tag C<Ptr> appended, and the name should not
703have embedded spaces if it will be a Perl package name. The
704destructor will be placed in a class corresponding to the
705class of the object and the PREFIX keyword will be used to
706trim the name to the word DESTROY as Perl will expect.
707
708 typedef struct netconfig Netconfig;
709
710 MODULE = RPC PACKAGE = RPC
711
712 Netconfig *
713 getnetconfigent(netid)
8e07c86e 714 char *netid
a0d0e21e 715
716 MODULE = RPC PACKAGE = NetconfigPtr PREFIX = rpcb_
717
718 void
719 rpcb_DESTROY(netconf)
8e07c86e 720 Netconfig *netconf
a0d0e21e 721 CODE:
722 printf("Now in NetconfigPtr::DESTROY\n");
723 free( netconf );
724
725This example requires the following typemap entry. Consult the typemap
726section for more information about adding new typemaps for an extension.
727
728 TYPEMAP
729 Netconfig * T_PTROBJ
730
731This example will be used with the following Perl statements.
732
733 use RPC;
734 $netconf = getnetconfigent("udp");
735
736When Perl destroys the object referenced by $netconf it will send the
737object to the supplied XSUB DESTROY function. Perl cannot determine, and
738does not care, that this object is a C struct and not a Perl object. In
739this sense, there is no difference between the object created by the
740getnetconfigent() XSUB and an object created by a normal Perl subroutine.
741
a0d0e21e 742=head2 The Typemap
743
744The typemap is a collection of code fragments which are used by the B<xsubpp>
745compiler to map C function parameters and values to Perl values. The
746typemap file may consist of three sections labeled C<TYPEMAP>, C<INPUT>, and
747C<OUTPUT>. The INPUT section tells the compiler how to translate Perl values
748into variables of certain C types. The OUTPUT section tells the compiler
749how to translate the values from certain C types into values Perl can
750understand. The TYPEMAP section tells the compiler which of the INPUT and
751OUTPUT code fragments should be used to map a given C type to a Perl value.
752Each of the sections of the typemap must be preceded by one of the TYPEMAP,
753INPUT, or OUTPUT keywords.
754
755The default typemap in the C<ext> directory of the Perl source contains many
756useful types which can be used by Perl extensions. Some extensions define
757additional typemaps which they keep in their own directory. These
758additional typemaps may reference INPUT and OUTPUT maps in the main
759typemap. The B<xsubpp> compiler will allow the extension's own typemap to
760override any mappings which are in the default typemap.
761
762Most extensions which require a custom typemap will need only the TYPEMAP
763section of the typemap file. The custom typemap used in the
764getnetconfigent() example shown earlier demonstrates what may be the typical
765use of extension typemaps. That typemap is used to equate a C structure
766with the T_PTROBJ typemap. The typemap used by getnetconfigent() is shown
767here. Note that the C type is separated from the XS type with a tab and
768that the C unary operator C<*> is considered to be a part of the C type name.
769
770 TYPEMAP
771 Netconfig *<tab>T_PTROBJ
772
773=head1 EXAMPLES
774
775File C<RPC.xs>: Interface to some ONC+ RPC bind library functions.
776
777 #include "EXTERN.h"
778 #include "perl.h"
779 #include "XSUB.h"
780
781 #include <rpc/rpc.h>
782
783 typedef struct netconfig Netconfig;
784
785 MODULE = RPC PACKAGE = RPC
786
787 void
788 rpcb_gettime(host="localhost")
8e07c86e 789 char *host
a0d0e21e 790 CODE:
791 {
792 time_t timep;
793 ST(0) = sv_newmortal();
794 if( rpcb_gettime( host, &timep ) )
795 sv_setnv( ST(0), (double)timep );
796 }
797
798 Netconfig *
799 getnetconfigent(netid="udp")
8e07c86e 800 char *netid
a0d0e21e 801
802 MODULE = RPC PACKAGE = NetconfigPtr PREFIX = rpcb_
803
804 void
805 rpcb_DESTROY(netconf)
8e07c86e 806 Netconfig *netconf
a0d0e21e 807 CODE:
808 printf("NetconfigPtr::DESTROY\n");
809 free( netconf );
810
811File C<typemap>: Custom typemap for RPC.xs.
812
813 TYPEMAP
814 Netconfig * T_PTROBJ
815
816File C<RPC.pm>: Perl module for the RPC extension.
817
818 package RPC;
819
820 require Exporter;
821 require DynaLoader;
822 @ISA = qw(Exporter DynaLoader);
823 @EXPORT = qw(rpcb_gettime getnetconfigent);
824
825 bootstrap RPC;
826 1;
827
828File C<rpctest.pl>: Perl test program for the RPC extension.
829
830 use RPC;
831
832 $netconf = getnetconfigent();
833 $a = rpcb_gettime();
834 print "time = $a\n";
835 print "netconf = $netconf\n";
836
837 $netconf = getnetconfigent("tcp");
838 $a = rpcb_gettime("poplar");
839 print "time = $a\n";
840 print "netconf = $netconf\n";
841
842
843=head1 AUTHOR
844
d1b91892 845Dean Roehrich F<E<lt>roehrich@cray.comE<gt>>
cb1a09d0 846Dec 10, 1995