Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

query for methods?

by ph0enix (Friar)
on Jul 03, 2002 at 09:10 UTC ( [id://179101]=perlquestion: print w/replies, xml ) Need Help??

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

Hi all

Is there way to determine method/function names for loaded module?

Replies are listed 'Best First'.
Re: query for methods?
by broquaint (Abbot) on Jul 03, 2002 at 09:20 UTC
    Is there way to determine method/function names for loaded module?
    Yes there is and it's the can method which is attached to every object. Here's a short example of how to use it
    package Foo; sub new { bless {}, shift } sub foo { print "i'm a method of Foo\n" } package main; my $o = Foo->new(); print "foo exists\n" if $o->can('foo'); print "bar does not exist\n" unless $o->can('bar'); __output__ foo exists bar does not exist
    For more info on can check out the special UNIVERSAL package.
    HTH

    _________
    broquaint

      This way can be used when I have some idea about function name:) I want to obtain list of all functions - don't know function names...

        Then just dump the symbol table of your given package either by hand or using Juerd's Devel::GetSymbols. If you want to roll you own something like this ought do the trick
        package Foo; our $VERSION = 0.1; sub new { bless {}, shift } sub foo { print "i'm a method of Foo\n" } package main; for my $sym (keys %Foo::) { print "method - $sym\n" if *{"Foo::$sym"}{CODE}; } __output__ method - foo method - new

        HTH

        _________
        broquaint

Re: query for methods?
by ariels (Curate) on Jul 03, 2002 at 09:26 UTC
    The debugger command `m' shows "methods" defined for a package, so you certainly can do it.

    How? Look for functions referenced in the %{PACKAGE_NAME::} hash. Here's an example...

    #!/usr/local/bin/perl -w # Find all functions in specified package my $pkg = shift; eval "use $pkg; 1" or die "Failed to load $pkg: $@\n"; # print functions while (my ($key, $val) = each %{"${pkg}::"}) { print "$key\n" if defined *{$val}{CODE}; }
    Run giving a package name as argument.

    Note that we're using some symrefs here (to access the %{"${pkg}::"} hash), so as least that line should be free of strictness.

Re: query for methods?
by davorg (Chancellor) on Jul 03, 2002 at 09:15 UTC

    Sure, read the documentation (or the source code).

    You can probably get a good first approximation by looking at the package's symbol table (using something like Devel::Symdump perhaps, but in general it's very difficult to do it accurately because things like inheritance and AUTOLOAD.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      Yes, of course - read documentation. My question was slightly bad.

      I need it for debuging purposes. My module use AUTOLOAD to generate new methods and I want to query for names of generated (and not only) methods.

Re: query for methods?
by rjray (Chaplain) on Jul 03, 2002 at 10:23 UTC

    Here's a little routine I whipped up that should handle the @ISA path for a class. Mind, it still won't tell you about tricky additions to the method space such as auto-loaded methods that aren't pre-declared with "use vars", for example. In other words, you can fool it pretty easily but if you're using the code in a fairly plain environment, it should at least be helpful.

    sub find_methods { my $pack = shift; my $tbl = shift || {}; no strict 'refs'; my ($key, $val); while (($key, $val) = each %{"${pack}::"}) { $tbl->{$key}++ if defined *{$val}{CODE}; } if (@{"${pack}::ISA"}) { find_methods($_, $tbl) for (@{"${pack}::ISA"}); } sort keys %$tbl; }

    It assumes that the code behind the specified class has already been loaded. You call it as:

    @methods = find_methods $classname;

    You won't get duplicate names in the cases where a sub-class overrides a method previously defined in a parent. Recursion is your friend.

    --rjray

Re: query for methods?
by dragonchild (Archbishop) on Jul 03, 2002 at 14:19 UTC
    I'm more concerned as to why you think you need to. If you wrote the module, you should be able to go through the various execution paths and determine what should be out there. Then, use UNIVERSAL::can to test your theories. If you're right on all counts, then you did it right.

    Now, if you cannot (or will not) do an execution trace by hand (or in your head), then that's another problem altogether.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://179101]
Approved by wil
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (2)
As of 2024-04-20 05:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found