1MooseX::Extended::ManuaUls:e:rCoCnosnttrruicbtuitoendM(o3Po)esrelX:D:oEcxutmeenndteadt:i:oMnanual::Construction(3)
2
3
4
6 MooseX::Extended::Manual::Construction - Objected construction for
7 MooseX::Extended
8
10 version 0.35
11
13 The normal "new", "BUILD", and "BUILDARGS" functions work as expected.
14 However, we apply MooseX::StrictConstructor to avoid this problem:
15
16 my $soldier = Soldier->new(
17 name => $name,
18 rank => $rank,
19 seriel => $serial, # should be serial
20 );
21
22 By default, misspelled arguments to the Moose constructor are silently
23 discarded, leading to hard-to-diagnose bugs. With MooseX::Extended,
24 they're a fatal error.
25
26 If you need to pass arbitrary "sideband" data, explicitly declare it as
27 such:
28
29 param sideband => ( isa => HashRef, default => sub { {} } );
30
31 Naturally, because we bundle MooseX::Extended::Types, you can do much
32 finer-grained data validation on that, if needed.
33
35 The following two functions are exported into your namespace.
36
37 "param"
38 param name => ( isa => NonEmptyStr );
39
40 A similar function to Moose's "has". A "param" is required. You may
41 pass it to the constructor, or use a "default" or "builder" to supply
42 this value.
43
44 The above "param" definition is equivalent to:
45
46 has name => (
47 is => 'ro',
48 isa => NonEmptyStr,
49 required => 1,
50 );
51
52 If you want a parameter that has no "default" or "builder" and can
53 optionally be passed to the constructor, just use "required => 0".
54
55 param title => ( isa => Str, required => 0 );
56
57 Note that "param", like "field", defaults to read-only, "is => 'ro'".
58 You can override this:
59
60 param name => ( is => 'rw', isa => NonEmptyStr );
61
62 Otherwise, it behaves like "has". You can pass in any arguments that
63 "has" accepts.
64
65 # we'll make it private, but allow it to be passed to the constructor
66 # as `name`
67 param _name => ( isa => NonEmptyStr, init_arg => 'name' );
68
69 "field"
70 field created => ( isa => PositiveInt, default => sub { time } );
71
72 A similar function to Moose's "has". A "field" is almost never allowed
73 to be passed to the constructor, but you can still use "default" or
74 "builder", as normal.
75
76 The above "field" definition is equivalent to:
77
78 has created => (
79 is => 'ro',
80 isa => PositiveInt,
81 init_arg => undef, # not allowed in the constructor
82 default => sub { time },
83 lazy => 1,
84 );
85
86 Note that "field", like "param", defaults to read-only, "is => 'ro'".
87 You can override this:
88
89 field some_data => ( is => 'rw', isa => NonEmptyStr );
90
91 Otherwise, it behaves like "has". You can pass in any arguments that
92 "has" accepts.
93
94 WARNING: if you pass "field" an "init_arg" with a defined value, The
95 code will "croak" unless that value begins with an underscore:
96
97 field created => (
98 isa => PositiveInt,
99 default => sub {time},
100 lazy => 0, # because it must fire at object creation
101 init_arg => '_created', # but let them override this in tests
102 );
103
104 The above allows you to pass "_created => 42" in the constructor. This
105 is useful when you wish to easily control this value for tests.
106
107 Otherwise, a "field" is just for internal instance data the class uses.
108 It's not to be passed to the constructor. If you want that, just use
109 "param".
110
111 Lazy Fields
112
113 A "field" is automatically lazy if it has a "builder" or "default".
114 This is because there's no guarantee the code will call them, but this
115 makes it very easy for a "field" to rely on a "param" value being
116 present. Note that is does mean if you need a "field" to be initialized
117 at construction time, you have to take care:
118
119 has created => ( isa => PositiveInt, lazy => 0, default => sub {time} );
120
121 No "param" is lazy by default, but you can add "lazy => 1" if you need
122 to.
123
125 Curtis "Ovid" Poe <curtis.poe@gmail.com>
126
128 This software is Copyright (c) 2022 by Curtis "Ovid" Poe.
129
130 This is free software, licensed under:
131
132 The Artistic License 2.0 (GPL Compatible)
133
134
135
136perl v5.36.1 2023M-o0o6s-e2X6::Extended::Manual::Construction(3)