Writing C Modules with XS
Supporting Named Parameters
In Perl, a better interface to the preceding MIME type functions might be this one:
$type = Gnome::MIME::file_type(filename => $filename,
default_type => "text/html",
read_file => 1);
This follows the named parameter function style introduced in Chapter 2.
Using XS
Unfortunately XS doesn't have native support for this highly Perl ish function style,
but it's not hard to support it with a little code (see Listing 9 6 for the full XSUB).
Listing 9 6. Named Parameter XSUB
const char *
file_type(...)
PREINIT:
char *filename = NULL; // variables for named params
char *default_type = NULL;
IV read_file = 0;
IV x; // loop counter
CODE:
// check that there are an even number of args
if (items % 2) croak("Usage: Gnome::MIME::file_type(k => v, ...)");
// loop over args by pairs and fill in parameters
for(x = 0; x < items; x+=2) {
char *key = SvPV(ST(x), PL_na);
if (strEQ(key, "filename")) {
filename = SvPV(ST(x+1), PL_na);
} else if (strEQ(key, "default_type")) {
default_type = SvPV(ST(x+1), PL_na);
} else if (strEQ(key, "read_file")) {
read_file = SvIV(ST(x+1));
} else {
croak("Unknown key found in Gnome::MIME::file_type parameter list: %s",
SvPV(ST(x), PL_na));
}
}
22
221
1
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