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. The main addition here is extra bits
41 to make the roles more "Moosey;" which is to say, it adds "has".
42
44 See "IMPORTED SUBROUTINES" in Role::Tiny for all the other subroutines
45 that are imported by this module.
46
47 has
48 has attr => (
49 is => 'ro',
50 );
51
52 Declares an attribute for the class to be composed into. See "has" in
53 Moo for all options.
54
56 Moo::Role cleans up its own imported methods and any imports declared
57 before the "use Moo::Role" statement automatically. Anything imported
58 after "use Moo::Role" will be composed into consuming packages. A
59 package that consumes this role:
60
61 package My::Role::ID;
62
63 use Digest::MD5 qw(md5_hex);
64 use Moo::Role;
65 use Digest::SHA qw(sha1_hex);
66
67 requires 'name';
68
69 sub as_md5 { my ($self) = @_; return md5_hex($self->name); }
70 sub as_sha1 { my ($self) = @_; return sha1_hex($self->name); }
71
72 1;
73
74 ..will now have a "$self->sha1_hex()" method available to it that
75 probably does not do what you expect. On the other hand, a call to
76 "$self->md5_hex()" will die with the helpful error message: "Can't
77 locate object method "md5_hex"".
78
79 See "CLEANING UP IMPORTS" in Moo for more details.
80
82 See Moo for support and contact information.
83
85 See Moo for authors.
86
88 See Moo for the copyright and license.
89
90
91
92perl v5.28.0 2017-12-01 Moo::Role(3)