6 Moose::Cookbook::Extending::Recipe2 - Providing a role for the base object class
10 package MooseX::Debugging;
16 use Moose::Util::MetaRole;
17 use MooseX::Debugging::Role::Object;
19 Moose::Exporter->setup_import_methods();
25 Moose::Util::MetaRole::apply_base_object_roles(
26 for_class => $options{for_class},
27 role => ['MooseX::Debugging::Role::Object'],
31 package MooseX::Debugging::Role::Object;
33 after 'BUILD' => sub {
36 warn "Made a new " . ref $self . " object\n";
41 In this example, we provide a role for the base object class that adds
42 some simple debugging output. Every time an object is created, it
43 spits out a warning saying what type of object it was.
45 Obviously, a real debugging role would do something more interesting,
46 but this recipe is all about how we apply that role.
48 In this case, with the combination of L<Moose::Exporter> and
49 L<Moose::Util::MetaRole>, we ensure that when a module does "S<use
50 MooseX::Debugging>", it automatically gets the debugging role applied
51 to its base object class.
55 Dave Rolsky E<lt>autarch@urth.orgE<gt>
57 =head1 COPYRIGHT AND LICENSE
59 Copyright 2008 by Infinity Interactive, Inc.
61 L<http://www.iinteractive.com>
63 This library is free software; you can redistribute it and/or modify
64 it under the same terms as Perl itself.