"ArrayRefOfHashrefs" 子类型在 Moose 中不起作用

"ArrayRefOfHashrefs" subtype not working in Moose

所以我有这个小草图,试图掌握子类型的窍门:

#!/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;

当我尝试 运行 时,我得到:

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

我做错了什么?

你打错了。 Hashref 被称为 HashRef,大写 R。其他的就好了。