Commit | Line | Data |
d5e84b86 |
1 | |
2 | =pod |
3 | |
4 | =head1 NAME |
5 | |
6 | Moose::Cookbook::Extending::Recipe1 - Providing an alternate base object class |
7 | |
8 | =head1 SYNOPSIS |
9 | |
10 | package MyApp::Base; |
11 | use Moose; |
12 | |
13 | extends 'Moose::Object'; |
14 | |
15 | before 'new' => sub { warn "Making a new " . $_[0] }; |
16 | |
17 | no Moose; |
18 | |
19 | package MyApp::UseMyBase; |
20 | use Moose (); |
21 | |
22 | sub import { |
23 | my $caller = caller(); |
24 | |
25 | return if $caller eq 'main'; |
26 | |
27 | Moose::init_meta( $caller, |
28 | 'MyApp::Object', |
29 | ); |
30 | |
31 | Moose->import( { into => $caller }, @_ ); |
32 | } |
33 | |
34 | sub unimport { |
35 | my $caller = caller(); |
36 | |
37 | Moose->unimport( { into => $caller }, @_ ); |
38 | } |
39 | |
40 | =head1 DESCRIPTION |
41 | |
42 | Often you find that you want to share some behavior between all your |
43 | classes. One way to do that is to make a base class and simply add |
44 | C<S<extends 'MyApp::Base'>> to every class in your |
45 | application. However, that can get tedious. Instead, you can simply |
46 | create your Moose-alike module that sets the base object class to |
47 | C<MyApp::Base> for you. |
48 | |
49 | Then, instead of writing C<S<use Moose>> you can write C<S<use |
50 | MyApp::UseMyBase>>. |
51 | |
52 | In this particular example, our base class issues some debugging |
53 | output every time a new object is created, but you can surely think of |
54 | some more interesting things to do with your own base class. |
55 | |
56 | =head1 AUTHOR |
57 | |
58 | Dave Rolsky E<lt>autarch@urth.orgE<gt> |
59 | |
60 | =head1 COPYRIGHT AND LICENSE |
61 | |
62 | Copyright 2006-2008 by Infinity Interactive, Inc. |
63 | |
64 | L<http://www.iinteractive.com> |
65 | |
66 | This library is free software; you can redistribute it and/or modify |
67 | it under the same terms as Perl itself. |
68 | |
69 | =cut |