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

In reply to Re: find out method name from the reference by syphilis
in thread find out method name from the reference by perlfan99

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.