Wednesday, June 17, 2009

perl5 introspection (thanks Moose!)

Many dynamic languages have a way to get a list of all methods for an object built into them.

I don't think perl5 does. But Moose adds it. :)
$ cat j.pl
package X;
use Moose;
sub dosomething1 {}
sub dosomething2 {}
sub dosomething3 {}

package main;
my $x = X->new();
print join ", ", sort $x->meta->get_all_method_names;
print "\n";

$ perl j.pl
BUILDALL, BUILDARGS, DEMOLISHALL, DESTROY, DOES, does,
dosomething1, dosomething2, dosomething3, dump, meta, new
Neato. :)

More info on Moose: http://moose.perl.org