File Coverage

blib/lib/Object/Pad/FieldAttr/Final.pm
Criterion Covered Total %
statement 9 9 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 13 13 100.0


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-2022 -- leonerd@leonerd.org.uk
5              
6             package Object::Pad::FieldAttr::Final 0.05;
7              
8 3     3   133948 use v5.14;
  3         29  
9 3     3   13 use warnings;
  3         4  
  3         79  
10              
11 3     3   502 use Object::Pad 0.50;
  3         8535  
  3         12  
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             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-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             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
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 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     3   158 $^H{"Object::Pad::FieldAttr::Final/Final"}++;
78             }
79              
80             =head1 AUTHOR
81              
82             Paul Evans
83              
84             =cut
85              
86             0x55AA;