Object...zyInit(3pm)

πŸ“¦
version 0.07 Β· 5 versions
, in ubuntu stonking

✨ 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 :param declaration
  • explicit assignment into it
  • using a :writer accessor

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