Chapter 9
Finally, RETVAL is assigned a reference to the hash to be returned:
// return a reference to the new hash
RETVAL = newRV_noinc((SV *)hv);
You might have expected to see a call to sv_2mortal() at the end, but XSUB
return values of SV* are automatically mortalized by xsubpp. As proof, here's the
relevant slice of the generated MIME.c:
// return a reference to the new hash
RETVAL = newRV_noinc((SV *)hv);
#line 52 "MIME.c"
ST(0) = RETVAL;
sv_2mortal(ST(0));
The generated code places RETVAL into the return stack and is then mortalized.
Since the reference to hv was created with newRV_noinc(), the hv has a refcount of 1
and will live as long RETVAL does. The same is true of the SVs stored in hv they have
a refcount of 1 and will be freed when hv is freed. The end result is a chain of Perl
objects with refcounts of 1 anchored by a single mortal reference. When building
up complex data structures in XS, this is the end result you should be working toward.
Returning Multiple Values in List Context
The new interface for Gnome::MIME::type_data() is a big improvement. Now a user
who wants to open a file can do something like the following:
$type_data = Gnome::MIME::type_data("text/html");
$program = $type_data >{open};
However, inevitably a less experienced user is going to mix up the hash ref
erence with a hash and try something like this:
%type_data = Gnome::MIME::type_data("text/html");
$program = $type_data{open};
This will only work if Gnome::MIME::type_data() is smart enough to return a list of
key/value pairs in list context. Supporting this usage provides a chance to demon
strate a new technique for building XSUBs: using PPCODE to access the return stack
directly. The complete listing for the new XSUB is in Listing 9 8.
228
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