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.

David Farrell
  • 427
  • 6
  • 16

1 Answers1

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.

nwellnhof
  • 32,319
  • 7
  • 89
  • 113