Commit | Line | Data |
f3ce0579 |
1 | |
2 | =pod |
3 | |
4 | =head1 NAME |
5 | |
6 | Moose::Cookbook::Extending::Recipe2 - Providing a role for the base object class |
7 | |
8 | =head1 SYNOPSIS |
9 | |
10 | package MooseX::Debugging; |
11 | |
09167788 |
12 | use Moose (); |
f3ce0579 |
13 | use Moose::Exporter; |
14 | use Moose::Util::MetaRole; |
f3ce0579 |
15 | |
871cda31 |
16 | Moose::Exporter->setup_import_methods; |
f3ce0579 |
17 | |
18 | sub init_meta { |
19 | shift; |
20 | my %options = @_; |
21 | |
a8de959b |
22 | my $meta = Moose->init_meta(%options); |
09167788 |
23 | |
9d72e8c0 |
24 | Moose::Util::MetaRole::apply_base_class_roles( |
f3ce0579 |
25 | for_class => $options{for_class}, |
b51819d6 |
26 | roles => ['MooseX::Debugging::Role::Object'], |
f3ce0579 |
27 | ); |
a8de959b |
28 | |
29 | return $meta; |
f3ce0579 |
30 | } |
31 | |
f3ce0579 |
32 | package MooseX::Debugging::Role::Object; |
33 | |
0df6b748 |
34 | use Moose::Role; |
35 | |
f3ce0579 |
36 | after 'BUILD' => sub { |
37 | my $self = shift; |
38 | |
39 | warn "Made a new " . ref $self . " object\n"; |
6a7e3999 |
40 | }; |
f3ce0579 |
41 | |
42 | =head1 DESCRIPTION |
43 | |
44 | In this example, we provide a role for the base object class that adds |
45 | some simple debugging output. Every time an object is created, it |
46 | spits out a warning saying what type of object it was. |
47 | |
48 | Obviously, a real debugging role would do something more interesting, |
49 | but this recipe is all about how we apply that role. |
50 | |
51 | In this case, with the combination of L<Moose::Exporter> and |
8efdbb91 |
52 | L<Moose::Util::MetaRole>, we ensure that when a module does C<S<use |
53 | MooseX::Debugging>>, it automatically gets the debugging role applied |
f3ce0579 |
54 | to its base object class. |
55 | |
871cda31 |
56 | There are a few pieces of code worth looking at more closely. |
57 | |
58 | Moose::Exporter->setup_import_methods; |
59 | |
60 | This creates an C<import> method in the C<MooseX::Debugging> |
61 | package. Since we are not actually exporting anything, we do not pass |
62 | C<setup_import_methods> any parameters. However, we need to have an |
63 | C<import> method to ensure that our C<init_meta> method is called. |
64 | |
65 | Then in our C<init_meta> method we have this line: |
66 | |
67 | Moose->init_meta(%options); |
68 | |
69 | This is a bit of boilerplate that almost every extension will |
70 | use. This ensures that the caller has a normal Moose metaclass |
71 | I<before> we go and add traits to it. |
72 | |
73 | The C<< Moose->init_meta >> method does ensures that the caller has a |
74 | sane metaclass, and we don't want to replicate that logic in our |
75 | extension. If the C<< Moose->init_meta >> was already called (because |
f08fd992 |
76 | the caller did C<S<use Moose>> before using our extension), then |
77 | calling C<< Moose->init_meta >> again is effectively a no-op. |
871cda31 |
78 | |
f3ce0579 |
79 | =head1 AUTHOR |
80 | |
81 | Dave Rolsky E<lt>autarch@urth.orgE<gt> |
82 | |
83 | =head1 COPYRIGHT AND LICENSE |
84 | |
2840a3b2 |
85 | Copyright 2009 by Infinity Interactive, Inc. |
f3ce0579 |
86 | |
87 | L<http://www.iinteractive.com> |
88 | |
89 | This library is free software; you can redistribute it and/or modify |
90 | it under the same terms as Perl itself. |
91 | |
92 | =cut |
93 | |