BuddyJoe BuddyJoe - 5 months ago 31
Perl Question

How do I loop over all the methods of a class in Perl?

How do you loop over all the methods of a class in Perl? Are there any good online references to Perl introspection or reflection?


The recommendation Todd Gardner gave to use Moose is a good one, but the example code he chose isn't very helpful.

If you're inspecting a non-Moose using class, you'd do something like this:

use Some::Class;
use Class::MOP;

my $meta = Class::MOP::Class->initialize('Some::Class');

for my $meth ( $meta->get_all_methods ) {
    print $meth->fully_qualified_name, "\n":

See the Class::MOP::Class docs for more details on how to do introspection.

You'll also note that I used Class::MOP instead of Moose. Class::MOP (MOP = Meta-Object Protocol) is the base on which Moose builds. If you're working with non-Moose classes, using Moose to introspect doesn't gain you anything.

If you wanted, you could use Moose () and Moose::Meta::Class->initialize instead of CMOP.