1Moo::Role(3) User Contributed Perl Documentation Moo::Role(3)
2
3
4
6 Moo::Role - Minimal Object Orientation support for Roles
7
9 package My::Role;
10
11 use Moo::Role;
12 use strictures 2;
13
14 sub foo { ... }
15
16 sub bar { ... }
17
18 has baz => (
19 is => 'ro',
20 );
21
22 1;
23
24 And elsewhere:
25
26 package Some::Class;
27
28 use Moo;
29 use strictures 2;
30
31 # bar gets imported, but not foo
32 with 'My::Role';
33
34 sub foo { ... }
35
36 1;
37
39 "Moo::Role" builds upon Role::Tiny, so look there for most of the
40 documentation on how this works (in particular, using "Moo::Role" also
41 enables strict and warnings). The main addition here is extra bits to
42 make the roles more "Moosey;" which is to say, it adds "has".
43
45 See "IMPORTED SUBROUTINES" in Role::Tiny for all the other subroutines
46 that are imported by this module.
47
48 has
49 has attr => (
50 is => 'ro',
51 );
52
53 Declares an attribute for the class to be composed into. See "has" in
54 Moo for all options.
55
57 Moo::Role cleans up its own imported methods and any imports declared
58 before the "use Moo::Role" statement automatically. Anything imported
59 after "use Moo::Role" will be composed into consuming packages. A
60 package that consumes this role:
61
62 package My::Role::ID;
63
64 use Digest::MD5 qw(md5_hex);
65 use Moo::Role;
66 use Digest::SHA qw(sha1_hex);
67
68 requires 'name';
69
70 sub as_md5 { my ($self) = @_; return md5_hex($self->name); }
71 sub as_sha1 { my ($self) = @_; return sha1_hex($self->name); }
72
73 1;
74
75 ..will now have a "$self->sha1_hex()" method available to it that
76 probably does not do what you expect. On the other hand, a call to
77 "$self->md5_hex()" will die with the helpful error message: "Can't
78 locate object method "md5_hex"".
79
80 See "CLEANING UP IMPORTS" in Moo for more details.
81
83 See Moo for support and contact information.
84
86 See Moo for authors.
87
89 See Moo for the copyright and license.
90
91
92
93perl v5.34.0 2022-01-21 Moo::Role(3)