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

  • Home
  • SEARCH
  • 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 3339802
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:32:04+00:00 2026-05-18T00:32:04+00:00

I have a Moose object with the following attribute: has ‘people’ => ( is

  • 0

I have a Moose object with the following attribute:

has 'people' => (
 is      => 'ro',
 isa     => 'ArrayRef[Person::Child]',
 traits  => ['Array'],
 default => sub { [] },
 handles => {
  all_people     => 'elements',
  get_people     => 'get',
  push_people    => 'push',
  pop_people     => 'pop',
  count_people   => 'count',
  sort_people    => 'sort',
  grep_people    => 'grep',
 },
);

Note the isa is set as ‘ArrayRef[Person::Child]’.

I would like to be able to choose between Person::Child, Person::Adult etc. upon creation of my object. Is that possible or must I create different objects that will be identical except the isa of the people attribute?

(This reminds me of Java generics).

  • 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-18T00:32:05+00:00Added an answer on May 18, 2026 at 12:32 am

    Why not move the definition of that attribute into a role and reuse it, with the
    appropriate parameterisation, in other classes?

    package MyApp::Thingy::HasPeople;
    
    use MooseX::Role::Parameterized;
    
    parameter person_type => (
        isa      => 'Str',
        required => 1,
    );
    
    role {
        my $person_type = shift->person_type;
    
        has 'people' => (
            is      => 'ro',
            isa     => "ArrayRef[${person_type}]",
            traits  => ['Array'],
            default => sub { [] },
            handles => {
                all_people   => 'elements',
                get_people   => 'get',
                push_people  => 'push',
                pop_people   => 'pop',
                count_people => 'count',
                sort_people  => 'sort',
                grep_people  => 'grep',
            },
        );
    };
    
    1;
    

    and somewhere else, in the classes that actually need that attribute

    package MyApp::Thingy::WithChildren;
    use Moose;
    
    with 'MyApp::Thingy::HasPeople' => { person_type => 'Person::Child' };
    
    1;
    

    or

    package MyApp::Thingy::WithAdults;
    use Moose;
    
    with 'MyApp::Thingy::HasPeople' => { person_type => 'Person::Adult' };
    
    1;
    

    That way you get to both not maintain the attribute in two places, and won’t end
    up with objects of the same class but different APIs, which tends to be a pretty
    big code smell.

    Alternatively, you could simply write a subtype of ArrayRef that accepts either a list of either Person::Child or Person::Adult or whatever other kinds of persons you have, but only as long as all elements of that list are of the same kind.

    use List::AllUtils 'all';
    subtype 'PersonList', as 'ArrayRef', where {
        my $class = blessed $_->[0];
        $_->[0]->isa('Person') && all { blessed $_ eq $class } @{ $_ };
    };
    
    has persons => (
        is  => 'ro',
        isa => 'PersonList',
        ...,
    );
    

    I’d probably go for the first solution in order to be able to decide based on an objects class if it contains children, adults, or whatever.

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

Sidebar

Related Questions

Suppose i have the following Moose package: package GSM::Cell; use Moose; has 'ID' =>
I have the following DataTemplate in a Listbox <ListBox Grid.Column=1 Grid.Row=2 ItemsSource={Binding People} SelectedItem={Binding
I have found this example on StackOverflow: var people = new List<Person> { new
I have a Moose class that i would like to store using Apache::Session::File. However,
Moose is a fantastic object framework. The trouble is that, taken together with its
i actually try to do the following: I have loaded an external image in
This is what I have On my .master page i have the following <script
I'm an absolute newbie to Moose and so far I have read Moose and
I'm pretty sure that many people have thought of this, but for some reason
Let say I have the following desire, to simplify the IConvertible's to allow me

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.