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.200002
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@cpan.org>
43
45 This software is copyright (c) 2013 by Ricardo SIGNES.
46
47 This is free software; you can redistribute it and/or modify it under
48 the same terms as the Perl 5 programming language system itself.
49
50
51
52perl v5.32.1 2021-01-27 MooseX::SetOnce(3)