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