# NOTE: we ask Perl if we even
# need to do this first, to avoid
# extra meta level calls
- return unless $_[0]->can('BUILD');
+ return if $_[0]->can('BUILD') == \&BUILD;
my ($self, $params) = @_;
foreach my $method (reverse Class::MOP::class_of($self)->find_all_methods_by_name('BUILD')) {
+ next if $method->{class} eq __PACKAGE__;
$method->{code}->execute($self, $params);
}
}
+sub BUILD { }
+
sub DEMOLISHALL {
my $self = shift;
my ($in_global_destruction) = @_;
# NOTE: we ask Perl if we even
# need to do this first, to avoid
# extra meta level calls
- return unless $self->can('DEMOLISH');
+ return if $self->can('DEMOLISH') == \&DEMOLISH;
my @isa;
if ( my $meta = Class::MOP::class_of($self ) ) {
foreach my $class (@isa) {
no strict 'refs';
+ next if $class eq __PACKAGE__;
my $demolish = *{"${class}::DEMOLISH"}{CODE};
$self->$demolish($in_global_destruction)
if defined $demolish;
}
}
+sub DEMOLISH { }
+
sub DESTROY {
my $self = shift;
use strict;
use warnings;
use Test::More;
+use Test::Moose;
BEGIN {
eval "use Test::Output;";
plan skip_all => "Test::Output is required for this test" if $@;
# role: sub BUILD, after BUILD
# continues to work to run code after object initialization, whether the class
# has a BUILD method or not
+# note: as of moose 0.95, this idiom is no longer necessary ('after BUILD' on
+# its own is sufficient) -doy
my @CALLS;
with 'TestRole';
};
-{
+do {
+ package TestRoleWithoutBUILD;
+ use Moose::Role;
+
+ before BUILD => sub { push @CALLS, 'TestRoleWithoutBUILD::BUILD:before' };
+ after BUILD => sub { push @CALLS, 'TestRoleWithoutBUILD::BUILD:after' };
+};
+
+do {
+ package AnotherClassWithBUILD;
+ use Moose;
+
+ ::stderr_is {
+ with 'TestRoleWithoutBUILD';
+ } '';
+
+ sub BUILD { push @CALLS, 'AnotherClassWithBUILD::BUILD' }
+};
+
+do {
+ package AnotherClassWithoutBUILD;
+ use Moose;
+
+ ::stderr_is {
+ with 'TestRoleWithoutBUILD';
+ } '';
+};
+
+with_immutable {
is_deeply([splice @CALLS], [], "no calls to BUILD yet");
ClassWithBUILD->new;
'TestRole::BUILD:after',
]);
- if (ClassWithBUILD->meta->is_mutable) {
- ClassWithBUILD->meta->make_immutable;
- ClassWithoutBUILD->meta->make_immutable;
- redo;
- }
-}
+ AnotherClassWithBUILD->new;
+
+ is_deeply([splice @CALLS], [
+ 'TestRoleWithoutBUILD::BUILD:before',
+ 'AnotherClassWithBUILD::BUILD',
+ 'TestRoleWithoutBUILD::BUILD:after',
+ ]);
+
+ AnotherClassWithoutBUILD->new;
+
+ is_deeply([splice @CALLS], [
+ 'TestRoleWithoutBUILD::BUILD:before',
+ 'TestRoleWithoutBUILD::BUILD:after',
+ ]);
+} qw(ClassWithBUILD ClassWithoutBUILD
+ AnotherClassWithBUILD AnotherClassWithoutBUILD);
done_testing;