| 1 |
NAME |
| 2 |
|
| 3 |
Object::Pad::FieldAttr::Final - declare Object::Pad fields readonly |
| 4 |
after construction |
| 5 |
|
| 6 |
SYNOPSIS |
| 7 |
|
| 8 |
use Object::Pad; |
| 9 |
use Object::Pad::FieldAttr::Final; |
| 10 |
|
| 11 |
class Rectangle { |
| 12 |
field $width :param :reader :Final; |
| 13 |
field $height :param :reader :Final; |
| 14 |
|
| 15 |
field $area :reader :Final; |
| 16 |
|
| 17 |
ADJUST { |
| 18 |
$area = $width * $height; |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
DESCRIPTION |
| 23 |
|
| 24 |
This module provides a third-party field attribute for |
| 25 |
Object::Pad-based classes, which declares that the field it is attached |
| 26 |
to shall be set as readonly when the constructor returns, disallowing |
| 27 |
further modification to it. |
| 28 |
|
| 29 |
WARNING The ability for Object::Pad to take third-party field |
| 30 |
attributes is still new and highly experimental, and subject to much |
| 31 |
API change in future. As a result, this module should be considered |
| 32 |
equally experimental. |
| 33 |
|
| 34 |
FIELD ATTRIBUTES |
| 35 |
|
| 36 |
:Final |
| 37 |
|
| 38 |
field $name :Final ...; |
| 39 |
field $name :Final ... = DEFAULT; |
| 40 |
|
| 41 |
Declares that the field variable will be set readonly at the end of the |
| 42 |
constructor, after any assignments from :param declarations or ADJUST |
| 43 |
blocks. At this point, the value cannot otherwise be modified by |
| 44 |
directly writing into the field variable. |
| 45 |
|
| 46 |
field $x :Final; |
| 47 |
|
| 48 |
ADJUST { $x = 123; } # this is permitted |
| 49 |
|
| 50 |
method m { $x = 456; } # this will fail |
| 51 |
|
| 52 |
Note that this is only a shallow readonly setting; if the field |
| 53 |
variable contains a reference to a data structure, that structure |
| 54 |
itself remains mutable. |
| 55 |
|
| 56 |
field $aref :Final; |
| 57 |
ADJUST { $aref = []; } |
| 58 |
|
| 59 |
method more { push @$aref, "another"; } # this is permitted |
| 60 |
|
| 61 |
AUTHOR |
| 62 |
|
| 63 |
Paul Evans <[email protected]> |
| 64 |
|