1Mo::default(3) User Contributed Perl Documentation Mo::default(3)
2
3
4
6 Mo::default - Adds the default feature to Mo's has
7
9 use Mo qw'default';
10 has job => ( default => 'Killer' );
11 has name => ( default => sub { 'Joe' } );
12 has age => 42;
13 has colors => [ 'green', 'blue' ];
14
16 Adds the default parameter to has, which expects a value. If the value
17 is a code ref, it is called to produce the default. If the value is a
18 hash or array ref, then a shallow copy is made for the default. If
19 there is an odd number of arguments after the attribute name, then the
20 first value is the default.
21
22 These 3 lines are the same:
23
24 has things => default => sub {+{}};
25 has things => default => {};
26 has things => {};
27
29 Default attribute values in Mo are lazy by default. This can be
30 changed by explicitly setting the "lazy" argument to false, in order to
31 cause it to be initialized during instantiation.
32
33 use Mo qw'default';
34 has status => 'active'; # lazy
35 has location => sub { ... }, lazy => 1; # lazy
36 has target => sub { ... }, lazy => 0; # eager
37
38 To change the default behavior and make attributes to be initialized
39 eagerly by default, import "nonlazy".
40
41 use Mo qw'default nonlazy';
42 has status => 'active'; # eager
43 has location => sub { ... }, lazy => 1; # lazy
44 has target => sub { ... }, lazy => 0; # eager
45
46
47
48perl v5.36.0 2022-07-22 Mo::default(3)