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