Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Determining what methods are available to a class

by weierophinney (Pilgrim)
on Aug 09, 2003 at 14:17 UTC ( #282412=perlquestion: print w/replies, xml ) Need Help??

weierophinney has asked for the wisdom of the Perl Monks concerning the following question:

I have a class that contains a number of 'build_*' methods. It is written to be subclassed, so that it can be easily extended. Most extension would be to create new 'build_' methods.

One routine I have written calls the various 'build_' methods based on XML elements. I would like to test and make sure that the method is available before calling it.

I've tried Module::Info, but this actually parses the given module file looking for methods; if the modules has been subclassed, it won't find the new methods. My other inclination is to simply wrap the method call in an eval statement.

So my question is: Is there an easy way of finding out what methods are available to an object? Or is perl way of doing it to test for exceptions?

  • Comment on Determining what methods are available to a class

Replies are listed 'Best First'.
Re: Determining what methods are available to a class
by The Mad Hatter (Priest) on Aug 09, 2003 at 14:30 UTC
    Use the can method (provided by the UNIVERSAL base class)...
    my $method = "build_a_kite"; if ($obj->can($method)) { print "We can!"; } else { print "We can't..."; }
    From perldoc UNIVERSAL...
    $obj->can( METHOD ), CLASS->can( METHOD ), can( VAL, METHOD ) can checks if the object or class has a method called METHOD. If it does then a reference to the sub is returned. If it does not then undef is returned. This includes methods inherited or imported by $obj, CLASS, or VAL. can cannot know whether an object will be able to provide a method through AUTOLOAD, so a return value of undef does not necessarily mean the object will not be able to handle the method call. To get around this some module authors use a forward declaration (see perlsub) for methods they will handle via AUTOLOAD. For such 'dummy' subs, can will still return a code reference, which, when called, will fall through to the AUTOLOAD. If no suitable AUTOLOAD is provided, calling the coderef will cause an error. can can be called as a class (static) method, an object method, or a function. When used as a function, if VAL is a blessed reference or package name which has a method called METHOD, can returns a reference to the subroutine. If VAL is not a blessed reference, or if it does not have a method METHOD, undef is returned.
      Thanks -- that's exactly what I was looking for. Need to read that perldoc a little more closely now!
Re: Determining what methods are available to a class
by larsen (Parson) on Aug 09, 2003 at 14:50 UTC
    Another way is to look into the package symbol table. For example:
    use strict; use warnings; use Devel::Symdump; use CGI; # for our example we will look into the CGI package my $obj = Devel::Symdump->rnew( 'CGI' ); my @f = $obj->functions; print join "\n", @f;
    And here part of the output:

      Unfortunately, that doesn't account for inherited methods or AUTOLOADed methods. (Remember to write your own can() when you AUTOLOAD() accessors and such.)

Re: Determining what methods are available to a class (-d/m)
by tye (Sage) on Aug 09, 2003 at 22:54 UTC
    > perl -de 0 DB<1> h m m expr Evals expression in array context, prints methods callable on the first element of the result. m class Prints methods callable via the given class. DB<2>

    So something like:

    > perl -MMy::Module -de 0 DB<1> m My::Module

                    - tye
Re: Determining what methods are available to a class
by adamk (Chaplain) on Aug 10, 2003 at 02:24 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://282412]
Approved by Courage
Front-paged by tye
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (8)
As of 2020-11-26 10:10 GMT
Find Nodes?
    Voting Booth?

    No recent polls found