|
C function parameters are normally initialized with their values from the argument stack
(which in turn contains the parameters that were passed to the XSUB from Perl). The typemaps
contain the code segments which are used to translate the Perl values to the C parameters. The
programmer, however, is allowed to override the typemaps and supply alternate (or additional)
initialization code. Initialization code starts with the first =, ;
or + on a line in the INPUT: section. The only exception happens if this ;
terminates the line, then this ; is quietly ignored.
The following code demonstrates how to supply initialization code for 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 $, @,
or \\] 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($arg,PL_na);
time_t &timep = 0;
OUTPUT:
timep
|
|
This should not be used to supply default values for parameters. One 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 =, then it is output in the declaration for
the input variable, replacing the initialization supplied by the typemap. If the
initialization begins with ; or +, then it is performed after all of
the input variables have been declared. In the ; case the initialization normally
supplied by the typemap is not performed. For the + case, the declaration for the
variable will include the initialization from the typemap. A global variable, %v,
is available for the truly rare case where information from one initialization is needed in
another initialization.
Here's a truly obscure example:
bool_t
rpcb_gettime(host,timep)
time_t &timep ; /* \$v{timep}=@{[$v{timep}=$arg]} */
char *host + SvOK($v{timep}) ? SvPV($arg,PL_na) : NULL;
OUTPUT:
timep
|
|
The construct \$v{timep}=@{[$v{timep}=$arg]} used in the above example has a
two-fold purpose: first, when this line is processed by xsubpp, the Perl snippet $v{timep}=$arg
is evaluated. Second, the text of the evaluated snippet is output into the generated C file
(inside a C comment)! During the processing of char *host line, $arg will
evaluate to ST(0), and $v{timep} will evaluate to ST(1).
|
|