From: Jesse Luehrs Date: Fri, 22 Apr 2011 16:26:43 +0000 (-0500) Subject: stop encouraging wrapping BUILDALL, it's a bad idea X-Git-Tag: 2.0001~4 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=4f8b00620eddbc7d5f05af5faab582b56ff6a224;p=gitmo%2FMoose.git stop encouraging wrapping BUILDALL, it's a bad idea --- diff --git a/lib/Moose/Cookbook/Extending/Recipe2.pod b/lib/Moose/Cookbook/Extending/Recipe2.pod index 03c3909..c1a8457 100644 --- a/lib/Moose/Cookbook/Extending/Recipe2.pod +++ b/lib/Moose/Cookbook/Extending/Recipe2.pod @@ -29,7 +29,8 @@ use Test::Requires { use Moose::Role; - after 'BUILDALL' => sub { + sub BUILD {} + after BUILD => sub { my $self = shift; warn "Made a new " . ( ref $self ) . " object\n"; @@ -63,6 +64,19 @@ C for us, since we passed the C parameter. The generated C will in turn call L. + sub BUILD {} + after BUILD => sub { + ... + }; + +Due to the way role composition currently works, if the class that a role is +composed into contains a C method, then that will override the C +method in any roles it composes, which is typically not what you want. Using a +method modifier on C avoids this issue, since method modifiers compose +together rather than being overridden. Method modifiers require that a method +exists in order to wrap, however, so we also provide a stub method to wrap if +no C method exists in the class. + =begin testing {