in reply to detecting improperly called subroutines

I think i may have read this question in a more generalized way then the orriginal poster was looking for, I started thinking:

How can you confirm that an object impliments a given method?

Which seems really usefull to me in a language without strict typing, or interfaces. Developers can provide APIs in which they say "you can pass me ny object of any class, as long as it impliments the following methods..." and the API can do "friendly" sanity checking of the objects to make sure they impliment all the neccessary methods prior to doing any processing, and generate a "friendly" error and return -- instead of just letting the Interpreter spit out an error msg and keep on processing.

But i honestly can't think of anyway straightforward way to do a check like this in perl. Looking in the Symbol table seems like a good approach but it doesn't take into account subclassing, so we'd need to "walk the symbol table" .. but i'm affraid my typeglob / symbol table knowledge doesn't extend to dealing with superclass relationships.

So i propose to extend the orriginal question be extended. Given a package name (as returned by the ref method) what's the best way to determing if "foo" is a valid method name for that package ?

  • Comment on Re: detecting improperly called subroutines

Replies are listed 'Best First'.
Re: Re: detecting improperly called subroutines
by Ovid (Cardinal) on Apr 16, 2002 at 00:37 UTC
    my $object = Foo->new; if ( $object->can( $some_method ) ) { # it's good } else { # it's bad }

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.