Neighbour has asked for the wisdom of the Perl Monks concerning the following question:
package Parent; use Moose; has 'knibbel' => ( is => 'rw', isa => 'HashRef[Maybe[Value]]', lazy_build => 1, ); has 'knabbel' => ( is => 'rw', isa => 'ArrayRef[Maybe[Value]]', lazy_build => 1, ); sub BUILD { my $self = shift; my $meta = $self->meta; print("BUILD called for " . __PACKAGE__ . "\n"); no strict; foreach my $attribute ($meta->get_attribute_list) { print("Creating builder for attribute [$attribute]\n"); *{__PACKAGE__ . '::_build_' . $attribute} = sub { my $self = shift; my $meta = $self->meta; my $fropsel = $meta->get_attribute($attribute); if ($fropsel->type_constraint->name =~ /^ArrayRef/) { retu +rn []; } if ($fropsel->type_constraint->name =~ /^HashRef/) { retur +n {}; } }; } use strict; } 1;
And this simple script to use it:package Child; use Moose; extends 'Parent'; has 'knuisje' => ( is => 'rw', isa => 'Str', lazy_build => 1, ); sub BUILD { my $self = shift; my $meta = $self->meta; print("BUILD called for " . __PACKAGE__ . "\n"); no strict; foreach my $attribute ($meta->get_attribute_list) { print("Creating builder for attribute [$attribute]\n"); *{__PACKAGE__ . '::_build_' . $attribute} = sub { my $self = shift; my $meta = $self->meta; my $fropsel = $meta->get_attribute($attribute); if ($fropsel->type_constraint->name =~ /^Str/) { return "" +; } }; } use strict; } 1;
Which gives the following output:#!/usr/bin/perl -w use strict; use Parent; use Child; my $child = Child->new();
Which is not quite what I expected, nor wanted :)$ ./testchild.pl BUILD called for Parent Creating builder for attribute [knuisje] BUILD called for Child Creating builder for attribute [knuisje]
|
|---|