So I have this little sketch, trying to get the hang of subtypes:
#!/usr/bin/perl
#
use strict;
use warnings;
package Foo;
use Moose;
use Moose::Util::TypeConstraints;
use Data::Dumper;
subtype 'ArrayRefOfHashrefs' => as 'ArrayRef[Hashref]';
has 'thingy' => ( 'is' => 'ro', 'isa' => 'ArrayRefOfHashrefs' );
package main;
my $foo = Foo->new('thingy' => [{ 'id' => 12 }]);
# The above produces:
use Data::Dumper;
print Dumper $foo->thingy;
And when I try to run it, I get:
Attribute (thingy) does not pass the type constraint because: Validation failed for 'ArrayRefOfHashrefs' with value ARRAY(0x7fda83028e08) at /[PATH]/site_perl/5.18.4/darwin2level/Moose/Object.pm line 24
Moose::Object::new('Foo', 'thingy', 'ARRAY(0x7fda83028e08)') called at ../sketches/arrayrefofhashrefs.pl line 17
What am I doing wrong?
You have a typo. The Hashref
is called a HashRef
with a capital R. The rest is fine.