1Moose::Cookbook::ExtendUisneMgro:o:CsDoeen:bt:urCgiogboiukntbgeo_doBkaP:se:erEClxltaDesonscdRuiomnlegen:(t:3aD)teiboungging_BaseClassRole(3)
2
3
4
6 Moose::Cookbook::Extending::Debugging_BaseClassRole - Providing a role
7 for the base object class
8
10 version 2.2011
11
13 package MooseX::Debugging;
14
15 use Moose::Exporter;
16
17 Moose::Exporter->setup_import_methods(
18 base_class_roles => ['MooseX::Debugging::Role::Object'],
19 );
20
21 package MooseX::Debugging::Role::Object;
22
23 use Moose::Role;
24
25 sub BUILD {}
26 after BUILD => sub {
27 my $self = shift;
28
29 warn "Made a new " . ( ref $self ) . " object\n";
30 };
31
33 In this example, we provide a role for the base object class that adds
34 some simple debugging output. Every time an object is created, it spits
35 out a warning saying what type of object it was.
36
37 Obviously, a real debugging role would do something more interesting,
38 but this recipe is all about how we apply that role.
39
40 In this case, with the combination of Moose::Exporter and
41 Moose::Util::MetaRole, we ensure that when a module does
42 "use MooseX::Debugging", it automatically gets the debugging role
43 applied to its base object class.
44
45 There are a few pieces of code worth looking at more closely.
46
47 Moose::Exporter->setup_import_methods(
48 base_class_roles => ['MooseX::Debugging::Role::Object'],
49 );
50
51 This creates an "import" method in the "MooseX::Debugging" package.
52 Since we are not actually exporting anything, we do not pass
53 "setup_import_methods" any parameters related to exports, but we need
54 to have an "import" method to ensure that our "init_meta" method is
55 called. The "init_meta" is created by "setup_import_methods" for us,
56 since we passed the "base_class_roles" parameter. The generated
57 "init_meta" will in turn call
58 Moose::Util::MetaRole::apply_base_class_roles.
59
60 sub BUILD {}
61 after BUILD => sub {
62 ...
63 };
64
65 Due to the way role composition currently works, if the class that a
66 role is composed into contains a "BUILD" method, then that will
67 override the "BUILD" method in any roles it composes, which is
68 typically not what you want. Using a method modifier on "BUILD" avoids
69 this issue, since method modifiers compose together rather than being
70 overridden. Method modifiers require that a method exists in order to
71 wrap, however, so we also provide a stub method to wrap if no "BUILD"
72 method exists in the class.
73
75 · Stevan Little <stevan.little@iinteractive.com>
76
77 · Dave Rolsky <autarch@urth.org>
78
79 · Jesse Luehrs <doy@tozt.net>
80
81 · Shawn M Moore <code@sartak.org>
82
83 · יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org>
84
85 · Karen Etheridge <ether@cpan.org>
86
87 · Florian Ragwitz <rafl@debian.org>
88
89 · Hans Dieter Pearcey <hdp@weftsoar.net>
90
91 · Chris Prather <chris@prather.org>
92
93 · Matt S Trout <mst@shadowcat.co.uk>
94
96 This software is copyright (c) 2006 by Infinity Interactive, Inc.
97
98 This is free software; you can redistribute it and/or modify it under
99 the same terms as the Perl 5 programming language system itself.
100
101
102
103perl v5.30.0 Moose::Coo2k0b1o9o-k0:7:-E2x6tending::Debugging_BaseClassRole(3)