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

Hi, all.

Example:
I have a module:
$!/usr/bin/perl -w package library; #use block use strict; use ... #global lib variables my $dbh; ... sub new { my $self = {}; shift; bless($self); return $self; }; sub method1 { ... }; sub method2 { ... }; ... 1;

Now I need when I make somewhere:
use library; my $object=new library($kernel); #$kernel is my parent object

Question:
How can I get list of methods names of object $object ?

Thank's for answers.
rumos.

Replies are listed 'Best First'.
Re: How to get list of methods inside object
by jeffa (Bishop) on Oct 11, 2005 at 13:59 UTC

    You can do this with Devel::Symdump:

    #!/usr/bin/perl -l use strict; use warnings; use Devel::Symdump; my $dump = Devel::Symdump->new('Foo'); print for $dump->functions; package Foo; sub new { bless {}, shift } sub foo {} sub bar {} sub baz {} sub qux {}

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      caveats: That will list all functions in the package, not just methods, but Perl can't tell the difference. Also, it won't list inherited methods unless the package overrides them.
        Two more things:
        • Package-table checks have no chance with AUTOLOAD
        • Walking the @ISA tree is tenuous at best, due to putting subrefs into @ISA.

        Update: Oops! I wasn't supposed to tell you - that was your Christmas gift. :-(


        My criteria for good software:
        1. Does it work?
        2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
      He'll probably want rnew('Foo'), which uses recursion (which I assume means it would check Foo's ISA tree).

      Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
      How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
Re: How to get list of methods inside object
by renodino (Curate) on Oct 11, 2005 at 21:23 UTC
    Here's what I'm doing in (hopefully soon-to-be-CPAN'd) Thread::Apartment, using Class::ISA and Class::Inspector:

    use Class::ISA; use Class::Inspector; sub introspect { my $obj = shift; my $base = ref $obj; my @isa = Class::ISA::self_and_super_path($base); my %method_hash = (); # # get simple methods names first # my $methods = Class::Inspector->methods($base, 'public'); # # then get fully qualified ones (ignoring our local methods) # foreach my $class (@isa) { next if ($class eq $base); $methods = Class::Inspector->methods($class, 'public'); $method_hash{$class . '::' . $_} = 0 foreach (@$methods); } return (\@isa, \%method_hash); }
    (That's actually a pared down version, but has the relevant bits for your question)
Re: How to get list of methods inside object
by zentara (Cardinal) on Oct 12, 2005 at 10:42 UTC
    #!/usr/bin/perl use strict; use warnings; #by Abigail of perlmonks # example usage: $0 IO::File my $class = shift or die "need a class\n"; eval "use $class"; die $@ if $@; # First, find all the classes, and the ones it inherits. my %classes = ($class => 1); my @classes = ($class); while (@classes) { my $class = pop @classes; no strict 'refs'; foreach my $class (@{"$class\::ISA"}) { next if $classes {$class} ++; push @classes => $class; } } # Then find the subs in those classes. while (my $class = each %classes) { no strict 'refs'; print "From class '$class':\n"; while (my $entry = each %{"$class\::"}) { print "\t$entry\n" if defined &{"$class\::$entry"}; } } __END__

    I'm not really a human, but I play one on earth. flash japh