1MooseX::Has::Sugar(3) User Contributed Perl DocumentationMooseX::Has::Sugar(3)
2
3
4
6 MooseX::Has::Sugar - Sugar Syntax for moose 'has' fields
7
9 version 1.000006
10
12 use Moose;
13 use MooseX::Types::Moose;
14 use MooseX::Has::Sugar;
15
16 has attrname => ( isa => Str, ro, required );
17 has otherattrname => ( isa => Str, rw, lazy_build );
18
20 "MooseX::Has::Sugar" and its related modules provide simple, short-
21 hand, bare-word functions that act as declarative macros for greatly
22 compacting "Moose" "has" declarations, in a similar way to those
23 provided by the declarative subroutines provided by "MooseX::Types"
24
25 This provides:
26
27 • Less typing when defining "has" constraints
28
29 • Faster, more skim-readable blocks of "has" constraints
30
31 • Perl Language Level syntax validation at compile time
32
34 Reduced Typing in "has" declarations.
35 The constant need to type "=>" and '' is fine for one-off cases, but
36 the instant you have more than about 4 attributes it starts to get
37 annoying.
38
39 More compact declarations.
40 Reduces much of the redundant typing in most cases, which makes your
41 life easier, and makes it take up less visual space, which makes it
42 faster to read.
43
44 No String Worries
45 Strings are often problematic, due to white-space etc. Noted that if
46 you do happen to mess them up, Moose should at least warn you that
47 you've done something daft. Using this alleviates that worry.
48
50 Classical Moose
51 has foo => (
52 isa => 'Str',
53 is => 'ro',
54 required => 1,
55 );
56
57 has bar => (
58 isa => 'Str',
59 is => 'rw'
60 lazy_build => 1,
61 );
62
63 Lazy Evil way to do it:
64 PLEASE DO NOT DO THIS
65
66 has qw( foo isa Str is ro required 1 );
67 has qw( bar isa Str is rw lazy_build 1 );
68
69 With this module
70 ( and with MooseX::Types )
71
72 use MooseX::Types::Moose qw( Str );
73 use MooseX::Has::Sugar;
74
75 has foo => (
76 isa => Str,
77 ro,
78 required,
79 );
80 has bar => (
81 isa => Str,
82 rw,
83 lazy_build,
84 );
85
86 Or even
87
88 use MooseX::Types::Moose qw( Str );
89 use MooseX::Has::Sugar;
90
91 has foo => ( isa => Str, ro, required, );
92 has bar => ( isa => Str, rw, lazy_build, );
93
95 Basic "is" Expansion Only
96 ( using ::Sugar::Minimal instead )
97
98 use MooseX::Types::Moose qw( Str );
99 use MooseX::Has::Sugar::Minimal;
100
101 has foo => (
102 isa => Str,
103 is => ro,
104 required => 1,
105 );
106 has bar => (
107 isa => Str,
108 is => rw,
109 lazy_build => 1,
110 );
111
112 Attribute Expansions with Basic Expansions
113 ( Combining parts of this and ::Sugar::Minimal )
114
115 use MooseX::Types::Moose qw( Str );
116 use MooseX::Has::Sugar::Minimal;
117 use MooseX::Has::Sugar qw( :attrs );
118
119 has foo => (
120 isa => Str,
121 is => ro,
122 required,
123 );
124 has bar => (
125 isa => Str,
126 is => rw,
127 lazy_build,
128 );
129
131 ":default"
132 Since 0.0300, this exports all our syntax, the same as ":attrs"
133 ":isattrs". Primarily because I found you generally want all the
134 sugar, not just part of it. This also gets rid of that nasty exclusion
135 logic.
136
137 ":isattrs"
138 This exports "ro", "rw" and "bare" as lists, so they behave as stand-
139 alone attributes like "lazy" does.
140
141 has foo => (
142 required,
143 isa => 'Str',
144 ro,
145 );
146
147 NOTE: This option is incompatible with ::Sugar::Minimal : "CONFLICTS"
148
149 ":attrs"
150 This exports "lazy" , "lazy_build" and "required", "coerce", "weak_ref"
151 and "auto_deref" as subs that assume positive.
152
153 has foo => (
154 required,
155 isa => 'Str',
156 );
157
158 NOTE: This option is incompatible with MooseX::Types and Moose's Type
159 Constraints Module : "CONFLICTS"
160
161 ":is"
162 DEPRECATED. See ::Sugar::Minimal for the same functionality
163
164 ":allattrs"
165 DEPRECATED, just use ":default" or do
166
167 use MooseX::Has::Sugar;
168
170 "bare"
171 returns "('is','bare')"
172
173 "ro"
174 returns "('is','ro')"
175
176 "rw"
177 returns "('is','rw')"
178
179 "required"
180 returns "('required',1)"
181
182 "lazy"
183 returns "('lazy',1)"
184
185 "lazy_build"
186 returns "('lazy_build',1)"
187
188 "weak_ref"
189 returns "('weak_ref',1)"
190
191 "coerce"
192 returns "('coerce',1)"
193
194 WARNING: Conflict with MooseX::Types and Moose::Util::TypeConstraints,
195 see "CONFLICTS".
196
197 "auto_deref"
198 returns "('auto_deref',1)"
199
201 MooseX::Has::Sugar::Minimal
202 MooseX::Has::Sugar::Saccharin
203 This module is not intended to be used in conjunction with
204 ::Sugar::Minimal or ::Sugar::Saccharin
205
206 We export many of the same symbols and its just not very sensible.
207
208 MooseX::Types
209 Moose::Util::TypeConstraints
210 due to exporting the "coerce" symbol, using us in the same scope as a
211 call to
212
213 use MooseX::Types ....
214
215 or
216 use Moose::Util::TypeConstraints
217
218 will result in a symbol collision.
219
220 We recommend using and creating proper type libraries instead, ( which
221 will absolve you entirely of the need to use MooseX::Types and
222 MooseX::Has::Sugar(::*)? in the same scope )
223
225 Kent Fredric <kentnl@cpan.org>
226
228 This software is copyright (c) 2017 by Kent Fredric
229 <kentfredric@gmail.com>.
230
231 This is free software; you can redistribute it and/or modify it under
232 the same terms as the Perl 5 programming language system itself.
233
234
235
236perl v5.32.1 2021-01-27 MooseX::Has::Sugar(3)