P is for Practical | |
PerlMonks |
Finding the interface implemented by an objectby oyse (Monk) |
on Nov 27, 2007 at 21:02 UTC ( [id://653366]=perlquestion: print w/replies, xml ) | Need Help?? |
oyse has asked for the wisdom of the Perl Monks concerning the following question: As part of implementing a module that need to find out at runtime what methods an object supports I tested the following:
This prints "Can dump()" while in reality $obj does not implement dump(). Is there any other way to find out which methods an object implements, i.e. it's interface? Update:As usual I have given too little background to my question. The reason I wanted to discover the interface of an object was that I was trying to implement yet another module for parameter validation for subroutines. Yes I know that there are more than enough of these already, but none of them works exactly I want them to and besides it was a challenge. Anyway, I wanted to be able to specify the type of a parameter, but instead of doing a isa() check on the supplied argument, it should check that it had the right functions. The check it self can be implemented via can(), but the module should be able to be given a package name and then determine what methods and object should be able to do. This is probably best illustrated with an example.
The intention in this example is that the module Signature::Checker in some way adds parameter validation to subroutines that have the attribute Signature attached to them. When the Signature::Checker sees the type A::Type it should try to discover what methods A::Type implements. By using can() it will see the functions a_func(), new(), a_method() and another_method() which was not as intended. As far as I understand Corion's answer this is not possible to implement in a general way. stvn's suggestion would work, but if I understand correctly, restrict the solutuion to only work with classes implemented with Moose. For me that would partly defeat the purpose of the module.
Back to
Seekers of Perl Wisdom
|
|