Have you searched CPAN for modules that walk the symbol table? The following will find all subs currently existing in a package. It can be expanded to walk the entire symbol table. It can be expanded to look at @ISA. There's no way to know whether the subs it finds are methods or not.
use strict;
use warnings;
use Scalar::Util qw( reftype );
sub is_glob {
for (@_ ? $_[0] : $_) {
return reftype(\$_) eq 'GLOB';
}
}
sub get_subs {
my ($pkg) = @_;
my $symtab = \%::;
for (split /::/, $pkg) {
$symtab = $symtab->{"${_}::"}
or return;
}
return grep is_glob($symtab->{$_})
&& *{$symtab->{$_}}{CODE},
keys %$symtab;
}
print "$_\n" for sort +get_subs('Foo::Bar');
Interesting tidbit: 'Foo::Bar' can be written as Foo::Bar::.
What other kind of code (besides string eval) can generate methods on the fly? Can we recognize them in a reasonable way?
Assignment of a sub ref to a glob.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|