Question
How do I list available methods on a given object or package in Perl?
How do I list available methods on a given object or package in Perl?
45 21915
45
Question
How do I list available methods on a given object or package in Perl?
Solution
There are (rather too) many ways to do this in Perl because there are so many ways to do things in Perl. As someone commented, autoloaded methods will always be a bit tricky. However, rather than rolling your own approach I would suggest that you take a look at Class::Inspector on CPAN. That will let you do something like:
my $methods = Class::Inspector->methods( 'Foo::Class', 'full', 'public' );
Solution
If you have a package called Foo
, this should do it:
no strict 'refs';
for(keys %Foo::) { # All the symbols in Foo's symbol table
print "$_\n" if exists &{"Foo::$_"}; # check if symbol is method
}
use strict 'refs';
Alternatively, to get a list of all methods in package Foo
:
no strict 'refs';
my @methods = grep { defined &{"Foo::$_"} } keys %Foo::;
use strict 'refs';