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 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 a trailing object following after the mouse. When the mouse gets out
I have the following DataTemplate in a Listbox <ListBox Grid.Column=1 Grid.Row=2 ItemsSource={Binding People} SelectedItem={Binding
I have the following event handler: private void rtb_MouseDown(object sender, MouseEventArgs e) { if
I have a function to follow the object after the mouse, and I want
I have a Moose class which composes a Mouse role. package My::Moose::Class; use Moose;
I have written a set of classes and interfaces that are implemented in Moose
I have a Moose class that i would like to store using Apache::Session::File. However,
I have a FLASH object that I mouse over which in turn calls the
I have a form with the following components: A TPageControl containing a single TTabSheet

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.