{
package MyArray;
use namespace::autoclean;
use Moose;
use Moose::Util::TypeConstraints;
subtype 'MyArray::Ents::Level2'
=> as 'ArrayRef[Str|Num|Int]';
coerce 'MyArray::Ents::Level2'
=> from 'Str|Num|Int'
=> via { [ $_ ] };
my $level2_constraint =
Moose::Util::TypeConstraints::find_type_constraint(
'MyArray::Ents::Level2');
# Doesn't work if I remove this?!?
subtype 'MyArray::Ents'
=> as 'ArrayRef[MyArray::Ents::Level2]';
coerce 'ArrayRef[MyArray::Ents::Level2]'
=> from 'ArrayRef[MyArray::Ents::Level2|Str|Num|Int]'
=> via { [ map $level2_constraint->coerce($_), @$_ ] };
no Moose::Util::TypeConstraints;
has 'myarray' => (
traits => ['Array'],
is => 'rw',
isa => 'ArrayRef[MyArray::Ents::Level2]',
default => sub { [] },
handles => {
push => 'push',
get => 'get',
set => 'set',
elements => 'elements',
count => 'count',
},
coerce => 1,
);
no Moose;
__PACKAGE__->meta->make_immutable;
}
use Modern::Perl;
use Data::Dumper qw( Dumper );
sub dump_it {
local $Data::Dumper::Useqq = 1;
local $Data::Dumper::Terse = 1;
local $Data::Dumper::Indent = 0;
print(Dumper($_[0]), "\n");
}
my $a0 = MyArray->new( myarray => [ 0, [ 0, 0, 0 ], [1,2,3] ] ) ;
dump_it($a0);
$a0->set(1, 'cat');
dump_it($a0);
$a0->push(2.0);
dump_it($a0);
####
bless( {"myarray" => [[0],[0,0,0],[1,2,3]]}, 'MyArray' )
bless( {"myarray" => [[0],["cat"],[1,2,3]]}, 'MyArray' )
bless( {"myarray" => [[0],["cat"],[1,2,3],[2]]}, 'MyArray' )
##
##
subtype 'MyArray::Ents::Level2'
=> as 'ArrayRef[Str|Num|Int]';
coerce 'MyArray::Ents::Level2'
=> from 'Str|Num|Int'
=> via { [ $_ ] };
my $level2_constraint =
Moose::Util::TypeConstraints::find_type_constraint(
'MyArray::Ents::Level2');
subtype 'MyArray::Ents'
=> as 'ArrayRef[MyArray::Ents::Level2]';
coerce 'MyArray::Ents'
=> from 'ArrayRef[MyArray::Ents::Level2|Str|Num|Int]'
=> via { [ map $level2_constraint->coerce($_), @$_ ] };
...
isa => 'MyArray::Ents'
##
##
Can't call method "is_a_type_of" on an undefined value at .../Moose/Meta/Attribute/Native/Trait.pm line 98