File Coverage

blib/lib/Object/Pad/FieldAttr/Final.pm
Criterion Covered Total %
statement 9 10 90.0
branch n/a
condition n/a
subroutine 4 5 80.0
pod n/a
total 13 15 86.6


line stmt bran cond sub pod time code
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     3   397006 use v5.14;
  3         24  
9 3     3   13 use warnings;
  3         5  
  3         72  
10              
11 3     3   533 use Object::Pad 0.66;
  3         8917  
  3         152  
12              
13             require XSLoader;
14             XSLoader::load( __PACKAGE__, our $VERSION );
15              
16             =head1 NAME
17              
18             C - declare C 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-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 The ability for L 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
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 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             {
77 2     2   142 $^H{"Object::Pad::FieldAttr::Final/Final"}++;
78             }
79              
80             sub unimport
81             {
82 0     0     delete $^H{"Object::Pad::FieldAttr::Final/Final"};
83             }
84              
85             =head1 AUTHOR
86              
87             Paul Evans
88              
89             =cut
90              
91             0x55AA;