Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do XS subs use const char *?

Tags:

perl

xs

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.

like image 819
David Farrell Avatar asked Jun 01 '16 23:06

David Farrell


1 Answers

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.

like image 134
nwellnhof Avatar answered Nov 07 '22 17:11

nwellnhof