1MooseX::SetOnce(3) User Contributed Perl Documentation MooseX::SetOnce(3)
2
3
4
6 MooseX::SetOnce - write-once, read-many attributes for Moose
7
9 version 0.201
10
12 Add the "SetOnce" trait to attributes:
13
14 package Class;
15 use Moose;
16 use MooseX::SetOnce;
17
18 has some_attr => (
19 is => 'rw',
20 traits => [ qw(SetOnce) ],
21 );
22
23 ...and then you can only set them once:
24
25 my $object = Class->new;
26
27 $object->some_attr(10); # works fine
28 $object->some_attr(20); # throws an exception: it's already set!
29
31 The 'SetOnce' attribute lets your class have attributes that are not
32 lazy and not set, but that cannot be altered once set.
33
34 The logic is very simple: if you try to alter the value of an
35 attribute with the SetOnce trait, either by accessor or writer, and the
36 attribute has a value, it will throw an exception.
37
38 If the attribute has a clearer, you may clear the attribute and set it
39 again.
40
42 Ricardo SIGNES <rjbs@semiotic.systems>
43
45 • Jesse Luehrs <doy@tozt.net>
46
47 • Karen Etheridge <ether@cpan.org>
48
49 • Kent Fredric <kentfredric@gmail.com>
50
52 This software is copyright (c) 2021 by Ricardo SIGNES.
53
54 This is free software; you can redistribute it and/or modify it under
55 the same terms as the Perl 5 programming language system itself.
56
57
58
59perl v5.34.0 2021-07-22 MooseX::SetOnce(3)