This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Integrate with Sarathy. perl.h and util.c required manual resolving.
[perl5.git] / pod / perlxs.pod
index ffbaa6b..ee582e0 100644 (file)
@@ -23,47 +23,20 @@ many common C types.  A supplement typemap must be created
 to handle special structures and types for the library being
 linked.
 
-=head2 Getting Started
-
-A new extension should begin with the B<h2xs> tool.  This will generate
-templates for the new Perl module (PM), the XS source file (XS), the MANIFEST
-file, and the Makefile.PL (PL) files.  The Makefile.PL file is a Perl script
-which will generate a Makefile.  This makefile knows how to find and run
-xsubpp for your extension.  When you type "make" your XS file will be run
-through xsubpp and a C file will be produced.  Then the C file will be
-compiled.  A simple example looks like this for an example module named
-B<Foo>:
-
-       $ h2xs -Afn Foo
-       $ cd ext/Foo
-       $ ls
-       Foo.pm Foo.xs MANIFEST Makefile.PL
-       $ perl5 Makefile.PL
-       $ ls
-       Foo.pm Foo.xs MANIFEST Makefile.PL Makefile
-       $ <edit Foo.pm and Foo.xs to add your stuff>
-       $ make
-       <you will see xsubpp run on Foo.xs and you'll see the C compiler
-       <run on Foo.c, and a bunch of other less-interesting things
-       <will happen.
-
-If your Perl was built with dynamic loading then the makefile will build a
-dynamically loadable extension.  If you don't have dynamic loading then the
-makefile will build a static extension and should create a new Perl binary.
-The default behavior depends on what is available.
-
-For more information about h2xs consult its manpage, embedded in the
-source.  For information about the Makefile.PL and Makefile consult the
-MakeMaker manpage.
+See L<perlxstut> for a tutorial on the whole extension creation process.
+
+Note: For many extensions, Dave Beazley's SWIG system provides a
+significantly more convenient mechanism for creating the XS glue
+code. See L<http://www.cs.utah.edu/~beazley/SWIG> for more 
+information.
 
 =head2 On The Road
 
-Many of the examples which follow will concentrate on creating an
-interface between Perl and the ONC+ RPC bind library functions.
-Specifically, the rpcb_gettime() function will be used to demonstrate many
-features of the XS language.  This function has two parameters; the first
-is an input parameter and the second is an output parameter.  The function
-also returns a status value.
+Many of the examples which follow will concentrate on creating an interface
+between Perl and the ONC+ RPC bind library functions.  The rpcb_gettime()
+function is used to demonstrate many features of the XS language.  This
+function has two parameters; the first is an input parameter and the second
+is an output parameter.  The function also returns a status value.
 
        bool_t rpcb_gettime(const char *host, time_t *timep);
 
@@ -151,16 +124,6 @@ the variable, as is demonstrated in the rpcb_gettime() function above.  See
 the section on typemaps for more about handling qualifiers and unary
 operators in C types.
 
-The parameter list of a function must not have whitespace after the
-open-parenthesis or before the close-parenthesis.  (This restriction will be
-relaxed in later versions of B<xsubpp>.)
-
-   INCORRECT                      CORRECT
-
-   double                         double
-   sin( x )                       sin(x)
-     double x                      double x
-
 The function name and the return type must be placed on
 separate lines.
 
@@ -170,6 +133,16 @@ separate lines.
     double x                       sin(x)
                                     double x
 
+The function body may be indented or left-adjusted.  The following example
+shows a function with its body left-adjusted.  Most examples in this
+document will indent the body.
+
+  CORRECT
+
+  double
+  sin(x)
+  double x
+
 =head2 The Argument Stack
 
 The argument stack is used to store the values which are
@@ -199,7 +172,21 @@ be received by Perl as the return value of the XSUB.
 
 If the XSUB has a return type of C<void> then the compiler will
 not supply a RETVAL variable for that function.  When using
-the PPCODE: directive the RETVAL variable may not be needed.
+the PPCODE: directive the RETVAL variable is not needed, unless used
+explicitly.
+
+If PPCODE: directive is not used, C<void> return value should be used
+only for subroutines which do not return a value, I<even if> CODE:
+directive is used which sets ST(0) explicitly.
+
+Older versions of this document recommended to use C<void> return
+value in such cases. It was discovered that this could lead to
+segfaults in cases when XSUB was I<truly> C<void>. This practice is
+now deprecated, and may be not supported at some future version. Use
+the return value C<SV *> in such cases. (Currently C<xsubpp> contains
+some heuristic code which tries to disambiguate between "truly-void"
+and "old-practice-declared-as-void" functions. Hence your code is at
+mercy of this heuristics unless you use C<SV *> as return value.)
 
 =head2 The MODULE Keyword
 
@@ -295,6 +282,17 @@ typemap.
           OUTPUT:
           timep sv_setnv(ST(1), (double)timep);
 
+B<xsubpp> emits an automatic C<SvSETMAGIC()> for all parameters in the
+OUTPUT section of the XSUB, except RETVAL.  This is the usually desired
+behavior, as it takes care of properly invoking 'set' magic on output
+parameters (needed for hash or array element parameters that must be
+created if they didn't exist).  If for some reason, this behavior is
+not desired, the OUTPUT section may contain a C<SETMAGIC: DISABLE> line
+to disable it for the remainder of the parameters in the OUTPUT section.
+Likewise,  C<SETMAGIC: ENABLE> can be used to reenable it for the
+remainder of the OUTPUT section.  See L<perlguts> for more details
+about 'set' magic.
+
 =head2 The CODE: Keyword
 
 This keyword is used in more complicated XSUBs which require
@@ -307,7 +305,7 @@ its parameters.  The Perl usage is given first.
 
      $status = rpcb_gettime( "localhost", $timep );
 
-The XSUB follows. 
+The XSUB follows.
 
      bool_t
      rpcb_gettime(host,timep)
@@ -319,15 +317,25 @@ The XSUB follows.
           timep
           RETVAL
 
-In many of the examples shown here the CODE: block (and
-other blocks) will often be contained within braces ( C<{> and
-C<}> ).  This protects the CODE: block from complex INPUT
-typemaps and ensures the resulting C code is legal.
+=head2 The INIT: Keyword
+
+The INIT: keyword allows initialization to be inserted into the XSUB before
+the compiler generates the call to the C function.  Unlike the CODE: keyword
+above, this keyword does not affect the way the compiler handles RETVAL.
+
+    bool_t
+    rpcb_gettime(host,timep)
+          char *host
+          time_t &timep
+         INIT:
+         printf("# Host is %s\n", host );
+          OUTPUT:
+          timep
 
 =head2 The NO_INIT Keyword
 
 The NO_INIT keyword is used to indicate that a function
-parameter is being used as only an output value.  The B<xsubpp>
+parameter is being used only as an output value.  The B<xsubpp>
 compiler will normally generate code to read the values of
 all function parameters from the argument stack and assign
 them to C variables upon entry to the function.  NO_INIT
@@ -336,7 +344,7 @@ output rather than for input and that they will be handled
 before the function terminates.
 
 The following example shows a variation of the rpcb_gettime() function.
-This function uses the timep variable as only an output variable and does
+This function uses the timep variable only as an output variable and does
 not care about its initial contents.
 
      bool_t
@@ -352,17 +360,19 @@ Function parameters are normally initialized with their
 values from the argument stack.  The typemaps contain the
 code segments which are used to transfer the Perl values to
 the C parameters.  The programmer, however, is allowed to
-override the typemaps and supply alternate initialization
-code.
+override the typemaps and supply alternate (or additional)
+initialization code.
 
 The following code demonstrates how to supply initialization code for
-function parameters.  The initialization code is eval'd by the compiler
-before it is added to the output so anything which should be interpreted
-literally, such as double quotes, must be protected with backslashes.
+function parameters.  The initialization code is eval'd within double
+quotes by the compiler before it is added to the output so anything
+which should be interpreted literally [mainly C<$>, C<@>, or C<\\>]
+must be protected with backslashes.  The variables $var, $arg,
+and $type can be used as in typemaps.
 
      bool_t
      rpcb_gettime(host,timep)
-          char *host = (char *)SvPV(ST(0),na);
+          char *host = (char *)SvPV($arg,PL_na);
           time_t &timep = 0;
           OUTPUT:
           timep
@@ -372,6 +382,24 @@ would normally use this when a function parameter must be processed by
 another library function before it can be used.  Default parameters are
 covered in the next section.
 
+If the initialization begins with C<=>, then it is output on
+the same line where the input variable is declared.  If the
+initialization begins with C<;> or C<+>, then it is output after
+all of the input variables have been declared.  The C<=> and C<;>
+cases replace the initialization normally supplied from the typemap.
+For the C<+> case, the initialization from the typemap will precede
+the initialization code included after the C<+>.  A global
+variable, C<%v>, is available for the truly rare case where
+information from one initialization is needed in another
+initialization.
+
+     bool_t
+     rpcb_gettime(host,timep)
+          time_t &timep ; /*\$v{time}=@{[$v{time}=$arg]}*/
+          char *host + SvOK($v{time}) ? SvPV($arg,PL_na) : NULL;
+          OUTPUT:
+          timep
+
 =head2 Default Parameter Values
 
 Default values can be specified for function parameters by
@@ -403,6 +431,103 @@ the parameters in the correct order for that function.
           timep
           RETVAL
 
+=head2 The PREINIT: Keyword
+
+The PREINIT: keyword allows extra variables to be declared before the
+typemaps are expanded.  If a variable is declared in a CODE: block then that
+variable will follow any typemap code.  This may result in a C syntax
+error.  To force the variable to be declared before the typemap code, place
+it into a PREINIT: block.  The PREINIT: keyword may be used one or more
+times within an XSUB.
+
+The following examples are equivalent, but if the code is using complex
+typemaps then the first example is safer.
+
+     bool_t
+     rpcb_gettime(timep)
+          time_t timep = NO_INIT
+         PREINIT:
+          char *host = "localhost";
+          CODE:
+         RETVAL = rpcb_gettime( host, &timep );
+          OUTPUT:
+          timep
+          RETVAL
+
+A correct, but error-prone example.
+
+     bool_t
+     rpcb_gettime(timep)
+          time_t timep = NO_INIT
+         CODE:
+          char *host = "localhost";
+         RETVAL = rpcb_gettime( host, &timep );
+          OUTPUT:
+          timep
+          RETVAL
+
+=head2 The SCOPE: Keyword
+
+The SCOPE: keyword allows scoping to be enabled for a particular XSUB. If
+enabled, the XSUB will invoke ENTER and LEAVE automatically.
+
+To support potentially complex type mappings, if a typemap entry used
+by this XSUB contains a comment like C</*scope*/> then scoping will
+automatically be enabled for that XSUB.
+
+To enable scoping:
+
+    SCOPE: ENABLE
+
+To disable scoping:
+
+    SCOPE: DISABLE
+
+=head2 The INPUT: Keyword
+
+The XSUB's parameters are usually evaluated immediately after entering the
+XSUB.  The INPUT: keyword can be used to force those parameters to be
+evaluated a little later.  The INPUT: keyword can be used multiple times
+within an XSUB and can be used to list one or more input variables.  This
+keyword is used with the PREINIT: keyword.
+
+The following example shows how the input parameter C<timep> can be
+evaluated late, after a PREINIT.
+
+    bool_t
+    rpcb_gettime(host,timep)
+          char *host
+         PREINIT:
+         time_t tt;
+         INPUT:
+          time_t timep
+          CODE:
+               RETVAL = rpcb_gettime( host, &tt );
+              timep = tt;
+          OUTPUT:
+          timep
+          RETVAL
+
+The next example shows each input parameter evaluated late.
+
+    bool_t
+    rpcb_gettime(host,timep)
+         PREINIT:
+         time_t tt;
+         INPUT:
+          char *host
+         PREINIT:
+         char *h;
+         INPUT:
+          time_t timep
+          CODE:
+              h = host;
+              RETVAL = rpcb_gettime( h, &tt );
+              timep = tt;
+          OUTPUT:
+          timep
+          RETVAL
+
 =head2 Variable-length Parameter Lists
 
 XSUBs can have variable-length parameter lists by specifying an ellipsis
@@ -426,18 +551,43 @@ The XS code, with ellipsis, follows.
      bool_t
      rpcb_gettime(timep, ...)
           time_t timep = NO_INIT
-          CODE:
-          {
+         PREINIT:
           char *host = "localhost";
-
-          if( items > 1 )
-               host = (char *)SvPV(ST(1), na);
-          RETVAL = rpcb_gettime( host, &timep );
-          }
+         STRLEN n_a;
+          CODE:
+                 if( items > 1 )
+                      host = (char *)SvPV(ST(1), n_a);
+                 RETVAL = rpcb_gettime( host, &timep );
           OUTPUT:
           timep
           RETVAL
 
+=head2 The C_ARGS: Keyword
+
+The C_ARGS: keyword allows creating of XSUBS which have different
+calling sequence from Perl than from C, without a need to write
+CODE: or CPPCODE: section.  The contents of the C_ARGS: paragraph is
+put as the argument to the called C function without any change.
+
+For example, suppose that C function is declared as
+
+    symbolic nth_derivative(int n, symbolic function, int flags);
+
+and that the default flags are kept in a global C variable
+C<default_flags>.  Suppose that you want to create an interface which
+is called as
+
+    $second_deriv = $function->nth_derivative(2);
+
+To do this, declare the XSUB as
+
+    symbolic
+    nth_derivative(function, n)
+       symbolic        function
+       int             n
+    C_ARGS:
+       n, function, default_flags
+
 =head2 The PPCODE: Keyword
 
 The PPCODE: keyword is an alternate form of the CODE: keyword and is used
@@ -455,15 +605,14 @@ Perl as a single list.
      void
      rpcb_gettime(host)
           char *host
-          PPCODE:
-          {
+         PREINIT:
           time_t  timep;
           bool_t  status;
+          PPCODE:
           status = rpcb_gettime( host, &timep );
-          EXTEND(sp, 2);
-          PUSHs(sv_2mortal(newSVnv(status)));
-          PUSHs(sv_2mortal(newSVnv(timep)));
-          }
+          EXTEND(SP, 2);
+          PUSHs(sv_2mortal(newSViv(status)));
+          PUSHs(sv_2mortal(newSViv(timep)));
 
 Notice that the programmer must supply the C code necessary
 to have the real rpcb_gettime() function called and to have
@@ -476,7 +625,7 @@ directive.
 
 The EXTEND() macro is used to make room on the argument
 stack for 2 return values.  The PPCODE: directive causes the
-B<xsubpp> compiler to create a stack pointer called C<sp>, and it
+B<xsubpp> compiler to create a stack pointer available as C<SP>, and it
 is this pointer which is being used in the EXTEND() macro.
 The values are then pushed onto the stack with the PUSHs()
 macro.
@@ -486,9 +635,12 @@ the following statement.
 
      ($status, $timep) = rpcb_gettime("localhost");
 
+When handling output parameters with a PPCODE section, be sure to handle
+'set' magic properly.  See L<perlguts> for details about 'set' magic.
+
 =head2 Returning Undef And Empty Lists
 
-Occasionally the programmer will want to simply return
+Occasionally the programmer will want to return simply
 C<undef> or an empty list if a function fails rather than a
 separate status value.  The rpcb_gettime() function offers
 just this situation.  If the function succeeds we would like
@@ -498,41 +650,39 @@ of $timep will either be undef or it will be a valid time.
 
      $timep = rpcb_gettime( "localhost" );
 
-The following XSUB uses the C<void> return type to disable the generation of
-the RETVAL variable and uses a CODE: block to indicate to the compiler
+The following XSUB uses the C<SV *> return type as a mnemonic only,
+and uses a CODE: block to indicate to the compiler
 that the programmer has supplied all the necessary code.  The
 sv_newmortal() call will initialize the return value to undef, making that
 the default return value.
 
-     void
+     SV *
      rpcb_gettime(host)
           char *  host
-          CODE:
-          {
+         PREINIT:
           time_t  timep;
           bool_t x;
+          CODE:
           ST(0) = sv_newmortal();
           if( rpcb_gettime( host, &timep ) )
                sv_setnv( ST(0), (double)timep);
-          }
 
 The next example demonstrates how one would place an explicit undef in the
 return value, should the need arise.
 
-     void
+     SV *
      rpcb_gettime(host)
           char *  host
-          CODE:
-          {
+         PREINIT:
           time_t  timep;
           bool_t x;
+          CODE:
           ST(0) = sv_newmortal();
           if( rpcb_gettime( host, &timep ) ){
                sv_setnv( ST(0), (double)timep);
           }
           else{
-               ST(0) = &sv_undef;
-          }
+               ST(0) = &PL_sv_undef;
           }
 
 To return an empty list one must use a PPCODE: block and
@@ -541,16 +691,30 @@ then not push return values on the stack.
      void
      rpcb_gettime(host)
           char *host
-          PPCODE:
-          {
+         PREINIT:
           time_t  timep;
+          PPCODE:
           if( rpcb_gettime( host, &timep ) )
-               PUSHs(sv_2mortal(newSVnv(timep)));
+               PUSHs(sv_2mortal(newSViv(timep)));
           else{
           /* Nothing pushed on stack, so an empty */
           /* list is implicitly returned. */
           }
-          }
+
+Some people may be inclined to include an explicit C<return> in the above
+XSUB, rather than letting control fall through to the end.  In those
+situations C<XSRETURN_EMPTY> should be used, instead.  This will ensure that
+the XSUB stack is properly adjusted.  Consult L<perlguts/"API LISTING"> for
+other C<XSRETURN> macros.
+
+=head2 The REQUIRE: Keyword
+
+The REQUIRE: keyword is used to indicate the minimum version of the
+B<xsubpp> compiler needed to compile the XS module.  An XS module which
+contains the following statement will compile with only B<xsubpp> version
+1.922 or greater:
+
+       REQUIRE: 1.922
 
 =head2 The CLEANUP: Keyword
 
@@ -577,15 +741,289 @@ terminate the code block.
      # bootstrap function executes.
      printf("Hello from the bootstrap!\n");
 
+=head2 The VERSIONCHECK: Keyword
+
+The VERSIONCHECK: keyword corresponds to B<xsubpp>'s C<-versioncheck> and
+C<-noversioncheck> options.  This keyword overrides the command line
+options.  Version checking is enabled by default.  When version checking is
+enabled the XS module will attempt to verify that its version matches the
+version of the PM module.
+
+To enable version checking:
+
+    VERSIONCHECK: ENABLE
+
+To disable version checking:
+
+    VERSIONCHECK: DISABLE
+
+=head2 The PROTOTYPES: Keyword
+
+The PROTOTYPES: keyword corresponds to B<xsubpp>'s C<-prototypes> and
+C<-noprototypes> options.  This keyword overrides the command line options.
+Prototypes are enabled by default.  When prototypes are enabled XSUBs will
+be given Perl prototypes.  This keyword may be used multiple times in an XS
+module to enable and disable prototypes for different parts of the module.
+
+To enable prototypes:
+
+    PROTOTYPES: ENABLE
+
+To disable prototypes:
+
+    PROTOTYPES: DISABLE
+
+=head2 The PROTOTYPE: Keyword
+
+This keyword is similar to the PROTOTYPES: keyword above but can be used to
+force B<xsubpp> to use a specific prototype for the XSUB.  This keyword
+overrides all other prototype options and keywords but affects only the
+current XSUB.  Consult L<perlsub/Prototypes> for information about Perl
+prototypes.
+
+    bool_t
+    rpcb_gettime(timep, ...)
+          time_t timep = NO_INIT
+         PROTOTYPE: $;$
+         PREINIT:
+          char *host = "localhost";
+         STRLEN n_a;
+          CODE:
+                 if( items > 1 )
+                      host = (char *)SvPV(ST(1), n_a);
+                 RETVAL = rpcb_gettime( host, &timep );
+          OUTPUT:
+          timep
+          RETVAL
+
+=head2 The ALIAS: Keyword
+
+The ALIAS: keyword allows an XSUB to have two or more unique Perl names
+and to know which of those names was used when it was invoked.  The Perl
+names may be fully-qualified with package names.  Each alias is given an
+index.  The compiler will setup a variable called C<ix> which contain the
+index of the alias which was used.  When the XSUB is called with its
+declared name C<ix> will be 0.
+
+The following example will create aliases C<FOO::gettime()> and
+C<BAR::getit()> for this function.
+
+    bool_t
+    rpcb_gettime(host,timep)
+          char *host
+          time_t &timep
+         ALIAS:
+           FOO::gettime = 1
+           BAR::getit = 2
+         INIT:
+         printf("# ix = %d\n", ix );
+          OUTPUT:
+          timep
+
+=head2 The INTERFACE: Keyword
+
+This keyword declares the current XSUB as a keeper of the given
+calling signature.  If some text follows this keyword, it is
+considered as a list of functions which have this signature, and
+should be attached to XSUBs.
+
+Say, if you have 4 functions multiply(), divide(), add(), subtract() all
+having the signature
+
+    symbolic f(symbolic, symbolic);
+
+you code them all by using XSUB
+
+    symbolic
+    interface_s_ss(arg1, arg2)  
+       symbolic        arg1
+       symbolic        arg2
+    INTERFACE:
+       multiply divide 
+       add subtract
+
+The advantage of this approach comparing to ALIAS: keyword is that one
+can attach an extra function remainder() at runtime by using
+    
+    CV *mycv = newXSproto("Symbolic::remainder", 
+                         XS_Symbolic_interface_s_ss, __FILE__, "$$");
+    XSINTERFACE_FUNC_SET(mycv, remainder);
+
+(This example supposes that there was no INTERFACE_MACRO: section,
+otherwise one needs to use something else instead of
+C<XSINTERFACE_FUNC_SET>.)
+
+=head2 The INTERFACE_MACRO: Keyword
+
+This keyword allows one to define an INTERFACE using a different way
+to extract a function pointer from an XSUB.  The text which follows
+this keyword should give the name of macros which would extract/set a
+function pointer.  The extractor macro is given return type, C<CV*>,
+and C<XSANY.any_dptr> for this C<CV*>.  The setter macro is given cv,
+and the function pointer.
+
+The default value is C<XSINTERFACE_FUNC> and C<XSINTERFACE_FUNC_SET>.
+An INTERFACE keyword with an empty list of functions can be omitted if
+INTERFACE_MACRO keyword is used.
+
+Suppose that in the previous example functions pointers for 
+multiply(), divide(), add(), subtract() are kept in a global C array
+C<fp[]> with offsets being C<multiply_off>, C<divide_off>, C<add_off>,
+C<subtract_off>.  Then one can use 
+
+    #define XSINTERFACE_FUNC_BYOFFSET(ret,cv,f) \
+       ((XSINTERFACE_CVT(ret,))fp[CvXSUBANY(cv).any_i32])
+    #define XSINTERFACE_FUNC_BYOFFSET_set(cv,f) \
+       CvXSUBANY(cv).any_i32 = CAT2( f, _off )
+
+in C section,
+
+    symbolic
+    interface_s_ss(arg1, arg2)  
+       symbolic        arg1
+       symbolic        arg2
+    INTERFACE_MACRO: 
+       XSINTERFACE_FUNC_BYOFFSET
+       XSINTERFACE_FUNC_BYOFFSET_set
+    INTERFACE:
+       multiply divide 
+       add subtract
+
+in XSUB section.
+
+=head2 The INCLUDE: Keyword
+
+This keyword can be used to pull other files into the XS module.  The other
+files may have XS code.  INCLUDE: can also be used to run a command to
+generate the XS code to be pulled into the module.
+
+The file F<Rpcb1.xsh> contains our C<rpcb_gettime()> function:
+
+    bool_t
+    rpcb_gettime(host,timep)
+          char *host
+          time_t &timep
+          OUTPUT:
+          timep
+
+The XS module can use INCLUDE: to pull that file into it.
+
+    INCLUDE: Rpcb1.xsh
+
+If the parameters to the INCLUDE: keyword are followed by a pipe (C<|>) then
+the compiler will interpret the parameters as a command.
+
+    INCLUDE: cat Rpcb1.xsh |
+
+=head2 The CASE: Keyword
+
+The CASE: keyword allows an XSUB to have multiple distinct parts with each
+part acting as a virtual XSUB.  CASE: is greedy and if it is used then all
+other XS keywords must be contained within a CASE:.  This means nothing may
+precede the first CASE: in the XSUB and anything following the last CASE: is
+included in that case.
+
+A CASE: might switch via a parameter of the XSUB, via the C<ix> ALIAS:
+variable (see L<"The ALIAS: Keyword">), or maybe via the C<items> variable
+(see L<"Variable-length Parameter Lists">).  The last CASE: becomes the
+B<default> case if it is not associated with a conditional.  The following
+example shows CASE switched via C<ix> with a function C<rpcb_gettime()>
+having an alias C<x_gettime()>.  When the function is called as
+C<rpcb_gettime()> its parameters are the usual C<(char *host, time_t *timep)>,
+but when the function is called as C<x_gettime()> its parameters are
+reversed, C<(time_t *timep, char *host)>.
+
+    long
+    rpcb_gettime(a,b)
+      CASE: ix == 1
+         ALIAS:
+         x_gettime = 1
+         INPUT:
+         # 'a' is timep, 'b' is host
+          char *b
+          time_t a = NO_INIT
+          CODE:
+               RETVAL = rpcb_gettime( b, &a );
+          OUTPUT:
+          a
+          RETVAL
+      CASE:
+         # 'a' is host, 'b' is timep
+          char *a
+          time_t &b = NO_INIT
+          OUTPUT:
+          b
+          RETVAL
+
+That function can be called with either of the following statements.  Note
+the different argument lists.
+
+       $status = rpcb_gettime( $host, $timep );
+
+       $status = x_gettime( $timep, $host );
+
+=head2 The & Unary Operator
+
+The & unary operator is used to tell the compiler that it should dereference
+the object when it calls the C function.  This is used when a CODE: block is
+not used and the object is a not a pointer type (the object is an C<int> or
+C<long> but not a C<int*> or C<long*>).
+
+The following XSUB will generate incorrect C code.  The xsubpp compiler will
+turn this into code which calls C<rpcb_gettime()> with parameters C<(char
+*host, time_t timep)>, but the real C<rpcb_gettime()> wants the C<timep>
+parameter to be of type C<time_t*> rather than C<time_t>.
+
+    bool_t
+    rpcb_gettime(host,timep)
+          char *host
+          time_t timep
+          OUTPUT:
+          timep
+
+That problem is corrected by using the C<&> operator.  The xsubpp compiler
+will now turn this into code which calls C<rpcb_gettime()> correctly with
+parameters C<(char *host, time_t *timep)>.  It does this by carrying the
+C<&> through, so the function call looks like C<rpcb_gettime(host, &timep)>.
+
+    bool_t
+    rpcb_gettime(host,timep)
+          char *host
+          time_t &timep
+          OUTPUT:
+          timep
+
 =head2 Inserting Comments and C Preprocessor Directives
 
-Comments and C preprocessor directives are allowed within
-CODE:, PPCODE:, BOOT:, and CLEANUP: blocks.  The compiler
-will pass the preprocessor directives through untouched and
-will remove the commented lines.  Comments can be added to
-XSUBs by placing a C<#> at the beginning of the line.  Care
-should be taken to avoid making the comment look like a C
-preprocessor directive, lest it be interpreted as such.
+C preprocessor directives are allowed within BOOT:, PREINIT: INIT:,
+CODE:, PPCODE:, and CLEANUP: blocks, as well as outside the functions.
+Comments are allowed anywhere after the MODULE keyword.  The compiler
+will pass the preprocessor directives through untouched and will remove
+the commented lines.
+
+Comments can be added to XSUBs by placing a C<#> as the first
+non-whitespace of a line.  Care should be taken to avoid making the
+comment look like a C preprocessor directive, lest it be interpreted as
+such.  The simplest way to prevent this is to put whitespace in front of
+the C<#>.
+
+If you use preprocessor directives to choose one of two
+versions of a function, use
+
+    #if ... version1
+    #else /* ... version2  */
+    #endif
+
+and not
+
+    #if ... version1
+    #endif
+    #if ... version2
+    #endif
+
+because otherwise xsubpp will believe that you made a duplicate
+definition of the function.  Also, put a blank line before the
+#else/#endif so it will not be seen as part of the function body.
 
 =head2 Using XS With C++
 
@@ -593,92 +1031,89 @@ If a function is defined as a C++ method then it will assume
 its first argument is an object pointer.  The object pointer
 will be stored in a variable called THIS.  The object should
 have been created by C++ with the new() function and should
-be blessed by Perl with the sv_setptrobj() macro.  The
-blessing of the object by Perl can be handled by the
-T_PTROBJ typemap.
+be blessed by Perl with the sv_setref_pv() macro.  The
+blessing of the object by Perl can be handled by a typemap.  An example
+typemap is shown at the end of this section.
 
 If the method is defined as static it will call the C++
 function using the class::method() syntax.  If the method is not static
-the function will be called using the THIS->method() syntax.
+the function will be called using the THIS-E<gt>method() syntax.
+
+The next examples will use the following C++ class.
+
+     class color {
+          public:
+          color();
+          ~color();
+          int blue();
+          void set_blue( int );
 
-=head2 Perl Variables
+          private:
+          int c_blue;
+     };
 
-The following demonstrates how the Perl variable $host can
-be accessed from an XSUB.  The function B<perl_get_sv()> is
-used to obtain a pointer to the variable, known as an B<SV>
-(Scalar Variable) internally.  The package name C<RPC> will be
-added to the name of the variable so perl_get_sv() will know
-in which package $host can be found.  If the package name is
-not supplied then perl_get_sv() will search package C<main> for
-the variable.  The macro B<SvPVX()> is then used to dereference
-the SV to obtain a C<char*> pointer to its contents.
+The XSUBs for the blue() and set_blue() methods are defined with the class
+name but the parameter for the object (THIS, or "self") is implicit and is
+not listed.
+
+     int
+     color::blue()
 
      void
-     rpcb_gettime()
-          PPCODE:
-          {
-          char *host;
-          SV *hostsv;
-          time_t timep;
-
-          hostsv = perl_get_sv( "RPC::host", FALSE );
-          if( hostsv != NULL ){
-               host = SvPVX( hostsv );
-               if( rpcb_gettime( host, &timep ) )
-                    PUSHs(sv_2mortal(newSVnv(timep)));
-          }
-          }
+     color::set_blue( val )
+          int val
 
-This Perl code can be used to call that XSUB.
+Both functions will expect an object as the first parameter.  The xsubpp
+compiler will call that object C<THIS> and will use it to call the specified
+method.  So in the C++ code the blue() and set_blue() methods will be called
+in the following manner.
 
-     $RPC::host = "localhost";
-     $timep = rpcb_gettime();
+     RETVAL = THIS->blue();
 
-In the above example the SV contained a C C<char*> but a Perl
-scalar variable may also contain numbers and references.  If
-the SV is expected to have a C C<int> then the macro B<SvIVX()>
-should be used to dereference the SV.  When the SV contains
-a C double then B<SvNVX()> should be used.
+     THIS->set_blue( val );
 
-The macro B<SvRV()> can be used to dereference an SV when it is a Perl
-reference.  The result will be another SV which points to the actual Perl
-variable.  This can then be dereferenced with SvPVX(), SvNVX(), or
-SvIVX().  The following XSUB will use SvRV().
+If the function's name is B<DESTROY> then the C++ C<delete> function will be
+called and C<THIS> will be given as its parameter.
 
      void
-     rpcb_gettime()
-          PPCODE:
-          {
-          char *host;
-          SV *rv;
-          SV *hostsv;
-          time_t timep;
-
-          rv = perl_get_sv( "RPC::host", FALSE );
-          if( rv != NULL ){
-               hostsv = SvRV( rv );
-               host = SvPVX( hostsv );
-               if( rpcb_gettime( host, &timep ) )
-                    PUSHs(sv_2mortal(newSVnv(timep)));
-          }
-          }
+     color::DESTROY()
+
+The C++ code will call C<delete>.
 
-This Perl code will create a variable $RPC::host which is a
-reference to $MY::host.  The variable $MY::host contains the
-hostname which will be used.
+     delete THIS;
 
-     $MY::host = "localhost";
-     $RPC::host = \$MY::host;
-     $timep = rpcb_gettime();
+If the function's name is B<new> then the C++ C<new> function will be called
+to create a dynamic C++ object.  The XSUB will expect the class name, which
+will be kept in a variable called C<CLASS>, to be given as the first
+argument.
 
-The second argument to perl_get_sv() will normally be B<FALSE>
-as shown in the above examples.  An argument of B<TRUE> will
-cause variables to be created if they do not already exist.
-One should not use TRUE unless steps are taken to deal with
-a possibly empty SV.
+     color *
+     color::new()
 
-XSUBs may use B<perl_get_av()>, B<perl_get_hv()>, and B<perl_get_cv()> to
-access Perl arrays, hashes, and code values.
+The C++ code will call C<new>.
+
+       RETVAL = new color();
+
+The following is an example of a typemap that could be used for this C++
+example.
+
+    TYPEMAP
+    color *            O_OBJECT
+
+    OUTPUT
+    # The Perl object is blessed into 'CLASS', which should be a
+    # char* having the name of the package for the blessing.
+    O_OBJECT
+       sv_setref_pv( $arg, CLASS, (void*)$var );
+
+    INPUT
+    O_OBJECT
+       if( sv_isobject($arg) && (SvTYPE(SvRV($arg)) == SVt_PVMG) )
+               $var = ($type)SvIV((SV*)SvRV( $arg ));
+       else{
+               warn( \"${Package}::$func_name() -- $var is not a blessed SV reference\" );
+               XSRETURN_UNDEF;
+       }
 
 =head2 Interface Strategy
 
@@ -708,41 +1143,6 @@ cases it may be helpful to use the T_PTROBJ typemap for
 these structures so they can be manipulated by Perl as
 blessed objects.
 
-=head2 The Perl Module
-
-The Perl module is the link between the extension library,
-which was generated from XS code, and the Perl interpreter.
-The module is used to tell Perl what the extension library
-contains.  The name and package of the module should match
-the name of the library.
-
-The following is a Perl module for an extension containing
-some ONC+ RPC bind library functions.
-
-     package RPC;
-
-     require Exporter;
-     require DynaLoader;
-     @ISA = qw(Exporter DynaLoader);
-     @EXPORT = qw( rpcb_gettime rpcb_getmaps rpcb_getaddr
-                     rpcb_rmtcall rpcb_set rpcb_unset );
-
-     bootstrap RPC;
-     1;
-
-The RPC extension contains the functions found in the
-@EXPORT list.  By using the C<Exporter> module the RPC module
-can make these function names visible to the rest of the
-Perl program.  The C<DynaLoader> module will allow the RPC
-module to bootstrap the extension library.  To load this
-extension and make the functions available, the following
-Perl statement should be used.
-
-     use RPC;
-
-For more information about the DynaLoader consult its documentation in the
-ext/DynaLoader directory in the Perl source.
-
 =head2 Perl Objects And C Structures
 
 When dealing with C structures one should select either
@@ -809,88 +1209,20 @@ does not care, that this object is a C struct and not a Perl object.  In
 this sense, there is no difference between the object created by the
 getnetconfigent() XSUB and an object created by a normal Perl subroutine.
 
-=head2 C Headers and Perl
-
-The B<h2xs> compiler is designed to convert C header files in
-/usr/include into Perl extensions.  This compiler will
-create a directory under the C<ext> directory of the Perl
-source and will populate it with a Makefile, a Perl Module,
-an XS source file, and a MANIFEST file.
-
-The following command will create an extension called C<Rusers>
-from the <rpcsvc/rusers.h> header.
-
-     h2xs rpcsvc/rusers
-
-When the Rusers extension has been compiled and installed
-Perl can use it to retrieve any C<#define> statements which
-were in the C header.
-
-     use Rusers;
-     print "RPC program number for rusers service: ";
-     print &RUSERSPROG, "\n";
-
-=head2 Creating A New Extension
-
-The B<h2xs> compiler can generate template source files and
-Makefiles.  These templates offer a suitable starting point
-for most extensions.  The following example demonstrates how
-one might use B<h2xs> to create an extension containing the RPC
-functions in this document.
-
-The extension will not use autoloaded functions and will not define
-constants, so the B<-A> option will be given to B<h2xs>.  When run from the
-Perl source directory, the B<h2xs> compiler will create the directory
-ext/RPC and will populate it with files called RPC.xs, RPC.pm, Makefile.PL,
-and MANIFEST.  The XS code for the RPC functions should be added to the
-RPC.xs file.  The @EXPORT list in RPC.pm should be updated to include the
-functions from RPC.xs.
-
-     h2xs -An RPC
-
-To compile the extension for dynamic loading the following
-command should be executed from the ext/RPC directory.
-
-     make dynamic
-
-If the extension will be statically linked into the Perl
-binary then the makefile (use C<makefile>, not C<Makefile>) in the
-Perl source directory should be edited to add C<ext/RPC/RPC.a>
-to the C<static_ext> variable.  Before making this change Perl
-should have already been built.  After the makefile has been
-updated the following command should be executed from the
-Perl source directory.
-
-     make
-
-Perl's B<Configure> script can also be used to add extensions.  The extension
-should be placed in the C<ext> directory under the Perl source before Perl
-has been built and prior to running Configure.  When Configure is run it
-will find the extension along with the other extensions in the C<ext>
-directory and will add it to the list of extensions to be built.  When make
-is run the extension will be built along with the other extensions.
-
-Configure recognizes extensions if they have an XS source
-file which matches the name of the extension directory.  If
-the extension directory includes a MANIFEST file Configure
-will search that file for any B<.SH> files and extract them
-after it extracts all the other .SH files listed in the main
-MANIFEST.  The main Perl Makefile will then run B<make> in the
-extension's directory if it finds an XS file matching the
-name of the extension's directory.
-
 =head2 The Typemap
 
 The typemap is a collection of code fragments which are used by the B<xsubpp>
 compiler to map C function parameters and values to Perl values.  The
 typemap file may consist of three sections labeled C<TYPEMAP>, C<INPUT>, and
-C<OUTPUT>.  The INPUT section tells the compiler how to translate Perl values
+C<OUTPUT>.  Any unlabelled initial section is assumed to be a C<TYPEMAP>
+section if a name is not explicitly specified.  The INPUT section tells
+the compiler how to translate Perl values
 into variables of certain C types.  The OUTPUT section tells the compiler
 how to translate the values from certain C types into values Perl can
 understand.  The TYPEMAP section tells the compiler which of the INPUT and
 OUTPUT code fragments should be used to map a given C type to a Perl value.
-Each of the sections of the typemap must be preceded by one of the TYPEMAP,
-INPUT, or OUTPUT keywords.
+The section labels C<TYPEMAP>, C<INPUT>, or C<OUTPUT> must begin
+in the first column on a line by themselves, and must be in uppercase.
 
 The default typemap in the C<ext> directory of the Perl source contains many
 useful types which can be used by Perl extensions.  Some extensions define
@@ -910,6 +1242,37 @@ that the C unary operator C<*> is considered to be a part of the C type name.
      TYPEMAP
      Netconfig *<tab>T_PTROBJ
 
+Here's a more complicated example: suppose that you wanted C<struct
+netconfig> to be blessed into the class C<Net::Config>.  One way to do
+this is to use underscores (_) to separate package names, as follows:
+
+        typedef struct netconfig * Net_Config;
+
+And then provide a typemap entry C<T_PTROBJ_SPECIAL> that maps underscores to
+double-colons (::), and declare C<Net_Config> to be of that type:
+
+
+        TYPEMAP
+        Net_Config      T_PTROBJ_SPECIAL
+
+        INPUT
+        T_PTROBJ_SPECIAL
+                if (sv_derived_from($arg, \"${(my $ntt=$ntype)=~s/_/::/g;\$ntt}\")) {
+                        IV tmp = SvIV((SV*)SvRV($arg));
+                $var = ($type) tmp;
+                }
+                else
+                        croak(\"$var is not of type ${(my $ntt=$ntype)=~s/_/::/g;\$ntt}\")
+
+        OUTPUT
+        T_PTROBJ_SPECIAL
+                sv_setref_pv($arg, \"${(my $ntt=$ntype)=~s/_/::/g;\$ntt}\",
+                (void*)$var);
+
+The INPUT and OUTPUT sections substitute underscores for double-colons
+on the fly, giving the desired effect.  This example demonstrates some
+of the power and versatility of the typemap facility.
+
 =head1 EXAMPLES
 
 File C<RPC.xs>: Interface to some ONC+ RPC bind library functions.
@@ -924,16 +1287,15 @@ File C<RPC.xs>: Interface to some ONC+ RPC bind library functions.
 
      MODULE = RPC  PACKAGE = RPC
 
-     void
+     SV *
      rpcb_gettime(host="localhost")
           char *host
-          CODE:
-          {
+         PREINIT:
           time_t  timep;
+          CODE:
           ST(0) = sv_newmortal();
           if( rpcb_gettime( host, &timep ) )
                sv_setnv( ST(0), (double)timep );
-          }
 
      Netconfig *
      getnetconfigent(netid="udp")
@@ -980,7 +1342,11 @@ File C<rpctest.pl>: Perl test program for the RPC extension.
      print "netconf = $netconf\n";
 
 
+=head1 XS VERSION
+
+This document covers features supported by C<xsubpp> 1.935.
+
 =head1 AUTHOR
 
-Dean Roehrich F<E<lt>roehrich@cray.comE<gt>>
-Oct 12, 1995
+Dean Roehrich <F<roehrich@cray.com>>
+Jul 8, 1996