use strict; use warnings; use lib "."; use Bah; use Data::Dumper; my $bah = Bah->new(); print $bah->toString() . "\n"; $bah->printBah(); # Create anonymous subroutine for generating all methods my $p_get_methods = find_methods(); my $presults = $p_get_methods->($bah); printf "Modules = %s\n\n", Dumper($presults); # # find_methods() # 061226 by liverpole # Based on 'methods_via()' from the Perl debugger 'perl5db.pl'. # # Takes 1 argument, a classname (eg. "Bah") or a blessed object (eg. $bah), # and returns a hash containing all methods for the given class and any # classes from which it is inherited. For example: # # { # 'Bah' => [ # 'new', # 'printBah' # ], # 'Object' => [ # 'new', # 'test_method', # 'toString' # ] # }; # # sub find_methods { my %seen; my %methods; my $psub = sub { my $class = shift; # Fix the class name (eg. "Bah=HASH(0x192a324)" => "Bah") $class =~ s/=.*//; # If we've processed this class already, just quit. if ($seen{$class}++) { return \%methods; } no strict; # Extract from all the symbols in this class. my @syms = sort keys %{"${class}::"}; # Get entire list of defined subroutines in this class. my @subs = grep { defined &{ ${"${class}::"}{$_} } } @syms; for my $subname (@subs) { # If we printed this already, skip it. next if $seen{$subname}++; # Save the new method name. local $\ = ''; local $, = ''; $methods{$class} ||= [ ]; push @{$methods{$class}}, $subname; } # Keep going up the tree. # Find all the classes this one is a subclass of. # my @super = @{"${class}::ISA"}; for $name (@super) { # Crawl up the tree and keep trying to crawl up. # Then dump the results into the %methods hash # my $pnewsub = find_methods(); my $pmethods = $pnewsub->($name); foreach my $val (values %$pmethods) { $methods{$name} ||= [ ]; # Fixed original line, which created a double # array reference: # push @{$methods{$name}}, $val; map { push @{$methods{$name}}, $_ } @$val; } } return \%methods; }; return $psub; }