6 Moose::Cookbook::Extending::Recipe2 - Providing a role for the base object class
10 package MooseX::Debugging;
17 use Moose::Util::MetaRole;
18 use MooseX::Debugging::Role::Object;
20 Moose::Exporter->setup_import_methods();
26 Moose->init_meta(%options);
28 Moose::Util::MetaRole::apply_base_class_roles(
29 for_class => $options{for_class},
30 roles => ['MooseX::Debugging::Role::Object'],
34 package MooseX::Debugging::Role::Object;
36 after 'BUILD' => sub {
39 warn "Made a new " . ref $self . " object\n";
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.
48 Obviously, a real debugging role would do something more interesting,
49 but this recipe is all about how we apply that role.
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.
58 Dave Rolsky E<lt>autarch@urth.orgE<gt>
60 =head1 COPYRIGHT AND LICENSE
62 Copyright 2009 by Infinity Interactive, Inc.
64 L<http://www.iinteractive.com>
66 This library is free software; you can redistribute it and/or modify
67 it under the same terms as Perl itself.