Object...zyInit(3pm)
β¨ Object::Pad::FieldAttr::LazyInit β the manual, but make it cute β¨
okay so bestie, this page is Object::Pad::FieldAttr::LazyInit(3pm), and yeah it lives in User Contributed Perl Documentation, and honestly? it's giving β¨main character energyβ¨ for your Object::Pad classes.
π NAME π
Object::Pad::FieldAttr::LazyInit β lazily initialise Object::Pad fields at first read, no cap π
π¦ SYNOPSIS π¦
use Object::Pad;
use Object::Pad::FieldAttr::LazyInit;
class Item {
field $uuid :reader :param :LazyInit(_make_uuid);
method _make_uuid {
require Data::GUID;
return Data::GUID->new->as_string;
}
}
look at that. the field literally does β¨nothingβ¨ until somebody reads it. iconic behaviour, honestly πΈ
π DESCRIPTION π
So real talk: this module hands you a third-party field attribute for Object::Pad-based classes. When you slap it onto a field, you're declaring that this field has a lazy initialisation method, and that method gets called the first time the field's value is read from. not before. never before. it just vibes there π«Ά
WARNING π¨ the ability for Object::Pad to take third-party field attributes is still new and highly experimental, and it's subject to much API change in future. so this module should be considered equally experimental. like, do not build your whole corporate codebase on this without knowing that, fr fr π
π FIELD ATTRIBUTES π
:LazyInit
field $name :LazyInit(NAME) ...;
here's the tea β this declares that if the field variable is read from before it has been otherwise initialised, then the named method (NAME, the one you passed in) will be called first to create an initial value for it.
but! if the field has been initialised some other way, the lazy initialiser gets ghosted π» no call, no nothing. and the ways to initialise it are:
- a
:paramdeclaration - explicit assignment into it
- using a
:writeraccessor
any of those sets the value for the field, which means the lazy initialiser will not be invoked. periodt.
Once the initialiser has been invoked, the value is stored by the field, and after that it behaves like a totally normal field: subsequent reads return the current value and the initialiser method will not be invoked again. one and done, bestie π
Now, the important bit β accidental recursion, aka the villain of this story π to avoid the possibility of it while you're generating the value, the recommendation is that the logic in your lazy initialisation method be as self-contained as possible. ideally:
- not invoking any methods on
$self - only making use of other fields already declared before the field being initialised
and here's a little trick to save yourself: if you place the initialiser method immediately after the field declaration, before any other fields, you can reduce the possibility of getting stuck in such a manner. iykyk π
field $field_zero :param;
field $field_one :LazyInit(_make_one);
method _make_one {
# we can safely use $field_zero in here
}
field $field_two :LazyInit(_make_two);
method _make_two {
# we can safely use $field_zero and $field_one
}
see how it flows? order matters, babes. $field_zero is fair game for _make_one. then _make_two gets $field_zero and $field_one to play with. no going backwards π₯
π AUTHOR π
Paul Evans <leonerd@leonerd.org.uk>
and that's the whole vibe, bestie π this was Object...zyInit(3pm) from the perl v5.42.3 distribution, dated 2026-08-22 β thanks for reading, mwah, go forth and be lazy (in the good, deferred-execution way) β¨π¦πΈ
π¦ where this page comes from
- ubuntu stonking: libobject-pad-fieldattr-lazyinit-perl 0.07-1build5 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms