Search code examples
perlmultidimensional-arraymoosetraitscoerce

How to set up an AoArrayrefs attribute with coercion from scalar into arrayref?


I would like to set up an attribute that is an array of arrayrefs with coercion of nonarrayrefs to array refs. eg.

[ 0, [ 0, 0, 0 ], [1,2,3] ] into [ [0], [ 0, 0, 0 ], [1,2,3] ]

also, I'd like to be able to push or set elements to the AoA with coercion as well. Here is my attempt:

{
    package MyArray;
    use namespace::autoclean;
    use Moose::Util::TypeConstraints;
    use Moose;

    subtype 'My::ArrayRef' => as 'ArrayRef';
    coerce  'My::ArrayRef'
      => from 'Num|Str'
         => via {[$_]};

    has 'ents' => (
        traits  => ['Array'],
        is      => 'rw',
        isa     => 'ArrayRef[My::ArrayRef]',
        default => sub { [] },
        handles => {
            push      => 'push',
            get       => 'get',
            set       => 'set',
            elements  => 'elements',
            count     => 'count',
        },
        coerce => 1,
    );

    __PACKAGE__->meta->make_immutable;

}

use Modern::Perl;

my $a0 = MyArray->new( ents => [ 0, [ 0, 0, 0 ], [1,2,3] ] ) ;

use Data::Dumper;

print Dumper $a0;

$a0->set(0,'cat');
print Dumper $a0;
$a0->push(1.0);
print Dumper $a0;

Solution

  • The type needs to fail to match before the coercion, but succeed after.

    This does the trick (tested):

    my $array_ref = Moose::Util::TypeConstraints::find_type_constraint('ArrayRef');
    
    # Create an alias so we don't affect others with our coercion.
    subtype 'My::Data::Structure'
       => as 'ArrayRef[ArrayRef[Str|Num]]';
    
    coerce 'My::Data::Structure'
       => from 'ArrayRef[ArrayRef[Str|Num]|Str|Num]'
          => via { [ map $array_ref->check($_) ? $_ : [ $_ ], @$_ ] };