blib/lib/Object/Pad/FieldAttr/Final.pm

filter: Statementstmt 100% 12 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 100% 16 / 16 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Build ↑ Summary
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2021-2022 -- leonerd@leonerd.org.uk
5  
6 package Object::Pad::FieldAttr::Final 0.05;
7  
8 3 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 ▶use Object::Pad 0.50;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 require XSLoader;
14 3 XSLoader::load( __PACKAGE__, our $VERSION );
15  
16 =head1 NAME
17  
18 C<Object::Pad::FieldAttr::Final> - declare C<Object::Pad> fields readonly after construction
19  
20 =head1 SYNOPSIS
21  
22 use Object::Pad;
23 use Object::Pad::FieldAttr::Final;
24  
25 class Rectangle {
26 has $width :param :reader :Final;
27 has $height :param :reader :Final;
28  
29 has $area :reader :Final;
30  
31 ADJUST {
32 $area = $width * $height;
33 }
34 }
35  
36 =head1 DESCRIPTION
37  
38 This module provides a third-party field attribute for L<Object::Pad>-based
39 classes, which declares that the field it is attached to shall be set as
40 readonly when the constructor returns, disallowing further modification to it.
41  
42 B<WARNING> The ability for L<Object::Pad> to take third-party field attributes
43 is still new and highly experimental, and subject to much API change in
44 future. As a result, this module should be considered equally experimental.
45  
46 =head1 FIELD ATTRIBUTES
47  
48 =head2 :Final
49  
50 has $field :Final ...;
51 has $field :Final ... = DEFAULT;
52  
53 Declares that the field variable will be set readonly at the end of the
54 constructor, after any assignments from C<:param> declarations or C<ADJUST>
55 blocks. At this point, the value cannot otherwise be modified by directly
56 writing into the field variable.
57  
58 has $field :Final;
59  
60 ADJUST { $field = 123; } # this is permitted
61  
62 method m { $field = 456; } # this will fail
63  
64 Note that this is only a I<shallow> readonly setting; if the field variable
65 contains a reference to a data structure, that structure itself remains
66 mutable.
67  
68 has $aref :Final;
69 ADJUST { $aref = []; }
70  
71 method more { push @$aref, "another"; } # this is permitted
72  
73 =cut
74  
75 sub import
76 {
77 3 ▶ $^H{"Object::Pad::FieldAttr::Final/Final"}++;
Subroutine import: called CC=1 SCAR=0.0
78 }
79  
80 =head1 AUTHOR
81  
82 Paul Evans <leonerd@leonerd.org.uk>
83  
84 =cut
85  
86 3 0x55AA;
« Build ↑ Summary