in reply to Re^2: Moose from ArrayRef[HashRef] to ArrayRef[Object]
in thread Moose from ArrayRef[HashRef] to ArrayRef[Object]

Using BUILDARGS to avoid effects at a distance:
{ package Page; use strict; use warnings; use Moose; has 'url' => ( is => 'ro', isa => 'Str', required => 1, ); no Moose; __PACKAGE__->meta->make_immutable(); } { package Container; use strict; use warnings; use Moose; my $pages_constraint = Moose::Util::TypeConstraints::create_paramet +erized_type_constraint('ArrayRef[Page]'); my $aoh_constraint = Moose::Util::TypeConstraints::create_paramet +erized_type_constraint('ArrayRef[HashRef]'); around BUILDARGS => sub { my $orig = shift; my $class = shift; my %args = ( @_ == 1 ? %{ $_[0] } : @_ ); if (exists($args{pages})) { my $pages = $args{pages}; if (!$pages_constraint->check($pages)) { # Need to coerce? if ($aoh_constraint->check($pages)) { # Can we coerce? $args{pages} = [ map Page->new($_), @$pages ]; } } } return $class->$orig(%args); }; has 'pages' => ( is => 'rw', isa => 'ArrayRef[Page]', default => sub { [ ] }, ); no Moose; __PACKAGE__->meta->make_immutable(); } Container->new( pages => [ { url => "a" }, { url => "b" } ] );

This is basically the same approach you tried, but correctly uses the more appropriate BUILDARGS instead of BUILD.

Coded so it can take both ArrayRef[HashRef] and ArrayRef[Page].

Replies are listed 'Best First'.
Re^4: Moose from ArrayRef[HashRef] to ArrayRef[Object]
by saintex (Scribe) on Feb 23, 2011 at 22:50 UTC
    thank you very much for your solutions:
    tomorrow I will try both of them.
Re^4: Moose from ArrayRef[HashRef] to ArrayRef[Object]
by saintex (Scribe) on Feb 24, 2011 at 09:16 UTC
    I tried the two samples.
    The second one is the one I like it (because conceptually you don't have to create a type not related to class in the base-class).

    Just a little precisation:
    Here:
    $args{pages} = [ map Page->new($_), @$pages ];

    must be:
    $args{pages} = [ map Page->new(URL=>$$_{Page}{URL})), @$pages ];


    And I have a question:

    I have also checked Moose manual:

    What "around" is?

    around BUILDARGS => sub {

    Thank you for your answers.