XS bits: Which class is this object blessed into?

So it turns out I'm not really good at this blogging thing. It's not that I feel I have nothing worth writing about, but that my time is usually better spent on other things. Instead of pretending to myself that I will eventually write long rants (and then effectively writing nothing), I'll try to post occasional bits of code that I found useful and not particularly obvious. I've been writing a lot of XS lately and the somewhat incomplete or at least inaccessible XS / perl API documentation could do with a little more cookbook style. Furthermore, when writing C/XS code that uses the perl API, I find myself trying to do the same things that are trivial in Perl and taking much, much longer to actually succeed. Thus, I'll start out with a bit of perl API.

Today's Not-So-Frequently-Encountered-XS-Problem:
Which class is this object blessed into?

If you wonder why this isn't a moderately frequent issue, then consider that 90% of the time, you really want to know whether the given object is blessed into a class that is derived from some class. This related issue is simple, using

bool    sv_derived_from(SV* sv, const char* name)
. There is no builtin function/macro to give you the class name itself. You can get it with:

const char* className = HvNAME(SvSTASH(SvRV(thePerlObject)));

This works by derefencing the Perl object using SvRV, then fetching the symbol table hash that the STASH pointer in the inner SV points to (SvSTASH), and then using HvNAME to get the stash's name.

I guess it's simple enough to figure out when you read the code, but it wasn't obvious to me when I wrote it, err, asked better programmers for help with it.

PS: Is there a web site that has the perlapi document in HTML form and HTML anchors for each function/macro to link to?

Update: Thanks to Christians comment, I added the appropriate links to documentation

2 Comments

perldoc.perl.org has anchors for perlapi.pod.

sv_derived_from

--
chansen

Leave a comment

About Steffen Mueller

user-pic I blog about Perl just like everyone else. Cf. http://search.cpan.org/~smueller, http://steffen-mueller.net