Add all our optional test deps as author_requires
[gitmo/Moose.git] / lib / Moose / Cookbook / Extending / Recipe2.pod
CommitLineData
f3ce0579 1
2=pod
3
6e646af9 4=begin testing-SETUP
5
6BEGIN {
7 eval 'use Test::Output;';
8 if ($@) {
9 diag 'Test::Output is required for this test';
10 ok(1);
11 exit 0;
12 }
13}
14
15=end testing-SETUP
16
f3ce0579 17=head1 NAME
18
19Moose::Cookbook::Extending::Recipe2 - Providing a role for the base object class
20
21=head1 SYNOPSIS
22
23 package MooseX::Debugging;
24
f3ce0579 25 use Moose::Exporter;
f3ce0579 26
95056a1e 27 Moose::Exporter->setup_import_methods(
28 base_class_roles => ['MooseX::Debugging::Role::Object'],
29 );
f3ce0579 30
f3ce0579 31 package MooseX::Debugging::Role::Object;
32
0df6b748 33 use Moose::Role;
34
6e646af9 35 after 'BUILDALL' => sub {
f3ce0579 36 my $self = shift;
37
6e646af9 38 warn "Made a new " . ( ref $self ) . " object\n";
6a7e3999 39 };
f3ce0579 40
41=head1 DESCRIPTION
42
43In this example, we provide a role for the base object class that adds
44some simple debugging output. Every time an object is created, it
45spits out a warning saying what type of object it was.
46
47Obviously, a real debugging role would do something more interesting,
48but this recipe is all about how we apply that role.
49
50In this case, with the combination of L<Moose::Exporter> and
8efdbb91 51L<Moose::Util::MetaRole>, we ensure that when a module does C<S<use
52MooseX::Debugging>>, it automatically gets the debugging role applied
f3ce0579 53to its base object class.
54
871cda31 55There are a few pieces of code worth looking at more closely.
56
95056a1e 57 Moose::Exporter->setup_import_methods(
58 base_class_roles => ['MooseX::Debugging::Role::Object'],
59 );
871cda31 60
edd82429 61This creates an C<import> method in the C<MooseX::Debugging> package. Since we
62are not actually exporting anything, we do not pass C<setup_import_methods>
63any parameters related to exports, but we need to have an C<import> method to
64ensure that our C<init_meta> method is called. The C<init_meta> is created by
65C<setup_import_methods> for us, since we passed the C<base_class_roles>
66parameter. The generated C<init_meta> will in turn call
67L<Moose::Util::MetaRole::apply_base_class_roles|Moose::Util::MetaRole/apply_base_class_roles>.
871cda31 68
f3ce0579 69=head1 AUTHOR
70
71Dave Rolsky E<lt>autarch@urth.orgE<gt>
72
73=head1 COPYRIGHT AND LICENSE
74
2840a3b2 75Copyright 2009 by Infinity Interactive, Inc.
f3ce0579 76
77L<http://www.iinteractive.com>
78
79This library is free software; you can redistribute it and/or modify
80it under the same terms as Perl itself.
81
6e646af9 82=begin testing
83
84{
85 package Debugged;
f3ce0579 86
6e646af9 87 use Moose;
88 MooseX::Debugging->import;
89}
90
91stderr_is(
92 sub { Debugged->new },
93 "Made a new Debugged object\n",
94 'got expected output from debugging role'
95);
96
97=end testing
98
99=cut