On Thu, 14 Feb 2013, Bill Moseley wrote:

Hi all,

I had a problem that I tracked down to BUILD getting run twice.

It turned out a subclass was (incorrectly) using "after 'BUILD'".   When
that happens the BUILD in the base class runs twice.

I realize it's an improper use of BUILD but just wanted to note it in case
it's not an expected behavior.

package baseclass;
use Moose;
sub BUILD { warn "in baseclass BUILD\n" }

package subclass;
use Moose;
extends 'baseclass';
# WRONG usage
after 'BUILD' => sub { warn "in bad subclass BUILD\n" };

package main;
subclass->new;

1;


Results in:

in baseclass BUILD
in baseclass BUILD
in bad subclass BUILD

This is definitely a bug but it might be tricky to fix. Basically, method modifiers effectively copy the modified sub into the class that has the modifier. Moose then sees a BUILD in the subclass and baseclass and dutifully calls both.

But as you point out, what you did is just wrong anyway. The whole point of BUILD is that defining it in subclasses doesn't hide the parent's implementation.

The only time you need modifiers on BUILD is when you want a role to modify BUILD.


-dave

/*============================================================
http://VegGuide.org               http://blog.urth.org
Your guide to all that's veg      House Absolute(ly Pointless)
============================================================*/

Reply via email to