Chapter 10
Supporting Named Parameters
One way in which XS modules and Inline::C modules are essentially the same is
that they both benefit from careful interface design. In terms of Gnome::MIME,
this means supporting a named parameter style interface to file_type():
$type = Gnome::MIME::file_type(filename => $filename,
default_type => "text/html",
read_file => 1);
The full inlined function implementing this interface is shown in Listing 10 3.
Listing 10 3. Inline Function with Named Parameter Support
char * file_type (SV *dummy, ...) {
Inline_Stack_Vars; // get access to the Inline stack macros
char *filename = NULL; // variables for named params values
char *default_type = NULL;
IV read_file = 0;
int x; // loop counter
// loop over args by pairs and fill in parameters
for (x = 0; x < Inline_Stack_Items; x+=2) {
char *key = SvPV(Inline_Stack_Item(x), PL_na);
if (strEQ(key, "filename")) {
filename = SvPV(Inline_Stack_Item(x+1), PL_na);
} else if (strEQ(key, "default_type")) {
default_type = SvPV(Inline_Stack_Item(x+1), PL_na);
} else if (strEQ(key, "read_file")) {
read_file = SvIV(Inline_Stack_Item(x+1));
} else {
croak("Unknown key found in Gnome::MIME::file_type parameter list: %s",
key);
}
}
// make sure we have a filename parameter
if (filename == NULL) croak("Missing required parameter filename.");
246
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