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 8554689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:56:24+00:00 2026-06-11T14:56:24+00:00

In trying to answer How to instantiate Moose classes from a big hash ,

  • 0

In trying to answer How to instantiate Moose classes from a big hash, I think I have hit another place where I don’t fully understand Moose type coercions. For some reason, the below code issues warnings:

You cannot coerce an attribute (departments) unless its type (ArrayRef[Company::Department]) has a coercion at ./test.pl line 12.
You cannot coerce an attribute (employees) unless its type (ArrayRef[Company::Person]) has a coercion at ./test.pl line 23.

but then succeeds.

#!/usr/bin/env perl

use warnings;
use strict;

package Company;
use Moose;
use Moose::Util::TypeConstraints;

has 'id'   => (is => 'ro', isa => 'Num');
has 'name' => (is => 'ro', isa => 'Str');
has 'departments' => (is => 'ro', isa => 'ArrayRef[Company::Department]', coerce => 1);

coerce 'ArrayRef[Company::Department]',
  from 'ArrayRef[HashRef]',
  via { [ map { Company::Department->new($_) } @$_ ] };

package Company::Department;
use Moose;

has 'id'   => (is => 'ro', isa => 'Num');
has 'name' => (is => 'ro', isa => 'Str');
has 'employees' => (is => 'ro', isa => 'ArrayRef[Company::Person]', coerce => 1);

package Company::Person;
use Moose;
use Moose::Util::TypeConstraints;

has 'id'         => (is => 'ro', isa => 'Num');
has 'name'  => (is => 'ro', isa => 'Str');
has 'age'        => (is => 'ro', isa => 'Num');

coerce 'ArrayRef[Company::Person]',
  from 'ArrayRef[HashRef]',
  via { [ map { Company::Person->new($_) } @$_ ] };

package main;

my %hash = (
    company => {
        id => 1,
        name => 'CorpInc',
        departments => [
            {
                id => 1,
                name => 'Sales',
                employees => [
                    {
                        id => 1,
                        name => 'John Smith',
                        age => '30',
                    },
                ],
            },
            {
                id => 2,
                name => 'IT',
                employees => [
                    {
                        id => 2,
                        name => 'Lucy Jones',
                        age => '28',
                    },
                    {
                        id => 3,
                        name => 'Miguel Cerveza',
                        age => '25',
                    },
                ],
            },
        ],
    }
);

my $company = Company->new($hash{company});
use Data::Dumper;
print Dumper $company;

How should this have been done? P.S. I tried simply doing

coerce 'Company::Department',
  from 'HashRef',
  via { Company::Department->new($_) };

but it died horribly.

  • 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-06-11T14:56:26+00:00Added an answer on June 11, 2026 at 2:56 pm

    Well, it doesn’t succeed completely, and you should feel it when you’ll try to update these fields with coerce => 1. That’s why:

    You cannot pass coerce => 1 unless the attribute’s type constraint has
    a coercion

    Previously, this was accepted, and it sort of worked,
    except that if you attempted to set the attribute after the object was
    created, you would get a runtime error.
    Now you will get an error when you attempt to define the attribute.

    Still, I think I find the way to fix it, by introducing subtypes, first, and changing the order of packages, second:

    package Company::Person;
    use Moose;
    use Moose::Util::TypeConstraints;
    
    subtype 'ArrayRefCompanyPersons',
      as 'ArrayRef[Company::Person]';
    
    coerce 'ArrayRefCompanyPersons',
      from 'ArrayRef[HashRef]',
      via { [ map { Company::Person->new($_) } @$_ ] };
    
    has 'id'         => (is => 'ro', isa => 'Num');
    has 'name'  => (is => 'ro', isa => 'Str');
    has 'age'        => (is => 'ro', isa => 'Num');
    
    package Company::Department;
    use Moose;
    
    has 'id'   => (is => 'ro', isa => 'Num');
    has 'name' => (is => 'ro', isa => 'Str');
    has 'employees' => (is => 'ro', isa => 'ArrayRefCompanyPersons', coerce => 1);
    
    package Company;
    use Moose;
    use Moose::Util::TypeConstraints;
    
    subtype 'ArrayRefCompanyDepartments',
      as 'ArrayRef[Company::Department]';
    
    coerce 'ArrayRefCompanyDepartments',
      from 'ArrayRef[HashRef]',
      via { [ map { Company::Department->new($_) } @$_ ] };
    
    has 'id'   => (is => 'ro', isa => 'Num');
    has 'name' => (is => 'ro', isa => 'Str');
    has 'departments' => (is => 'ro', isa => 'ArrayRefCompanyDepartments', coerce => 1);
    

    The rest of the code is the same as in your version. This works without any warnings, and more-o-less behaves like (again, I think) it should be.

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

Sidebar

Related Questions

I was trying to answer another question about polymorphism vs sharing when I stumbled
I have a job interview tomorrow and I'm trying to answer this question: There
I have been searching for an answer and trying out stuff for days now
While trying to write an answer for another SO question something really peculiar happened.
While trying to answer another question, I was serializing a C# object to an
I think I have a fairly basic question here. I'm not trying to waste
I am trying to instantiate a Java abstract class from my Groovy code. Considering
While trying to answer this question I found that the code int* p =
While trying to answer this question I found without () (which invokes C++ most
I am trying to answer the following question as part of my college revision:

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.