Writing C Modules with XS
  # make sure filename is specified
  croak("Missing required parameter filename.") unless exists $args{filename};
  # call appropriate back end function
  if ($args{read_file} and $args{default_type}) {
    return Gnome::MIME::Backend::type_or_default_of_file($args{filename},
                                                 $args{default_type});
  } elsif ($args{read_file}) {
    return Gnome::MIME::Backend::type_of_file($args{filename});
  } elsif ($args{default_type}) {
    return Gnome::MIME::Backend::type_or_default($args{filename},
                                                 $args{default_type});
  } else {
    return Gnome::MIME::Backend::type($args{filename});
  }
}
This code essentially does the same things as the XS code in the previous section, 
but instead of being in C, it's in good old maintainable, forgiving Perl. As an added 
bonus, instead of translating the calls to croak() in the XS version into die() calls, 
I added a use Carp and used Carp's version of croak(), which will yield much better 
error messages than die() or its C equivalent.
This pattern, a thin layer of XS with a Perl front end, is worthy of emulation. It 
provides a way to write Perl ish interfaces in Perl and get the most out of XS at the 
same time.
Providing Access to Complex Data Structures
Many C interfaces are more complicated than the gnome_mime_type functions 
they manipulate complex data structures, often using C structs and arrays. Pro 
viding a convenient interface to Perl programmers is often a matter of translating 
data from C structs into Perl arrays and hashes, and back again.
It just so happens that the Gnome::MIME module has need of this functionality. 
The Gnome MIME API supplies two functions that access a set of key/value pairs 
associated with each MIME type, gnome_mime_get_keys() and gnome_mime_get_value().
There are keys for the program the user has chosen to open the MIME type (that is, 
an image viewer for image/gif, a text editor for text/plain, and so on) as well as 
other types of metadata.
It would, of course, be possible to provide a Perl interface directly to these 
calls. For example, to print out the available key/value pairs for image/gif, you 
could do the following:
22
225
5






footer




 

 

 

 

 Home | About Us | Network | Services | Support | FAQ | Control Panel | Order Online | Sitemap | Contact

web hosting perl

 

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