Subject: | BUILD not called in child classes |
Date: | Fri, 11 Feb 2011 14:59:54 +1300 |
To: | bug-Moo [...] rt.cpan.org |
From: | Mark Lawrence <nomad [...] null.net> |
The docs say this:
... you can define a "BUILD" method on your class and the
constructor will automatically call the "BUILD" method from parent
down to child after the object has been instantiated. Typically
this is used for object validation or possibly logging.
But doesn't seem to be the case in practice:
$ cat test.t
use Test::More tests => 2;
use strict;
use warnings;
{
package parent;
use Moo;
has 'name' => (
is => 'rw',
);
sub BUILD {
my $self = shift;
Test::More::ok 1, "Building parent";
}
}
{
package child;
use Moo;
extends 'parent';
sub BUILD {
my $self = shift;
Test::More::ok 1, "Building child";
}
}
child->new;
$ perl test.t
1..2
ok 1 - Building parent
# Looks like you planned 2 tests but ran 1.
$ perl -p -i -e 's/Moo/Moose/g' test.t
$ perl test.t
1..2
ok 1 - Building parent
ok 2 - Building child
$ perl -p -i -e 's/Moose/Mouse/g' test.t
$ perl test.t
1..2
ok 1 - Building parent
ok 2 - Building child
--
Mark Lawrence