{ package Page; use strict; use warnings; use Moose; has 'url' => ( is => 'ro', isa => 'Str', required => 1, ); use Moose::Util::TypeConstraints; subtype 'Pages' => as 'ArrayRef[Page]'; coerce 'Pages' => from 'ArrayRef[HashRef]' => via { [ map __PACKAGE__->new($_), @{ $_[0] } ] }; no Moose::Util::TypeConstraints; no Moose; __PACKAGE__->meta->make_immutable(); } { package Container; use strict; use warnings; use Moose; has 'pages' => ( is => 'rw', isa => 'Pages', coerce => 1, default => sub { [ ] }, ); no Moose; __PACKAGE__->meta->make_immutable(); } Container->new( pages => [ { url => "a" }, { url => "b" } ] );
I don't really like the global-ness of the coercion. I'll be posting an alternative that uses BUILDARGS shortly.
In reply to Re^3: Moose from ArrayRef[HashRef] to ArrayRef[Object]
by ikegami
in thread Moose from ArrayRef[HashRef] to ArrayRef[Object]
by saintex
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |