File Coverage

blib/lib/Object/Pad/SlotAttr/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 -- leonerd@leonerd.org.uk
5              
6             package Object::Pad::SlotAttr::Final 0.04;
7              
8 3     3   131646 use v5.14;
  3         29  
9 3     3   12 use warnings;
  3         6  
  3         76  
10              
11 3     3   481 use Object::Pad 0.50;
  3         7811  
  3         12  
12              
13             require XSLoader;
14             XSLoader::load( __PACKAGE__, our $VERSION );
15              
16             =head1 NAME
17              
18             C - declare C slots readonly after construction
19              
20             =head1 SYNOPSIS
21              
22             use Object::Pad;
23             use Object::Pad::SlotAttr::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 slot attribute for L-based
39             classes, which declares that the slot 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 slot 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 SLOT ATTRIBUTES
47              
48             =head2 :Final
49              
50             has $slot :Final ...;
51             has $slot :Final ... = DEFAULT;
52              
53             Declares that the slot 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 slot variable.
57              
58             has $slot :Final;
59              
60             ADJUST { $slot = 123; } # this is permitted
61              
62             method m { $slot = 456; } # this will fail
63              
64             Note that this is only a I readonly setting; if the slot 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   151 $^H{"Object::Pad::SlotAttr::Final/Final"}++;
78             }
79              
80             =head1 AUTHOR
81              
82             Paul Evans
83              
84             =cut
85              
86             0x55AA;