Controlling the build
- BUILD is called after new creates the object but before it is returned to the caller
package Class;
use Moose;
...
sub BUILD {
my $self = shift;
$self->do_stuff;
return; # return value is ignored
}
- Checking complex constraints
sub BUILD {
my $self = shift;
confess 'if foo is 42, then bar should be less than 8'
unless $self->foo == 42 && $self->bar < 8;
$self->bar(64) if $self->quux->is_bazzy;
}
- DEMOLISH is like destroy