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

filter: Statementstmt 92.3% 12 / 13 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 80.0% 4 / 5 Podpod n/a totaltotal 88.8% 16 / 18 CC 1 SCAR 1.8
CC
1
Cov
92%
unimport
2.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.2
↑ Summary lib/Object/Pad/FieldAttr/Final.xs »
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-2023 -- leonerd@leonerd.org.uk
5  
6 package Object::Pad::FieldAttr::Final 0.06;
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.66;
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 field $width :param :reader :Final;
27 field $height :param :reader :Final;
28  
29 field $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 field $name :Final ...;
51 field $name :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 field $x :Final;
59  
60 ADJUST { $x = 123; } # this is permitted
61  
62 method m { $x = 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 field $aref :Final;
69 ADJUST { $aref = []; }
70  
71 method more { push @$aref, "another"; } # this is permitted
72  
73 =cut
74  
75 sub import
76 2 ▶{
Subroutine import: called CC=1 SCAR=0.0
77 2 $^H{"Object::Pad::FieldAttr::Final/Final"}++;
78 }
79  
80 sub unimport
81 0 ▶{
Subroutine unimport: not called CC=1 SCAR=6.9
82 0 delete $^H{"Object::Pad::FieldAttr::Final/Final"};
83 }
84  
85 =head1 AUTHOR
86  
87 Paul Evans <leonerd@leonerd.org.uk>
88  
89 =cut
90  
91 3 0x55AA;
↑ Summary lib/Object/Pad/FieldAttr/Final.xs »