1 package Moose::Cookbook::Extending::Recipe3;
3 # ABSTRACT: Providing an alternate base object class
13 'Test::Output' => '0',
23 extends 'Moose::Object';
25 before 'new' => sub { warn "Making a new " . $_[0] };
29 package MyApp::UseMyBase;
33 Moose::Exporter->setup_import_methods( also => 'Moose' );
37 return Moose->init_meta( @_, base_class => 'MyApp::Base' );
42 A common extension is to provide an alternate base class. One way to
43 do that is to make a C<MyApp::base> and add C<S<extends
44 'MyApp::Base'>> to every class in your application. That's pretty
45 tedious. Instead, you can create a Moose-alike module that sets the
46 base object class to C<MyApp::Base> for you.
48 Then, instead of writing C<S<use Moose>> you can write C<S<use
51 In this particular example, our base class issues some debugging
52 output every time a new object is created, but you can think of some
53 more interesting things to do with your own base class.
55 This uses the magic of L<Moose::Exporter>. When we call C<<
56 Moose::Exporter->setup_import_methods( also => 'Moose' ) >> it builds
57 C<import> and C<unimport> methods for you. The C<< also => 'Moose' >>
58 bit says that we want to export everything that Moose does.
60 The C<import> method that gets created will call our C<init_meta>
61 method, passing it C<< for_caller => $caller >> as its
62 arguments. The C<$caller> is set to the class that actually imported
63 us in the first place.
65 See the L<Moose::Exporter> docs for more details on its API.
67 =head1 USING MyApp::UseMyBase
69 To actually use our new base class, we simply use C<MyApp::UseMyBase>
70 I<instead> of C<Moose>. We get all the Moose sugar plus our new base
77 has 'size' => ( is => 'rw' );
83 This is an awful lot of magic for a simple base class. You will often
84 want to combine a metaclass trait with a base class extension, and
85 that's when this technique is useful.
92 MyApp::UseMyBase->import;
94 has( 'size' => ( is => 'rw' ) );
97 ok( Foo->isa('MyApp::Base'), 'Foo isa MyApp::Base' );
99 ok( Foo->can('size'), 'Foo has a size method' );
103 sub { $foo = Foo->new( size => 2 ) },
104 qr/^Making a new Foo/,
105 'got expected warning when calling Foo->new'
108 is( $foo->size(), 2, '$foo->size is 2' );