Website hosting service by Active-Venture.com
  

 Back to Index

The Anatomy of an XSUB

The simplest XSUBs consist of 3 parts: a description of the return value, the name of the XSUB routine and the names of its arguments, and a description of types or formats of the arguments.

The following XSUB allows a Perl program to access a C library function called sin(). The XSUB will imitate the C function which takes a single argument and returns a single value.

 
     double
     sin(x)
       double x  

Optionally, one can merge the description of types and the list of argument names, rewriting this as

 
     double
     sin(double x)  

This makes this XSUB look similar to an ANSI C declaration. An optional semicolon is allowed after the argument list, as in

 
     double
     sin(double x);  

Parameters with C pointer types can have different semantic: C functions with similar declarations

 
     bool string_looks_as_a_number(char *s);
     bool make_char_uppercase(char *c);  

are used in absolutely incompatible manner. Parameters to these functions could be described xsubpp like this:

 
     char *  s
     char    &c  

Both these XS declarations correspond to the char* C type, but they have different semantics, see "The & Unary Operator".

It is convenient to think that the indirection operator * should be considered as a part of the type and the address operator & should be considered part of the variable. See "The Typemap" for more info about handling qualifiers and unary operators in C types.

The function name and the return type must be placed on separate lines and should be flush left-adjusted.

 
  INCORRECT                        CORRECT

  double sin(x)                    double
    double x                       sin(x)
				     double x  

The rest of the function description 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 for better readability.

 
  CORRECT

  double
  sin(x)
  double x  

More complicated XSUBs may contain many other sections. Each section of an XSUB starts with the corresponding keyword, such as INIT: or CLEANUP:. However, the first two lines of an XSUB always contain the same data: descriptions of the return type and the names of the function and its parameters. Whatever immediately follows these is considered to be an INPUT: section unless explicitly marked with another keyword. (See The INPUT: Keyword.)

An XSUB section continues until another section-start keyword is found.

 

  

 

Domain name registration - 
Register cheap domain name from $7.95 and enjoy free domain services 
 

Cheap domain name search service -
Domain name services at just
$8.95/year only
 

Register domain name -
Buy domain name registration and cheap domain transfer at low, affordable price.

© 2002-2004 Active-Venture.com Web Site Hosting Service

 

[ Any significant boost in technology could just as easily be a rigged demo   ]

 

 
 
 

Disclaimer: This documentation is provided only for the benefits of our web hosting customers.
For authoritative source of the documentation, please refer to http://www.perldoc.com