You can use Class::MOP or it's offspring Moose to do this.

First the Class::MOP version ...

package TestPackage; use metaclass; # this sets up your class to use Class::MOP use Data::Dump qw( dump ); my $obj = bless {}, __PACKAGE__; print 'Can dump()' if $obj->meta->has_method('dump');
This will not print "Can dump()" since Class::MOP knows that the method is not actually from your package, but has been imported from another package. To find the "interface" with Class::MOP, you have a few choices. First you can get all the locally defined methods* like this:
my @methods = $obj->meta->get_method_list;
This will return a list of method names suitable for doing things like $obj->meta->has_method($method_name) and $obj->meta->get_method($method_name) and other such introspection and reflection. Or, your second choice is to get all the methods this $obj supports, including inherited methods. In this case you do the following:
my @methods = $obj->meta->compute_all_applicable_methods;
The items in @methods this time are slightly different, you get a HASH reference which contains the method name (in the name key), the name of the class in which the method lives (in the class key) and a CODE reference for the actual method (in the code key).

Now, you could also use Moose and Moose::Role to do similar things. Moose is basically syntactic sugar over the mechanisms that Class::MOP provides, so all the above examples apply here as well. With roles** and Moose::Role you can do the interface checking you are talking about. Here is an example:

package MyFooInterface; use Moose::Role; requires 'bar';
Then you apply the role to your class, which checks the role "interface" at compile time.
package MyFoo; use Moose; with 'MyFooInterface'; sub bar { ... } # the bar method is required, the # class will fail to compile if it is # not present
And you can also do your checking at runtime ...
my $foo = MyFoo->new; if ($foo->does('MyFooInterface')) { ... }
For more information, see the CPAN pages for Moose or Class::MOP, or for a quicker overview you can check out one of these talks recently given on the subject: And lastly, you can check out the two part article on Moose written by our very own merlyn.

Footnotes:
* Locally defined methods are those defined directly in your class, and not inherited.
** If you don't already know, a role is not unlike a mixin or the traits mentioned above, but with a little bit more of a Perl-ish twist. They are a core part of Perl 6 in fact.

-stvn

In reply to Re: Finding the interface implemented by an object by stvn
in thread Finding the interface implemented by an object by oyse

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.