Make Test::Output optional again
[gitmo/Moose.git] / lib / Moose / Cookbook / Extending / Recipe3.pod
CommitLineData
d5e84b86 1
2=pod
3
5547fba7 4=begin testing-SETUP
c79239a2 5
6BEGIN {
3d659f7f 7 eval 'use Test::Output;';
8 if ($@) {
9 diag 'Test::Output is required for this test';
10 ok(1);
11 exit 0;
12 }
c79239a2 13}
14
5547fba7 15=end testing-SETUP
c79239a2 16
d5e84b86 17=head1 NAME
18
c8d5f1e1 19Moose::Cookbook::Extending::Recipe3 - Providing an alternate base object class
d5e84b86 20
21=head1 SYNOPSIS
22
23 package MyApp::Base;
24 use Moose;
25
26 extends 'Moose::Object';
27
28 before 'new' => sub { warn "Making a new " . $_[0] };
29
30 no Moose;
31
32 package MyApp::UseMyBase;
33 use Moose ();
554b7648 34 use Moose::Exporter;
d5e84b86 35
aedcb7d9 36 Moose::Exporter->setup_import_methods( also => 'Moose' );
d5e84b86 37
554b7648 38 sub init_meta {
39 shift;
a8de959b 40 return Moose->init_meta( @_, base_class => 'MyApp::Base' );
d5e84b86 41 }
42
43=head1 DESCRIPTION
44
9ac00c2b 45A common extension is to provide an alternate base class. One way to
46do that is to make a C<MyApp::base> and add C<S<extends
47'MyApp::Base'>> to every class in your application. That's pretty
48tedious. Instead, you can create a Moose-alike module that sets the
49base object class to C<MyApp::Base> for you.
d5e84b86 50
51Then, instead of writing C<S<use Moose>> you can write C<S<use
52MyApp::UseMyBase>>.
53
54In this particular example, our base class issues some debugging
9ac00c2b 55output every time a new object is created, but you can think of some
56more interesting things to do with your own base class.
d5e84b86 57
5a87a5ca 58This uses the magic of L<Moose::Exporter>. When we call C<<
59Moose::Exporter->setup_import_methods( also => 'Moose' ) >> it builds
60C<import> and C<unimport> methods for you. The C<< also => 'Moose' >>
61bit says that we want to export everything that Moose does.
554b7648 62
63The C<import> method that gets created will call our C<init_meta>
5a87a5ca 64method, passing it C<< for_caller => $caller >> as its
9ac00c2b 65arguments. The C<$caller> is set to the class that actually imported
66us in the first place.
554b7648 67
68See the L<Moose::Exporter> docs for more details on its API.
69
70=head1 USING MyApp::UseMyBase
71
72To actually use our new base class, we simply use C<MyApp::UseMyBase>
73I<instead> of C<Moose>. We get all the Moose sugar plus our new base
74class.
75
76 package Foo;
77
78 use MyApp::UseMyBase;
79
80 has 'size' => ( is => 'rw' );
81
82 no MyApp::UseMyBase;
83
9ac00c2b 84=head1 CONCLUSION
85
86This is an awful lot of magic for a simple base class. You will often
87want to combine a metaclass trait with a base class extension, and
88that's when this technique is useful.
89
d5e84b86 90=head1 AUTHOR
91
92Dave Rolsky E<lt>autarch@urth.orgE<gt>
93
94=head1 COPYRIGHT AND LICENSE
95
2840a3b2 96Copyright 2006-2009 by Infinity Interactive, Inc.
d5e84b86 97
98L<http://www.iinteractive.com>
99
100This library is free software; you can redistribute it and/or modify
101it under the same terms as Perl itself.
102
c79239a2 103=begin testing
104
105{
106 package Foo;
107
108 MyApp::UseMyBase->import;
109
110 has( 'size' => ( is => 'rw' ) );
111}
112
113ok( Foo->isa('MyApp::Base'), 'Foo isa MyApp::Base' );
114
115ok( Foo->can('size'), 'Foo has a size method' );
116
117my $foo;
8b23f9f8 118stderr_like(
c79239a2 119 sub { $foo = Foo->new( size => 2 ) },
8b23f9f8 120 qr/^Making a new Foo/,
c79239a2 121 'got expected warning when calling Foo->new'
122);
123
124is( $foo->size(), 2, '$foo->size is 2' );
125
126=end testing
127
d5e84b86 128=cut