Note that there are some explanatory texts on larger screens.

plurals
  1. POPerl class attribute composition?
    text
    copied!<p>Suppose I have multiple roles, each one defining a set of items:</p> <pre><code>package A; use Moose::Role; sub items () { qw/apple orange/ } package B; use Moose::Role; with 'A'; sub items () { qw/watermelon/ } package C; use Moose::Role; sub items () { qw/banana/ } </code></pre> <p>Suppose I use them in another class and I want to collect all those items:</p> <pre><code>package Foo; use Moose; with qw(B C); sub do_something { my $self = shift; my @items = ???; # How can I get apple, orange, watermelon, banana here? .... } </code></pre> <p>One possible solution is to adopt <a href="http://search.cpan.org/~rjbs/MooseX-ComposedBehavior-0.003/lib/MooseX/ComposedBehavior.pm" rel="nofollow" title="MooseX::ComposedBehavior">MooseX::ComposedBehavior</a> but its POD says (at the time of writing, of course) that its API "is not quite stable" and also that "the current implementation is something of a hack, and should be replaced by a more robust one". Thus I'm investigating whether this could be accomplished without relying on such a "hack".</p> <p><strong>Warning:</strong> if you are reading this in the future, please go to check the POD of <a href="http://search.cpan.org/~rjbs/MooseX-ComposedBehavior-0.003/lib/MooseX/ComposedBehavior.pm" rel="nofollow" title="MooseX::ComposedBehavior">MooseX::ComposedBehavior</a> (current version: 0.003) because it might have changed in the mean time. Things change quickly. CPAN authors release new versions. What's "not quite stable" at the moment might become more stable in the future. There might even be other modules. Check yourself.</p> <p>Ideally there should be something like: <code>my @items = map $_-&gt;items, @ISA;</code> However that won't work with Moose. Are there any nicer and more reliable solutions?</p> <hr> <p><strong>Update:</strong> I ended up with this three-line solution:</p> <pre><code>package A; use Moose::Role; sub items () { qw/apple orange/ } package B; use Moose::Role; with 'A'; sub items () { qw/watermelon/ } package C; use Moose::Role; sub items () { qw/banana/ } package Foo; use Moose; with qw(B C); sub items () {} sub do_something { my $self = shift; my @items = map $_-&gt;execute, grep $_, map $_-&gt;get_method('items'), $self-&gt;meta-&gt;calculate_all_roles_with_inheritance; ... } </code></pre> <hr> <p><strong>Update:</strong> As various people requested me in the <a href="http://irc://irc.perl.org/#moose" rel="nofollow">#moose IRC channel</a> I removed my previous assertion that MooseX::ComposedBehavior "is not stable" and replaced it with literal text taken from its POD.</p> <hr> <p><strong>Update:</strong> I wrote a <a href="http://search.cpan.org/perldoc?MooseX%3a%3aCollect" rel="nofollow">MooseX::Collect</a> module which allows the following syntax:</p> <pre><code>package Foo; use Moose; use MooseX::Collect; collect 'items'; with qw(B C); sub do_something { my $self = shift; my @items = $self-&gt;items; ... } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload