7 eval 'use Test::Output;';
9 diag 'Test::Output is required for this test';
19 Moose::Cookbook::Extending::Recipe2 - Providing a role for the base object class
23 package MooseX::Debugging;
27 Moose::Exporter->setup_import_methods(
28 base_class_roles => ['MooseX::Debugging::Role::Object'],
31 package MooseX::Debugging::Role::Object;
35 after 'BUILDALL' => sub {
38 warn "Made a new " . ( ref $self ) . " object\n";
43 In this example, we provide a role for the base object class that adds
44 some simple debugging output. Every time an object is created, it
45 spits out a warning saying what type of object it was.
47 Obviously, a real debugging role would do something more interesting,
48 but this recipe is all about how we apply that role.
50 In this case, with the combination of L<Moose::Exporter> and
51 L<Moose::Util::MetaRole>, we ensure that when a module does C<S<use
52 MooseX::Debugging>>, it automatically gets the debugging role applied
53 to its base object class.
55 There are a few pieces of code worth looking at more closely.
57 Moose::Exporter->setup_import_methods(
58 base_class_roles => ['MooseX::Debugging::Role::Object'],
61 This creates an C<import> method in the C<MooseX::Debugging> package. Since we
62 are not actually exporting anything, we do not pass C<setup_import_methods>
63 any parameters related to exports, but we need to have an C<import> method to
64 ensure that our C<init_meta> method is called. The C<init_meta> is created by
65 C<setup_import_methods> for us, since we passed the C<base_class_roles>
66 parameter. The generated C<init_meta> will in turn call
67 L<Moose::Util::MetaRole::apply_base_class_roles|Moose::Util::MetaRole/apply_base_class_roles>.
71 Dave Rolsky E<lt>autarch@urth.orgE<gt>
73 =head1 COPYRIGHT AND LICENSE
75 Copyright 2009 by Infinity Interactive, Inc.
77 L<http://www.iinteractive.com>
79 This library is free software; you can redistribute it and/or modify
80 it under the same terms as Perl itself.
88 MooseX::Debugging->import;
92 sub { Debugged->new },
93 "Made a new Debugged object\n",
94 'got expected output from debugging role'