A lot of Perl XS code uses const char *
as the return value of an XS sub but never just char *
. For example:
const char *
version(...)
CODE:
RETVAL = chromaprint_get_version();
OUTPUT: RETVAL
code from xs-fun
Can someone explain why const
is preferred? In my testing, the returned scalar is modifiable whether const
is used or not.
It's only for clarity. The chromaprint_get_version
function returns a const char *
, so the XSUB should be defined with a const char *
return type as well. If you have a look at the built-in typemap, it doesn't make a difference whether you use const char *
, char *
, or even unsigned char *
. They all use the T_PV
typemap. In all cases, the XSUB will return an SV containing a copy of the C string, which is always modifiable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With