| 1 | package MOP4Import::Util::FindMethods; | ||
| 2 | use strict; | ||
| 3 | use warnings qw(FATAL all NONFATAL misc); | ||
| 4 | |||
| 5 | use Exporter qw(import); | ||
| 6 | our @EXPORT = qw(FindMethods); | ||
| 7 | |||
| 8 | use MOP4Import::Util qw(symtab); | ||
| 9 | |||
| 10 | # XXX: rewrite this with mro::get_linear_isa; | ||
| 11 | sub FindMethods { | ||
| 12 | # depth first, pre-order search of 'sub'. | ||
| 13 | # In the real sense, this should be called 'Findsubs'. | ||
| 14 | my ($obj, $pattern, $visited, $found) = @_; | ||
| 15 | $visited ||= {}; | ||
| 16 | $found ||= {}; | ||
| 17 | my $class = ref($obj) ? ref($obj) : $obj; | ||
| 18 | $visited->{$class} = 1; | ||
| 19 | my $symtab = symtab($class); | ||
| 20 | local $_; | ||
| 21 | foreach my $orig (keys %$symtab) { | ||
| 22 | $_ = $orig; | ||
| 23 | if ($pattern) { | ||
| 24 | if (ref $pattern eq 'CODE') { | ||
| 25 | $pattern->($_) or next; | ||
| 26 | } else { | ||
| 27 | $_ =~ $pattern or next; | ||
| 28 | } | ||
| 29 | } | ||
| 30 | my $globOrCode = $symtab->{$orig}; | ||
| 31 | if (ref $globOrCode eq 'CODE') { | ||
| 32 | ; #ok | ||
| 33 | } elsif (ref \$globOrCode eq 'GLOB' and *{$globOrCode}{CODE}) { | ||
| 34 | ; #ok | ||
| 35 | } else { | ||
| 36 | next; | ||
| 37 | } | ||
| 38 | $found->{$_} //= $class; | ||
| 39 | } | ||
| 40 | |||
| 41 | my $isa = $symtab->{ISA}; | ||
| 42 | if (defined $isa and *{$isa}{ARRAY}) { | ||
| 43 | foreach my $super (@{*{$isa}{ARRAY}}) { | ||
| 44 | FindMethods($super, $pattern, $visited, $found) | ||
| 45 | unless $visited->{$super}; | ||
| 46 | } | ||
| 47 | } | ||
| 48 | |||
| 49 | if (wantarray) { | ||
| 50 | sort keys %$found | ||
| 51 | } else { | ||
| 52 | $found; | ||
| 53 | } | ||
| 54 | } | ||
| 55 | |||
| 56 | 1; |