Hi,
Always fun to try to come up with an XSub that does the job. Assuming you're prepared to speculate on what the name is (as in your example), the following works fine for me on perl 5.10:
use warnings;
use strict;
use Inline C => <<'EOC';
SV * test (CV * x, char * name) {
if(x == get_cv(name, 0)) return newSVuv(1);
return newSVuv(0);
}
EOC
package Foo;
sub bar {};
package main;
my $coderef = \&Foo::bar;
my $null;
print test($coderef, "Foo::bar"), "\n"; # 1
print test($coderef, "Foo::Bar"), "\n"; # 0
print test($null, "Foo::Bar"), "\n"; # dies appropriately
I think (untested) that for perl 5.8 you would need to rewrite the XSub as:
SV * test (SV * x, char * name) {
if((CV*)SvRV(x) == get_cv(name, 0)) return newSVuv(1);
return newSVuv(0);
}
I don't know about the wisdom of using that approach - and there's quite possibly a better solution anyway.
It should also be possible to construct an XSub that actually returns the "package::method" name. Update: For the package name alone:
SV * foo1(SV * x) {
return newSVpv(HvNAME(CvSTASH((CV*)SvRV(x))), 0);
}
But first make sure it *is* a code reference that you're passing. Better still, just use B; as betterworld suggests.
Cheers, Rob |