Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3841794
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T15:42:08+00:00 2026-05-19T15:42:08+00:00

Suppose I have multiple roles, each one defining a set of items: package A;

  • 0

Suppose I have multiple roles, each one defining a set of items:

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/ }

Suppose I use them in another class and I want to collect all those items:

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?
    ....
}

One possible solution is to adopt MooseX::ComposedBehavior 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”.

Warning: if you are reading this in the future, please go to check the POD of MooseX::ComposedBehavior (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.

Ideally there should be something like: my @items = map $_->items, @ISA;
However that won’t work with Moose. Are there any nicer and more reliable solutions?


Update: I ended up with this three-line solution:

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 $_->execute, grep $_, 
        map $_->get_method('items'),
        $self->meta->calculate_all_roles_with_inheritance;

    ...
}

Update: As various people requested me in the #moose IRC channel I removed my previous assertion that MooseX::ComposedBehavior “is not stable” and replaced it with literal text taken from its POD.


Update: I wrote a MooseX::Collect module which allows the following syntax:

package Foo;
use Moose;
use MooseX::Collect;

collect 'items';
with qw(B C);

sub do_something {
    my $self = shift;
    my @items = $self->items;
    ...
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T15:42:09+00:00Added an answer on May 19, 2026 at 3:42 pm

    You need to use around:

    package A;
    use Moose::Role;
    requires 'items';
    around items => sub {
        my ($orig, $self, @args) = @_;
        return ($self->$orig(@args), qw/apple orange/);
    };
    
    package B;
    use Moose::Role;
    requires 'items';
    with 'A'; # not required, do it if you want it
    around items => sub {
        my ($orig, $self, @args) = @_;
        return ($self->$orig(@args), qw/watermelon/);
    };
    
    package C;
    use Moose::Role;
    requires 'items';
    around items => sub {
        my ($orig, $self, @args) = @_;
        return ($self->$orig(@args), qw/banana/);
    };
    
    package Class;
    use Moose;
    with qw/B C/;
    sub items {}
    

    But in general, using classes to represent data is wrong, that’s what
    instances of classes are for. It’s hard to provide further advice since your example is so trival. What are you really trying to do?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

can it make sense for one table to have multiple foreign keys? Suppose I
so 1GvG:s/..../g can replace over an entire buffer However, suppose I have multiple vim
Suppose I have a string a foobar and I use ^a\s* to match a
Is this possible in fluent nhibernate having multiple mappings for one table? Lets suppose
In Windows, suppose you have multiple windows (HWNDs) of the same window class open.
Suppose we have a map that is shared between multiple threads. It represents a
How can I select average of multiple columns? Suppose I have some data like:
Suppose you have 2 different ASP.NET applications in IIS. Also, you have some ASCX
Suppose I have a stringbuilder in C# that does this: StringBuilder sb = new
Suppose we have a table A: itemid mark 1 5 2 3 and table

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.