How do I list available methods on a given object or package in Perl?
-
Refer to: https://stackoverflow.com/questions/25810035/python-dir-equivalent-in-perl – Tim Potapov Mar 27 '21 at 14:15
5 Answers
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' );

- 7,051
- 4
- 31
- 40
-
2I've finally included inline Class::Inspector in my one-file script, that's actually the simpler solution. Thanks. – Benoît Jun 02 '09 at 07:27
-
2Note that Class::Inspector has limitations. It can see defined methods, but does not handle anything in UNIVERSAL. – brian d foy Jun 21 '09 at 13:21
-
2[perldoc Class::Inspector](http://search.cpan.org/dist/Class-Inspector/lib/Class/Inspector.pm) says: "Returns a reference to an array of the names of all the available methods..." - indeed: `@methods` should be `$methods`. – Peter V. Mørch Dec 03 '15 at 07:32
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';

- 991
- 1
- 12
- 15

- 73,191
- 16
- 130
- 183
-
4
-
Testing this on XML::Simple, Scalar::Util and Exporter shows all methods that are explicitly exported. Recursing down @ISA shouldn't be that hard, though. – Chris Lutz May 26 '09 at 12:54
-
3
-
@Manni , and of course, you'll also have a hard time /using/ autoloaded methods. – Kent Fredric May 26 '09 at 14:53
-
exists instead of defined will show autoloaded methods for which there are forward declarations. – ysth May 26 '09 at 14:57
-
Thanks, that's exactly what i was looking for. Unfortunately, i can't depend on Class::Inspector nor moose because i need my one-file script to run a wide variety of systems (solaris 2.6 -> 10, IRIX, HPUX, Linux), so that builtin introspection is very fine. – Benoît May 27 '09 at 09:34
-
Glad to help. As has been mentioned, it may be prudent for each package to check @Foo::ISA, and then check all those packagaes. You can make a subroutine that you send a package name and it checks that package and all packages in that package's ISA recursively. It shouldn't be too hard. – Chris Lutz May 27 '09 at 10:37
-
Actually, i'd like to write a method in my base package to list all the available method on a given derived object. like A <- B <- C, $a = new $C; $c->available_methods() -> (A::*, B::*, C::*). I think i'll eventually copy/paste Class::Inspector into my 1-file script. – Benoît May 28 '09 at 13:38
-
2`defined &{$_}` should probably be `defined &{"Foo::$_"}` or the check won't work from a **different** package. – Paul Kulchenko Jan 30 '16 at 00:40
-
@briandfoy I don't think [Rose::DB::Object](http://search.cpan.org/~jsiracusa/Rose-DB-Object-0.815/lib/Rose/DB/Object.pm) even existed back when this thread was active. But figuring out all of the methods Rose::DB::Object created from the `make_classes` call is really valuable. – Randall Feb 25 '16 at 17:50
if you have a package that is using Moose its reasonably simple:
print PackageNameHere->meta->dump;
And for more complete data:
use Data::Dumper;
print Dumper( PackageNameHere->meta );
Will get you started. For everything else, theres the methods that appear on ->meta
that are documented in Class::MOP::Class
You can do a bit of AdHoc faking of moose goodness for packages without it with:
use Class::MOP::Class;
my $meta = Class::MOP::Class->initialize( PackageNameHere );
and then proceed to use the Class::MOP methods like you would with Moose.
For starters:
$meta->get_method_map();
use Moose; #, its awesome.

- 56,416
- 14
- 107
- 150
-
1initialize will return the metaclass if it's cached, no need to check for it manually, see for example the implementations for all the sugar in Moose.pm – perigrin May 27 '09 at 02:42
-
1Hmm and with further investigation I appear to be both right and wrong. Class::MOP::class_of() will handles instances as well as Class names, while the function that initialize uses (Class::MOP::get_metaclass_by_name()) *only* handles Class names. – perigrin May 27 '09 at 12:04
-
Kent - this doesn't work for me - maybe something has changed in the Moose world. I have a gist here: https://gist.github.com/rjattrill/6119205 – Ross Attrill Jul 31 '13 at 04:12
In general, you can't do this with a dynamic language like Perl. The package might define some methods that you can find, but it can also make up methods on the fly that don't have definitions until you use them. Additionally, even calling a method (that works) might not define it. That's the sort of things that make dynamic languages nice. :)
What task are you trying to solve?

- 129,424
- 31
- 207
- 592
A good answer from How to get structure and inheritance history
The classes from which an object's class currently inherits can be found using the following:
use mro qw( );
use Scalar::Util qw( blessed );
say join ", ", @{ mro::get_linear_isa(blessed($o)) };

- 50
- 9