Consider the following minimal demonstration classes:
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;
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;
And this simple script to use it:
#!/usr/bin/perl -w
use strict;
use Parent;
use Child;
my $child = Child->new();
Which gives the following output:
$ ./testchild.pl
BUILD called for Parent
Creating builder for attribute [knuisje]
BUILD called for Child
Creating builder for attribute [knuisje]
Which is not quite what I expected, nor wanted :)
How can I avoid this issue, and access the attributes of the parent in the parent's BUILD-sub?
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.