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; |
18 | use MooseX::Debugging::Role::Object; |
19 | |
20 | Moose::Exporter->setup_import_methods(); |
21 | |
22 | sub init_meta { |
23 | shift; |
24 | my %options = @_; |
25 | |
09167788 |
26 | Moose->init_meta(%options); |
27 | |
9d72e8c0 |
28 | Moose::Util::MetaRole::apply_base_class_roles( |
f3ce0579 |
29 | for_class => $options{for_class}, |
b51819d6 |
30 | roles => ['MooseX::Debugging::Role::Object'], |
f3ce0579 |
31 | ); |
32 | } |
33 | |
f3ce0579 |
34 | package MooseX::Debugging::Role::Object; |
35 | |
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 |
52 | L<Moose::Util::MetaRole>, we ensure that when a module does "S<use |
53 | MooseX::Debugging>", it automatically gets the debugging role applied |
54 | to its base object class. |
55 | |
56 | =head1 AUTHOR |
57 | |
58 | Dave Rolsky E<lt>autarch@urth.orgE<gt> |
59 | |
60 | =head1 COPYRIGHT AND LICENSE |
61 | |
2840a3b2 |
62 | Copyright 2009 by Infinity Interactive, Inc. |
f3ce0579 |
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 |
70 | |