I haven't done anything with Moose, but I suppose that using Module::Pluggable to find all candidates and then using some part of the Moose API to inspect $package->metaclass is the approach I'd use. Actually, looking at Moose::Meta::Class, the following should tell you whether a class does a role:

package App; use Module::Pluggable sub_name => 'plugins'; my @candidates = App->plugins; for my $class (@candidates) { if ($class->meta->does_role( $role ) { print "$class does $role\n"; }; };

Of course, with this unchecked approach, you force all plugins into the Moose tarpit, which imposes a burden+learning curve on plugin authors. You might want to be more defensive, by wrapping $class->meta in an eval block or by providing a base class for plugins which sets up enough routines for a bare-bones plugin that does no harm.

Update: Upon rereading, I see that you only want to look at the classes that have been loaded - then, I'd stay with the defensive approach outlined above, but fill the @candidates from %INC instead via Module::Pluggable, if you want to avoid setting up Module::Pluggable with the correct search paths or want to avoid (re)loading failing modules.


In reply to Re: find all modules that "do" a role by Corion
in thread find all modules that "do" a role by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.