1Moose::Util(3) User Contributed Perl Documentation Moose::Util(3)
2
3
4
6 Moose::Util - Utilities for working with Moose classes
7
9 version 2.2011
10
12 use Moose::Util qw/find_meta does_role search_class_by_role/;
13
14 my $meta = find_meta($object) || die "No metaclass found";
15
16 if (does_role($object, $role)) {
17 print "The object can do $role!\n";
18 }
19
20 my $class = search_class_by_role($object, 'FooRole');
21 print "Nearest class with 'FooRole' is $class\n";
22
24 This module provides a set of utility functions. Many of these
25 functions are intended for use in Moose itself or MooseX modules, but
26 some of them may be useful for use in your own code.
27
29 find_meta($class_or_obj)
30 This method takes a class name or object and attempts to find a
31 metaclass for the class, if one exists. It will not create one if it
32 does not yet exist.
33
34 is_role($package_or_obj)
35 Returns true if the provided package name or object is a Moose::Role.
36
37 does_role($class_or_obj, $role_or_obj)
38 Returns true if $class_or_obj does the given $role_or_obj. The role can
39 be provided as a name or a Moose::Meta::Role object.
40
41 The class must already have a metaclass for this to work. If it
42 doesn't, this function simply returns false.
43
44 search_class_by_role($class_or_obj, $role_or_obj)
45 Returns the first class in the class's precedence list that does
46 $role_or_obj, if any. The role can be either a name or a
47 Moose::Meta::Role object.
48
49 The class must already have a metaclass for this to work.
50
51 apply_all_roles($applicant, @roles)
52 This function applies one or more roles to the given $applicant. The
53 applicant can be a role name, class name, or object.
54
55 The $applicant must already have a metaclass object.
56
57 The list of @roles should a list of names or Moose::Meta::Role objects,
58 each of which can be followed by an optional hash reference of options
59 ("-excludes" and "-alias").
60
61 ensure_all_roles($applicant, @roles)
62 This function is similar to "apply_all_roles", but only applies roles
63 that $applicant does not already consume.
64
65 with_traits($class_name, @role_names)
66 This function creates a new class from $class_name with each of
67 @role_names applied. It returns the name of the new class.
68
69 get_all_attribute_values($meta, $instance)
70 Returns a hash reference containing all of the $instance's attributes.
71 The keys are attribute names.
72
73 get_all_init_args($meta, $instance)
74 Returns a hash reference containing all of the "init_arg" values for
75 the instance's attributes. The values are the associated attribute
76 values. If an attribute does not have a defined "init_arg", it is
77 skipped.
78
79 This could be useful in cloning an object.
80
81 resolve_metaclass_alias($category, $name, %options)
82 resolve_metatrait_alias($category, $name, %options)
83 Resolves a short name to a full class name. Short names are often used
84 when specifying the "metaclass" or "traits" option for an attribute:
85
86 has foo => (
87 metaclass => "Bar",
88 );
89
90 The name resolution mechanism is covered in "Metaclass and Trait Name
91 Resolution" in Moose.
92
93 meta_class_alias($to[, $from])
94 meta_attribute_alias($to[, $from])
95 Create an alias from the class $from (or the current package, if $from
96 is unspecified), so that "Metaclass and Trait Name Resolution" in Moose
97 works properly.
98
99 english_list(@items)
100 Given a list of scalars, turns them into a proper list in English ("one
101 and two", "one, two, three, and four"). This is used to help us make
102 nicer error messages.
103
104 throw_exception( $class_name, %arguments_to_exception)
105 Calls die with an object of Moose::Exception::$class_name, with
106 %arguments_to_exception passed as arguments.
107
109 Here is a list of possible functions to write
110
111 discovering original method from modified method
112 search for origin class of a method or attribute
113
115 See "BUGS" in Moose for details on reporting bugs.
116
118 · Stevan Little <stevan.little@iinteractive.com>
119
120 · Dave Rolsky <autarch@urth.org>
121
122 · Jesse Luehrs <doy@tozt.net>
123
124 · Shawn M Moore <code@sartak.org>
125
126 · יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org>
127
128 · Karen Etheridge <ether@cpan.org>
129
130 · Florian Ragwitz <rafl@debian.org>
131
132 · Hans Dieter Pearcey <hdp@weftsoar.net>
133
134 · Chris Prather <chris@prather.org>
135
136 · Matt S Trout <mst@shadowcat.co.uk>
137
139 This software is copyright (c) 2006 by Infinity Interactive, Inc.
140
141 This is free software; you can redistribute it and/or modify it under
142 the same terms as the Perl 5 programming language system itself.
143
144
145
146perl v5.28.1 2018-05-16 Moose::Util(3)