1Moose::Cookbook::ExtendUisneMgro:o:CsDoeen:bt:urCgiogboiukntbgeo_doBkaP:se:erEClxltaDesonscdRuiomnlegen:(t:3aD)teiboungging_BaseClassRole(3)
2
3
4

NAME

6       Moose::Cookbook::Extending::Debugging_BaseClassRole - Providing a role
7       for the base object class
8

VERSION

10       version 2.2014
11

SYNOPSIS

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

DESCRIPTION

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

AUTHORS

75       •   Stevan Little <stevan@cpan.org>
76
77       •   Dave Rolsky <autarch@urth.org>
78
79       •   Jesse Luehrs <doy@cpan.org>
80
81       •   Shawn M Moore <sartak@cpan.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@cpan.org>
90
91       •   Chris Prather <chris@prather.org>
92
93       •   Matt S Trout <mstrout@cpan.org>
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.32.1            Moose::Coo2k0b2o1o-k0:1:-E2x7tending::Debugging_BaseClassRole(3)
Impressum