Why do XS subs use const char *?
Asked Answered
U

1

8

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.

Upcast answered 1/6, 2016 at 23:29 Comment(0)
M
8

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.

Mural answered 2/6, 2016 at 0:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.