[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Omaha.pm] 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

j



=============================
Here's me failing to read the backlog from 7 hours ago, and lots of guts...

irc.perl.org #moose
00:25 <@Sartak> cpan -i MooseX::AttributeHelpers
00:25 < tome> gotit, thanks
00:28 < tome> Sartak: one thing I noticed with AttributeHelpers is that I tend to mix private and public methods when using it. I have a HashRef attribute and maybe the get method would be public as getConfig but then there are lots of 'private' methods that I use only in that class, I name them starting with _. Is there some pattern where these are separated or done differently that people use?
00:28 <@Sartak> that's the pattern we use
00:29 < tome> now to find what methods a class exports you have to look both at the attributes and the class itself, it can get confusing. 00:29 < tome> but I think the functionality is great, makes things much clearer than accessing the hash itself..
01:00 <@confound> "methods" aren't "exported"
01:30 < tome> confound: What do you mean by that
04:43 < ilmari> to find the public methods of the class, read its documentation 04:43 < ilmari> you don't care if it's implemented directly or delegated to an attribute 07:31 < jhannah> ilmari: Moose has introspection, doesn't it? (tell me all the methods this class has?)
07:33 <@Sartak> Class->meta->get_all_method_names
07:33 <@Penfold> yes
07:34 <@Sartak> but that's not his point
07:34 <@Sartak> his point is don't use what the author doesn't want you yo use 07:41 < jhannah> _foo() is the Moose convention for "private" subs, I assume
07:41 < jhannah> _
07:46 < daxim> that's been the perl convention for a long time before moose 07:46 < jhannah> ya, k. just checking that Moose didn't invent a "cooler" way :)
07:47  * jhannah tries to keep up with the Jones'
07:47 < jhannah> Joneses?
07:47 < jhannah> Jones' Brothers?
08:21 < jhannah> so is Moose's get_all_method_names() implemented in Class::MOP::Class? that's the only hit for that sub when searching search.cpan.org
08:21 <@t0m> yes
08:22 <@rafl> Moose::Meta::Class isa CMOP::Class
08:27 <@perigrin> jhannah: most of what you think of as the metaclass introspection is probably in CMOP 08:27 <@perigrin> Moose really only adds Roles and TypeConstraints and sugar.
08:27 <@perigrin> and there is talk of pushing Roles back into CMOP