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