|
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.
Optionally, one can merge the description of types and the list of argument names,
rewriting this as
This makes this XSUB look similar to an ANSI C declaration. An optional semicolon is
allowed after the argument list, as in
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:
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.
|