1Moose::Util::MetaRole(3U)ser Contributed Perl DocumentatiMoonose::Util::MetaRole(3)
2
3
4
6 Moose::Util::MetaRole - Apply roles to any metaclass, as well as the
7 object base class
8
10 package MyApp::Moose;
11
12 use Moose ();
13 use Moose::Exporter;
14 use Moose::Util::MetaRole;
15
16 use MyApp::Role::Meta::Class;
17 use MyApp::Role::Meta::Method::Constructor;
18 use MyApp::Role::Object;
19
20 Moose::Exporter->setup_import_methods( also => 'Moose' );
21
22 sub init_meta {
23 shift;
24 my %args = @_;
25
26 Moose->init_meta(%args);
27
28 Moose::Util::MetaRole::apply_metaroles(
29 for => $args{for_class},
30 class_metaroles => {
31 class => => ['MyApp::Role::Meta::Class'],
32 constructor => ['MyApp::Role::Meta::Method::Constructor'],
33 },
34 );
35
36 Moose::Util::MetaRole::apply_base_class_roles(
37 for => $args{for_class},
38 roles => ['MyApp::Role::Object'],
39 );
40
41 return $args{for_class}->meta();
42 }
43
45 This utility module is designed to help authors of Moose extensions
46 write extensions that are able to cooperate with other Moose
47 extensions. To do this, you must write your extensions as roles, which
48 can then be dynamically applied to the caller's metaclasses.
49
50 This module makes sure to preserve any existing superclasses and roles
51 already set for the meta objects, which means that any number of
52 extensions can apply roles in any order.
53
55 It is very important that you only call this module's functions when
56 your module is imported by the caller. The process of applying roles to
57 the metaclass reinitializes the metaclass object, which wipes out any
58 existing attributes already defined. However, as long as you do this
59 when your module is imported, the caller should not have any attributes
60 defined yet.
61
62 The easiest way to ensure that this happens is to use Moose::Exporter,
63 which can generate the appropriate "init_meta" method for you, and make
64 sure it is called when imported.
65
67 This module provides two functions.
68
69 apply_metaroles( ... )
70 This function will apply roles to one or more metaclasses for the
71 specified class. It will return a new metaclass object for the class or
72 role passed in the "for" parameter.
73
74 It accepts the following parameters:
75
76 · for => $name
77
78 This specifies the class or for which to alter the meta classes.
79 This can be a package name, or an appropriate meta-object (a
80 Moose::Meta::Class or Moose::Meta::Role).
81
82 · class_metaroles => \%roles
83
84 This is a hash reference specifying which metaroles will be applied
85 to the class metaclass and its contained metaclasses and helper
86 classes.
87
88 Each key should in turn point to an array reference of role names.
89
90 It accepts the following keys:
91
92 class
93 attribute
94 method
95 wrapped_method
96 instance
97 constructor
98 destructor
99 error
100 · role_metaroles => \%roles
101
102 This is a hash reference specifying which metaroles will be applied
103 to the role metaclass and its contained metaclasses and helper
104 classes.
105
106 It accepts the following keys:
107
108 role
109 attribute
110 method
111 required_method
112 conflicting_method
113 application_to_class
114 application_to_role
115 application_to_instance
116 application_role_summation
117
118 apply_base_class_roles( for => $class, roles => \@roles )
119 This function will apply the specified roles to the object's base
120 class.
121
123 See "BUGS" in Moose for details on reporting bugs.
124
126 Dave Rolsky <autarch@urth.org>
127
129 Copyright 2009 by Infinity Interactive, Inc.
130
131 <http://www.iinteractive.com>
132
133 This library is free software; you can redistribute it and/or modify it
134 under the same terms as Perl itself.
135
136
137
138perl v5.12.2 2010-08-28 Moose::Util::MetaRole(3)