10 our $VERSION = '0.01';
11 our $AUTHORITY = 'cpan:STEVAN';
19 Sub::Exporter::setup_exporter({
21 groups => { all => \@exports }
24 ## some utils for the utils ...
28 return Class::MOP::get_metaclass_by_name(ref($_[0]) || $_[0]);
34 my ($class_or_obj, $role) = @_;
36 my $meta = find_meta($class_or_obj);
38 return unless defined $meta;
40 return 1 if $meta->does_role($role);
44 sub search_class_by_role {
45 my ($class_or_obj, $role_name) = @_;
47 my $meta = find_meta($class_or_obj);
49 return unless defined $meta;
51 foreach my $class ($meta->class_precedence_list) {
53 my $_meta = find_meta($class);
55 next unless defined $_meta;
57 foreach my $role (@{ $_meta->roles || [] }) {
58 return $class if $role->name eq $role_name;
73 Moose::Util - Utilities for working with Moose classes
77 use Moose::Util qw/find_meta does_role search_class_by_role/;
79 my $meta = find_meta($object) || die "No metaclass found";
81 if (does_role($object, $role)) {
82 print "The object can do $role!\n";
85 my $class = search_class_by_role($object, 'FooRole');
86 print "Nearest class with 'FooRole' is $class\n";
90 This is a set of utility functions to help working with Moose classes. This
91 is an experimental module, and it's not 100% clear what purpose it will serve.
92 That said, ideas, suggestions and contributions to this collection are most
93 welcome. See the L<TODO> section below for a list of ideas for possible
96 =head1 EXPORTED FUNCTIONS
100 =item B<find_meta ($class_or_obj)>
102 This will attempt to locate a metaclass for the given C<$class_or_obj>
105 =item B<does_role ($class_or_obj, $role_name)>
107 Returns true if C<$class_or_obj> can do the role C<$role_name>.
109 =item B<search_class_by_role ($class_or_obj, $role_name)>
111 Returns first class in precedence list that consumed C<$role_name>.
117 Here is a list of possible functions to write
121 =item discovering original method from modified method
123 =item search for origin class of a method or attribute
129 All complex software has bugs lurking in it, and this module is no
130 exception. If you find a bug please either email me, or add the bug
135 Anders Nor Berle E<lt>debolaz@gmail.comE<gt>
137 B<with contributions from:>
139 Robert (phaylon) Sedlacek
143 =head1 COPYRIGHT AND LICENSE
145 Copyright 2007-2008 by Infinity Interactive, Inc.
147 L<http://www.iinteractive.com>
149 This library is free software; you can redistribute it and/or modify
150 it under the same terms as Perl itself.