Writing C Modules with XS
you'll receive this compilation error:
Error: const gchar * not in typemap in MIME.xs, line 10
Error: const char * not in typemap in MIME.xs, line 10
To use types that XS doesn't natively support, you need to create a new file
called typemap in your module directory that contains code to translate to and from
the new types. In this case, only two lines are required:
const char * T_PV
const gchar * T_PV
This tells XS that const char * and const gchar * are both to be treated as T_PV,
which is the supplied typemap for char *. The T_PV typemap is defined in the system
wide typemap file installed with Perl. You can find it in under the module directory
for the ExtUtils modules in your Perl library. I'll explore typemaps in more detail
later in this chapter.
The preceding code still doesn't work, though; it produces a syntax error
because it doesn't recognize Gnome's gchar type. To fix this problem, I need to add
an #include line below the three #includes already in the file:
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include
This is part of the section before the MODULE command that is passed through to the
generated MIME.c file verbatim.
Modifying the XSUB Name with PREFIX
Calling gnome_mime_type() from Perl with the XSUB shown earlier is done using a
line that looks like this one:
my $type = Gnome::MIME::gnome_mime_type($filename);
This works just fine, but it's awfully verbose; the user is forced to type the
words Gnome and MIME twice. One solution would be to export the function
as is, but XS offers a simpler solution. By modifying the MODULE line, shown here:
MODULE = Gnome::MIME PACKAGE = Gnome::MIME
21
217
7
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved